|
| 1 | +#!/usr/bin/env python |
| 2 | +import roslib; roslib.load_manifest('tutorial_atlas_control') |
| 3 | +import rospy, yaml, sys |
| 4 | +from osrf_msgs.msg import JointCommands |
| 5 | +from sensor_msgs.msg import JointState |
| 6 | +from numpy import zeros, array, linspace |
| 7 | +from math import ceil |
| 8 | + |
| 9 | +atlasJointNames = [ |
| 10 | + 'atlas::back_lbz', 'atlas::back_mby', 'atlas::back_ubx', 'atlas::neck_ay', |
| 11 | + 'atlas::l_leg_uhz', 'atlas::l_leg_mhx', 'atlas::l_leg_lhy', 'atlas::l_leg_kny', 'atlas::l_leg_uay', 'atlas::l_leg_lax', |
| 12 | + 'atlas::r_leg_uhz', 'atlas::r_leg_mhx', 'atlas::r_leg_lhy', 'atlas::r_leg_kny', 'atlas::r_leg_uay', 'atlas::r_leg_lax', |
| 13 | + 'atlas::l_arm_usy', 'atlas::l_arm_shx', 'atlas::l_arm_ely', 'atlas::l_arm_elx', 'atlas::l_arm_uwy', 'atlas::l_arm_mwx', |
| 14 | + 'atlas::r_arm_usy', 'atlas::r_arm_shx', 'atlas::r_arm_ely', 'atlas::r_arm_elx', 'atlas::r_arm_uwy', 'atlas::r_arm_mwx'] |
| 15 | + |
| 16 | +currentJointState = JointState() |
| 17 | +def jointStatesCallback(msg): |
| 18 | + global currentJointState |
| 19 | + currentJointState = msg |
| 20 | + |
| 21 | +if __name__ == '__main__': |
| 22 | + # first make sure the input arguments are correct |
| 23 | + if len(sys.argv) != 3: |
| 24 | + print "usage: traj_yaml.py YAML_FILE TRAJECTORY_NAME" |
| 25 | + print " where TRAJECTORY is a dictionary defined in YAML_FILE" |
| 26 | + sys.exit(1) |
| 27 | + traj_yaml = yaml.load(file(sys.argv[1], 'r')) |
| 28 | + traj_name = sys.argv[2] |
| 29 | + if not traj_name in traj_yaml: |
| 30 | + print "unable to find trajectory %s in %s" % (traj_name, sys.argv[1]) |
| 31 | + sys.exit(1) |
| 32 | + traj_len = len(traj_yaml[traj_name]) |
| 33 | + |
| 34 | + # Setup subscriber to atlas states |
| 35 | + rospy.Subscriber("/atlas/joint_states", JointState, jointStatesCallback) |
| 36 | + |
| 37 | + # initialize JointCommands message |
| 38 | + command = JointCommands() |
| 39 | + command.name = list(atlasJointNames) |
| 40 | + n = len(command.name) |
| 41 | + command.position = zeros(n) |
| 42 | + command.velocity = zeros(n) |
| 43 | + command.effort = zeros(n) |
| 44 | + command.kp_position = zeros(n) |
| 45 | + command.ki_position = zeros(n) |
| 46 | + command.kd_position = zeros(n) |
| 47 | + command.kp_velocity = zeros(n) |
| 48 | + command.i_effort_min = zeros(n) |
| 49 | + command.i_effort_max = zeros(n) |
| 50 | + |
| 51 | + # now get gains from parameter server |
| 52 | + rospy.init_node('tutorial_atlas_control') |
| 53 | + for i in xrange(len(command.name)): |
| 54 | + name = command.name[i] |
| 55 | + command.kp_position[i] = rospy.get_param('atlas_controller/gains/' + name[7::] + '/p') |
| 56 | + command.ki_position[i] = rospy.get_param('atlas_controller/gains/' + name[7::] + '/i') |
| 57 | + command.kd_position[i] = rospy.get_param('atlas_controller/gains/' + name[7::] + '/d') |
| 58 | + command.i_effort_max[i] = rospy.get_param('atlas_controller/gains/' + name[7::] + '/i_clamp') |
| 59 | + command.i_effort_min[i] = -command.i_effort_max[i] |
| 60 | + |
| 61 | + # set up the publisher |
| 62 | + pub = rospy.Publisher('/atlas/joint_commands', JointCommands) |
| 63 | + |
| 64 | + # for each trajectory |
| 65 | + for i in xrange(0, traj_len): |
| 66 | + # get initial joint positions |
| 67 | + initialPosition = array(currentJointState.position) |
| 68 | + # get joint commands from yaml |
| 69 | + y = traj_yaml[traj_name][i] |
| 70 | + # first value is time duration |
| 71 | + dt = float(y[0]) |
| 72 | + # subsequent values are desired joint positions |
| 73 | + commandPosition = array([ float(x) for x in y[1].split() ]) |
| 74 | + # desired publish interval |
| 75 | + dtPublish = 0.02 |
| 76 | + n = ceil(dt / dtPublish) |
| 77 | + for ratio in linspace(0, 1, n): |
| 78 | + interpCommand = (1-ratio)*initialPosition + ratio * commandPosition |
| 79 | + command.position = [ float(x) for x in interpCommand ] |
| 80 | + pub.publish(command) |
| 81 | + rospy.sleep(dt / float(n)) |
0 commit comments