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
00040
00041
00042 from __future__ import division
00043 import roslib; roslib.load_manifest('pr2_gripper_reactive_approach')
00044 import random, time
00045 import rospy
00046 import tf
00047 from geometry_msgs.msg import PoseStamped, Pose, Point, Quaternion, PointStamped, Vector3Stamped
00048 from pr2_mechanism_msgs.srv import SwitchController, LoadController, UnloadController, ListControllers
00049 import actionlib
00050 from pr2_controllers_msgs.msg import JointTrajectoryAction, JointTrajectoryGoal, \
00051 Pr2GripperCommandGoal, Pr2GripperCommandAction
00052 from pr2_gripper_sensor_msgs.msg import PR2GripperFindContactAction, PR2GripperFindContactGoal, \
00053 PR2GripperGrabAction, PR2GripperGrabGoal, PR2GripperEventDetectorAction, PR2GripperEventDetectorGoal
00054 from trajectory_msgs.msg import JointTrajectoryPoint
00055 from simple_Jtranspose_controller.srv import CheckMoving, MoveToPose, MoveToPoseRequest
00056 from std_srvs.srv import Empty, EmptyRequest
00057 from move_arm_msgs.msg import MoveArmAction, MoveArmGoal
00058 from motion_planning_msgs.msg import JointConstraint, PositionConstraint, OrientationConstraint, \
00059 ArmNavigationErrorCodes, AllowedContactSpecification
00060 from geometric_shapes_msgs.msg import Shape
00061 import numpy
00062 import math
00063 import pdb
00064 import copy
00065 from interpolated_ik_motion_planner import ik_utilities
00066 from object_manipulator.convert_functions import *
00067 import joint_states_listener
00068 from actionlib_msgs.msg import GoalStatus
00069 from tf_conversions import posemath
00070
00071
00072 class JointParams:
00073
00074 def __init__(self, whicharm):
00075 self.whicharm = whicharm
00076 self.default_p = [800., 800., 200., 100., 500., 400., 400.]
00077 self.default_d = [10., 10., 3., 3., 6., 4., 4.]
00078 joint_names = ['_shoulder_pan_joint', '_shoulder_lift_joint',
00079 '_upper_arm_roll_joint', '_elbow_flex_joint',
00080 '_forearm_roll_joint', '_wrist_flex_joint',
00081 '_wrist_roll_joint']
00082 self.controller_name = whicharm+"_arm_controller"
00083 self.joint_names = [whicharm+joint_name for joint_name in joint_names]
00084
00085
00086 def set_gains_fraction(self, fraction):
00087 for i in range(7):
00088 rospy.set_param(self.controller_name+'/gains/%s/p'%self.joint_names[i], self.default_p[i]*fraction)
00089 rospy.set_param(self.controller_name+'/gains/%s/d'%self.joint_names[i], self.default_d[i]*fraction)
00090
00091
00092
00093
00094 class JTCartesianParams:
00095
00096 def __init__(self, whicharm):
00097 self.whicharm = whicharm
00098 self.max_vel_trans=.05
00099 self.max_vel_rot=.1
00100 self.max_acc_trans=0.2
00101 self.max_acc_rot=0.3
00102 self.pose_fb_trans_p=400.
00103 self.pose_fb_trans_d=6.
00104 self.pose_fb_rot_p=40.
00105 self.pose_fb_rot_d=0.
00106
00107 self.tcname = self.whicharm+'_arm_cartesian_trajectory_controller'
00108 self.pcname = self.whicharm+'_arm_cartesian_pose_controller'
00109 self.tip_frame = self.whicharm+"_wrist_roll_link"
00110 self.base_frame = "base_link"
00111
00112
00113 self.set_params()
00114
00115
00116
00117 def set_params_to_defaults(self, set_tip_frame = 1, set_params_on_server = 1):
00118 self.max_vel_trans=.15
00119 self.max_vel_rot=.3
00120 self.max_acc_trans=0.4
00121 self.max_acc_rot=0.6
00122 self.pose_fb_trans_p=2000.
00123 self.pose_fb_trans_d=30.
00124 self.pose_fb_rot_p=200.
00125 self.pose_fb_rot_d=0.
00126 if set_tip_frame:
00127 self.tip_frame = self.whicharm+"_wrist_roll_link"
00128 if set_params_on_server:
00129 self.set_params()
00130
00131
00132
00133 def set_params_to_gentle(self, set_tip_frame = 1, set_params_on_server = 1):
00134 self.max_vel_trans=.05
00135 self.max_vel_rot=.1
00136 self.max_acc_trans=0.2
00137 self.max_acc_rot=0.3
00138 self.pose_fb_trans_p=400.
00139 self.pose_fb_trans_d=6.
00140 self.pose_fb_rot_p=40.
00141 self.pose_fb_rot_d=0.
00142 if set_tip_frame:
00143 self.tip_frame = self.whicharm+'_wrist_roll_link'
00144 rospy.loginfo("setting tip frame back to wrist_roll_link")
00145 if set_params_on_server:
00146 self.set_params()
00147
00148
00149
00150 def set_params(self):
00151 rospy.loginfo("setting Cartesian controller parameters")
00152 rospy.set_param(self.tcname+'/type', "CartesianTrajectoryController")
00153 rospy.set_param(self.tcname+'/root_name', self.base_frame)
00154 rospy.set_param(self.tcname+'/tip_name', self.tip_frame)
00155 rospy.set_param(self.tcname+'/output', self.pcname)
00156
00157 rospy.set_param(self.pcname+'/type', "CartesianPoseController")
00158 rospy.set_param(self.pcname+'/root_name', self.base_frame)
00159 rospy.set_param(self.pcname+'/tip_name', self.tip_frame)
00160 rospy.set_param(self.pcname+'/output', 'r_arm_cartesian_twist_controller')
00161
00162 rospy.set_param(self.tcname+'/max_vel_trans', self.max_vel_trans)
00163 rospy.set_param(self.tcname+'/max_vel_rot', self.max_vel_rot)
00164 rospy.set_param(self.tcname+'/max_acc_trans', self.max_acc_trans)
00165 rospy.set_param(self.tcname+'/max_acc_rot', self.max_acc_rot)
00166
00167 rospy.set_param(self.pcname+'/fb_trans/p', self.pose_fb_trans_p)
00168 rospy.set_param(self.pcname+'/fb_trans_d', self.pose_fb_trans_d)
00169 rospy.set_param(self.pcname+'/fb_rot/p', self.pose_fb_rot_p)
00170 rospy.set_param(self.pcname+'/fb_rot/d', self.pose_fb_rot_d)
00171
00172
00173
00174
00175
00176
00177 class ControllerManager():
00178
00179
00180 def __init__(self, whicharm, tf_listener = None, using_slip_controller = 0, using_slip_detection = 0):
00181 self.whicharm = whicharm
00182
00183 self.using_slip_controller = using_slip_controller
00184 self.using_slip_detection = using_slip_detection
00185
00186 rospy.loginfo("initializing "+whicharm+" controller manager")
00187 rospy.loginfo("controller manager: using_slip_controller:"+str(using_slip_controller))
00188 rospy.loginfo("controller manager: using_slip_detection:"+str(using_slip_detection))
00189
00190
00191 rospy.loginfo("controller manager waiting for pr2_controller_manager services to be there")
00192 load_controller_serv_name = 'pr2_controller_manager/load_controller'
00193 unload_controller_serv_name = 'pr2_controller_manager/unload_controller'
00194 switch_controller_serv_name = 'pr2_controller_manager/switch_controller'
00195 list_controllers_serv_name = 'pr2_controller_manager/list_controllers'
00196 self.wait_for_service(load_controller_serv_name)
00197 self.wait_for_service(unload_controller_serv_name)
00198 self.wait_for_service(switch_controller_serv_name)
00199 self.wait_for_service(list_controllers_serv_name)
00200 self.load_controller_service = \
00201 rospy.ServiceProxy(load_controller_serv_name, LoadController)
00202 self.unload_controller_service = \
00203 rospy.ServiceProxy(unload_controller_serv_name, UnloadController)
00204 self.switch_controller_service = \
00205 rospy.ServiceProxy(switch_controller_serv_name, SwitchController)
00206 self.list_controllers_service = \
00207 rospy.ServiceProxy(list_controllers_serv_name, ListControllers)
00208
00209
00210 self.joint_states_listener = joint_states_listener.LatestJointStates()
00211
00212
00213 joint_trajectory_action_name = whicharm+'_arm_controller/joint_trajectory_action'
00214 self.joint_action_client = \
00215 actionlib.SimpleActionClient(joint_trajectory_action_name, JointTrajectoryAction)
00216
00217
00218 if self.using_slip_controller:
00219 gripper_action_name = whicharm+'_gripper_sensor_controller/gripper_action'
00220 self.gripper_action_client = actionlib.SimpleActionClient(gripper_action_name, Pr2GripperCommandAction)
00221
00222
00223 else:
00224 gripper_action_name = whicharm+'_gripper_controller/gripper_action'
00225 self.gripper_action_client = \
00226 actionlib.SimpleActionClient(gripper_action_name, Pr2GripperCommandAction)
00227
00228
00229 if self.using_slip_detection:
00230 gripper_find_contact_action_name = whicharm+'_gripper_sensor_controller/find_contact'
00231 gripper_grab_action_name = whicharm+'_gripper_sensor_controller/grab'
00232 gripper_event_detector_action_name = whicharm+'_gripper_sensor_controller/event_detector'
00233
00234 self.gripper_find_contact_action_client = actionlib.SimpleActionClient(gripper_find_contact_action_name, \
00235 PR2GripperFindContactAction)
00236 self.gripper_grab_action_client = actionlib.SimpleActionClient(gripper_grab_action_name, \
00237 PR2GripperGrabAction)
00238 self.gripper_event_detector_action_client = actionlib.SimpleActionClient(gripper_event_detector_action_name, \
00239 PR2GripperEventDetectorAction)
00240
00241
00242 self.move_arm_client = None
00243
00244
00245 self.wait_for_action_server(self.joint_action_client, joint_trajectory_action_name)
00246 self.wait_for_action_server(self.gripper_action_client, gripper_action_name)
00247 if self.using_slip_detection:
00248 self.wait_for_action_server(self.gripper_find_contact_action_client, gripper_find_contact_action_name)
00249 self.wait_for_action_server(self.gripper_grab_action_client, gripper_grab_action_name)
00250 self.wait_for_action_server(self.gripper_event_detector_action_client, gripper_event_detector_action_name)
00251
00252
00253 if tf_listener == None:
00254 self.tf_listener = tf.TransformListener()
00255 else:
00256 self.tf_listener = tf_listener
00257
00258
00259 self.cartesian_controllers = ['_arm_cartesian_pose_controller',
00260 '_arm_cartesian_trajectory_controller']
00261 self.cartesian_controllers = [self.whicharm+x for x in self.cartesian_controllers]
00262 self.joint_controller = self.whicharm+'_arm_controller'
00263 if self.using_slip_controller:
00264 self.gripper_controller = self.whicharm+'_gripper_sensor_controller'
00265 else:
00266 self.gripper_controller = self.whicharm+'_gripper_controller'
00267
00268
00269 self.cart_params = JTCartesianParams(self.whicharm)
00270
00271
00272 self.joint_params = JointParams(self.whicharm)
00273
00274
00275 rospy.loginfo("loading any unloaded Cartesian controllers")
00276 self.load_cartesian_controllers()
00277 time.sleep(2)
00278 rospy.loginfo("done loading controllers")
00279
00280
00281 cartesian_check_moving_name = whicharm+'_arm_cartesian_trajectory_controller/check_moving'
00282 cartesian_move_to_name = whicharm+'_arm_cartesian_trajectory_controller/move_to'
00283 cartesian_preempt_name = whicharm+'_arm_cartesian_trajectory_controller/preempt'
00284 self.wait_for_service(cartesian_check_moving_name)
00285 self.wait_for_service(cartesian_move_to_name)
00286 self.wait_for_service(cartesian_preempt_name)
00287 self.cartesian_moving_service = rospy.ServiceProxy(cartesian_check_moving_name, CheckMoving)
00288 self.cartesian_cmd_service = rospy.ServiceProxy(cartesian_move_to_name, MoveToPose)
00289 self.cartesian_preempt_service = rospy.ServiceProxy(cartesian_preempt_name, Empty)
00290
00291
00292 self.cart_params.set_params_to_gentle()
00293 self.reload_cartesian_controllers()
00294
00295
00296 rospy.loginfo("creating IKUtilities class objects")
00297 if whicharm == 'r':
00298 self.ik_utilities = ik_utilities.IKUtilities('right', self.tf_listener)
00299 else:
00300 self.ik_utilities = ik_utilities.IKUtilities('left', self.tf_listener)
00301 rospy.loginfo("done creating IKUtilities class objects")
00302
00303
00304 joint_names = ["_shoulder_pan_joint",
00305 "_shoulder_lift_joint",
00306 "_upper_arm_roll_joint",
00307 "_elbow_flex_joint",
00308 "_forearm_roll_joint",
00309 "_wrist_flex_joint",
00310 "_wrist_roll_joint"]
00311 self.joint_names = [whicharm + x for x in joint_names]
00312
00313 rospy.loginfo("done with controller_manager init for the %s arm"%whicharm)
00314
00315
00316
00317 def wait_for_action_server(self, client, name):
00318 while not rospy.is_shutdown():
00319 rospy.loginfo("controller manager: waiting for %s to be there"%name)
00320 if client.wait_for_server(rospy.Duration(5.0)):
00321 break
00322 rospy.loginfo("controller manager: %s found"%name)
00323
00324
00325
00326 def wait_for_service(self, name):
00327 while not rospy.is_shutdown():
00328 rospy.loginfo("controller manager: waiting for %s to be there"%name)
00329 try:
00330 rospy.wait_for_service(name, 5.0)
00331 except rospy.ROSException:
00332 continue
00333 break
00334 rospy.loginfo("controller manager: %s found"%name)
00335
00336
00337
00338
00339 def find_gripper_contact(self, contacts_desired, zero_fingertips = 0, blocking = 1, timeout = 12.):
00340
00341 goal = PR2GripperFindContactGoal()
00342 contacts_desired_dict = {"both":goal.command.BOTH, "left":goal.command.LEFT, "right":goal.command.RIGHT, "either":goal.command.EITHER}
00343
00344 goal.command.contact_conditions = contacts_desired_dict[contacts_desired]
00345 goal.command.zero_fingertip_sensors = zero_fingertips
00346
00347 rospy.loginfo("controller manager: sending find contact goal")
00348 self.gripper_find_contact_action_client.send_goal(goal)
00349
00350
00351 if blocking:
00352 finished_within_time = self.gripper_find_contact_action_client.wait_for_result(rospy.Duration(timeout))
00353 if not finished_within_time:
00354 self.gripper_find_contact_action_client.cancel_goal()
00355 rospy.logerr("Gripper didn't see the desired number of contacts while closing in time")
00356 return (0, 0, 0, 0)
00357 state = self.gripper_find_contact_action_client.get_state()
00358 if state == GoalStatus.SUCCEEDED:
00359 result = self.gripper_find_contact_action_client.get_result()
00360 return (result.data.left_fingertip_pad_contact, result.data.right_fingertip_pad_contact, \
00361 result.data.left_fingertip_pad_contact_force, result.data.right_fingertip_pad_contact_force)
00362 return (0,0,0,0)
00363
00364
00365
00366 def get_find_gripper_contact_state(self):
00367 return self.gripper_find_contact_action_client.get_state()
00368
00369
00370
00371 def get_find_gripper_contact_result(self):
00372 result = self.gripper_find_contact_action_client.get_result()
00373 if result:
00374 return (result.data.left_fingertip_pad_contact, result.data.right_fingertip_pad_contact, \
00375 result.data.left_fingertip_pad_force, result.data.right_fingertip_pad_force)
00376 else:
00377 rospy.logerr("gripper_find_contact_action_client.get_result returned None!")
00378 return (0, 0, 0, 0)
00379
00380
00381
00382 def start_gripper_grab(self, hardness_gain = 0.035, timeout = 10., blocking = 1):
00383
00384 goal = PR2GripperGrabGoal()
00385 goal.command.hardness_gain = hardness_gain
00386
00387 rospy.loginfo("starting slip controller grab")
00388 self.gripper_grab_action_client.send_goal(goal)
00389
00390 if blocking:
00391 finished_within_time = self.gripper_grab_action_client.wait_for_result(rospy.Duration(timeout))
00392 if not finished_within_time:
00393 self.gripper_find_contact_action_client.cancel_goal()
00394 rospy.logerr("Gripper grab timed out")
00395 state = self.gripper_find_contact_action_client.get_state()
00396 if state == GoalStatus.SUCCEEDED:
00397 return 1
00398 return 0
00399
00400 return 1
00401
00402
00403
00404
00405 def start_gripper_event_detector(self, blocking = 0, timeout = 15.):
00406
00407 goal = PR2GripperEventDetectorGoal()
00408 goal.command.trigger_conditions = goal.command.FINGER_SIDE_IMPACT_OR_SLIP_OR_ACC
00409 goal.command.acceleration_trigger_magnitude = 3.25
00410 goal.command.slip_trigger_magnitude = 0.008
00411
00412 rospy.loginfo("starting gripper event detector")
00413 self.gripper_event_detector_action_client.send_goal(goal)
00414
00415
00416 if blocking:
00417 finished_within_time = self.gripper_event_detector_action_client.wait_for_result(rospy.Duration(timeout))
00418 if not finished_within_time:
00419 rospy.logerr("Gripper didn't see the desired event trigger before timing out")
00420 return 0
00421 state = self.gripper_event_detector_action_client.get_state()
00422 if state == GoalStatus.SUCCEEDED:
00423 result = self.gripper_event_detector_action_client.get_result()
00424 if result.data.placed:
00425 return 1
00426 return 0
00427
00428
00429
00430 def get_gripper_event_detector_state(self):
00431 return self.gripper_event_detector_action_client.get_state()
00432
00433
00434
00435 def create_move_arm_goal(self):
00436 goal = MoveArmGoal()
00437 if self.whicharm == "r":
00438 goal.motion_plan_request.group_name = "right_arm"
00439 else:
00440 goal.motion_plan_request.group_name = "left_arm"
00441 goal.motion_plan_request.num_planning_attempts = 2;
00442 goal.motion_plan_request.allowed_planning_time = rospy.Duration(5.0);
00443 goal.motion_plan_request.planner_id = ""
00444 goal.planner_service_name = "ompl_planning/plan_kinematic_path"
00445 return goal
00446
00447
00448
00449 def send_move_arm_goal(self, goal, blocking = 1):
00450
00451
00452 if self.move_arm_client == None:
00453 if self.whicharm == "r":
00454 self.move_arm_client = actionlib.SimpleActionClient('move_right_arm', MoveArmAction)
00455 if self.whicharm == "l":
00456 self.move_arm_client = actionlib.SimpleActionClient('move_left_arm', MoveArmAction)
00457 rospy.loginfo("waiting for move arm server")
00458 self.move_arm_client.wait_for_server()
00459 rospy.loginfo("move arm server was there")
00460
00461
00462 self.move_arm_client.send_goal(goal)
00463
00464
00465 if blocking:
00466 for add_contact_tries in range(10):
00467 finished_within_time = self.move_arm_client.wait_for_result(rospy.Duration(60))
00468 if not finished_within_time:
00469 self.move_arm_client.cancel_goal()
00470 rospy.logerr("Timed out when asking move_arm to go to a joint goal")
00471 return 0
00472
00473 state = self.move_arm_client.get_state()
00474 result = self.move_arm_client.get_result()
00475 if state == GoalStatus.SUCCEEDED:
00476 if result.error_code.val == 1:
00477 rospy.loginfo("move_arm succeeded")
00478 break
00479
00480
00481 elif result.error_code.val == ArmNavigationErrorCodes.START_STATE_IN_COLLISION:
00482 rospy.loginfo("move arm start state in collision! Adding allowed contact regions and trying again, try number %d"%add_contact_tries)
00483 self.modify_move_arm_goal(goal, result.contacts)
00484 self.move_arm_client.send_goal(goal)
00485 continue
00486 else:
00487 rospy.logerr("move_arm did not succeed, state %d, error code %d"%(state, result.error_code.val))
00488 break
00489 return result.error_code.val
00490 else:
00491 return 1
00492
00493
00494
00495 def move_arm_joint(self, joint_angles, blocking = 1):
00496 self.check_controllers_ok('joint')
00497
00498 rospy.loginfo("asking move_arm to go to angles: %s"%self.pplist(joint_angles))
00499
00500 goal = self.create_move_arm_goal()
00501
00502
00503
00504 for (joint_name, joint_angle) in zip(self.joint_names, joint_angles):
00505 joint_constraint = JointConstraint()
00506 joint_constraint.joint_name = joint_name
00507 joint_constraint.position = joint_angle
00508 joint_constraint.tolerance_below = .1
00509 joint_constraint.tolerance_above = .1
00510 goal.motion_plan_request.goal_constraints.joint_constraints.append(joint_constraint)
00511
00512
00513 result = self.send_move_arm_goal(goal, blocking)
00514 return result
00515
00516
00517
00518
00519 def modify_move_arm_goal(self, goal, contact_info):
00520 allowed_contacts = []
00521 allowed_penetration_depth = .5
00522 for (i, contact) in enumerate(contact_info):
00523 allowed_contact_tmp = AllowedContactSpecification()
00524 allowed_contact_tmp.shape.type = allowed_contact_tmp.shape.BOX
00525 allowed_contact_tmp.shape.dimensions = [allowed_penetration_depth]*3
00526 allowed_contact_tmp.pose_stamped.pose.position = contact.position
00527 set_xyzw(allowed_contact_tmp.pose_stamped.pose.orientation, [0,0,0,1])
00528 allowed_contact_tmp.pose_stamped.header.stamp = rospy.Time.now()
00529 allowed_contact_tmp.pose_stamped.header.frame_id = contact.header.frame_id
00530 allowed_contact_tmp.link_names.append(contact.contact_body_1)
00531 allowed_contact_tmp.penetration_depth = allowed_penetration_depth
00532 allowed_contacts.append(allowed_contact_tmp);
00533 rospy.loginfo("Added allowed contact region: %d"%i)
00534
00535
00536
00537 rospy.loginfo("Bodies : %s and %s"%(contact.contact_body_1, contact.contact_body_2))
00538 goal.motion_plan_request.allowed_contacts.extend(allowed_contacts)
00539
00540
00541
00542
00543
00544 def move_arm_constrained(self, constraint_in = None, start_angles = None, location = None, blocking = 1, compute_feasible_goal = True):
00545 self.check_controllers_ok('joint')
00546 current_pose = self.get_current_wrist_pose_stamped('base_link')
00547 move_arm_goal = self.create_move_arm_goal()
00548 if move_arm_goal.motion_plan_request.group_name == "right_arm":
00549 move_arm_goal.motion_plan_request.group_name = "right_arm_cartesian"
00550 if move_arm_goal.motion_plan_request.group_name == "left_arm":
00551 move_arm_goal.motion_plan_request.group_name = "left_arm_cartesian"
00552
00553
00554 position_constraint = PositionConstraint()
00555 position_constraint.header.frame_id = current_pose.header.frame_id
00556 position_constraint.link_name = self.ik_utilities.link_name
00557 set_xyz(position_constraint.position, location)
00558 position_constraint.constraint_region_shape.type = Shape.BOX
00559 position_constraint.constraint_region_shape.dimensions = [0.02]*3
00560 position_constraint.constraint_region_orientation.w = 1.0
00561 position_constraint.weight = 1.0
00562 move_arm_goal.motion_plan_request.goal_constraints.position_constraints.append(position_constraint)
00563
00564 orientation_constraint = copy.deepcopy(constraint_in.orientation_constraints[0])
00565
00566
00567
00568 if compute_feasible_goal == True:
00569 current_pose_mat = pose_to_mat(current_pose.pose)
00570 found_ik_solution = 0
00571 for angle in range(0, 180, 5):
00572 for dir in [1, -1]:
00573 rot_mat = tf.transformations.rotation_matrix(dir*angle/180.*math.pi, [0,0,1])
00574 rotated_pose_mat = rot_mat * current_pose_mat
00575 desired_pose = stamp_pose(mat_to_pose(rotated_pose_mat), 'base_link')
00576 desired_pose.pose.position = position_constraint.position
00577
00578
00579 (solution, error_code) = self.ik_utilities.run_ik(desired_pose, start_angles, \
00580 self.ik_utilities.link_name, collision_aware = 1)
00581 if error_code == "SUCCESS":
00582 found_ik_solution = 1
00583 break
00584 if found_ik_solution:
00585 rospy.loginfo("IK solution found for constrained goal")
00586 orientation_constraint.orientation = desired_pose.pose.orientation
00587 break
00588 else:
00589 rospy.loginfo("no IK solution found for goal, aborting")
00590 return 0
00591
00592 move_arm_goal.motion_plan_request.goal_constraints.orientation_constraints.append(orientation_constraint)
00593
00594 joint_constraint = JointConstraint()
00595 if move_arm_goal.motion_plan_request.group_name == "right_arm_cartesian":
00596 joint_constraint.joint_name = "r_upper_arm_roll_joint"
00597 if move_arm_goal.motion_plan_request.group_name == "left_arm_cartesian":
00598 joint_constraint.joint_name = "l_upper_arm_roll_joint"
00599 joint_constraint.position = solution[2]
00600 joint_constraint.tolerance_below = .1
00601 joint_constraint.tolerance_above = .1
00602 move_arm_goal.motion_plan_request.goal_constraints.joint_constraints.append(joint_constraint)
00603
00604
00605 path_orientation_constraint = copy.deepcopy(orientation_constraint)
00606
00607
00608 path_orientation_constraint.header.stamp = rospy.Time(orientation_constraint.header.stamp.secs)
00609 path_orientation_constraint.orientation = current_pose.pose.orientation
00610 move_arm_goal.motion_plan_request.path_constraints.orientation_constraints.append(path_orientation_constraint)
00611
00612 move_arm_goal.motion_plan_request.planner_id = "SBLkConfig1"
00613 move_arm_goal.disable_ik = True
00614 move_arm_goal.planner_service_name= "/ompl_planning/plan_kinematic_path/"
00615
00616
00617 result = self.send_move_arm_goal(move_arm_goal, blocking)
00618 return result
00619
00620
00621
00622 def move_arm_pose(self, pose_stamped, start_angles = None, blocking = 1):
00623 if start_angles == None:
00624 start_angles = self.get_current_arm_angles()
00625 (solution, error_code) = self.ik_utilities.run_ik(pose_stamped, start_angles, \
00626 self.ik_utilities.link_name)
00627 if not solution:
00628 rospy.logerr("no IK solution found for goal pose!")
00629 return 0
00630 else:
00631 result = self.move_arm_joint(solution, blocking)
00632 return result
00633
00634
00635
00636
00637 def normalize_trajectory(self, trajectory):
00638 current_angles = self.get_current_arm_angles()
00639 trajectory_copy = [list(angles) for angles in trajectory]
00640 for angles in trajectory_copy:
00641 angles[4] = self.normalize_angle(angles[4], current_angles[4])
00642 angles[6] = self.normalize_angle(angles[6], current_angles[6])
00643 return trajectory_copy
00644
00645
00646
00647
00648 def normalize_angle(self, angle, current_angle):
00649 while current_angle-angle > math.pi:
00650 angle += 2*math.pi
00651 while angle - current_angle > math.pi:
00652 angle -= 2*math.pi
00653 return angle
00654
00655
00656
00657
00658 def command_interpolated_ik(self, end_pose, start_pose = None, collision_aware = 1, step_size = .02, max_joint_vel = .05):
00659 self.check_controllers_ok('joint')
00660
00661
00662 current_angles = self.get_current_arm_angles()
00663
00664 if start_pose == None:
00665
00666 (current_trans, current_rot) = self.return_cartesian_pose()
00667
00668
00669 start_pose = create_pose_stamped(current_trans+current_rot)
00670
00671
00672
00673
00674
00675 (trajectory, error_codes) = self.ik_utilities.check_cartesian_path(start_pose, \
00676 end_pose, current_angles, step_size, .1, math.pi/4, collision_aware)
00677
00678
00679 if any(error_codes):
00680 rospy.loginfo("can't execute an interpolated IK trajectory to that pose")
00681 return 0
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692 self.command_joint_trajectory(trajectory, max_joint_vel)
00693 return 1
00694
00695
00696
00697 def get_current_arm_angles(self):
00698 (found, positions, vels, efforts) = \
00699 self.joint_states_listener.return_joint_states(self.joint_names)
00700 return positions
00701
00702
00703
00704 def get_current_wrist_pose_stamped(self, frame = 'base_link'):
00705 (current_trans, current_rot) = self.return_cartesian_pose(frame)
00706 return create_pose_stamped(current_trans+current_rot, frame)
00707
00708
00709
00710 def get_current_gripper_opening(self):
00711 (found, positions, vels, efforts) = \
00712 self.joint_states_listener.return_joint_states([self.whicharm+'_gripper_joint'])
00713 return positions[0]
00714
00715
00716
00717
00718
00719 def command_cartesian(self, pose, frame_id = 'base_link'):
00720 self.check_controllers_ok('cartesian')
00721
00722 if type(pose) == list:
00723 m = create_pose_stamped(pose, frame_id)
00724 else:
00725 m = pose
00726 req = MoveToPoseRequest()
00727 req.pose = m
00728 try:
00729 self.cartesian_preempt_service(EmptyRequest())
00730 except:
00731 rospy.loginfo("preempt unnecessary, robot not moving")
00732 else:
00733 rospy.loginfo("Cartesian movement preempted before issuing new command")
00734
00735 self.cartesian_cmd_service(req)
00736
00737
00738
00739
00740 def check_cartesian_done(self):
00741 resp = self.cartesian_moving_service()
00742 return resp.ismoving
00743
00744
00745
00746
00747 def check_cartesian_near_pose(self, goal_pose, pos_thres, rot_thres):
00748 if type(goal_pose) == list:
00749 goal_pos = goal_pose[0:3]
00750 goal_rot = goal_pose[3:7]
00751 else:
00752 (goal_pos, goal_rot) = pose_stamped_to_lists(self.tf_listener, goal_pose, 'base_link')
00753
00754 (current_pos, current_rot) = self.return_cartesian_pose()
00755
00756
00757 diff = [x-y for (x,y) in zip(goal_pos, current_pos)]
00758 pos_diff = self.ik_utilities.vect_norm(diff)
00759
00760
00761 norm_goal_rot = self.ik_utilities.normalize_vect(goal_rot)
00762 norm_current_rot = self.ik_utilities.normalize_vect(current_rot)
00763 rot_diff = self.ik_utilities.quat_angle(norm_current_rot, norm_goal_rot)
00764
00765 if pos_diff < pos_thres and rot_diff < rot_thres:
00766 return 1
00767 return 0
00768
00769
00770
00771 def check_cartesian_really_done(self, goal_pose, pos_thres, rot_thres):
00772 if not self.check_cartesian_done():
00773 return 0
00774 return self.check_cartesian_near_pose(goal_pose, pos_thres, rot_thres)
00775
00776
00777
00778
00779 def wait_cartesian_really_done(self, goal_pose, pos_thres = .02, rot_thres = .1, timeout = 0., settling_time = rospy.Duration(3.0)):
00780 start_time = rospy.get_rostime()
00781 done_time = None
00782 while(1):
00783 if self.check_cartesian_really_done(goal_pose, pos_thres, rot_thres):
00784 rospy.loginfo("got to the goal")
00785 return "success"
00786 if done_time == None and self.check_cartesian_done():
00787
00788 done_time = rospy.get_rostime()
00789 if done_time and rospy.get_rostime() - done_time > settling_time:
00790 rospy.loginfo("settling time ran out")
00791 return "failed"
00792 if timeout != 0. and rospy.get_rostime() - start_time > timeout:
00793 rospy.loginfo("timed out")
00794 return "timed out"
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805 def move_cartesian(self, goal_pose, collision_aware = 0, blocking = 1,
00806 step_size = .005, pos_thres = .02, rot_thres = .1,
00807 timeout = rospy.Duration(30.),
00808 settling_time = rospy.Duration(1.)):
00809
00810 (current_pos, current_rot) = self.return_cartesian_pose('base_link')
00811 start_pose = create_pose_stamped(current_pos+current_rot)
00812
00813
00814 if collision_aware:
00815 current_angles = self.get_current_arm_angles()
00816 (traj, error_codes) = self.ik_utilities.check_cartesian_path(start_pose, \
00817 goal_pose, current_angles, .01, .1, math.pi/4, 1)
00818 if any(error_codes):
00819 return "no solution"
00820
00821
00822 self.command_cartesian(goal_pose)
00823 if not blocking:
00824 return "sent goal"
00825
00826
00827 result = self.wait_cartesian_really_done(goal_pose, pos_thres, \
00828 rot_thres, timeout = timeout, settling_time = settling_time)
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838 def move_cartesian_ik(self, goal_pose, collision_aware = 0, blocking = 1,
00839 step_size = .005, pos_thres = .02, rot_thres = .1,
00840 timeout = rospy.Duration(30.),
00841 settling_time = rospy.Duration(0.25), vel = .15):
00842
00843
00844 result = self.command_interpolated_ik(goal_pose, collision_aware = collision_aware, \
00845 step_size = step_size, max_joint_vel = vel)
00846 if not result:
00847 return "no solution"
00848 if not blocking:
00849 return "sent goal"
00850
00851
00852 joint_action_result = self.wait_joint_trajectory_done(timeout)
00853 if joint_action_result == "timed out":
00854 return "timed out"
00855
00856 if self.check_cartesian_really_done(goal_pose, pos_thres, rot_thres):
00857 return "success"
00858 return "failed"
00859
00860
00861
00862
00863 def check_controllers_ok(self, mode):
00864 modewrong = 0
00865 if mode == 'joint':
00866
00867
00868
00869
00870
00871
00872 self.check_controller_states()
00873 if not self.joint_controller_state == 'running' and \
00874 any([x == 'running' for x in self.cartesian_controllers_state]):
00875 self.switch_to_joint_mode()
00876 elif not self.joint_controller_state == 'running':
00877 rospy.logwarn("joint controllers aren't running! Attempting to start")
00878 self.start_joint_controllers()
00879 elif any([x == 'running' for x in self.cartesian_controllers_state]):
00880 rospy.logwarn("Cartesian controllers are running! Attempting to stop")
00881 self.stop_controllers(stop_cartesian = 1)
00882
00883 elif mode == 'cartesian':
00884
00885
00886
00887
00888
00889
00890 self.check_controller_states()
00891 if not all([x == 'running' for x in self.cartesian_controllers_state]) and \
00892 self.joint_controller_state == 'running':
00893 self.switch_to_cartesian_mode()
00894 elif not all([x == 'running' for x in self.cartesian_controllers_state]):
00895 rospy.logwarn("Cartesian controllers aren't running! Attempting to start")
00896 self.start_cartesian_controllers()
00897 elif self.joint_controller_state == 'running':
00898 rospy.logwarn("joint controllers are running! Attempting to stop")
00899 self.stop_controllers(stop_joint = 1)
00900
00901
00902
00903 def command_joint(self, jointangles):
00904 self.check_controllers_ok('joint')
00905
00906 goal = JointTrajectoryGoal()
00907 goal.trajectory.header.stamp = rospy.get_rostime()
00908
00909 goal.trajectory.joint_names = self.joint_names
00910
00911 point = JointTrajectoryPoint(jointangles, [0]*7, [0]*7, rospy.Duration(5.0))
00912 goal.trajectory.points = [point,]
00913
00914 self.joint_action_client.send_goal(goal)
00915
00916
00917
00918 def command_joint_trajectory(self, trajectory, max_joint_vel = 0.2, current_angles = None, blocking = 0, times_and_vels = None):
00919 self.check_controllers_ok('joint')
00920
00921 goal = JointTrajectoryGoal()
00922 goal.trajectory.joint_names = self.joint_names
00923
00924
00925 trajectory = self.normalize_trajectory(trajectory)
00926
00927
00928 goal.trajectory.header.stamp = rospy.get_rostime() + rospy.Duration(0.05)
00929
00930
00931 if times_and_vels == None:
00932
00933
00934 if current_angles == None:
00935 current_angles = list(self.get_current_arm_angles())
00936
00937
00938 trajectory = [current_angles] + trajectory
00939
00940
00941 (times, vels) = self.ik_utilities.trajectory_times_and_vels(trajectory, [max_joint_vel]*7)
00942
00943 else:
00944 (times, vels) = times_and_vels
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955 for ind in range(len(trajectory)):
00956 point = JointTrajectoryPoint(trajectory[ind], vels[ind], [0]*7, rospy.Duration(times[ind]))
00957 goal.trajectory.points.append(point)
00958
00959
00960 self.joint_action_client.send_goal(goal)
00961
00962
00963 if blocking:
00964 self.wait_joint_trajectory_done(timeout = rospy.Duration(times[-1]+5))
00965
00966
00967
00968 def check_joint_trajectory_done(self):
00969 state = self.joint_action_client.get_state()
00970 return state > 1
00971
00972
00973
00974
00975
00976 def wait_joint_trajectory_done(self, timeout = rospy.Duration(0)):
00977 finished = self.joint_action_client.wait_for_result(timeout)
00978 if not finished:
00979 return "timed out"
00980 state = self.joint_action_client.get_state()
00981 if state == 3:
00982 return "succeeded"
00983 return "other"
00984
00985
00986
00987 def freeze_arm(self):
00988 current_angles = self.get_current_arm_angles()
00989 self.command_joint(current_angles)
00990
00991
00992
00993 def command_gripper(self, position, max_effort, blocking = 0):
00994 if not self.gripper_controller_state == 'running':
00995 self.check_controller_states()
00996 if not self.gripper_controller_state == 'running':
00997 rospy.logwarn("gripper controller not running! Attempting to start")
00998 self.start_gripper_controller()
00999
01000 goal = Pr2GripperCommandGoal()
01001 goal.command.position = position
01002 goal.command.max_effort = max_effort
01003 self.gripper_action_client.send_goal(goal)
01004
01005
01006 if blocking:
01007 self.gripper_action_client.wait_for_result(rospy.Duration(4.0))
01008 time.sleep(.5)
01009
01010
01011
01012
01013
01014 def check_controller_states(self):
01015 resp = self.list_controllers_service()
01016
01017 if self.gripper_controller in resp.controllers:
01018 self.gripper_controller_state = resp.state[resp.controllers.index(self.gripper_controller)]
01019 else:
01020 self.gripper_controller_state = 'not loaded'
01021
01022 if self.joint_controller in resp.controllers:
01023 self.joint_controller_state = resp.state[resp.controllers.index(self.joint_controller)]
01024 else:
01025 self.joint_controller_state = 'not loaded'
01026
01027 self.cartesian_controllers_state = []
01028 for controller in self.cartesian_controllers:
01029 if controller not in resp.controllers:
01030 self.cartesian_controllers_state.append('not loaded')
01031 else:
01032 self.cartesian_controllers_state.append(resp.state[resp.controllers.index(controller)])
01033
01034
01035
01036 def load_cartesian_controllers(self):
01037 self.check_controller_states()
01038
01039 for (controller, state) in zip(self.cartesian_controllers, self.cartesian_controllers_state):
01040 if state == 'not loaded':
01041 success = self.load_controller_service(controller)
01042 if not success:
01043 rospy.logerr("error in loading Cartesian controller!")
01044 break
01045 else:
01046 rospy.loginfo("all Cartesian controllers loaded")
01047
01048 self.check_controller_states()
01049
01050
01051
01052 def unload_cartesian_controllers(self):
01053
01054 self.check_controller_states()
01055 if any([x=='running' for x in self.cartesian_controllers_state]):
01056 self.stop_controllers(stop_cartesian = 1)
01057
01058 if all([x=='not loaded' for x in self.cartesian_controllers_state]):
01059 return
01060
01061 cart_controllers_reversed = self.cartesian_controllers[:]
01062 cart_controllers_reversed.reverse()
01063 state_reversed = self.cartesian_controllers_state[:]
01064 state_reversed.reverse()
01065 for (controller, state) in zip(cart_controllers_reversed, state_reversed):
01066 if state == 'stopped':
01067 success = self.unload_controller_service(controller)
01068 if not success:
01069 rospy.logerr("error in unloading Cartesian controller!")
01070 break
01071 else:
01072 rospy.loginfo("Cartesian controllers unloaded")
01073 self.check_controller_states()
01074
01075
01076
01077 def load_joint_controllers(self):
01078 self.check_controller_states()
01079 if self.joint_controller_state == 'not loaded':
01080 success = self.load_controller_service(self.joint_controller)
01081 if not success:
01082 rospy.logerr("error in loading joint controller!")
01083 else:
01084 rospy.loginfo("joint controller already loaded")
01085
01086 self.check_controller_states()
01087
01088
01089
01090 def unload_joint_controllers(self):
01091 self.check_controller_states()
01092 if self.joint_controller_state == 'running':
01093 self.stop_controllers(stop_joint = 1)
01094 elif self.joint_controller_state == 'not loaded':
01095 return
01096
01097 success = self.unload_controller_service(self.joint_controller)
01098 if not success:
01099 rospy.logerr("error in unloading joint controller!")
01100
01101 self.check_controller_states()
01102
01103
01104
01105 def switch_to_cartesian_mode(self):
01106 self.check_controller_states()
01107 if all([x=='stopped' for x in self.cartesian_controllers_state]) and \
01108 self.joint_controller_state == 'running':
01109 success = self.switch_controller_service(self.cartesian_controllers, [self.joint_controller,], 2)
01110 if success:
01111 rospy.loginfo("switched joint to Cartesian successfully")
01112 else:
01113 rospy.logerr("switching joint to Cartesian failed")
01114 else:
01115 self.start_cartesian_controllers()
01116 self.stop_controllers(stop_joint = 1)
01117 self.check_controller_states()
01118
01119
01120
01121 def switch_to_joint_mode(self):
01122 self.check_controller_states()
01123 if self.joint_controller_state == 'stopped' and \
01124 any([x=='running' for x in self.cartesian_controllers_state]):
01125 success = self.switch_controller_service([self.joint_controller,], self.cartesian_controllers, 2)
01126 if success:
01127 rospy.loginfo("switched Cartesian to joint successfully")
01128 else:
01129 rospy.logerr("switching Cartesian to joint failed")
01130 else:
01131 self.start_joint_controllers()
01132 self.stop_controllers(stop_cartesian = 1)
01133 self.check_controller_states()
01134
01135
01136
01137 def start_joint_controllers(self):
01138 self.check_controller_states()
01139 if self.joint_controller_state == 'stopped':
01140 success = self.switch_controller_service([self.joint_controller,], [], 2)
01141 if success:
01142 rospy.loginfo("started joint controller successfully")
01143 else:
01144 rospy.logerr("starting joint controller failed")
01145 elif self.joint_controller_state == 'not loaded':
01146 rospy.logerr("joint controller not loaded!")
01147 self.check_controller_states()
01148
01149
01150
01151 def start_cartesian_controllers(self):
01152 self.check_controller_states()
01153 if any([x=='not loaded' for x in self.cartesian_controllers_state]):
01154 print "not all Cartesian controllers are loaded!"
01155
01156 elif any([x=='stopped' for x in self.cartesian_controllers_state]):
01157 success = self.switch_controller_service(self.cartesian_controllers, [], 2)
01158 if success:
01159 rospy.loginfo("started Cartesian controllers successfully")
01160 else:
01161 rospy.logerr("starting Cartesian controllers failed")
01162 self.check_controller_states()
01163
01164
01165
01166 def start_gripper_controller(self):
01167 self.check_controller_states()
01168 if self.gripper_controller_state == 'stopped':
01169 success = self.switch_controller_service([self.gripper_controller,], [], 2)
01170 if success:
01171 rospy.loginfo("started gripper controller successfully")
01172 else:
01173 rospy.logerr("starting gripper controller failed")
01174 elif self.gripper_controller_state == 'not loaded':
01175 rospy.logerr("gripper controller isn't loaded!")
01176 self.check_controller_states()
01177
01178
01179
01180 def stop_all_controllers(self):
01181 self.stop_controllers(stop_joint = 1, stop_cartesian = 1, stop_gripper = 1)
01182
01183
01184
01185 def stop_controllers(self, stop_joint = 0, stop_cartesian = 0, stop_gripper = 0):
01186 self.check_controller_states()
01187
01188
01189 if stop_cartesian and any([x=='running' for x in self.cartesian_controllers_state]):
01190 success = self.switch_controller_service([], self.cartesian_controllers, 2)
01191 if success:
01192 rospy.loginfo("stopped Cartesian controllers successfully")
01193 else:
01194 rospy.logerr("stopping Cartesian controllers failed")
01195
01196
01197 if stop_joint and self.joint_controller_state == 'running':
01198 success = self.switch_controller_service([], [self.joint_controller,], 2)
01199 if success:
01200 rospy.loginfo("stopped joint controller successfully")
01201 else:
01202 rospy.logerr("stopping joint controller failed")
01203
01204
01205 if stop_gripper and self.gripper_controller_state == 'running':
01206 success = self.switch_controller_service([], [self.gripper_controller,], 2)
01207 if success:
01208 rospy.loginfo("stopped gripper controller successfully")
01209 else:
01210 rospy.logerr("stopping gripper controller failed")
01211
01212 self.check_controller_states()
01213
01214
01215
01216 def reload_cartesian_controllers(self, set_params = 1):
01217 self.check_controller_states()
01218
01219
01220 if set_params:
01221 self.cart_params.set_params()
01222
01223
01224 restart = 0
01225 if any([x=='running' for x in self.cartesian_controllers_state]):
01226 restart = 1
01227 self.switch_to_joint_mode()
01228
01229
01230 self.unload_cartesian_controllers()
01231
01232
01233 self.load_cartesian_controllers()
01234
01235
01236 if restart:
01237 self.switch_to_cartesian_mode()
01238
01239 self.check_controller_states()
01240
01241
01242
01243 def reload_joint_controllers(self, fraction = 1.):
01244 self.check_controller_states()
01245
01246
01247 self.joint_params.set_gains_fraction(fraction)
01248
01249
01250 restart = 0
01251 if self.joint_controller_state == 'running':
01252 restart = 1
01253 self.switch_to_cartesian_mode()
01254
01255
01256 self.unload_joint_controllers()
01257
01258
01259 self.load_joint_controllers()
01260
01261
01262 if restart:
01263 self.switch_to_joint_mode()
01264
01265 self.check_controller_states()
01266
01267
01268
01269 def return_cartesian_pose(self, frame = 'base_link'):
01270 (trans, rot) = self.tf_listener.lookupTransform(frame, self.whicharm+'_wrist_roll_link', rospy.Time(0))
01271 return (list(trans), list(rot))
01272
01273
01274
01275 def print_cartesian_pose(self):
01276 (trans, rot) = self.return_cartesian_pose()
01277 print "Cartesian pose trans:", self.pplist(trans), "rot:", self.pplist(rot)
01278 print "rotation matrix:\n", self.ppmat(tf.transformations.quaternion_matrix(rot))
01279
01280
01281
01282 def pplist(self, list):
01283 return ' '.join(['%5.3f'%x for x in list])
01284
01285
01286
01287 def ppmat(self, mat):
01288 str = ''
01289 for i in range(mat.shape[0]):
01290 for j in range(mat.shape[1]):
01291 str += '%2.3f\t'%mat[i,j]
01292 str += '\n'
01293 return str
01294
01295
01296
01297
01298 if __name__ == '__main__':
01299
01300 def keypause():
01301 print "press enter to continue"
01302 raw_input()
01303
01304 rospy.init_node('controller_manager', anonymous=True)
01305
01306
01307 points = [
01308 [0.5, -0.5, 0.8, 0.5, 0.0, 0.0, 0.5],
01309 [0.6, -0.2, 0.4, 0.0, 0.0, 0.5, 0.5],
01310 [0.2, -0.8, 0.4, 0.0, 0.5, 0.0, 0.5],
01311 [0.5, -0.5, 1.2, 0.5, 0.0, 0.0, 0.5],
01312 [0.6, -0.2, 1.2, 0.0, 0.0, 0.5, 0.5],
01313 [0.2, -0.8, 1.2, 0.0, 0.5, 0.0, 0.5],
01314 [.62, -.05, .65, -0.5, 0.5, 0.5, 0.5],
01315 [.62, -.05, .56, -0.5, 0.5, 0.5, 0.5]
01316 ]
01317
01318 zeros = [0]*7
01319 sideangles = [-0.447, -0.297, -2.229, -0.719, 0.734, -1.489, 1.355]
01320
01321 cm = ControllerManager('r')
01322
01323 test_angles = [
01324 [-0.094, 0.711, -0.000, -1.413, -2.038, -1.172, -0.798],
01325 [0.126, 0.832, 0.000, -1.740, -2.977, -1.091, 3.043]]
01326
01327 print "going through test_angles and waiting for the trajectory to finish"
01328 cm.command_joint_trajectory(test_angles)
01329 result = cm.wait_joint_trajectory_done(rospy.Duration(30.0))
01330 print result
01331
01332 print "starting gripper controller"
01333 cm.start_gripper_controller()
01334
01335 print "switching to cartesian controllers"
01336 cm.switch_to_cartesian_mode()
01337
01338 print "moving to point 6 and waiting for it to get there"
01339 cm.command_cartesian(points[6])
01340 result = cm.wait_cartesian_really_done(points[6], .01, .1, rospy.Duration(30.0), rospy.Duration(10.0))
01341 print result
01342
01343 print "desired pose:", cm.pplist(points[6])
01344 cm.print_cartesian_pose()
01345
01346 print "going to all 0 angles with joint controllers, testing contingency-switch"
01347 cm.command_joint(zeros)
01348 keypause()
01349
01350 print "opening gripper"
01351 cm.command_gripper(0.08, -1.0)
01352 keypause()
01353
01354
01355
01356 print "going to near-side-grasp angles"
01357 cm.command_joint(sideangles)
01358 keypause()
01359
01360
01361
01362
01363
01364 print "commanding an interpolated IK trajectory to go from side approach to side grasp"
01365 start_angles = sideangles
01366 tiltangle = math.pi/18.
01367 sideapproachmat = numpy.array([[0., -1., 0., 0.],
01368 [math.cos(tiltangle), 0., math.sin(tiltangle), 0.],
01369 [-math.sin(tiltangle), 0., math.cos(tiltangle), 0.],
01370 [0., 0., 0., 1.]])
01371 sideapproachpos = [.62, -.3, .6]
01372 approachmat = sideapproachmat
01373 approachpos = sideapproachpos
01374 approachquat = list(tf.transformations.quaternion_from_matrix(approachmat))
01375 sidegrasppos = sideapproachpos[:]
01376 sidegrasppos[1] += .05
01377 grasppos = sidegrasppos
01378 graspquat = approachquat[:]
01379
01380 start_pose = create_pose_stamped(approachpos+approachquat)
01381 end_pose = create_pose_stamped(grasppos+graspquat)
01382
01383
01384 cm.command_interpolated_ik(end_pose, start_pose)
01385 keypause()
01386
01387 print "closing gripper (blocking)"
01388 cm.command_gripper(0.0, 50.0, 1)
01389 print "done"
01390
01391 print "moving to point 0 in Cartesian mode, testing contingency switch"
01392 cm.command_cartesian(points[0])
01393 keypause()
01394 cm.print_cartesian_pose()
01395
01396 print "reloading cartesian controllers with zero PID terms (should be unable to move)"
01397 cm.cart_params.pose_fb_trans_p = 0.
01398 cm.cart_params.pose_fb_trans_d = 0.
01399 cm.cart_params.pose_fb_rot_p = 0.
01400 cm.cart_params.pose_fb_rot_d = 0.
01401 cm.reload_cartesian_controllers()
01402
01403 print "trying to move to point 2 (should fail)"
01404 cm.command_cartesian(points[2])
01405 keypause()
01406
01407 print "reloading cartesian controllers with normal PID terms"
01408 cm.cart_params.pose_fb_trans_p=800.
01409 cm.cart_params.pose_fb_trans_d=12.
01410 cm.cart_params.pose_fb_rot_p=80.
01411 cm.cart_params.pose_fb_rot_d=0.0
01412 cm.reload_cartesian_controllers()
01413
01414 print "trying again to move to point 2 (should succeed)"
01415 cm.command_cartesian(points[2])
01416 keypause()
01417
01418 print "starting joint controllers and going to all 0 angles"
01419 cm.command_joint([0,0,0,0,0,0,0])
01420 keypause()
01421
01422 print "stopping all controllers"
01423 cm.stop_all_controllers()
01424
01425