18 1- Added functions for receiving online dmp trajectory from pyride
22 import PyPR2, numpy, math, time
25 from math_tools
import general_math
as gen
72 rt_position = numpy.zeros(3)
73 rt_velocity = numpy.zeros(3)
74 rt_acceleration = numpy.zeros(3)
85 global larm_reached, rarm_reached
86 if hasattr( PyPR2,
'onMoveArmPoseComplete' )
and hasattr( PyPR2.onMoveArmPoseComplete,
'__call__' ):
87 PyPR2.onMoveArmPoseComplete( is_left_arm )
101 global larm_failed, rarm_failed
102 if hasattr( PyPR2,
'onMoveArmPoseFailed' )
and hasattr( PyPR2.onMoveArmPoseFailed,
'__call__' ):
103 PyPR2.onMoveArmPoseFailed( is_left_arm )
122 global bl_cnt, bl_dist, bl_active
128 global tl_cnt, tl_dist, tl_active
134 PyPR2.onMoveArmActionSuccess = on_move_arm_finished
135 PyPR2.onMoveArmActionFailed = on_move_arm_failed
136 PyPR2.onMoveBodySuccess = on_move_body_finished
137 PyPR2.onMoveBodyFailed = on_move_body_failed
140 global rt_cnt, rt_active
143 rt_position[0] = data[
'position'][0]
144 rt_position[1] = data[
'position'][1]
145 rt_position[2] = data[
'position'][2]
147 rt_velocity[0] = data[
'velocity'][0]
148 rt_velocity[1] = data[
'velocity'][1]
149 rt_velocity[2] = data[
'velocity'][2]
151 rt_acceleration[0] = data[
'acceleration'][0]
152 rt_acceleration[1] = data[
'acceleration'][1]
153 rt_acceleration[2] = data[
'acceleration'][2]
155 rt_orientation[0] = data[
'acceleration'][2]
166 g={
'l_wrist_roll_joint': q[6],
'l_forearm_roll_joint': q[4],
'l_elbow_flex_joint': q[3],
'l_shoulder_lift_joint': q[1] - math.pi/2,
'l_upper_arm_roll_joint': q[2],
'l_wrist_flex_joint': q[5],
'l_shoulder_pan_joint': q[0],
'time_to_reach': time_to_reach }
170 g={
'r_wrist_roll_joint': q[6],
'r_forearm_roll_joint': q[4],
'r_elbow_flex_joint': q[3],
'r_shoulder_lift_joint': q[1] - math.pi/2,
'r_upper_arm_roll_joint': q[2],
'r_wrist_flex_joint': q[5],
'r_shoulder_pan_joint': q[0],
'time_to_reach': time_to_reach }
174 g={
'r_wrist_roll_joint': {
'position':q[6],
'velocity': q_dot[6]},
'r_forearm_roll_joint': {
'position':q[4],
'velocity': q_dot[4]},
'r_elbow_flex_joint': {
'position':q[3],
'velocity': q_dot[3]},
'r_shoulder_lift_joint': {
'position':q[1] - math.pi/2,
'velocity': q_dot[1]},
'r_upper_arm_roll_joint': {
'position':q[2],
'velocity': q_dot[2]},
'r_wrist_flex_joint': {
'position':q[5],
'velocity': q_dot[5]},
'r_shoulder_pan_joint': {
'position':q[0],
'velocity': q_dot[0]},
'time_to_reach':time_to_reach}
178 g={
'l_wrist_roll_joint': {
'position':q[6],
'velocity': q_dot[6]},
'l_forearm_roll_joint': {
'position':q[4],
'velocity': q_dot[4]},
'l_elbow_flex_joint': {
'position':q[3],
'velocity': q_dot[3]},
'l_shoulder_lift_joint': {
'position':q[1] - math.pi/2,
'velocity': q_dot[1]},
'l_upper_arm_roll_joint': {
'position':q[2],
'velocity': q_dot[2]},
'l_wrist_flex_joint': {
'position':q[5],
'velocity': q_dot[5]},
'l_shoulder_pan_joint': {
'position':q[0],
'velocity': q_dot[0]},
'time_to_reach':time_to_reach}
182 g={
'l_wrist_roll_joint': q[6],
'l_forearm_roll_joint': q[4],
'l_elbow_flex_joint': q[3],
'l_shoulder_lift_joint': q[1],
'l_upper_arm_roll_joint': q[2],
'l_wrist_flex_joint': q[5],
'l_shoulder_pan_joint': q[0]}
186 g={
'r_wrist_roll_joint': q[6],
'r_forearm_roll_joint': q[4],
'r_elbow_flex_joint': q[3],
'r_shoulder_lift_joint': q[1],
'r_upper_arm_roll_joint': q[2],
'r_wrist_flex_joint': q[5],
'r_shoulder_pan_joint': q[0]}
191 q[0] = g[
'r_shoulder_pan_joint']
192 q[1] = g[
'r_shoulder_lift_joint'] + math.pi/2
193 q[2] = g[
'r_upper_arm_roll_joint']
194 q[3] = g[
'r_elbow_flex_joint']
195 q[4] = g[
'r_forearm_roll_joint']
196 q[5] = g[
'r_wrist_flex_joint']
197 q[6] = g[
'r_wrist_roll_joint']
202 q[0] = g[
'l_shoulder_pan_joint']
203 q[1] = g[
'l_shoulder_lift_joint'] + math.pi/2
204 q[2] = g[
'l_upper_arm_roll_joint']
205 q[3] = g[
'l_elbow_flex_joint']
206 q[4] = g[
'l_forearm_roll_joint']
207 q[5] = g[
'l_wrist_flex_joint']
208 q[6] = g[
'l_wrist_roll_joint']
215 returns the current angle of the shoulder pan joint of the left arm
221 lajd = PyPR2.getArmJointPositions(
True)
222 return(gain*lajd[
'l_shoulder_pan_joint'])
226 returns the current angle of the shoulder pan joint of the right arm
232 rajd = PyPR2.getArmJointPositions(
False)
233 return(gain*rajd[
'r_shoulder_pan_joint'])
237 returns the current angle of the shoulder lift joint of the right arm
243 rajd = PyPR2.getArmJointPositions(
False)
244 return(gain*rajd[
'r_shoulder_lift_joint'])
248 returns the current angle of the shoulder lift joint of the left arm
254 rajd = PyPR2.getArmJointPositions(
True)
255 return(gain*rajd[
'l_shoulder_lift_joint'])
259 returns the current angle of the shoulder pan joint of the left arm
265 lajd = PyPR2.getArmJointPositions(
True)
266 return(gain*lajd[
'l_elbow_flex_joint'])
270 returns the current angle of the shoulder pan joint of the right arm
276 rajd = PyPR2.getArmJointPositions(
False)
277 return(gain*rajd[
'r_elbow_flex_joint'])
284 rajd = PyPR2.getArmJointPositions(
False)
292 lajd = PyPR2.getArmJointPositions(
True)
297 returns the wrist position of the left arm comparable to function wrist_position() in the arm object
299 p0 = PyPR2.getRelativeTF(
'torso_lift_link' ,
'l_shoulder_pan_link')[
'position']
300 p = PyPR2.getRelativeTF(
'torso_lift_link' ,
'l_wrist_flex_link')[
'position']
304 pos = numpy.array([x,y,z])
309 returns the wrist orientation of the left arm (the same orientation returned by function wrist_orientation() in the arm object but in quaternions)
311 q = PyPR2.getRelativeTF(
'torso_lift_link' ,
'l_wrist_flex_link')[
'orientation']
316 returns the wrist orientation of the right arm (the same orientation returned by function wrist_orientation() in the arm object but in quaternions)
318 q = PyPR2.getRelativeTF(
'torso_lift_link' ,
'r_wrist_flex_link')[
'orientation']
322 def rarm_gripper_position():
324 returns the gripper position of the right arm comparable to function wrist_position() in the arm object
326 p0 = PyPR2.getRelativeTF('torso_lift_link' , 'r_shoulder_pan_link')['position']
327 pr = vecmat.as_vector(PyPR2.getRelativeTF('torso_lift_link' , 'r_gripper_r_finger_tip_link')['position'])
328 pl = vecmat.as_vector(PyPR2.getRelativeTF('torso_lift_link' , 'r_gripper_l_finger_tip_link')['position'])
339 returns the wrist position of the right arm comparable to function wrist_position() in the arm object
341 p0 = PyPR2.getRelativeTF(
'torso_lift_link' ,
'r_shoulder_pan_link')[
'position']
342 p = PyPR2.getRelativeTF(
'torso_lift_link' ,
'r_wrist_flex_link')[
'position']
346 pos = numpy.array([x,y,z])
351 returns the elbow position of the right arm comparable to function wrist_position() in the arm object
353 p0 = PyPR2.getRelativeTF(
'torso_lift_link' ,
'r_shoulder_pan_link')[
'position']
354 p = PyPR2.getRelativeTF(
'torso_lift_link' ,
'r_elbow_flex_link')[
'position']
358 pos = numpy.array([x,y,z])
363 returns the elbow position of the right arm comparable to function wrist_position() in the arm object
365 p0 = PyPR2.getRelativeTF(
'torso_lift_link' ,
'l_shoulder_pan_link')[
'position']
366 p = PyPR2.getRelativeTF(
'torso_lift_link' ,
'l_elbow_flex_link')[
'position']
370 pos = numpy.array([x,y,z])
375 Returns the right arm gripper position vector(endeffector position) with respect to the torso shoulder pan joint center
377 p0 = PyPR2.getRelativeTF(
'torso_lift_link' ,
'r_shoulder_pan_link')[
'position']
378 pr = vecmat.as_vector(PyPR2.getRelativeTF(
'torso_lift_link' ,
'r_gripper_r_finger_tip_link')[
'position'])
379 pl = vecmat.as_vector(PyPR2.getRelativeTF(
'torso_lift_link' ,
'r_gripper_l_finger_tip_link')[
'position'])
384 pos = numpy.array([x,y,z])
389 Returns the left arm gripper position vector(endeffector position) with respect to the (torso) at the origin.
390 The torso origin is at the floor footprint (projection) of the middle point between the two shoulder pan joint centers.
391 The torso origin is about 51 mm below the base_link origin and 50 mm shifted towards the back. The orientations of torso and base are identical.
394 p0 = PyPR2.getRelativeTF(
'base_link' ,
'l_shoulder_pan_link')[
'position']
401 Returns the right arm gripper position vector(endeffector position) with respect to the (torso) at the origin. (Refer to pos_larm_grip_wrt_tor())
404 p0 = PyPR2.getRelativeTF(
'base_link' ,
'r_shoulder_pan_link')[
'position']
411 Returns the left arm gripper position vector(endeffector position) with respect to the torso shoulder pan joint center
413 p0 = PyPR2.getRelativeTF(
'torso_lift_link' ,
'l_shoulder_pan_link')[
'position']
414 pr = vecmat.as_vector(PyPR2.getRelativeTF(
'torso_lift_link' ,
'l_gripper_r_finger_tip_link')[
'position'])
415 pl = vecmat.as_vector(PyPR2.getRelativeTF(
'torso_lift_link' ,
'l_gripper_l_finger_tip_link')[
'position'])
420 pos = numpy.array([x,y,z])
425 Returns the global position of the right arm gripper finger tip
427 pr = vecmat.as_vector(PyPR2.getRelativeTF(
'base_footprint' ,
'r_gripper_r_finger_tip_link')[
'position'])
428 pl = vecmat.as_vector(PyPR2.getRelativeTF(
'base_footprint' ,
'r_gripper_l_finger_tip_link')[
'position'])
431 orient = geo.Orientation_3D(PyPR2.getRobotPose()[
'orientation'], representation =
'quaternion')
432 pg = numpy.dot(orient.matrix(), p)
434 p0 = PyPR2.getRobotPose()[
'position']
440 pos = numpy.array([x,y,z])
445 Returns the global position of the left arm gripper finger tip
447 pr = vecmat.as_vector(PyPR2.getRelativeTF(
'base_footprint' ,
'l_gripper_r_finger_tip_link')[
'position'])
448 pl = vecmat.as_vector(PyPR2.getRelativeTF(
'base_footprint' ,
'l_gripper_l_finger_tip_link')[
'position'])
451 orient = geo.Orientation_3D(PyPR2.getRobotPose()[
'orientation'], representation =
'quaternion')
453 qt = quat(h) # convert to cgkit quaternion
454 Rb = qt.toMat3() # convert to rotation matrix
456 pg = numpy.dot(orient.matrix(), p)
458 p0 = PyPR2.getRobotPose()[
'position']
464 pos = numpy.array([x,y,z])
468 return(PyPR2.getRobotPose()[
'position'])
476 orient = geo.Orientation_3D(PyPR2.getRobotPose()[
'orientation'], representation =
'quaternion')
479 return (gen.sign(u[2])*gain*tau)
483 Returns the univrsal coordinates of the base laser middle point
485 p0 = PyPR2.getRelativeTF(
'base_footprint' ,
'base_laserscan')[
'position']
490 def activate_trajectory_input(name = 'fig8', amplitude = 1.0, system_freq = 0.1, sample_freq = 2, cycle = 1):
493 PyPR2.registerRawTrajectoryInput( on_trajectory_received )
494 PyPR2.recallRhythDMPTrajectory(name=name, amplitude=amplitude, system_freq= system_freq, sample_freq= sample_freq, cycle= cycle)
499 To start receiving raw trajectory input, you should send a ROS service request call:
500 rosservice call /rhyth_dmp/recall_dmp_traj <figure name> <amplitude> <frequency> <sampling frequency> <number of cycles>
502 rosservice call /rhyth_dmp/recall_dmp_traj fig8 0.05 0.2 20 3
504 generates 3 cycles of figure eight with frequency 1.0
508 PyPR2.registerRawTrajectoryInput( on_trajectory_received )
509 assert PyPR2.useJointVelocityControl(
True)
512 global rt_cnt, rt_position, rt_velocity, rt_acceleration, rt_orientation
514 PyPR2.registerRawTrajectoryInput(
None )
515 assert PyPR2.useJointVelocityControl(
False)
516 rt_orientation =
None
517 rt_position = numpy.zeros(3)
518 rt_velocity = numpy.zeros(3)
519 rt_acceleration = numpy.zeros(3)
524 PyPR2.registerBaseScanCallback( on_base_laser )
529 PyPR2.registerTiltScanCallback( on_tilt_laser )
533 PyPR2.registerBaseScanCallback(
None )
538 PyPR2.registerBaseScanCallback(
None )
543 sets the position of left gripper from 0 to 8
550 PyPR2.setGripperPosition(1, 0.01*x)
554 sets the position of right gripper from 0 to 8
561 PyPR2.setGripperPosition(2, 0.01*x)
565 joint_name =
'l_forearm_roll_joint'
567 joint_name =
'r_forearm_roll_joint'
569 jd = PyPR2.getArmJointPositions(is_left_arm)
570 jd[joint_name] = jd[joint_name] + angle*math.pi/180.0
571 PyPR2.moveArmWithJointPos(**jd)
574 jd = PyPR2.getArmJointPositions(
True)
575 jd[
'l_wrist_roll_joint'] = jd[
'l_wrist_roll_joint'] + angle*math.pi/180.0
576 PyPR2.moveArmWithJointPos(**jd)
579 jd = PyPR2.getArmJointPositions(
False)
580 jd[
'r_wrist_roll_joint'] = jd[
'r_wrist_roll_joint'] + angle*math.pi/180.0
581 PyPR2.moveArmWithJointPos(**jd)
585 pos = PyPR2.getRelativeTF(
'/base_link',
'/l_gripper_tool_frame' )[
'position']
586 PyPR2.pointHeadTo(
'base_link', pos[0],pos[1],pos[2])
589 pos = PyPR2.getRelativeTF(
'/base_link',
'/r_gripper_tool_frame' )[
'position']
590 PyPR2.pointHeadTo(
'base_link', pos[0],pos[1],pos[2])
593 global rarm_reached, rarm_failed
597 PyPR2.moveArmWithJointPos(**g)
600 global larm_reached, larm_failed
604 PyPR2.moveArmWithJointPos(**g)
607 global body_reached, body_failed
610 rp = PyPR2.getRobotPose()
620 PyPR2.moveBodyTo(dx, dy, 0.0, time_to_reach)
623 global body_reached, body_failed
624 max_speed = math.pi/18.0
632 d_theta = gain*(desired_angle - tau0)
633 if time_to_reach ==
None:
634 time_to_reach = abs(d_theta/max_speed)
636 PyPR2.moveBodyTo(0.0, 0.0, d_theta, time_to_reach + 2.0)
639 global body_reached, body_failed
646 rp = PyPR2.getRobotPose()
652 o = quat(rp['orientation']) # convert to cgkit quaternion
653 R = numpy.linalg.inv(o.toMat3())
655 o = geo.Orientation_3D(rp[
'orientation'], representation =
'quaternion')
656 R = numpy.linalg.inv(o.matrix())
658 dp = numpy.dot(R.T, vecmat.as_vector([dx, dy, 0.0]))
660 PyPR2.moveBodyTo( dp[0], dp[1], gain*(tau - tau0), time_to_reach)
662 def finished(limb_list = ['body', 'rarm', 'larm']):
664 returns True if the motion of all the items in the limb_list is reached or failed.
666 global body_reached, body_failed
668 if 'body' in limb_list:
669 fin = fin
and (body_reached
or body_failed)
670 if 'rarm' in limb_list:
671 fin = fin
and (rarm_reached
or rarm_failed)
672 if 'larm' in limb_list:
673 fin = fin
and (larm_reached
or larm_failed)
678 waits until the motion of the items in the limb_list is reached or failed.
679 the function will return False if the motion of items is not finished by the max_time
683 while (t < max_time)
and (
not finished(limb_list)):
695 pos_traj.set_phi(0.0)
699 pos_traj.set_phi(phi_dot*t)
700 ori_traj.set_phi(phi_dot*t)
701 xd = pos_traj.current_position[0]
702 yd = pos_traj.current_position[1]
703 thd = ori_traj.current_position[0]
705 vxd = pos_traj.current_velocity[0]*phi_dot
706 vyd = pos_traj.current_velocity[1]*phi_dot
707 vthd = ori_traj.current_velocity[0]*phi_dot
710 rp = PyPR2.getRobotPose()
720 vx = vxd - k*(xa - xd)
721 vy = vyd - k*(ya - yd)
722 vth = vthd - k*(tha - thd)
724 PyPR2.moveBodyWithSpeed(vx, vy, vth)
726 t = time.time() - t_s
730 def run_config_trajectory(j_traj, n = 10, is_left_arm = False):
736 j_traj.set_phi(i*j_traj.phi_end/(n-1))
738 g = gen_larm_joint_dict(q = j_traj.current_position, time_to_reach = ttr)
740 g = gen_rarm_joint_dict(q = j_traj.current_position, time_to_reach = ttr)
742 ttr = j_traj.current_phi - phi
743 phi = j_traj.current_phi
746 PyPR2.moveArmWithJointTrajectory(dic_list)
752 global rarm_reached, rarm_failed
753 global larm_reached, larm_failed
766 phi_dot = j_traj.phi_end/duration
772 if (t > duration)
or gen.equal(t, duration, epsilon = 0.1*dt):
776 j_traj.set_phi(t*phi_dot)
786 PyPR2.moveArmWithJointTrajectoryAndSpeed(dic_list)
794 PyPR2.moveArmWithJointVelocity(**g)
799 >>> ps.pint.larm_joints(in_degrees = False)
800 array([ 0.51655997, 1.7272608 , 2.4242567 , -1.93160875, -1.00904676,
801 -1.99999936, -6.15865849])
803 array([ 0.51620768, 1.72741597, 2.42425919, -1.93156323, -1.00904459,
804 -2.26706906, 0.1245308 ])
def gen_larm_joint_posvel_dict
def rarm_elbow_flex_joint
def run_navigation_trajectory
def gen_rarm_joint_posvel_dict
def deactivate_base_laser
def gen_rarm_joint_vector_from_dic
def activate_trajectory_input
def deactivate_trajectory_input
def gen_larm_joint_vector_from_dic
def deactivate_tilt_laser
def run_config_trajectory
Runs a given arm joint trajectory on the robot.
def on_move_body_finished
def on_move_arm_finished
Callback function for arm reach: This function is called when the arm joints reach their target value...
def gen_larm_joint_vel_dict
def pos_rarm_grip_wrt_tor_shpan
def rarm_shoulder_lift_joint
def set_callback_functions
def on_trajectory_received
def larm_shoulder_lift_joint
def gen_larm_joint_dict
Use this function to convert a vector of left arm joint values, into a joint dictionary known by pyri...
def on_move_arm_failed
Callback function for arm reach failure: This function is called when the arm joints fail to reach th...
def gen_rarm_joint_vel_dict
def larm_shoulder_pan_joint
def rarm_shoulder_pan_joint
def pos_rarm_grip_wrt_tor
def larm_wrist_orientation
def pos_larm_grip_wrt_tor
def pos_larm_grip_wrt_tor_shpan
def rarm_wrist_orientation
def larm_elbow_flex_joint