startJAFS.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 __author__ = 'tom'
00004 import rospy
00005 import actionlib
00006 
00007 from control_msgs.msg import FollowJointTrajectoryAction
00008 from dynamixel_msgs.msg import JointState as JointStateDynamixel
00009 from sensor_msgs.msg import JointState
00010 from std_msgs.msg import Float64
00011 
00012 
00013 class JointSubscriber:
00014     def __init__(self, joint):
00015         rospy.Subscriber(joint + '/state', JointStateDynamixel, self.joint_state_cb)
00016         
00017         rospy.loginfo('Subscribing for %s joint state', joint)
00018 
00019         self.joint_name = joint
00020         self.state = JointStateDynamixel()
00021         self.received = False
00022 
00023     def joint_state_cb(self, msg):
00024         if not self.received:
00025             self.received = True
00026         self.state = msg
00027 
00028     def get_position(self):
00029         return self.state.current_pos
00030 
00031 
00032 class JointCommander:
00033     def __init__(self, joint):
00034         self.joint_name = joint
00035         self.pub = rospy.Publisher(joint + '/command', Float64, queue_size=10)
00036 
00037     def command(self, pos):
00038         self.pub.publish(pos)
00039 
00040 
00041 class FollowController:
00042     def __init__(self):
00043         self.ns = 'komodo_arm_controller'
00044         self.joints = rospy.get_param('dynamixel/arm_controller/arm_joints_name')
00045         rospy.loginfo('Configured for' + str(len(self.joints)) + 'joints')
00046         self.joints_pub = [JointCommander(name + '_controller') for name in self.joints]
00047         self.joints_names = []
00048         
00049         for idx in xrange((len(self.joints))):
00050             self.joints_names.append(self.joints[idx] + '_joint')
00051 
00052         self.name = self.ns + '/follow_joint_trajectory'
00053         self.server = actionlib.SimpleActionServer(self.name, FollowJointTrajectoryAction, execute_cb=self.actionCb,
00054                                                    auto_start=False)
00055 
00056         rospy.loginfo('Started FollowController (' + self.name + '). joints: ' + str(self.joints))
00057 
00058     def startup(self):
00059         self.server.start()
00060 
00061     def actionCb(self, goal):
00062         rospy.loginfo(self.name + ': Action goal received.')
00063         traj = goal.trajectory
00064         if set(self.joints_names) != set(traj.joint_names):
00065             print set(self.joints_names)
00066             print set(traj.joint_names)
00067             msg = "Trajectory joint does not match action controlled joints" + str(traj.joint_names)
00068             rospy.logerr(msg)
00069             self.server.set_aborted(text=msg)
00070             return
00071         if not traj.points:
00072             msg = "Trajectory empty"
00073             rospy.logerr(msg)
00074             self.server.set_aborted(text=msg)
00075             return
00076 
00077         try:
00078             indexes = [traj.joint_names.index(joint) for joint in self.joints_names]
00079         except ValueError as val:
00080             msg = 'Trajectory invalid'
00081             rospy.logerr(msg)
00082             self.server.set_aborted(text=msg)
00083             return
00084         if self.executeTrajectory(traj):
00085             self.server.set_succeeded()
00086             rospy.loginfo('Executed')
00087         else:
00088             rospy.logerr('Execution failed')
00089             self.server.set_aborted(text='Execution failed')
00090 
00091     def executeTrajectory(self, traj):
00092         rospy.loginfo('Executing trajectory with' + str(len((traj.points))) + 'points(s)')
00093         time = rospy.Time.now()
00094         start = traj.header.stamp
00095 
00096         try:
00097             indexes = [traj.joint_names.index(joint) for joint in self.joints_names]
00098         except ValueError as val:
00099             msg = 'Trajectory invalid'
00100             rospy.logerr(msg)
00101             self.server.set_aborted(text=msg)
00102             return False
00103 
00104         for point in traj.points:
00105             if self.server.is_preempt_requested():
00106                 rospy.loginfo('Stopping arm movement')
00107                 self.server.set_preempted()
00108                 break
00109             while rospy.Time.now() + rospy.Duration(0.01) < start:
00110                 rospy.sleep(0.01)
00111 
00112             desired = [point.positions[k] for k in indexes]
00113             endtime = start + point.time_from_start
00114 
00115             for i in xrange(len(self.joints)):
00116                 self.joints_pub[i].command(desired[i])
00117 
00118             while rospy.Time.now() + rospy.Duration(0.01) < endtime:
00119                 rospy.sleep(0.01)
00120         return True
00121 
00122 
00123 if __name__ == '__main__':
00124     rospy.init_node('follow_joint_controller')
00125     rospy.loginfo('Follow action server')
00126     action_server = FollowController()
00127     action_server.startup()
00128 
00129     rospy.loginfo('Spinning')
00130     rospy.spin()


ric_robot
Author(s): RoboTiCan
autogenerated on Fri May 20 2016 03:50:58