MAGIKS  1.1
Manipulator General Inverse Kinematic Solver
 All Classes Namespaces Files Functions Variables Pages
pyride_interpreter.py
Go to the documentation of this file.
1 ## @file pyride_interpreter.py
2 # @brief Contains simplified functions to control PR2 using pyride engine
3 # @author Nima Ramezani Taghiabadi
4 #
5 # PhD Researcher
6 # Faculty of Engineering and Information Technology
7 # University of Technology Sydney (UTS)
8 # Broadway, Ultimo, NSW 2007, Australia
9 # Phone No. : 04 5027 4611
10 # Email(1) : nima.ramezani@gmail.com
11 # Email(2) : Nima.RamezaniTaghiabadi@uts.edu.au
12 # @version 5.0
13 #
14 # Last Revision: 13 May 2015
15 
16 '''
17 Changes from ver 4.0:
18  1- Added functions for receiving online dmp trajectory from pyride
19 
20 '''
21 
22 import PyPR2, numpy, math, time
23 from math_tools.algebra import vectors_and_matrices as vecmat
24 from math_tools.geometry import geometry as geo
25 from math_tools import general_math as gen
26 
27 # from cgkit.cgtypes import quat, mat3
28 
29 # Global Variables
30 
31 ## A boolean global variable indicating if the left arm joints have reached their target.
32 # Set as True by default, this variable switches to False immidiately after any move arm task function is called for the left arm
33 # and is set back to True when the left arm reaches its target
34 larm_reached = True
35 
36 ## A boolean global variable indicating if the right arm joints have reached their target.
37 # Set as True by default, this variable switches to False immidiately after any move arm task function is called for the right arm
38 # and is set back to True when the right arm reaches its target
39 rarm_reached = True
40 
41 ## A boolean global variable indicating if the body joints (non-arm joints) have reached their target.
42 # Set as True by default, this variable switches to False immidiately after any move function is called
43 # for the body navigation joints or the prismatic lifter joint
44 # and is set back to True when the body joints reach their target, so it always shows the status of the lastest task.
45 body_reached = False
46 
47 ## A boolean global variable indicating if the left arm joints failed to reach their target.
48 # Set as False by default, this variable switches to True only if any move arm task function is called
49 # and the left arm can not reach its target within a certain time.
50 # It is set back to False immidiately after a move arm task function is called for left arm, so it always shows the status of the lastest task.
51 larm_failed = False
52 
53 
54 rarm_failed = False
55 body_failed = False
56 counter = 0
57 
58 ## Counter for the base laser scan. This counter starts increasing as soon as
59 # the base laser scan is activated by function activate_base_laser()
60 bl_cnt = 0
61 ## Counter for the raw trajectory function calls. This counter starts increasing as soon as
62 # the raw trajectory input is activated by function activate_raw_trajectory_input()
63 rt_cnt = 0
64 
65 bl_dist = None
66 bl_active = False
67 tl_cnt = 0
68 tl_dist = None
69 tl_active = False
70 
71 rt_orientation = None
72 rt_position = numpy.zeros(3)
73 rt_velocity = numpy.zeros(3)
74 rt_acceleration = numpy.zeros(3)
75 rt_active = False
76 
77 # Service event functions
78 
79 ## Callback function for arm reach: This function is called when the arm joints reach their target values
80 # Global variable larm_reached or rarm_reached will be switched to True depending on which arm is specified by argument is_left_arm
81 # @param is_left_arm A boolean parameter specifying which arm is considered:
82 # True for the left arm and False for the right arm.
83 # @return None
84 def on_move_arm_finished( is_left_arm ):
85  global larm_reached, rarm_reached
86  if hasattr( PyPR2, 'onMoveArmPoseComplete' ) and hasattr( PyPR2.onMoveArmPoseComplete, '__call__' ):
87  PyPR2.onMoveArmPoseComplete( is_left_arm )
88  if is_left_arm:
89  print "larm reached"
90  larm_reached = True
91  else:
92  print "rarm reached"
93  rarm_reached = True
94 
95 ## Callback function for arm reach failure: This function is called when the arm joints fail to reach their target values
96 # Global variable larm_failed or rarm_failed will be switched to True depending on which arm is specified by argument is_left_arm
97 # @param is_left_arm A boolean parameter specifying which arm is considered:
98 # True for the left arm and False for the right arm.
99 # @return None
100 def on_move_arm_failed( 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 )
104  if is_left_arm:
105  print "larm failed"
106  larm_failed = True
107  else:
108  rarm_failed = True
109  print "rarm failed"
110 
112  global body_reached
113  body_reached = True
114  print "body reached"
115 
117  global body_failed
118  body_failed = True
119  print "body failed"
120 
121 def on_base_laser(range, intensity):
122  global bl_cnt, bl_dist, bl_active
123  bl_active = True
124  bl_cnt += 1
125  bl_dist = range
126 
127 def on_tilt_laser(range, intensity):
128  global tl_cnt, tl_dist, tl_active
129  tl_active = True
130  tl_cnt += 1
131  tl_dist = range
132 
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
138 
140  global rt_cnt, rt_active
141  rt_active = True
142  rt_cnt += 1
143  rt_position[0] = data['position'][0]
144  rt_position[1] = data['position'][1]
145  rt_position[2] = data['position'][2]
146 
147  rt_velocity[0] = data['velocity'][0]
148  rt_velocity[1] = data['velocity'][1]
149  rt_velocity[2] = data['velocity'][2]
150 
151  rt_acceleration[0] = data['acceleration'][0]
152  rt_acceleration[1] = data['acceleration'][1]
153  rt_acceleration[2] = data['acceleration'][2]
154 
155  rt_orientation[0] = data['acceleration'][2]
156 
158 
159 # Conversion Functions
160 
161 ## Use this function to convert a vector of left arm joint values, into a joint dictionary known by pyride
162 # @param q A numpy vector of 7 elements containing the desired left arm joint values
163 # @time_to_reach A float specifying the amount of time (in seconds) needed to reach the target
164 # @return A dictionary known by pyride which can be passed to function <provide the link>
165 def gen_larm_joint_dict(q, time_to_reach = 5.0):
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 }
167  return(g)
168 
169 def gen_rarm_joint_dict(q, time_to_reach = 5.0):
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 }
171  return(g)
172 
173 def gen_rarm_joint_posvel_dict(q, q_dot, 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}
175  return(g)
176 
177 def gen_larm_joint_posvel_dict(q, q_dot, 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}
179  return(g)
180 
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]}
183  return(g)
184 
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]}
187  return(g)
188 
190  q = numpy.zeros(7)
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']
198  return q
199 
201  q = numpy.zeros(7)
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']
209  return q
210 
211 # Sensory Functions
212 
213 def larm_shoulder_pan_joint(in_degrees = True):
214  '''
215  returns the current angle of the shoulder pan joint of the left arm
216  '''
217  if in_degrees:
218  gain = 180.0/math.pi
219  else:
220  gain = 1.0
221  lajd = PyPR2.getArmJointPositions(True)
222  return(gain*lajd['l_shoulder_pan_joint'])
223 
224 def rarm_shoulder_pan_joint(in_degrees = True):
225  '''
226  returns the current angle of the shoulder pan joint of the right arm
227  '''
228  if in_degrees:
229  gain = 180.0/math.pi
230  else:
231  gain = 1.0
232  rajd = PyPR2.getArmJointPositions(False)
233  return(gain*rajd['r_shoulder_pan_joint'])
234 
235 def rarm_shoulder_lift_joint(in_degrees = True):
236  '''
237  returns the current angle of the shoulder lift joint of the right arm
238  '''
239  if in_degrees:
240  gain = 180.0/math.pi
241  else:
242  gain = 1.0
243  rajd = PyPR2.getArmJointPositions(False)
244  return(gain*rajd['r_shoulder_lift_joint'])
245 
246 def larm_shoulder_lift_joint(in_degrees = True):
247  '''
248  returns the current angle of the shoulder lift joint of the left arm
249  '''
250  if in_degrees:
251  gain = 180.0/math.pi
252  else:
253  gain = 1.0
254  rajd = PyPR2.getArmJointPositions(True)
255  return(gain*rajd['l_shoulder_lift_joint'])
256 
257 def larm_elbow_flex_joint(in_degrees = True):
258  '''
259  returns the current angle of the shoulder pan joint of the left arm
260  '''
261  if in_degrees:
262  gain = 180.0/math.pi
263  else:
264  gain = 1.0
265  lajd = PyPR2.getArmJointPositions(True)
266  return(gain*lajd['l_elbow_flex_joint'])
267 
268 def rarm_elbow_flex_joint(in_degrees = True):
269  '''
270  returns the current angle of the shoulder pan joint of the right arm
271  '''
272  if in_degrees:
273  gain = 180.0/math.pi
274  else:
275  gain = 1.0
276  rajd = PyPR2.getArmJointPositions(False)
277  return(gain*rajd['r_elbow_flex_joint'])
278 
279 def rarm_joints(in_degrees = True):
280  if in_degrees:
281  gain = 180.0/math.pi
282  else:
283  gain = 1.0
284  rajd = PyPR2.getArmJointPositions(False)
285  return(gain*gen_rarm_joint_vector_from_dic(rajd))
286 
287 def larm_joints(in_degrees = True):
288  if in_degrees:
289  gain = 180.0/math.pi
290  else:
291  gain = 1.0
292  lajd = PyPR2.getArmJointPositions(True)
293  return(gain*gen_larm_joint_vector_from_dic(lajd))
294 
296  '''
297  returns the wrist position of the left arm comparable to function wrist_position() in the arm object
298  '''
299  p0 = PyPR2.getRelativeTF('torso_lift_link' , 'l_shoulder_pan_link')['position']
300  p = PyPR2.getRelativeTF('torso_lift_link' , 'l_wrist_flex_link')['position']
301  x = p[0] - p0[0]
302  y = p[1] - p0[1]
303  z = p[2] - p0[2]
304  pos = numpy.array([x,y,z])
305  return(pos)
306 
308  '''
309  returns the wrist orientation of the left arm (the same orientation returned by function wrist_orientation() in the arm object but in quaternions)
310  '''
311  q = PyPR2.getRelativeTF('torso_lift_link' , 'l_wrist_flex_link')['orientation']
312  return(q)
313 
315  '''
316  returns the wrist orientation of the right arm (the same orientation returned by function wrist_orientation() in the arm object but in quaternions)
317  '''
318  q = PyPR2.getRelativeTF('torso_lift_link' , 'r_wrist_flex_link')['orientation']
319  return(q)
320 
321 """
322 def rarm_gripper_position():
323  '''
324  returns the gripper position of the right arm comparable to function wrist_position() in the arm object
325  '''
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'])
329  p = (pl + pr)/2
330  x = p[0] - p0[0]
331  y = p[1] - p0[1]
332  z = p[2] - p0[2]
333  pos = [x,y,z]
334  return(pos)
335 """
336 
338  '''
339  returns the wrist position of the right arm comparable to function wrist_position() in the arm object
340  '''
341  p0 = PyPR2.getRelativeTF('torso_lift_link' , 'r_shoulder_pan_link')['position']
342  p = PyPR2.getRelativeTF('torso_lift_link' , 'r_wrist_flex_link')['position']
343  x = p[0] - p0[0]
344  y = p[1] - p0[1]
345  z = p[2] - p0[2]
346  pos = numpy.array([x,y,z])
347  return(pos)
348 
350  '''
351  returns the elbow position of the right arm comparable to function wrist_position() in the arm object
352  '''
353  p0 = PyPR2.getRelativeTF('torso_lift_link' , 'r_shoulder_pan_link')['position']
354  p = PyPR2.getRelativeTF('torso_lift_link' , 'r_elbow_flex_link')['position']
355  x = p[0] - p0[0]
356  y = p[1] - p0[1]
357  z = p[2] - p0[2]
358  pos = numpy.array([x,y,z])
359  return(pos)
360 
362  '''
363  returns the elbow position of the right arm comparable to function wrist_position() in the arm object
364  '''
365  p0 = PyPR2.getRelativeTF('torso_lift_link' , 'l_shoulder_pan_link')['position']
366  p = PyPR2.getRelativeTF('torso_lift_link' , 'l_elbow_flex_link')['position']
367  x = p[0] - p0[0]
368  y = p[1] - p0[1]
369  z = p[2] - p0[2]
370  pos = numpy.array([x,y,z])
371  return(pos)
372 
374  '''
375  Returns the right arm gripper position vector(endeffector position) with respect to the torso shoulder pan joint center
376  '''
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'])
380  p = (pl + pr)/2
381  x = p[0] - p0[0]
382  y = p[1] - p0[1]
383  z = p[2] - p0[2]
384  pos = numpy.array([x,y,z])
385  return(pos)
386 
388  '''
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.
392  '''
393 
394  p0 = PyPR2.getRelativeTF('base_link' , 'l_shoulder_pan_link')['position']
395  p = p0 + pos_larm_grip_wrt_tor_shpan() + numpy.array([0.05, 0.0, 0.051])
396 
397  return p
398 
400  '''
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())
402  '''
403 
404  p0 = PyPR2.getRelativeTF('base_link' , 'r_shoulder_pan_link')['position']
405  p = p0 + pos_rarm_grip_wrt_tor_shpan() + numpy.array([0.0, 0.0, 0.051])
406 
407  return p
408 
410  '''
411  Returns the left arm gripper position vector(endeffector position) with respect to the torso shoulder pan joint center
412  '''
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'])
416  p = (pl + pr)/2
417  x = p[0] - p0[0]
418  y = p[1] - p0[1]
419  z = p[2] - p0[2]
420  pos = numpy.array([x,y,z])
421  return(pos)
422 
424  '''
425  Returns the global position of the right arm gripper finger tip
426  '''
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'])
429  p = (pl + pr)/2
430 
431  orient = geo.Orientation_3D(PyPR2.getRobotPose()['orientation'], representation = 'quaternion')
432  pg = numpy.dot(orient.matrix(), p) # p global is Rb Multiply by p
433 
434  p0 = PyPR2.getRobotPose()['position']
435 
436  x = pg[0] + p0[0]
437  y = pg[1] + p0[1]
438  z = pg[2] + p0[2]
439 
440  pos = numpy.array([x,y,z])
441  return(pos)
442 
444  '''
445  Returns the global position of the left arm gripper finger tip
446  '''
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'])
449  p = (pl + pr)/2
450 
451  orient = geo.Orientation_3D(PyPR2.getRobotPose()['orientation'], representation = 'quaternion')
452  '''
453  qt = quat(h) # convert to cgkit quaternion
454  Rb = qt.toMat3() # convert to rotation matrix
455  '''
456  pg = numpy.dot(orient.matrix(), p) # p global is Rb Multiply by p
457 
458  p0 = PyPR2.getRobotPose()['position']
459 
460  x = pg[0] + p0[0]
461  y = pg[1] + p0[1]
462  z = pg[2] + p0[2]
463 
464  pos = numpy.array([x,y,z])
465  return(pos)
466 
468  return(PyPR2.getRobotPose()['position'])
469 
470 def body_angle(in_degrees=True ):
471  if in_degrees:
472  gain = 180.0/math.pi
473  else:
474  gain = 1.0
475 
476  orient = geo.Orientation_3D(PyPR2.getRobotPose()['orientation'], representation = 'quaternion')
477  tau = orient.angle()
478  u = orient.axis()
479  return (gen.sign(u[2])*gain*tau)
480 
481 def bl_midpoint():
482  '''
483  Returns the univrsal coordinates of the base laser middle point
484  '''
485  p0 = PyPR2.getRelativeTF('base_footprint' , 'base_laserscan')['position']
486 
487 # Actuating Functions
488 
489 '''
490 def activate_trajectory_input(name = 'fig8', amplitude = 1.0, system_freq = 0.1, sample_freq = 2, cycle = 1):
491  global rt_cnt
492  rt_cnt = 0
493  PyPR2.registerRawTrajectoryInput( on_trajectory_received )
494  PyPR2.recallRhythDMPTrajectory(name=name, amplitude=amplitude, system_freq= system_freq, sample_freq= sample_freq, cycle= cycle)
495 '''
496 
498  '''
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>
501  For example:
502  rosservice call /rhyth_dmp/recall_dmp_traj fig8 0.05 0.2 20 3
503 
504  generates 3 cycles of figure eight with frequency 1.0
505  '''
506  global rt_cnt
507  rt_cnt = 0
508  PyPR2.registerRawTrajectoryInput( on_trajectory_received )
509  assert PyPR2.useJointVelocityControl(True)
510 
512  global rt_cnt, rt_position, rt_velocity, rt_acceleration, rt_orientation
513  rt_cnt = 0
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)
520 
522  global bl_cnt
523  bl_cnt = 0
524  PyPR2.registerBaseScanCallback( on_base_laser )
525 
527  global tl_cnt
528  tl_cnt = 0
529  PyPR2.registerTiltScanCallback( on_tilt_laser )
530 
532  global bl_active
533  PyPR2.registerBaseScanCallback( None )
534  bl_active = False
535 
537  global tl_active
538  PyPR2.registerBaseScanCallback( None )
539  tl_active = False
540 
541 def set_lg(x = 1):
542  '''
543  sets the position of left gripper from 0 to 8
544  '''
545  if x > 8:
546  x = 8
547  if x < 0:
548  x = 0
549 
550  PyPR2.setGripperPosition(1, 0.01*x)
551 
552 def set_rg(x = 1):
553  '''
554  sets the position of right gripper from 0 to 8
555  '''
556  if x > 8:
557  x = 8
558  if x < 0:
559  x = 0
560 
561  PyPR2.setGripperPosition(2, 0.01*x)
562 
563 def twist_forearm(angle = 180.0, is_left_arm = True):
564  if is_left_arm:
565  joint_name = 'l_forearm_roll_joint'
566  else:
567  joint_name = 'r_forearm_roll_joint'
568 
569  jd = PyPR2.getArmJointPositions(is_left_arm)
570  jd[joint_name] = jd[joint_name] + angle*math.pi/180.0
571  PyPR2.moveArmWithJointPos(**jd)
572 
573 def twist_lg(angle = 180.0):
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)
577 
578 def twist_rg(angle = 180.0):
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)
582 
583 def look_lg():
584  #pos = pos_larm_grip_wrt_tor_shpan()
585  pos = PyPR2.getRelativeTF( '/base_link', '/l_gripper_tool_frame' )['position']
586  PyPR2.pointHeadTo('base_link', pos[0],pos[1],pos[2])
587 
588 def look_rg():
589  pos = PyPR2.getRelativeTF( '/base_link', '/r_gripper_tool_frame' )['position']
590  PyPR2.pointHeadTo('base_link', pos[0],pos[1],pos[2])
591 
592 def take_rarm_to(qd, time_to_reach = 5.0):
593  global rarm_reached, rarm_failed
594  rarm_reached = False
595  rarm_failed = False
596  g = gen_rarm_joint_dict(qd, time_to_reach)
597  PyPR2.moveArmWithJointPos(**g)
598 
599 def take_larm_to(qd, time_to_reach = 5.0):
600  global larm_reached, larm_failed
601  larm_reached = False
602  larm_failed = False
603  g = gen_larm_joint_dict(qd, time_to_reach)
604  PyPR2.moveArmWithJointPos(**g)
605 
606 def take_robot_to(X, Y, time_to_reach = 5.0):
607  global body_reached, body_failed
608  body_reached = False
609  body_failed = False
610  rp = PyPR2.getRobotPose()
611  P0 = rp['position']
612  dX = X - P0[0]
613  dY = Y - P0[1]
614  tau = body_angle(in_degrees = False)
615  S = math.sin(tau)
616  C = math.cos(tau)
617  dx = C*dX + S*dY
618  dy = -S*dX + C*dY
619 
620  PyPR2.moveBodyTo(dx, dy, 0.0, time_to_reach)
621 
622 def rotate_robot_to(desired_angle, time_to_reach = None, in_degrees = True):
623  global body_reached, body_failed
624  max_speed = math.pi/18.0 # (The maximum speed is 10 degrees per second)
625  if in_degrees:
626  gain = math.pi/180.0
627  else:
628  gain = 1.0
629  body_reached = False
630  body_failed = False
631  tau0 = body_angle(in_degrees = in_degrees)
632  d_theta = gain*(desired_angle - tau0)
633  if time_to_reach == None:
634  time_to_reach = abs(d_theta/max_speed)
635 
636  PyPR2.moveBodyTo(0.0, 0.0, d_theta, time_to_reach + 2.0)
637 
638 def move_robot_to(x, y , tau, time_to_reach = 5.0, in_degrees = True):
639  global body_reached, body_failed
640  if in_degrees:
641  gain = math.pi/180.0
642  else:
643  gain = 1.0
644  body_reached = False
645  body_failed = False
646  rp = PyPR2.getRobotPose()
647  p0 = rp['position']
648  dx = x - p0[0]
649  dy = y - p0[1]
650 
651  '''
652  o = quat(rp['orientation']) # convert to cgkit quaternion
653  R = numpy.linalg.inv(o.toMat3())
654  '''
655  o = geo.Orientation_3D(rp['orientation'], representation = 'quaternion')
656  R = numpy.linalg.inv(o.matrix())
657 
658  dp = numpy.dot(R.T, vecmat.as_vector([dx, dy, 0.0]))
659  tau0 = body_angle(in_degrees = in_degrees)
660  PyPR2.moveBodyTo( dp[0], dp[1], gain*(tau - tau0), time_to_reach)
661 
662 def finished(limb_list = ['body', 'rarm', 'larm']):
663  '''
664  returns True if the motion of all the items in the limb_list is reached or failed.
665  '''
666  global body_reached, body_failed
667  fin = True
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)
674  return fin
675 
676 def wait_until_finished(limb_list = ['body', 'rarm', 'larm'], max_time = 20.0 ):
677  '''
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
680  '''
681  t0 = time.time()
682  t = 0.0
683  while (t < max_time) and (not finished(limb_list)):
684  t = time.time() - t0
685  time.sleep(0.01)
686  if finished(limb_list):
687  return True
688  else:
689  print "Time Out !"
690  return False
691 
692 def run_navigation_trajectory(duration, pos_traj, ori_traj, phi_dot = 1.0, k = 1.0):
693  t_s = time.time()
694  t = 0.0
695  pos_traj.set_phi(0.0)
696  while t < duration:
697  t0 = t
698  # Get the desired pose:
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]
704  # Get the desired pose speed:
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
708  # Get the actual pose:
709  PyPR2.getRobotPose()
710  rp = PyPR2.getRobotPose()
711  p0 = rp['position']
712  xa = p0[0]
713  ya = p0[1]
714  tha = body_angle(in_degrees = False)
715  # calculate error:
716  ex = xa - xd
717  ey = ya - yd
718  eth = tha - thd
719  # find the control speed to be sent:
720  vx = vxd - k*(xa - xd)
721  vy = vyd - k*(ya - yd)
722  vth = vthd - k*(tha - thd)
723 
724  PyPR2.moveBodyWithSpeed(vx, vy, vth)
725 
726  t = time.time() - t_s
727  dt = t - t0
728 
729 '''
730 def run_config_trajectory(j_traj, n = 10, is_left_arm = False):
731  phi = 0.0
732  ttr = 5.0
733  dic_list = []
734 
735  for i in range(n):
736  j_traj.set_phi(i*j_traj.phi_end/(n-1))
737  if is_left_arm:
738  g = gen_larm_joint_dict(q = j_traj.current_position, time_to_reach = ttr)
739  else:
740  g = gen_rarm_joint_dict(q = j_traj.current_position, time_to_reach = ttr)
741 
742  ttr = j_traj.current_phi - phi
743  phi = j_traj.current_phi
744  dic_list.append(g)
745 
746  PyPR2.moveArmWithJointTrajectory(dic_list)
747 '''
748 
749 ## Runs a given arm joint trajectory on the robot.
750 # @param j_traj An instance of class packages.nima.robotics.kinematics.task_space.trajectory.Trajectory()
751 def run_config_trajectory(j_traj, duration = 10.0, dt = None, phi_dot = None, is_left_arm = False):
752  global rarm_reached, rarm_failed
753  global larm_reached, larm_failed
754  if is_left_arm:
755  larm_failed = False
756  larm_reached = False
757  else:
758  rarm_failed = False
759  rarm_reached = False
760 
761  phi = 0.0
762  dic_list = []
763  t = 0.0
764 
765  if phi_dot == None:
766  phi_dot = j_traj.phi_end/duration
767  if dt == None:
768  dt = 0.1*duration
769 
770  stay = True
771  while stay:
772  if (t > duration) or gen.equal(t, duration, epsilon = 0.1*dt):
773  t = duration
774  stay = False
775 
776  j_traj.set_phi(t*phi_dot)
777  if is_left_arm:
778  g = gen_larm_joint_posvel_dict(j_traj.current_position, j_traj.current_velocity*phi_dot, dt)
779  else:
780  g = gen_rarm_joint_posvel_dict(j_traj.current_position, j_traj.current_velocity*phi_dot, dt)
781 
782  dic_list.append(g)
783 
784  t = t + dt
785 
786  PyPR2.moveArmWithJointTrajectoryAndSpeed(dic_list)
787 
788 def send_arm_joint_speed(q_dot, is_left_arm = False):
789  if is_left_arm:
790  g = gen_larm_joint_vel_dict(q_dot)
791  else:
792  g = gen_rarm_joint_vel_dict(q_dot)
793 
794  PyPR2.moveArmWithJointVelocity(**g)
795 
796 '''
797 
798 The problem is here:
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])
802 >>> b.larm.config.q
803 array([ 0.51620768, 1.72741597, 2.42425919, -1.93156323, -1.00904459,
804  -2.26706906, 0.1245308 ])
805 '''
def run_config_trajectory
Runs a given arm joint trajectory on the robot.
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_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...