00001
00002 import roslib
00003 roslib.load_manifest('pr2_keyboard_teleoperator')
00004 import rospy, sys, select, termios, tty, os
00005
00006 import signal
00007 import time
00008
00009 from trajectory_msgs.msg import *
00010 from pr2_mechanism_msgs.msg import *
00011 from pr2_mechanism_msgs.srv import *
00012 from pr2_controllers_msgs.msg import *
00013 from geometry_msgs.msg import Twist
00014
00015
00016 usage = """
00017 Reading from the keyboard
00018 --------------------------
00019
00020 Moving the Base:
00021
00022 7 8 9
00023 4 5 6
00024 1 2 3
00025
00026 Moving the Arm's Joints:
00027
00028 Shoulder pan joint: e/d
00029 Shoulder lift joint: r/f
00030 Upper arm roll joint: t/g
00031 Elbow flex joint: y/h
00032 Forearm roll joint: u/j
00033 Wrist flex joint: i/k
00034 Wrist roll joint: o/l
00035
00036 Moving The Head:
00037
00038 Panning: v/b
00039 Tilting: n/m
00040
00041 Moving The Torso:
00042
00043 Lower/Raise: ,/.
00044
00045 Using the gripper:
00046
00047 Open/Close (current arm): c
00048
00049 Parameters:
00050
00051 Switching between arms: z/x
00052 Increasing/Decreasing delta (in radians): q/a
00053
00054 CTRL-C to quit
00055 (p) to reprint USAGE
00056 --------------------------
00057 """
00058
00059
00060 pub_right = rospy.Publisher('r_arm_controller/command', JointTrajectory, latch=True)
00061 pub_left = rospy.Publisher('l_arm_controller/command', JointTrajectory, latch=True)
00062 rhand_pub = rospy.Publisher('r_gripper_controller/command', Pr2GripperCommand)
00063 lhand_pub = rospy.Publisher('l_gripper_controller/command', Pr2GripperCommand)
00064 head_pub = rospy.Publisher('head_traj_controller/command', JointTrajectory, latch=True)
00065 torso_pub = rospy.Publisher('torso_controller/command', JointTrajectory)
00066
00067
00068 open_cmd = Pr2GripperCommand()
00069 open_cmd.position = 0.08
00070 open_cmd.max_effort = -1.0
00071
00072
00073 close_cmd = Pr2GripperCommand()
00074 close_cmd.position = -100.00
00075 close_cmd.max_effort = -1.0
00076
00077
00078 jointBindings = {
00079 'e':(1,0),
00080 'd':(2,0),
00081 'r':(3,0),
00082 'f':(4,0),
00083 't':(5,0),
00084 'g':(6,0),
00085 'y':(7,0),
00086 'h':(8,0),
00087 'u':(9,0),
00088 'j':(10,0),
00089 'i':(11,0),
00090 'k':(12,0),
00091 'o':(13,0),
00092 'l':(14,0),
00093 }
00094
00095 moveBindings = {
00096 '8':(1,0),
00097 '9':(1,-1),
00098 '4':(0,1),
00099 '6':(0,-1),
00100 '7':(1,1),
00101 '2':(-1,0),
00102 '3':(-1,1),
00103 '1':(-1,-1),
00104 }
00105
00106 paramterBindings ={
00107 'z':(1,0),
00108 'x':(2,0),
00109 'q':(3,0),
00110 'a':(4,0),
00111 'p':(5,0),
00112 }
00113
00114 headGripperBindings ={
00115 'c':(1,0),
00116 'v':(2,0),
00117 'b':(3,0),
00118 'n':(4,0),
00119 'm':(5,0),
00120 ',':(6,0),
00121 '.':(7,0),
00122 }
00123
00124 def handleArms(side, positions):
00125 traj = JointTrajectory()
00126 traj.joint_names = ["%s_shoulder_pan_joint" % side,
00127 "%s_shoulder_lift_joint" % side,
00128 "%s_upper_arm_roll_joint" % side,
00129 "%s_elbow_flex_joint" % side,
00130 "%s_forearm_roll_joint" % side,
00131 "%s_wrist_flex_joint" % side,
00132 "%s_wrist_roll_joint" % side]
00133 traj.points = []
00134 for p in positions:
00135 traj.points.append(JointTrajectoryPoint(positions = p[1:],
00136 velocities = [0.0] * (len(p) - 1),
00137 accelerations = [],
00138 time_from_start = rospy.Duration(p[0])))
00139
00140 traj.header.stamp = rospy.get_rostime() + rospy.Duration(0.001)
00141 if(side == 'l'):
00142 pub_left.publish(traj)
00143 else:
00144 pub_right.publish(traj)
00145
00146
00147 def handleTorso(x):
00148 p = [x]
00149 traj = JointTrajectory()
00150 traj.joint_names = ["torso_lift_joint"]
00151 traj.points = []
00152 traj.points.append(JointTrajectoryPoint(positions = p,
00153 velocities = [0.0] * (len(p)),
00154 accelerations = [],
00155 time_from_start = rospy.Duration(0.0)))
00156
00157 traj.header.stamp = rospy.get_rostime() + rospy.Duration(0.001)
00158 torso_pub.publish(traj)
00159
00160
00161 def handleHead(x,y):
00162 p = [x,y]
00163 traj = JointTrajectory()
00164 traj.joint_names = ["head_pan_joint", "head_tilt_joint"]
00165 traj.points = []
00166 traj.points.append(JointTrajectoryPoint(positions = p,
00167 velocities = [0.0] * (len(p)),
00168 accelerations = [],
00169 time_from_start = rospy.Duration(0.0)))
00170
00171 traj.header.stamp = rospy.get_rostime() + rospy.Duration(0.001)
00172 head_pub.publish(traj)
00173
00174
00175 def getKey():
00176 tty.setraw(sys.stdin.fileno())
00177 select.select([sys.stdin], [], [], 0)
00178 key = sys.stdin.read(1)
00179 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
00180 return key
00181
00182 def initialize():
00183 print 'Placing arms, grippers, torso, and head in default positions'
00184 initial_positions = [[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]]
00185 positions = [[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]]
00186 handleArms ('r', initial_positions)
00187 handleArms ('l', initial_positions)
00188 handleHead (0,0)
00189 handleTorso(0)
00190
00191 speed = .5
00192 turn = 1
00193
00194
00195 if __name__=="__main__":
00196
00197 rospy.init_node('pr2_keyboard_teleoperator', anonymous = False)
00198 rospy.wait_for_service('pr2_controller_manager/switch_controller')
00199
00200 settings = termios.tcgetattr(sys.stdin)
00201 pub_base = rospy.Publisher('/base_controller/command', Twist)
00202
00203 delta = 0.1
00204 arm = 'r'
00205 x = 0
00206 th = 0
00207
00208 l_joint_1 = 0
00209 l_joint_2 = 0
00210 l_joint_3 = 0
00211 l_joint_4 = 0
00212 l_joint_5 = 0
00213 l_joint_6 = 0
00214 l_joint_7 = 0
00215 r_joint_1 = 0
00216 r_joint_2 = 0
00217 r_joint_3 = 0
00218 r_joint_4 = 0
00219 r_joint_5 = 0
00220 r_joint_6 = 0
00221 r_joint_7 = 0
00222
00223 r_gripper = 0
00224 l_gripper = 0
00225
00226 head_pan = 0
00227 head_tilt = 0
00228 torso = 0
00229
00230 initialize()
00231
00232 try:
00233 print usage
00234 while(1):
00235 key = getKey()
00236
00237 if key in paramterBindings.keys():
00238 x = paramterBindings[key][0]
00239
00240 if x == 1:
00241 arm = 'l'
00242 print 'Now controlling Left Arm'
00243
00244 elif x == 2:
00245 arm = 'r'
00246 print 'Now controlling Right Arm'
00247
00248 elif x == 3:
00249 delta += 0.01;
00250 print 'New delta value = ' + str(delta)
00251
00252 elif x == 4 and delta > 0.02:
00253 delta -= 0.01;
00254 print 'New delta value = ' + str(delta)
00255
00256 elif x == 5:
00257 print usage
00258
00259
00260 elif key in moveBindings.keys():
00261 x = moveBindings[key][0]
00262 th = moveBindings[key][1]
00263
00264 twist = Twist()
00265 twist.linear.x = x*speed; twist.linear.y = 0; twist.linear.z = 0
00266 twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = th*turn
00267 pub_base.publish(twist)
00268
00269
00270 elif key in headGripperBindings.keys():
00271 z = headGripperBindings[key][0]
00272
00273 if z == 1:
00274 if arm == 'l':
00275 if l_gripper == 0:
00276 l_gripper = 1
00277 lhand_pub.publish(open_cmd)
00278 elif l_gripper == 1:
00279 l_gripper = 0
00280 lhand_pub.publish(close_cmd)
00281 elif arm == 'r':
00282 if r_gripper == 0:
00283 r_gripper = 1
00284 rhand_pub.publish(open_cmd)
00285 elif r_gripper == 1:
00286 r_gripper = 0
00287 rhand_pub.publish(close_cmd)
00288
00289 elif z == 2:
00290 head_tilt += 0.1
00291 handleHead (head_tilt, head_pan)
00292 elif z == 3:
00293 head_tilt -= 0.1
00294 handleHead (head_tilt, head_pan)
00295 elif z == 4:
00296 head_pan -= 0.1
00297 handleHead (head_tilt, head_pan)
00298 elif z == 5:
00299 head_pan += 0.1
00300 handleHead (head_tilt, head_pan)
00301
00302 elif z == 6 and torso > 0:
00303 torso -= 0.01
00304 handleTorso(torso)
00305 elif z == 7 and torso < 0.3:
00306 torso += 0.01
00307 handleTorso(torso)
00308
00309
00310 elif key in jointBindings.keys():
00311 y = jointBindings[key][0]
00312
00313
00314 if y == 1:
00315 if arm == 'l':
00316 l_joint_1 += delta
00317 elif arm == 'r':
00318 r_joint_1 += delta
00319
00320 elif y == 2:
00321 if arm == 'l':
00322 l_joint_1 -= delta
00323 elif arm == 'r':
00324 r_joint_1 -= delta
00325
00326
00327
00328 elif y == 4:
00329 if arm == 'l':
00330 l_joint_2 += delta
00331 elif arm == 'r':
00332 r_joint_2 += delta
00333
00334 elif y == 3:
00335 if arm == 'l':
00336 l_joint_2 -= delta
00337 elif arm == 'r':
00338 r_joint_2 -= delta
00339
00340
00341
00342 elif y == 5:
00343 if arm == 'l':
00344 l_joint_3 += delta
00345 elif arm == 'r':
00346 r_joint_3 += delta
00347
00348 elif y == 6:
00349 if arm == 'l':
00350 l_joint_3 -= delta
00351 elif arm == 'r':
00352 r_joint_3 -= delta
00353
00354
00355
00356 elif y == 8:
00357 if arm == 'l':
00358 l_joint_4 += delta
00359 elif arm == 'r':
00360 r_joint_4 += delta
00361
00362 elif y == 7:
00363 if arm == 'l':
00364 l_joint_4 -= delta
00365 elif arm == 'r':
00366 r_joint_4 -= delta
00367
00368
00369
00370 elif y == 9:
00371 if arm == 'l':
00372 l_joint_5 += delta
00373 elif arm == 'r':
00374 r_joint_5 += delta
00375
00376 elif y == 10:
00377 if arm == 'l':
00378 l_joint_5 -= delta
00379 elif arm == 'r':
00380 r_joint_5 -= delta
00381
00382
00383
00384 elif y == 12:
00385 if arm == 'l':
00386 l_joint_6 += delta
00387 elif arm == 'r':
00388 r_joint_6 += delta
00389
00390 elif y == 11:
00391 if arm == 'l':
00392 l_joint_6 -= delta
00393 elif arm == 'r':
00394 r_joint_6 -= delta
00395
00396
00397
00398 elif y == 13:
00399 if arm == 'l':
00400 l_joint_7 += delta
00401 elif arm == 'r':
00402 r_joint_7 += delta
00403
00404 elif y == 14:
00405 if arm == 'l':
00406 l_joint_7 -= delta
00407 elif arm == 'r':
00408 r_joint_7 -= delta
00409
00410
00411
00412 if arm == 'l':
00413 positions = [[0.0,float(l_joint_1),float(l_joint_2),float(l_joint_3),float(l_joint_4),float(l_joint_5),float(l_joint_6),float(l_joint_7)]]
00414 elif arm == 'r':
00415 positions = [[0.0,float(r_joint_1),float(r_joint_2),float(r_joint_3),float(r_joint_4),float(r_joint_5),float(r_joint_6),float(r_joint_7)]]
00416
00417 handleArms (arm, positions)
00418
00419
00420 else:
00421 if (key == '\x03'):
00422 break
00423 except:
00424 print usage
00425
00426 finally:
00427 initialize()
00428
00429 twist = Twist()
00430 twist.linear.x = 0; twist.linear.y = 0; twist.linear.z = 0
00431 twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = 0
00432 pub_base.publish(twist)
00433
00434 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
00435
00436