00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 from __future__ import division
00040 import roslib
00041 roslib.load_manifest('pr2_gripper_reactive_approach')
00042 import time
00043 import rospy
00044 import os
00045 import controller_manager
00046 import sys
00047 from motion_planning_msgs.msg import OrderedCollisionOperations, CollisionOperation
00048 from geometry_msgs.msg import PoseStamped, Pose, Point, Quaternion
00049 from object_manipulation_msgs.msg import GripperTranslation, ManipulationPhase
00050 import tf
00051 import scipy
00052 import math
00053 import pdb
00054 import hand_sensor_listeners
00055 from object_manipulator.convert_functions import *
00056 import copy
00057 from actionlib_msgs.msg import GoalStatus
00058
00059
00060 def keypause():
00061 print "press enter to continue"
00062 input = raw_input()
00063 return input
00064
00065
00066 class Aborted(Exception): pass
00067
00068
00069 class ReactiveGrasper:
00070
00071 def __init__(self, cm):
00072
00073 self.cm = cm
00074 self.whicharm = self.cm.whicharm
00075 print "initializing %s fingertip sensor listener"%self.whicharm
00076 self.pressure_listener = hand_sensor_listeners.FingertipSensorListener(self.whicharm)
00077
00078 print "done"
00079
00080
00081 self.using_slip_detection = self.cm.using_slip_detection
00082
00083
00084 self.close_force = 100
00085 if self.using_slip_detection:
00086 self.close_force = 10
00087
00088
00089 self._closed_hard = 0
00090
00091
00092 self._table_name = "table"
00093
00094
00095 self._photo = 0
00096
00097
00098 self._phase_pub = rospy.Publisher('/reactive_manipulation_phase', ManipulationPhase)
00099
00100
00101 self.manipulation_phase_dict = {}
00102 for element in dir(ManipulationPhase):
00103 if element[0].isupper():
00104 self.manipulation_phase_dict[eval('ManipulationPhase.'+element)] = element
00105
00106
00107 self.reactive_approach_result_dict = {"success":0, "within goal threshold":1, "both fingers touching":2, "ran out of tries":3, "touching table":4, "saw palm contact":5, "grasp infeasible":6}
00108
00109
00110 self.reactive_grasp_result_dict = {"success":0, "ran out of grasp tries":1, "ran out of approach tries":2, \
00111 "aborted":3, "grasp infeasible":4}
00112
00113 rospy.loginfo("done with ReactiveGrasper init for the %s arm"%self.whicharm)
00114
00115
00116
00117 def broadcast_phase(self, phase):
00118 rospy.loginfo("broadcasting reactive phase %s"%self.manipulation_phase_dict[phase])
00119 self._phase_pub.publish(phase)
00120
00121
00122
00123 def check_preempt(self):
00124 pass
00125
00126
00127
00128 def throw_exception(self):
00129 raise Aborted
00130
00131
00132
00133
00134 def get_fingertip_readings_during_find_contact(self, contacts_desired, timeout = 10.):
00135 saved_l_pressure = [0.]*22
00136 saved_r_pressure = [0.]*22
00137 start_time = rospy.get_rostime()
00138 left_touching = right_touching = left_force = right_force = 0.
00139
00140 while not rospy.is_shutdown():
00141 state = self.cm.get_find_gripper_contact_state()
00142
00143
00144 if state not in [GoalStatus.ACTIVE, GoalStatus.PENDING]:
00145
00146
00147 if state == GoalStatus.SUCCEEDED:
00148 (left_touching, right_touching, left_force, right_force) = self.cm.get_find_gripper_contact_result()
00149 rospy.loginfo("left_touching: %d, left_force: %0.3f, right_touching: %d, right_force: %0.3f"\
00150 %(left_touching, left_force, right_touching, right_force))
00151 else:
00152 rospy.loginfo("GoalStatus was not SUCCEEDED; state=%d"%state)
00153 break
00154
00155
00156 if rospy.get_rostime() - start_time > rospy.Duration(timeout):
00157 rospy.loginfo("timeout reached on find_gripper_contact")
00158 break
00159
00160
00161 (l_pressure, r_pressure) = self.pressure_listener.get_thresholded_readings()
00162 if any(l_pressure[7:22]) and not any(saved_l_pressure[7:22]):
00163 rospy.loginfo("saving l_pressure:"+pplist(l_pressure))
00164 saved_l_pressure = l_pressure
00165 if any(r_pressure[7:22]) and not any(saved_r_pressure[7:22]):
00166 rospy.loginfo("saving r_pressure:"+pplist(r_pressure))
00167 saved_r_pressure = r_pressure
00168
00169 time.sleep(.02)
00170
00171
00172 if contacts_desired == "either" and (not any(saved_l_pressure[7:22]) and not any(saved_r_pressure[7:22])) or \
00173 contacts_desired == "both" and (not any(saved_l_pressure[7:22]) or not any(saved_r_pressure[7:22])):
00174 start_time = rospy.get_rostime()
00175 while not rospy.is_shutdown():
00176
00177
00178 if rospy.get_rostime() - start_time > rospy.Duration(1.0):
00179 rospy.loginfo("timed out on extra second of waiting for readings")
00180 break
00181
00182
00183 (l_pressure, r_pressure) = self.pressure_listener.get_thresholded_readings()
00184 if any(l_pressure[7:22]) and not any(saved_l_pressure[7:22]):
00185 rospy.loginfo("saving l_pressure:"+pplist(l_pressure))
00186 saved_l_pressure = l_pressure
00187 if any(r_pressure[7:22]) and not any(saved_r_pressure[7:22]):
00188 rospy.loginfo("saving r_pressure:"+pplist(r_pressure))
00189 saved_r_pressure = r_pressure
00190 if contacts_desired == "either" and (any(saved_l_pressure[7:22]) or any(saved_r_pressure[7:22])):
00191 break
00192 if contacts_desired == "both" and (any(saved_l_pressure[7:22]) and any(saved_r_pressure[7:22])):
00193 break
00194 time.sleep(.02)
00195
00196
00197
00198 (rel_l_pressure, rel_r_pressure) = self.pressure_listener.get_relative_readings()
00199 if left_touching and not any(saved_l_pressure[7:22]):
00200 most_likely_on = 7+rel_l_pressure[7:22].index(max(rel_l_pressure[7:22]))
00201 saved_l_pressure[most_likely_on] = 1
00202 rospy.loginfo("left_touching but not any saved_l_pressure, fixing")
00203 if right_touching and not any(saved_r_pressure[7:22]):
00204 most_likely_on = 7+rel_r_pressure[7:22].index(max(rel_r_pressure[7:22]))
00205 saved_r_pressure[most_likely_on] = 1
00206 rospy.loginfo("right_touching but not any saved_r_pressure, fixing")
00207
00208 return (saved_l_pressure, saved_r_pressure, left_touching, right_touching, left_force, right_force)
00209
00210
00211
00212
00213 def compliant_close(self):
00214 self.check_preempt()
00215
00216 self.broadcast_phase(ManipulationPhase.CLOSING)
00217 self.cm.check_controllers_ok('cartesian')
00218
00219 use_slip_controller_to_close = self.using_slip_detection
00220
00221
00222 step_time = .05
00223 left_touching = right_touching = 0
00224
00225
00226 if use_slip_controller_to_close:
00227 rospy.loginfo("finding first gripper contact")
00228 self.cm.find_gripper_contact(contacts_desired = "either", zero_fingertips = 1, blocking = 0, timeout = 10.)
00229 (l_pressure, r_pressure, left_touching, right_touching, left_force, right_force) = \
00230 self.get_fingertip_readings_during_find_contact(contacts_desired = "either")
00231 else:
00232
00233 desired_pos = self.cm.get_current_gripper_opening()
00234 while(desired_pos > .01):
00235 self.cm.command_gripper(desired_pos, 50)
00236 (l_pressure, r_pressure) = self.pressure_listener.get_thresholded_readings()
00237 if any(l_pressure):
00238 left_touching = 1
00239 break
00240 if any(r_pressure):
00241 right_touching = 1
00242 break
00243 desired_pos -= .001
00244 time.sleep(step_time)
00245
00246 self.check_preempt()
00247
00248
00249 if not (left_touching and right_touching):
00250
00251
00252 reloaded_controllers = 0
00253 if left_touching or right_touching:
00254
00255 if not use_slip_controller_to_close:
00256
00257 desired_pos += .002
00258 self.cm.command_gripper(desired_pos, 200)
00259
00260
00261 print "saw first contact! Reloading Cartesian controllers"
00262 if left_touching:
00263 self.cm.cart_params.tip_frame = self.whicharm+'_gripper_l_finger_tip_link'
00264 else:
00265 self.cm.cart_params.tip_frame = self.whicharm+'_gripper_r_finger_tip_link'
00266 self.cm.cart_params.set_params_to_defaults(set_tip_frame = 0, set_params_on_server = 1)
00267 self.cm.reload_cartesian_controllers(set_params = 0)
00268 reloaded_controllers = 1
00269 print "done reloading Cartesian controllers"
00270
00271 if self._photo:
00272 keypause()
00273
00274 self.check_preempt()
00275
00276
00277 if use_slip_controller_to_close:
00278 rospy.loginfo("finding second gripper contact")
00279 contacts_desired = "both"
00280 self.cm.find_gripper_contact(contacts_desired, zero_fingertips = 0, blocking = 0, timeout = 10.)
00281 (l_pressure, r_pressure, left_touching, right_touching, left_force, right_force) = \
00282 self.get_fingertip_readings_during_find_contact(contacts_desired = "both")
00283
00284 else:
00285 left_touching = right_touching = 0
00286 while(desired_pos > 0):
00287 self.cm.command_gripper(desired_pos, 50)
00288 (l_pressure, r_pressure) = self.pressure_listener.get_thresholded_readings()
00289 if any(l_pressure):
00290 left_touching = 1
00291 if any(r_pressure):
00292 right_touching = 1
00293 if left_touching and right_touching:
00294 print "saw both left and right contacts, stopping"
00295 break
00296 desired_pos -= .001
00297 time.sleep(step_time)
00298
00299 if self._photo:
00300 keypause()
00301
00302 self.check_preempt()
00303
00304
00305 if reloaded_controllers:
00306 print "putting control frame back"
00307 self.cm.cart_params.set_params_to_gentle(set_tip_frame = 1, set_params_on_server = 1)
00308 self.cm.reload_cartesian_controllers(set_params = 0)
00309 print "done reloading Cartesian controllers"
00310
00311 self.check_preempt()
00312
00313 return (l_pressure, r_pressure)
00314
00315
00316
00317 def move_cartesian_step(self, pose, timeout = 10.0, settling_time = 3.0, blocking = 0):
00318 self.check_preempt()
00319
00320 if type(pose) == list:
00321 pose = create_pose_stamped(pose, 'base_link')
00322 self.cm.move_cartesian(pose, blocking = blocking, \
00323 pos_thres = .0025, rot_thres = .05, \
00324 timeout = rospy.Duration(timeout), \
00325 settling_time = rospy.Duration(settling_time))
00326
00327
00328
00329 def check_guarded_move_contacts(self):
00330
00331 left_touching = right_touching = palm_touching = 0
00332
00333
00334 (l_regions_touching, r_regions_touching) = self.pressure_listener.regions_touching()
00335 if self.palm_touching():
00336 palm_touching = 1
00337 rospy.loginfo("saw palm sensor, stopping")
00338 if any(l_regions_touching[0:3]) or l_regions_touching[4]:
00339 left_touching = 1
00340 rospy.loginfo("saw left fingertip tip or side, stopping")
00341 if any(r_regions_touching[0:3] or r_regions_touching[4]):
00342 right_touching = 1
00343 rospy.loginfo("saw right fingertip tip or side, stopping")
00344 return (left_touching, right_touching, palm_touching)
00345
00346
00347
00348
00349 def guarded_move_cartesian(self, pose_stamped, timeout = 3.0, settling_time = 0.5):
00350
00351 self.check_preempt()
00352
00353
00354 rospy.loginfo("sending goal to Cartesian controllers")
00355 (pos, rot) = pose_stamped_to_lists(self.cm.tf_listener, pose_stamped, 'base_link')
00356 self.move_cartesian_step(pos+rot, timeout, settling_time)
00357
00358
00359 start_time = rospy.get_rostime()
00360 done_time = None
00361 while(1):
00362
00363 self.check_preempt()
00364
00365
00366 (left_touching, right_touching, palm_touching) = self.check_guarded_move_contacts()
00367
00368
00369 if left_touching or right_touching or palm_touching:
00370 rospy.loginfo("saw contact")
00371 self.cm.switch_to_joint_mode()
00372 self.cm.freeze_arm()
00373 break
00374
00375
00376 if self.cm.check_cartesian_really_done(pose_stamped, .0025, .05):
00377 rospy.loginfo("actually got there")
00378 break
00379
00380
00381 if not done_time and self.cm.check_cartesian_done():
00382 rospy.loginfo("check_cartesian_done returned 1")
00383 done_time = rospy.get_rostime()
00384
00385
00386 if done_time and rospy.get_rostime() - done_time > rospy.Duration(settling_time):
00387 rospy.loginfo("done settling")
00388 break
00389
00390
00391 if timeout != 0. and rospy.get_rostime() - start_time > rospy.Duration(timeout):
00392 rospy.loginfo("timed out")
00393 break
00394
00395
00396 return (left_touching, right_touching, palm_touching)
00397
00398
00399
00400 def guarded_move_ik(self, pose_stamped, max_joint_vel = .05):
00401
00402 self.check_preempt()
00403
00404
00405 success = self.cm.command_interpolated_ik(pose_stamped, collision_aware = 0, step_size = .005, max_joint_vel = max_joint_vel)
00406
00407
00408 if not success:
00409 (left_touching, right_touching, palm_touching) = self.guarded_move_cartesian(pose_stamped, 10.0, 5.0)
00410 return (left_touching, right_touching, palm_touching)
00411
00412
00413 while(1):
00414
00415 self.check_preempt()
00416
00417
00418 (left_touching, right_touching, palm_touching) = self.check_guarded_move_contacts()
00419
00420
00421 if left_touching or right_touching or palm_touching:
00422 self.cm.freeze_arm()
00423 break
00424
00425
00426 if self.cm.check_joint_trajectory_done():
00427 break
00428
00429
00430 return (left_touching, right_touching, palm_touching)
00431
00432
00433
00434 def vect_norm(self, vect):
00435 return sum([x**2 for x in vect])**.5
00436
00437
00438 def normalize_vect(self, vect):
00439 return list(scipy.array(vect)/self.vect_norm(vect))
00440
00441
00442 def vect_dot(self, vect1, vect2):
00443 return sum([x*y for (x,y) in zip(vect1, vect2)])
00444
00445
00446 def vect_proj(self, vect1, vect2):
00447 norm_vect2 = self.normalize_vect(vect2)
00448 mag = self.vect_dot(vect1, norm_vect2)
00449 proj = [mag*x for x in norm_vect2]
00450 return proj
00451
00452
00453 def make_orthogonal(self, vect1, vect2):
00454 vect1_mag = self.vect_norm(vect1)
00455 projected_comp = self.vect_proj(vect1, vect2)
00456 orthogonal_comp = [x-y for (x,y) in zip(vect1, projected_comp)]
00457 orthogonal_comp_mag = self.vect_norm(orthogonal_comp)
00458 orthogonal_vect = [vect1_mag * x / orthogonal_comp_mag for x in orthogonal_comp]
00459 return orthogonal_vect
00460
00461
00462
00463
00464
00465 def return_rel_pose(self, vector, frame, start_pose = None, orthogonal_to_vect = None, orthogonal_to_vect_frame = 'base_link'):
00466
00467
00468 if start_pose == None:
00469 (start_trans, start_rot) = self.cm.return_cartesian_pose('base_link')
00470 else:
00471 start_pose.header.stamp = rospy.Time(0)
00472 (start_trans, start_rot) = pose_stamped_to_lists(self.cm.tf_listener, start_pose, 'base_link')
00473
00474
00475 if frame != 'base_link':
00476 vector3_stamped = create_vector3_stamped(vector, frame)
00477 base_link_vector = vector3_stamped_to_list(self.cm.tf_listener, vector3_stamped, 'base_link')
00478 else:
00479 base_link_vector = vector
00480
00481
00482
00483
00484
00485
00486 if orthogonal_to_vect != None:
00487
00488
00489 if orthogonal_to_vect_frame != 'base_link':
00490 ortho_vector3_stamped = create_vector3_stamped(orthogonal_to_vect, orthogonal_to_vect_frame)
00491 ortho_base_link_vector = vector3_stamped_to_list(self.cm.tf_listener, ortho_vector3_stamped, 'base_link')
00492 else:
00493 ortho_base_link_vector = orthogonal_to_vect
00494
00495
00496 base_link_vector = self.make_orthogonal(base_link_vector, ortho_base_link_vector)
00497
00498
00499 new_trans = [x+y for (x,y) in zip(start_trans, base_link_vector)]
00500
00501
00502 pose_stamped = create_pose_stamped(new_trans+start_rot)
00503
00504
00505
00506
00507
00508
00509
00510 return pose_stamped
00511
00512
00513
00514 def check_goal_reached(self, approach_vect, goal_pos, goal_pos_thres):
00515 (current_pos, current_rot) = self.cm.return_cartesian_pose()
00516
00517
00518 current_proj = sum([x*y for (x,y) in zip(current_pos, approach_vect)])
00519 goal_proj = sum([x*y for (x,y) in zip(goal_pos, approach_vect)])
00520
00521 posdiff = goal_proj-current_proj
00522 print "current_proj = %5.3f, goal_proj = %5.3f, posdiff:%5.3f"%(current_proj, goal_proj, posdiff)
00523 print "current_pos:", self.cm.pplist(current_pos)
00524
00525 if posdiff < goal_pos_thres:
00526 return 1
00527
00528 return 0
00529
00530
00531
00532 def compute_approach_dir(self, approach_pose, grasp_pose):
00533 approach_vect = self.compute_approach_vect(approach_pose, grasp_pose)
00534 approach_dir = approach_vect/scipy.dot(approach_vect, approach_vect)**.5
00535 return approach_dir
00536
00537
00538
00539 def compute_approach_vect(self, approach_pose, grasp_pose):
00540 (approach_pos, approach_rot) = pose_stamped_to_lists(self.cm.tf_listener, approach_pose, 'base_link')
00541 (grasp_pos, grasp_rot) = pose_stamped_to_lists(self.cm.tf_listener, grasp_pose, 'base_link')
00542 approach_vect = scipy.array(grasp_pos) - scipy.array(approach_pos)
00543 return approach_vect
00544
00545
00546
00547 def palm_touching(self):
00548
00549
00550 return 0
00551
00552
00553
00554
00555
00556
00557
00558
00559 def reactive_approach(self, approach_dir, grasp_pose, joint_path = None,
00560 side_step = .015, back_step = .03, \
00561 num_tries = 10, goal_pos_thres = 0.01):
00562
00563 self.check_preempt()
00564
00565 self.broadcast_phase(ManipulationPhase.MOVING_TO_GRASP)
00566
00567 ever_saw_left = ever_saw_right = 0
00568
00569
00570 (left_touching, right_touching, palm_touching) = self.check_guarded_move_contacts()
00571
00572 if not left_touching and not right_touching and not palm_touching:
00573 if 0:
00574
00575
00576
00577 self.cm.command_joint_trajectory(joint_path, 0.1)
00578 while not self.cm.check_joint_trajectory_done():
00579 (left_touching, right_touching, palm_touching) = self.check_guarded_move_contacts()
00580
00581 if left_touching or right_touching or palm_touching:
00582 self.cm.freeze_arm()
00583 break
00584 else:
00585
00586
00587 (left_touching, right_touching, palm_touching) = self.guarded_move_ik(grasp_pose)
00588
00589
00590 if not left_touching and not right_touching and not palm_touching:
00591 rospy.loginfo("approach move finished with no contacts")
00592 return self.reactive_approach_result_dict["success"]
00593
00594
00595
00596 time.sleep(1.0)
00597 (goal_pos, goal_rot) = pose_stamped_to_lists(self.cm.tf_listener, grasp_pose, 'base_link')
00598 if self.check_goal_reached(approach_dir, goal_pos, goal_pos_thres):
00599 rospy.loginfo("made it to within goal threshold")
00600 return self.reactive_approach_result_dict["within goal threshold"]
00601
00602
00603 if palm_touching:
00604 rospy.loginfo("saw palm contact")
00605 return self.reactive_approach_result_dict["saw palm contact"]
00606
00607
00608 if left_touching and right_touching:
00609 rospy.loginfo("both fingers touching")
00610 return self.reactive_approach_result_dict["both fingers touching"]
00611
00612
00613 if (left_touching or right_touching) and self.check_fingers_in_table():
00614 rospy.loginfo("touching the table! Returning")
00615 return self.reactive_approach_result_dict["touching table"]
00616
00617
00618 print "saw one contact"
00619 tries = 0
00620 new_goal = grasp_pose
00621 while(tries < num_tries):
00622
00623 rospy.loginfo("approach step %d"%tries)
00624
00625 if self._photo:
00626 keypause()
00627
00628
00629 back_off_vect = -approach_dir*back_step
00630 pose = self.return_rel_pose(back_off_vect, 'base_link')
00631 self.cm.move_cartesian_ik(pose)
00632
00633
00634 self.check_preempt()
00635
00636
00637 self.pressure_listener.set_thresholds(short = 1)
00638
00639 self.check_preempt()
00640
00641 if self._photo:
00642 keypause()
00643
00644
00645 if left_touching:
00646 rospy.loginfo("side-stepping to the left")
00647 side_step_vect = [0., side_step, 0.]
00648
00649
00650 if right_touching:
00651 rospy.loginfo("side-stepping to the right")
00652 side_step_vect = [0., -side_step, 0.]
00653
00654
00655 pose = self.return_rel_pose(side_step_vect, self.whicharm+'_wrist_roll_link', orthogonal_to_vect = approach_dir)
00656 (left_touching, right_touching, palm_touching) = self.guarded_move_ik(pose)
00657
00658 new_goal = self.return_rel_pose(side_step_vect, self.whicharm+'_wrist_roll_link', new_goal)
00659
00660 self.check_preempt()
00661
00662 if self._photo:
00663 keypause()
00664
00665
00666
00667 if not left_touching and not right_touching:
00668 (goal_pos, goal_rot) = pose_stamped_to_lists(self.cm.tf_listener, new_goal, 'base_link')
00669 rospy.loginfo("going to new goal, pos: "+self.cm.pplist(goal_pos)+" rot: "+self.cm.pplist(goal_rot))
00670 (left_touching, right_touching, palm_touching) = self.guarded_move_ik(new_goal)
00671
00672
00673 self.check_preempt()
00674
00675 if self._photo:
00676 keypause()
00677
00678
00679 if not left_touching and not right_touching and not palm_touching:
00680 rospy.loginfo("made it all the way without touching anything")
00681 return self.reactive_approach_result_dict["success"]
00682
00683
00684
00685 time.sleep(1.0)
00686 if self.check_goal_reached(approach_dir, goal_pos, goal_pos_thres):
00687 rospy.loginfo("within goal threshold")
00688 return self.reactive_approach_result_dict["within goal threshold"]
00689
00690 else:
00691 rospy.loginfo("hit something while side-stepping")
00692
00693
00694 if palm_touching:
00695 rospy.loginfo("saw palm contact")
00696 return self.reactive_approach_result_dict["saw palm contact"]
00697
00698
00699 if left_touching and right_touching:
00700 rospy.loginfo("seeing both contacts! Returning")
00701 return self.reactive_approach_result_dict["both fingers touching"]
00702
00703
00704 if (left_touching or right_touching) and self.check_fingers_in_table():
00705 rospy.loginfo("touching the table! Returning")
00706 return self.reactive_approach_result_dict["touching table"]
00707
00708
00709 ever_saw_left = ever_saw_left or left_touching
00710 ever_saw_right = ever_saw_right or right_touching
00711
00712
00713 if ever_saw_left and ever_saw_right:
00714 side_step /= 2.
00715 rospy.loginfo("saw both contacts, halving the side-step size to %5.3f"%side_step)
00716 ever_saw_right = right_touching
00717 ever_saw_left = left_touching
00718
00719 tries += 1
00720
00721 rospy.loginfo("ran out of tries")
00722 return self.reactive_approach_result_dict["ran out of tries"]
00723
00724
00725
00726
00727
00728
00729 def check_if_contacts_need_adjusting(self, min_contact_row, l_readings = None, r_readings = None, check_table = 1):
00730
00731 (l_readings, r_readings, l_regions_touching, r_regions_touching, l_rows_touching, r_rows_touching, \
00732 l_columns_touching, r_columns_touching) = self.pressure_listener.get_sensor_status(l_readings, r_readings)
00733 self.pressure_listener.print_sensor_status(l_readings, r_readings)
00734
00735 front_touching = 1
00736 adjust_x = 0
00737 adjust_z = 0
00738 adjust_rot = 0
00739 fingers_in_table = 0
00740
00741
00742 if check_table and self.check_fingers_in_table():
00743 rospy.loginfo("fingers near table, grasp is probably fine despite not having any front contacts")
00744 return (1, 0, 0, 0)
00745
00746
00747 if not any(l_rows_touching) and not any(r_rows_touching):
00748
00749
00750 rospy.loginfo("no front elements touching")
00751 return (0, 0, 0, 0)
00752
00753
00754
00755 if min_contact_row >= 0:
00756 if self.palm_touching():
00757 rospy.loginfo("palm touching, can't move forward")
00758 else:
00759 r_contact_row = -1
00760 if any(r_rows_touching):
00761 r_contact_row = 4-r_rows_touching.index(1)
00762 l_contact_row = -1
00763 if any(l_rows_touching):
00764 l_contact_row = 4-l_rows_touching.index(1)
00765 rospy.loginfo("l contact row is %d, r contact row is %d"%(l_contact_row, r_contact_row))
00766 if l_contact_row < min_contact_row and r_contact_row < min_contact_row:
00767 rospy.loginfo("left and right contacts both not far enough in, need to move forward")
00768 adjust_x = 1
00769 if not adjust_x:
00770 rospy.loginfo("contacts are fine in x-direction")
00771
00772
00773
00774
00775 if l_columns_touching[1] or r_columns_touching[1]:
00776 rospy.loginfo("at least one center column touching, no need to adjust in z-direction")
00777
00778
00779 elif (l_columns_touching[0] or r_columns_touching[0]) and not (l_columns_touching[2] or r_columns_touching[2]):
00780 adjust_z = 1
00781
00782
00783 elif (l_columns_touching[2] or r_columns_touching[2]) and not (l_columns_touching[0] or r_columns_touching[0]):
00784 adjust_z = -1
00785
00786
00787 else:
00788 rospy.loginfo("contacts not all on one side, no need to adjust in z-direction")
00789
00790
00791
00792
00793 if l_columns_touching[0] and r_columns_touching[2] and not (l_columns_touching[2] or r_columns_touching[0]):
00794 adjust_rot = -1
00795
00796
00797 elif l_columns_touching[2] and r_columns_touching[0] and not(l_columns_touching[0] or r_columns_touching[2]):
00798 adjust_rot = 1
00799
00800 else:
00801 rospy.loginfo("no need to rotate about x")
00802
00803 rospy.loginfo("front_touching:%d, adjust_x:%d, adjust_z:%d, adjust_rot:%d"%(front_touching, adjust_x, adjust_z, adjust_rot))
00804 return (front_touching, adjust_x, adjust_z, adjust_rot)
00805
00806
00807
00808 def ppmat(self, mat):
00809 str = ''
00810 for i in range(mat.shape[0]):
00811 for j in range(mat.shape[1]):
00812 str += '%5.3f\t'%mat[i,j]
00813 str += '\n'
00814 return str
00815
00816
00817
00818 def check_gripper_opening(self, min_gripper_opening = .01, max_gripper_opening = .1):
00819 gripper_opening = self.cm.get_current_gripper_opening()
00820 rospy.loginfo("min_gripper_opening:"+str(min_gripper_opening))
00821 if min_gripper_opening < gripper_opening and gripper_opening < max_gripper_opening:
00822 rospy.loginfo("gripper opening was acceptable: %5.4f"%gripper_opening)
00823 return 1
00824
00825 rospy.loginfo("gripper opening unacceptable: %5.4f"%gripper_opening)
00826 return 0
00827
00828
00829
00830
00831
00832
00833 def lift_carefully(self, gripper_translation, min_gripper_opening = .0021, max_gripper_opening = .1, hardness_gain = 0.060):
00834
00835
00836 base_link_translation = change_vector3_stamped_frame(self.cm.tf_listener, gripper_translation.direction, 'base_link')
00837
00838
00839 start_wrist_pose = self.cm.get_current_wrist_pose_stamped()
00840
00841
00842
00843 if self.using_slip_detection:
00844 self.cm.start_gripper_grab(hardness_gain)
00845
00846 self.broadcast_phase(ManipulationPhase.LIFTING)
00847
00848
00849 desired_wrist_pose = copy.deepcopy(start_wrist_pose)
00850
00851
00852 desired_wrist_pose.header.stamp = rospy.Time(start_wrist_pose.header.stamp.secs)
00853 desired_wrist_pose.pose.position.x += base_link_translation.vector.x * gripper_translation.desired_distance
00854 desired_wrist_pose.pose.position.y += base_link_translation.vector.y * gripper_translation.desired_distance
00855 desired_wrist_pose.pose.position.z += base_link_translation.vector.z * gripper_translation.desired_distance
00856 self.move_cartesian_step(desired_wrist_pose, blocking = 1)
00857
00858
00859 success = self.check_gripper_opening(min_gripper_opening, max_gripper_opening)
00860
00861 return success
00862
00863
00864
00865
00866 def shake_object(self, arm_angles = None, min_gripper_opening = .0021, max_gripper_opening = .1,
00867 wrist_roll_shake_time = 0.5, wrist_flex_shake_time = 0.5, arm_pan_shake_time = 0.25):
00868
00869
00870 wrist_flex = -1.03
00871 wrist_roll = -.5
00872 if arm_angles == None:
00873 if self.whicharm == 'r':
00874 arm_angles = [-0.28, -0.21, -0.78, -1.02, -1.02, wrist_flex, wrist_roll]
00875 else:
00876 arm_angles = [0.28, -0.21, 0.78, -1.02, 1.02, wrist_flex, wrist_roll]
00877
00878 rospy.loginfo("moving to shake arm angles")
00879 self.broadcast_phase(ManipulationPhase.MOVING_WITH_OBJECT)
00880 self.cm.command_joint_trajectory([arm_angles], blocking = 1)
00881
00882
00883 self.broadcast_phase(ManipulationPhase.HOLDING_OBJECT)
00884 time.sleep(4)
00885
00886
00887 success = self.check_gripper_opening(min_gripper_opening, max_gripper_opening)
00888 if not success:
00889 return 0
00890
00891
00892 shake_amplitude = 1.75*1.57
00893 roll_pos_angles = arm_angles[:]
00894 roll_pos_angles[6] += shake_amplitude
00895 roll_neg_angles = arm_angles[:]
00896 roll_neg_angles[6] -= shake_amplitude
00897
00898 roll_trajectory = [roll_pos_angles, roll_neg_angles, roll_pos_angles, roll_neg_angles, arm_angles]
00899 times = [wrist_roll_shake_time*i+0.05 for i in range(len(roll_trajectory))]
00900 vels = [[0.0]*7 for i in range(len(roll_trajectory))]
00901
00902 rospy.loginfo("starting roll shake")
00903 self.broadcast_phase(ManipulationPhase.SHAKING)
00904 self.cm.command_joint_trajectory(roll_trajectory, blocking = 1, times_and_vels = (times, vels))
00905
00906
00907 time.sleep(4)
00908
00909
00910 success = self.check_gripper_opening(min_gripper_opening, max_gripper_opening)
00911 if not success:
00912 return 0
00913
00914
00915 flex_pos_angles = arm_angles[:]
00916 flex_pos_angles[5] += .78
00917 flex_neg_angles = arm_angles[:]
00918 flex_neg_angles[5] -= .78
00919
00920 flex_trajectory = [flex_pos_angles, flex_neg_angles, flex_pos_angles, flex_neg_angles, arm_angles]
00921 times = [wrist_flex_shake_time*i+0.05 for i in range(len(flex_trajectory))]
00922 vels = [[0.0]*7 for i in range(len(flex_trajectory))]
00923
00924 rospy.loginfo("starting flex shake")
00925 self.broadcast_phase(ManipulationPhase.SHAKING)
00926 self.cm.command_joint_trajectory(flex_trajectory, blocking = 1, times_and_vels = (times, vels))
00927
00928
00929 time.sleep(4)
00930
00931
00932 success = self.check_gripper_opening(min_gripper_opening, max_gripper_opening)
00933 if not success:
00934 return 0
00935
00936
00937 pan_pos_angles = arm_angles[:]
00938 pan_neg_angles = arm_angles[:]
00939 shake_amplitude = 0.125
00940 if self.whicharm == 'r':
00941 pan_pos_angles[0] += shake_amplitude
00942 pan_neg_angles[0] -= shake_amplitude
00943 else:
00944 pan_pos_angles[0] -= shake_amplitude
00945 pan_neg_angles[0] += shake_amplitude
00946 pan_trajectory = [pan_pos_angles, pan_neg_angles, pan_pos_angles, pan_neg_angles, arm_angles]
00947 times = [arm_pan_shake_time*i+0.05 for i in range(len(pan_trajectory))]
00948 vels = [[0.0]*7 for i in range(len(pan_trajectory))]
00949
00950 rospy.loginfo("starting pan shake")
00951 self.broadcast_phase(ManipulationPhase.SHAKING)
00952 self.cm.command_joint_trajectory(pan_trajectory, blocking = 1, times_and_vels = (times, vels))
00953
00954
00955 time.sleep(4)
00956
00957
00958 success = self.check_gripper_opening(min_gripper_opening, max_gripper_opening)
00959 if not success:
00960 return 0
00961
00962 return 1
00963
00964
00965
00966
00967
00968 def place_carefully(self, final_place_pose, place_overshoot, min_gripper_opening = .0021, \
00969 max_gripper_opening = .1):
00970
00971 self.broadcast_phase(ManipulationPhase.PLACING)
00972
00973
00974 if not self.check_gripper_opening(min_gripper_opening, max_gripper_opening):
00975 self.broadcast_phase(ManipulationPhase.FAILED)
00976 self.cm.switch_to_joint_mode()
00977 return 0
00978
00979
00980 (start_wrist_pos, start_wrist_rot) = self.cm.return_cartesian_pose()
00981
00982
00983 (place_pos, place_rot) = pose_stamped_to_lists(self.cm.tf_listener, final_place_pose, 'base_link')
00984 place_vect = scipy.array(place_pos) - scipy.array(start_wrist_pos)
00985 place_vect_mag = (place_vect*place_vect).sum()
00986 overshoot_vect = place_vect / place_vect_mag * (place_vect_mag + place_overshoot)
00987 overshoot_pos = overshoot_vect + place_pos
00988 overshoot_pose = create_pose_stamped(overshoot_pos.tolist() + place_rot, 'base_link')
00989
00990
00991 self.check_preempt()
00992 if self.using_slip_detection:
00993 self.cm.start_gripper_event_detector(blocking = 0)
00994
00995
00996 self.move_cartesian_step(overshoot_pose, blocking = 0)
00997
00998
00999 start_time = rospy.get_rostime()
01000 timeout = 5.
01001 while not rospy.is_shutdown():
01002 self.check_preempt()
01003
01004
01005 if self.using_slip_detection:
01006 state = self.cm.get_gripper_event_detector_state()
01007
01008
01009 if state not in [GoalStatus.ACTIVE, GoalStatus.PENDING]:
01010 rospy.loginfo("place carefully saw the trigger, stopping the arm")
01011 break
01012
01013
01014 if rospy.get_rostime() - start_time > rospy.Duration(timeout):
01015 rospy.loginfo("place carefully timed out before seeing the trigger, stopping the arm")
01016 break
01017
01018 time.sleep(0.1)
01019
01020
01021 self.cm.switch_to_joint_mode()
01022 self.cm.freeze_arm()
01023
01024
01025 self.broadcast_phase(ManipulationPhase.OPENING)
01026 self.cm.command_gripper(.1, -1, 1)
01027
01028 return 1
01029
01030
01031
01032
01033 def close_hard(self, force = None):
01034
01035 if not self.using_slip_detection:
01036 self.broadcast_phase(ManipulationPhase.CLOSING)
01037 tries = 0
01038 self._closed_hard = 1
01039 if force == None:
01040 force = self.close_force
01041 self.cm.command_gripper(0, force, 1)
01042
01043
01044
01045 def open_and_reset_fingertips(self, reset = 0):
01046
01047
01048 rospy.loginfo("opening gripper")
01049 self.cm.command_gripper(.1, -1, 1)
01050
01051 self.check_preempt()
01052
01053
01054 if self._closed_hard:
01055 rospy.loginfo("waiting for the fingertips to release")
01056 time.sleep(2)
01057 self._closed_hard = 0
01058 else:
01059 time.sleep(1)
01060
01061 self.check_preempt()
01062
01063 if reset:
01064
01065 self.pressure_listener.set_thresholds(short = 1)
01066
01067 self.check_preempt()
01068
01069
01070
01071
01072 def check_fingers_in_table(self, gripper_pose = None):
01073
01074
01075 (l_regions_touching, r_regions_touching) = self.pressure_listener.regions_touching()
01076
01077
01078 if not (l_regions_touching[0] or r_regions_touching[0]):
01079 return 0
01080
01081 if gripper_pose == None:
01082 gripper_pose = self.cm.get_current_wrist_pose_stamped()
01083 current_angles = self.cm.get_current_arm_angles()
01084 collision_oper1 = CollisionOperation(object1 = CollisionOperation.COLLISION_SET_ALL, \
01085 object2 = CollisionOperation.COLLISION_SET_ALL, \
01086 operation = CollisionOperation.DISABLE)
01087 collision_oper2 = CollisionOperation(object1 = self.whicharm+"_gripper_r_finger_tip_link", \
01088 object2 = self._table_name, \
01089 operation = CollisionOperation.ENABLE)
01090 collision_oper3 = CollisionOperation(object1 = self.whicharm+"_gripper_l_finger_tip_link", \
01091 object2 = self._table_name, \
01092 operation = CollisionOperation.ENABLE)
01093 ordered_collision_operations = OrderedCollisionOperations([collision_oper1, collision_oper2, collision_oper3])
01094
01095 (pos, error_code) = self.cm.ik_utilities.run_ik(gripper_pose, current_angles, self.cm.ik_utilities.link_name, collision_aware = 1, \
01096 ordered_collision_operations = ordered_collision_operations)
01097 if error_code == "IK_LINK_IN_COLLISION":
01098 rospy.loginfo("fingers are near the table")
01099 return 1
01100 rospy.loginfo("fingers not near the table")
01101 return 0
01102
01103
01104
01105
01106 def adjust_grasp(self, grasp_pose, initial_l_readings, initial_r_readings, z_step = .015, x_step = .015, min_gripper_opening = .0021, max_gripper_opening = .1, min_contact_row = 1, num_tries = 4):
01107
01108 self.check_preempt()
01109
01110
01111 if not self.check_gripper_opening(min_gripper_opening, max_gripper_opening):
01112 return ("gripper opening unacceptable", grasp_pose)
01113
01114
01115 (front_touching, adjust_x, adjust_z, adjust_rot) = self.check_if_contacts_need_adjusting(min_contact_row, initial_l_readings, initial_r_readings)
01116
01117
01118 tries = 0
01119 current_grasp_pose = grasp_pose
01120 self._closed_hard = 0
01121 while(1):
01122
01123 self.check_preempt()
01124
01125
01126 if tries >= num_tries:
01127
01128
01129 rospy.loginfo("ran out of tries while adjusting the grasp, trying to close hard anyway")
01130 self.close_hard()
01131
01132
01133 if not self.check_gripper_opening(min_gripper_opening, max_gripper_opening):
01134 return ("gripper opening unacceptable", current_grasp_pose)
01135 return ("grasp okay", current_grasp_pose)
01136
01137
01138
01139 rospy.loginfo("adjust_grasp tries: "+str(tries))
01140
01141
01142 if adjust_x or adjust_z:
01143
01144 self.broadcast_phase(ManipulationPhase.ADJUSTING_GRASP)
01145
01146
01147 adjust_mat = scipy.matrix(scipy.identity(4))
01148 adjust_vect = scipy.matrix([0,0,0]).T
01149 if adjust_z:
01150 adjust_mat[2, 3] = adjust_z * z_step
01151 if adjust_x:
01152 adjust_mat[0, 3] = x_step
01153
01154 new_grasp_pose = transform_pose_stamped(current_grasp_pose, adjust_mat)
01155
01156 self.check_preempt()
01157
01158
01159 rospy.loginfo("opening gripper to adjust hand")
01160 self.open_and_reset_fingertips(reset = 1)
01161
01162 self.check_preempt()
01163 if self._photo:
01164 keypause()
01165
01166
01167 current_grasp_pose = new_grasp_pose
01168
01169
01170 (left_touching, right_touching, palm_touching) = self.guarded_move_ik(current_grasp_pose)
01171
01172 self.check_preempt()
01173 if self._photo:
01174 keypause()
01175
01176
01177 (l_readings, r_readings) = self.compliant_close()
01178
01179 self.check_preempt()
01180 if self._photo:
01181 keypause()
01182
01183
01184 current_grasp_pose = self.cm.get_current_wrist_pose_stamped()
01185
01186
01187 if not self.check_gripper_opening(min_gripper_opening, max_gripper_opening):
01188 return ("gripper opening unacceptable", current_grasp_pose)
01189
01190
01191 (front_touching, adjust_x, adjust_z, adjust_rot) = self.check_if_contacts_need_adjusting(min_contact_row, l_readings, r_readings)
01192
01193
01194 grasp_okay = 1
01195 if not front_touching or adjust_x or adjust_z:
01196 grasp_okay = 0
01197
01198
01199 if grasp_okay:
01200 self.check_preempt()
01201
01202 rospy.loginfo("contacts are fine")
01203 self.close_hard()
01204
01205 if self._photo:
01206 keypause()
01207
01208
01209 if not self.check_gripper_opening(min_gripper_opening, max_gripper_opening):
01210 return ("gripper opening unacceptable", current_grasp_pose)
01211
01212
01213 (front_touching, adjust_x, adjust_z, adjust_rot) = self.check_if_contacts_need_adjusting(min_contact_row)
01214 grasp_okay = 1
01215 if not front_touching or adjust_x or adjust_z:
01216 grasp_okay = 0
01217
01218
01219 if grasp_okay:
01220 rospy.loginfo("contacts are fine")
01221 result = "grasp okay"
01222 break
01223
01224 tries += 1
01225
01226 return (result, current_grasp_pose)
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237 def reactive_grasp(self, approach_pose, grasp_pose, joint_path = None, side_step = .015, back_step = .03, \
01238 approach_num_tries = 10, goal_pos_thres = 0.01, min_gripper_opening = 0.0021, max_gripper_opening = 0.1, \
01239 grasp_num_tries = 2, forward_step = 0.03, min_contact_row = 1, object_name = "points", table_name = "table", grasp_adjust_x_step = .02, grasp_adjust_z_step = .015, grasp_adjust_num_tries = 3):
01240
01241 self._table_name = table_name
01242
01243 self.check_preempt()
01244
01245
01246 if approach_pose == None:
01247 approach_pose = self.cm.get_current_wrist_pose_stamped()
01248
01249
01250 self.pressure_listener.set_thresholds()
01251
01252
01253 self.check_preempt()
01254
01255
01256 if approach_pose.header.frame_id != 'base_link':
01257 self.cm.tf_listener.waitForTransform('base_link', approach_pose.header.frame_id, rospy.Time.now(), rospy.Duration(5))
01258 try:
01259 approach_pose = self.cm.tf_listener.transformPose('base_link', approach_pose)
01260 except:
01261 rospy.logerr("tf transform was not there!")
01262 self.throw_exception()
01263 return self.reactive_grasp_result_dict["aborted"]
01264
01265 if grasp_pose.header.frame_id != 'base_link':
01266 self.cm.tf_listener.waitForTransform('base_link', grasp_pose.header.frame_id, rospy.Time.now(), rospy.Duration(5))
01267 try:
01268 grasp_pose = self.cm.tf_listener.transformPose('base_link', grasp_pose)
01269 except:
01270 rospy.logerr("tf transform was not there!")
01271 self.throw_exception()
01272 return self.reactive_grasp_result_dict["aborted"]
01273
01274
01275 approach_dir = self.compute_approach_dir(approach_pose, grasp_pose)
01276
01277 num_tries = 0
01278 current_goal_pose = grasp_pose
01279 while 1:
01280
01281 if approach_num_tries:
01282
01283 approach_result = self.reactive_approach(approach_dir, current_goal_pose, joint_path, \
01284 side_step, back_step, \
01285 approach_num_tries, goal_pos_thres)
01286
01287
01288 if approach_result == self.reactive_approach_result_dict["ran out of tries"]:
01289 return self.reactive_grasp_result_dict["ran out of approach tries"]
01290 elif approach_result == self.reactive_approach_result_dict["grasp infeasible"]:
01291 return self.reactive_grasp_result_dict["grasp infeasible"]
01292
01293 else:
01294
01295
01296 self.broadcast_phase(ManipulationPhase.MOVING_TO_GRASP)
01297 rospy.loginfo("reactive approach is disabled, moving to the goal")
01298 self.move_cartesian_step(grasp_pose, timeout = 10.0, settling_time = 5.0, blocking = 1)
01299
01300
01301 rospy.loginfo("starting compliant_close")
01302 (l_readings, r_readings) = self.compliant_close()
01303
01304
01305 current_goal_pose = self.cm.get_current_wrist_pose_stamped()
01306
01307
01308 (adjust_result, current_goal_pose) = self.adjust_grasp(current_goal_pose, l_readings, r_readings, z_step = grasp_adjust_z_step, \
01309 x_step = grasp_adjust_x_step, min_gripper_opening = min_gripper_opening, max_gripper_opening = max_gripper_opening, \
01310 min_contact_row = min_contact_row, num_tries = grasp_adjust_num_tries)
01311 rospy.loginfo("adjust_result: "+adjust_result)
01312
01313
01314 if adjust_result == "grasp okay":
01315 return self.reactive_grasp_result_dict["success"]
01316
01317
01318 if self.palm_touching():
01319 return self.reactive_grasp_result_dict["success"]
01320
01321
01322 num_tries += 1
01323 if num_tries < grasp_num_tries:
01324
01325
01326 move_forward_vect = approach_dir*forward_step
01327 current_goal_pose = self.return_rel_pose(move_forward_vect, 'base_link', current_goal_pose)
01328
01329 joint_path = None
01330
01331
01332 (goal_pos, goal_rot) = pose_stamped_to_lists(self.cm.tf_listener, current_goal_pose, 'base_link')
01333 rospy.loginfo("trying approach again with new goal, pos: "+self.cm.pplist(goal_pos)+" rot: "+self.cm.pplist(goal_rot))
01334
01335 if self._photo:
01336 keypause()
01337
01338
01339 self.open_and_reset_fingertips(reset = 1)
01340
01341 if self._photo:
01342 keypause()
01343
01344
01345 else:
01346 rospy.loginfo("ran out of grasp tries!")
01347 return self.reactive_grasp_result_dict["ran out of grasp tries"]
01348
01349
01350
01351
01352
01353 if __name__ == "__main__":
01354
01355 rospy.init_node('grasp_demo', anonymous = True)
01356
01357 print "initializing controller manager"
01358 cm = controller_manager.ControllerManager('r')
01359
01360 print "opening gripper"
01361 cm.command_gripper(.1, -1, 1)
01362 print "done"
01363
01364 print "initializing reactive grasper"
01365 rg = ReactiveGrasper(cm)
01366 print "done initialization"
01367
01368 cm.start_joint_controllers()
01369 cm.start_gripper_controller()
01370
01371
01372
01373
01374 table_height = .7239
01375 tip_dist_to_table = .12
01376 wrist_height = table_height + tip_dist_to_table + .02
01377
01378 topapproachpos = [.52, -.05, wrist_height+.1]
01379 topapproachquat = [-0.5, 0.5, 0.5, 0.5]
01380 topgrasppos = topapproachpos[:]
01381 topgrasppos[2] -= .08
01382 top_approach_pose = create_pose_stamped(topapproachpos+topapproachquat)
01383 top_grasp_pose = create_pose_stamped(topgrasppos+topapproachquat)
01384
01385
01386 sideangles = [-0.447, -0.297, -2.229, -0.719, 0.734, -1.489, 1.355]
01387 side_tip_dist_to_table = .14
01388 start_angles = sideangles
01389 tiltangle = math.pi/18.
01390 sideapproachmat = scipy.array([[0., -1., 0., 0.],
01391 [math.cos(tiltangle), 0., math.sin(tiltangle), 0.],
01392 [-math.sin(tiltangle), 0., math.cos(tiltangle), 0.],
01393 [0., 0., 0., 1.]])
01394 sideapproachpos = [.63, -.3, table_height-.035+side_tip_dist_to_table]
01395 sideapproachquat = list(tf.transformations.quaternion_from_matrix(sideapproachmat))
01396 sidegrasppos = sideapproachpos[:]
01397 sidegrasppos[1] += .09
01398
01399 side_approach_pose = create_pose_stamped(sideapproachpos+sideapproachquat)
01400 side_grasp_pose = create_pose_stamped(sidegrasppos+sideapproachquat)
01401
01402
01403 top_approach_vect = rg.compute_approach_vect(top_approach_pose, top_grasp_pose)
01404 side_approach_vect = rg.compute_approach_vect(side_approach_pose, side_grasp_pose)
01405
01406 mode = 'none'
01407
01408 (pos, rot) = cm.return_cartesian_pose()
01409 currentgoal = pos+rot
01410
01411 small_step = .1
01412
01413 while(not rospy.is_shutdown()):
01414 print "enter top to go to the top approach position"
01415 print "side to go to the side approach position"
01416 print "rg to run the full reactive grasp"
01417 print "ra to approach once reactively"
01418 print "cc to close compliantly"
01419 print "c to close, o to open"
01420 print "u to go up, d to go down, r to go right, l to go left"
01421 print "a to go away from the robot, t to go toward the robot"
01422 print "j to go to a set of nearish-side-grasp angles"
01423 print "reset to reset the fingertip thresholds"
01424 print "f to print the current finger sensor values"
01425 print "s to stop"
01426 c = keypause()
01427
01428 if c == 'top':
01429 print "moving to top approach position"
01430 step_size = .01
01431 max_joint_vel = .05
01432 if mode == 'side' or mode == 'none':
01433 step_size = .03
01434 max_joint_vel = .15
01435 cm.command_interpolated_ik(top_approach_pose, collision_aware = 0, step_size = step_size, max_joint_vel = max_joint_vel)
01436 while not cm.check_joint_trajectory_done() and not rospy.is_shutdown():
01437 time.sleep(.1)
01438 print "done"
01439
01440
01441 (pos, rot) = cm.return_cartesian_pose()
01442 currentgoal = pos+rot
01443 mode = 'top'
01444
01445 elif c == 'side':
01446 print "moving to side approach position"
01447 step_size = .01
01448 max_joint_vel = .05
01449 if mode == 'top' or mode == 'none':
01450 step_size = .03
01451 max_joint_vel = .15
01452 cm.command_interpolated_ik(side_approach_pose, collision_aware = 0, step_size = step_size, max_joint_vel = max_joint_vel)
01453 while not cm.check_joint_trajectory_done():
01454 time.sleep(.1)
01455 print "done"
01456
01457
01458 (pos, rot) = cm.return_cartesian_pose()
01459 currentgoal = pos+rot
01460 mode = 'side'
01461
01462 elif c == 'j':
01463 print "moving to near-side-grasp angles"
01464 cm.command_joint(sideangles)
01465
01466
01467
01468
01469 elif c == 'rg' or c == 'ra':
01470 print "performing reactive grasp relative to current pose"
01471 current_pose = rg.cm.get_current_wrist_pose_stamped()
01472 if mode == 'top':
01473 new_grasp_pose = rg.return_rel_pose(top_approach_vect, 'base_link')
01474 grasp_num_tries = 2
01475 forward_step = .02
01476 elif mode == 'side':
01477 new_grasp_pose = rg.return_rel_pose(side_approach_vect, 'base_link')
01478 grasp_num_tries = 2
01479 forward_step = .06
01480 else:
01481 print "go to either top or side approach first"
01482 continue
01483
01484
01485 current_angles = cm.get_current_arm_angles()
01486 (trajectory, error_codes) = cm.ik_utilities.check_cartesian_path(current_pose, new_grasp_pose, current_angles, collision_aware = 0)
01487
01488
01489 if any(error_codes):
01490 rospy.loginfo("can't execute an interpolated IK trajectory to the grasp pose, using Cartesian controllers instead")
01491 if c == 'ra':
01492
01493
01494 approach_dir = rg.compute_approach_dir(current_pose, new_grasp_pose)
01495
01496 rg.reactive_approach(approach_dir, new_grasp_pose)
01497 else:
01498 result = rg.reactive_grasp(current_pose, new_grasp_pose, forward_step = forward_step, grasp_num_tries = grasp_num_tries)
01499
01500
01501 else:
01502 if c == 'ra':
01503
01504 approach_dir = rg.compute_approach_dir(current_pose, new_grasp_pose)
01505 print "approach_dir:", approach_dir
01506
01507 rg.reactive_approach(approach_dir, new_grasp_pose, trajectory)
01508 else:
01509 result = rg.reactive_grasp(current_pose, new_grasp_pose, trajectory, forward_step = forward_step, grasp_num_tries = grasp_num_tries)
01510
01511
01512 (pos, rot) = cm.return_cartesian_pose()
01513 currentgoal = pos+rot
01514
01515
01516 if c == 'rg' and result == 0:
01517 currentgoal[2] += .1
01518 rg.move_cartesian_step(currentgoal, blocking = 1)
01519
01520 elif c == 'cc':
01521 print "compliantly closing gripper"
01522 rg.compliant_close()
01523
01524
01525 (pos, rot) = cm.return_cartesian_pose()
01526 currentgoal = pos+rot
01527
01528 elif c == 'c':
01529 print "closing gripper"
01530 rg.close_hard()
01531
01532 elif c == 'o':
01533 print "opening gripper"
01534 rg.open_and_reset_fingertips(reset = 1)
01535
01536 elif c == 'u':
01537 print "going up"
01538 currentgoal[2] += .1
01539 rg.move_cartesian_step(currentgoal, blocking = 1)
01540 elif c == 'us':
01541 print "going up a small amount"
01542 currentgoal[2] += .02
01543 rg.move_cartesian_step(currentgoal, blocking = 1)
01544 elif c == 'd':
01545 print "going down"
01546 currentgoal[2] -= .05
01547 rg.move_cartesian_step(currentgoal, blocking = 1)
01548 elif c == 'ds':
01549 print "going down a small amount"
01550 currentgoal[2] -= .02
01551 rg.move_cartesian_step(currentgoal, blocking = 1)
01552 elif c == 'r':
01553 print "moving right"
01554 currentgoal[1] -= small_step
01555 rg.move_cartesian_step(currentgoal, blocking = 1)
01556 elif c == 'l':
01557 print "moving left"
01558 currentgoal[1] += small_step
01559 rg.move_cartesian_step(currentgoal, blocking = 1)
01560 elif c == 'a':
01561 print "moving away from robot"
01562 currentgoal[0] += small_step
01563 rg.move_cartesian_step(currentgoal, blocking = 1)
01564 elif c == 't':
01565 print "moving toward the robot"
01566 currentgoal[0] -= small_step
01567 rg.move_cartesian_step(currentgoal, blocking = 1)
01568 elif c == 'reset':
01569 print "resetting the fingertip thresholds"
01570 rg.pressure_listener.set_thresholds()
01571 elif c == 'f':
01572 print "printing the finger sensor values"
01573 rg.pressure_listener.print_sensor_status()
01574 elif c == 's':
01575 print "quitting"
01576 break
01577
01578 print "stopping Cartesian controllers"
01579 cm.stop_controllers(stop_cartesian = 1)
01580