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
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 import roslib
00060 roslib.load_manifest('srs_scenarios')
00061 import rospy
00062
00063 import rosbag
00064 from std_msgs.msg import Int32, String
00065
00066 import tf
00067 from tf.msg import *
00068 from tf.transformations import euler_from_quaternion
00069
00070 from simple_script_server import *
00071 sss = simple_script_server()
00072
00073 from kinematics_msgs.srv import *
00074 from std_msgs.msg import *
00075 from sensor_msgs.msg import *
00076 from move_base_msgs.msg import *
00077
00078 import math
00079 import rostopic
00080
00081 import os
00082 import sys, subprocess
00083
00084 import itertools
00085
00086 import threading
00087
00088 class joint_state_aggregator():
00089
00090 def __init__(self):
00091
00092
00093 self.lock = threading.Lock()
00094
00095 self.effort = []
00096 self.position = []
00097 self.velocity = []
00098 self.names = []
00099
00100 self.jointsMsg = rospy.wait_for_message("/joint_states", sensor_msgs.msg.JointState, 10)
00101
00102
00103
00104 if(len(self.jointsMsg.name) == len(self.jointsMsg.position) == len(self.jointsMsg.velocity) == len(self.jointsMsg.effort)):
00105 rospy.logdebug("Dimensions of joint_state message are ok!")
00106 else:
00107 excep = "Joint_states dimensions are not correct:" + "Names_dim " + (str)(len(self.jointsMsg.name)) + "," + "Pos_dim " + (str)(len(self.jointsMsg.position)) + "," + "Vel_dim " + (str)(len(self.jointsMsg.velocity)) + "," + "Eff_dim " + (str)(len(self.jointsMsg.effort))
00108 raise Exception(excep)
00109
00110
00111 for a,b,c, d in itertools.izip(self.jointsMsg.name, self.jointsMsg.position,self.jointsMsg.velocity, self.jointsMsg.effort):
00112 self.names.append(a)
00113 self.velocity.append(b)
00114 self.position.append(c)
00115 self.effort.append(d)
00116
00117 rospy.Subscriber('/joint_states', sensor_msgs.msg.JointState, self.process_joints)
00118
00119
00120
00121
00122 def joint_states_listener(self):
00123 rospy.Subscriber('/joint_states', JointState, self.process_joints)
00124 rospy.spin()
00125
00126 def process_joints(self, msg):
00127 self.lock.acquire()
00128 jMsg = msg
00129 self.temp_name = jMsg.name
00130 self.temp_vel = jMsg.velocity
00131 self.temp_pos = jMsg.position
00132 self.temp_ef = jMsg.effort
00133
00134 if(len(jMsg.name) == len(jMsg.position) == len(jMsg.velocity) == len(jMsg.effort)):
00135 pass
00136 else:
00137 excep = "Joint_states dimensions are not correct:" + "Names_dim " + (str)(len(jMsg.name)) + "," + "Pos_dim " + (str)(len(jMsg.position)) + "," + "Vel_dim " + (str)(len(jMsg.velocity)) + "," + "Eff_dim " + (str)(len(jMsg.effort)) + "joint_names = " + str(jMsg.name)
00138 raise Exception(excep)
00139
00140 for a,b,c, d in itertools.izip(jMsg.name, jMsg.position,jMsg.velocity, jMsg.effort):
00141
00142 if(a) not in self.names:
00143 self.names.append(a)
00144 self.position.append(b)
00145 self.velocity.append(c)
00146 self.effort.append(d)
00147
00148
00149
00150 self.position[self.names.index(a)] = self.temp_pos[self.temp_name.index(a)]
00151 self.velocity[self.names.index(a)] = self.temp_vel[self.temp_name.index(a)]
00152 self.effort[self.names.index(a)] = self.temp_ef[self.temp_name.index(a)]
00153
00154
00155 self.jointsMsg.name = self.names
00156 self.jointsMsg.position = self.position
00157 self.jointsMsg.velocity = self.velocity
00158 self.jointsMsg.effort = self.effort
00159
00160 self.lock.release()
00161
00162
00163 if __name__=="__main__":
00164
00165 rospy.init_node('joint_test')
00166 ja = joint_state_aggregator()
00167 rate = rospy.Rate(10)
00168
00169 while not rospy.is_shutdown():
00170
00171
00172
00173 rospy.loginfo("Current message:")
00174 rospy.loginfo(ja.jointsMsg)
00175 rospy.loginfo("Collected names:")
00176 rospy.loginfo(ja.names)
00177 rospy.loginfo(ja.position)
00178 rospy.loginfo(ja.velocity)
00179
00180 rate.sleep()