Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 from threading import Thread
00042
00043 import rospy
00044
00045 from sensor_msgs.msg import JointState
00046
00047 class JointStatePublisher():
00048 def __init__(self, controller_namespace, controllers):
00049 self.update_rate = 1000
00050 self.state_update_rate = 50
00051 self.trajectory = []
00052
00053 self.controller_namespace = controller_namespace
00054 self.joint_names = [c.joint_name for c in controllers]
00055 self.pre_position = []
00056 self.abs_position = []
00057
00058 self.joint_to_controller = {}
00059 for c in controllers:
00060 self.joint_to_controller[c.joint_name] = c
00061
00062 self.port_to_joints = {}
00063 for c in controllers:
00064 if c.port_namespace not in self.port_to_joints: self.port_to_joints[c.port_namespace] = []
00065 self.port_to_joints[c.port_namespace].append(c.joint_name)
00066
00067 self.port_to_io = {}
00068 for c in controllers:
00069 if c.port_namespace in self.port_to_io: continue
00070 self.port_to_io[c.port_namespace] = c.dxl_io
00071
00072 def initialize(self):
00073 self.msg = JointState()
00074 return True
00075
00076
00077 def start(self):
00078 self.running = True
00079 self.state_pub = rospy.Publisher('joint_states', JointState , queue_size=100)
00080
00081 Thread(target=self.update_state).start()
00082
00083
00084 def stop(self):
00085 self.running = False
00086
00087 def update_state(self):
00088 rate = rospy.Rate(self.state_update_rate)
00089 while self.running and not rospy.is_shutdown():
00090 self.msg.header.stamp = rospy.Time.now()
00091 self.msg.name = []
00092 self.msg.position = []
00093 self.msg.velocity = []
00094 self.msg.effort = []
00095
00096 i = 0
00097 for port, joints in self.port_to_joints.items():
00098 vals = []
00099 rospy.logdebug("joints : "+" ".join(joints))
00100 for joint in joints:
00101 j = self.joint_names.index(joint)
00102
00103 motor_id = self.joint_to_controller[joint].motor_id
00104 co = self.joint_to_controller[joint]
00105 io = self.port_to_io[port]
00106 rospy.logdebug("port={} id={}, {}".format(port, motor_id, joint))
00107
00108 self.msg.name.append(joint)
00109 po = ve = ef = 0
00110 try:
00111 ret = io.get_feedback(motor_id)
00112 if len(self.abs_position) == 0:
00113 po = self.raw_to_rad_pos(ret['position'],co)
00114 else:
00115 if (ret['position'] - self.pre_position[i]) < -3072:
00116 self.abs_position[i] = self.abs_position[i] + (ret['position'] - self.pre_position[i] + 4096)
00117 elif (ret['position'] - self.pre_position[i]) > 3072:
00118 self.abs_position[i] = self.abs_position[i] + (ret['position'] - self.pre_position[i] - 4096)
00119 else:
00120 self.abs_position[i] = self.abs_position[i] + (ret['position'] - self.pre_position[i])
00121 self.pre_position[i] = ret['position']
00122 po = self.raw_to_rad_pos(self.abs_position[i],co)
00123 ve = self.raw_to_rad_spd(ret['speed'],co)
00124 ef = self.raw_to_rad_spd(ret['load'],co)
00125
00126
00127
00128 except Exception as e:
00129 rospy.logerr(e)
00130 self.msg.position.append(po)
00131 self.msg.velocity.append(ve)
00132 self.msg.effort.append(ef)
00133 i += 1
00134
00135
00136 if len(self.pre_position) != len(self.msg.position):
00137 self.pre_position = [0]*len(self.msg.position)
00138 self.abs_position = [0]*len(self.msg.position)
00139 i = 0
00140 for port, joints in self.port_to_joints.items():
00141 for joint in joints:
00142 j = self.joint_names.index(joint)
00143 motor_id = self.joint_to_controller[joint].motor_id
00144 io = self.port_to_io[port]
00145 ret = io.get_feedback(motor_id)
00146 self.pre_position[i] = ret['position']
00147 self.abs_position[i] = ret['position']
00148 i+= 1
00149
00150 self.state_pub.publish(self.msg)
00151 rate.sleep()
00152
00153 def raw_to_rad_pos(self, raw, c):
00154 return (c.initial_position_raw - raw if c.flipped else raw - c.initial_position_raw) * c.RADIANS_PER_ENCODER_TICK
00155 def raw_to_rad_spd(self, raw, c):
00156 return (- raw if c.flipped else raw ) * c.RADIANS_PER_ENCODER_TICK