38 import roslib; roslib.load_manifest(
'rcprg_ros_utils')
44 from sensor_msgs.msg
import JointState
46 import xml.dom.minidom
as minidom
47 from rcprg_ros_utils
import marker_publisher
49 joint_states_lock = threading.Lock()
54 with joint_states_lock:
57 if __name__ ==
"__main__":
58 rospy.init_node(
'publish_joints_visualization', anonymous=
False)
63 robot_description = rospy.get_param(
"/robot_description")
65 print "Some ROS parameters are not provided:" 71 shown_joints = rospy.get_param(
"~shown_joints")
73 print "ROS param 'shown_joints' is not set. All joints are visualized." 75 dom = minidom.parseString(robot_description)
76 robot = dom.getElementsByTagName(
"robot")
78 print "robot_description does not contain 'robot' node" 83 for j
in robot[0].childNodes:
84 if j.localName !=
"joint":
86 name = j.getAttribute(
"name")
87 joint_type = j.getAttribute(
"type")
88 if joint_type ==
"fixed":
90 origin = j.getElementsByTagName(
"origin")
92 print "joint '" + name +
"' does contain wrong number of 'origin' nodes: " + str(len(origin))
94 axis = j.getElementsByTagName(
"axis")
96 print "joint '" + name +
"' contain wrong number of 'axis' nodes: " + str(len(axis))
98 limit = j.getElementsByTagName(
"limit")
100 print "joint '" + name +
"' contain wrong number of 'limit' nodes: " + str(len(limit))
102 parent = j.getElementsByTagName(
"parent")
104 print "joint '" + name +
"' contain wrong number of 'parent' nodes: " + str(len(parent))
107 xyz = origin[0].getAttribute(
"xyz")
108 rpy = origin[0].getAttribute(
"rpy")
111 orig = PyKDL.Frame(PyKDL.Rotation.RPY(float(rpy[0]), float(rpy[1]), float(rpy[2])), PyKDL.Vector(float(xyz[0]), float(xyz[1]), float(xyz[2])))
113 axis_xyz = axis[0].getAttribute(
"xyz")
114 axis_xyz = axis_xyz.split()
115 ax = PyKDL.Vector(float(axis_xyz[0]), float(axis_xyz[1]), float(axis_xyz[2]))
117 limit_lo = float(limit[0].getAttribute(
"lower"))
118 limit_up = float(limit[0].getAttribute(
"upper"))
119 print name +
": " + str(limit_lo) +
" " + str(limit_up) +
" axis: " + str(ax.x()) +
" " + str(ax.y()) +
" " + str(ax.z()) +
" " 121 parent_link = parent[0].getAttribute(
"link")
124 if abs(axis_z.z()) > 0.7:
125 axis_x = PyKDL.Vector(1,0,0)
127 axis_x = PyKDL.Vector(0,0,1)
128 axis_y = axis_z * axis_x
129 axis_x = axis_y * axis_z
133 rot = PyKDL.Frame(PyKDL.Rotation(axis_x,axis_y,axis_z))
135 joints.append( (name, parent_link, orig, ax, limit_lo, limit_up, rot) )
137 listener_joint_states = rospy.Subscriber(
'/joint_states', JointState, jointStatesCallback)
141 pub = marker_publisher.MarkerPublisher(
'/joints_vis')
147 while angle < j[5]-0.15:
150 pl.append(PyKDL.Vector())
151 pl.append(rot*PyKDL.Vector(math.cos(angle_prev), math.sin(angle_prev),0)*limit_scale)
152 pl.append(rot*PyKDL.Vector(math.cos(angle), math.sin(angle),0)*limit_scale)
153 pl.append(PyKDL.Vector())
154 pl.append(rot*PyKDL.Vector(math.cos(angle), math.sin(angle),0)*limit_scale)
155 pl.append(rot*PyKDL.Vector(math.cos(j[5]), math.sin(j[5]),0)*limit_scale)
156 points_list[j[0]] = pl
158 while not rospy.is_shutdown():
160 with joint_states_lock:
161 if joint_state !=
None:
163 for joint_name
in joint_state.name:
164 js[joint_name] = joint_state.position[joint_idx]
168 if shown_joints !=
None and not j[0]
in shown_joints:
171 m_id = pub.publishVectorMarker(j[2]*PyKDL.Vector(), j[2]*(j[3]*0.2), m_id, 1, 0, 0, a=1.0, namespace=
"axes", frame=j[1], scale=0.01)
172 m_id = pub.publishTriangleListMarker(points_list[j[0]], m_id, r=0, g=1, b=0, a=1.0, namespace=
"limits", frame_id=j[1], T=j[2])
175 vec = rot*PyKDL.Vector(math.cos(js[j[0]]), math.sin(js[j[0]]),0)
176 m_id = pub.publishVectorMarker(j[2]*PyKDL.Vector(), j[2]*(vec*0.15), m_id, 0, 0, 1, a=1.0, namespace=
"limits", frame=j[1], scale=0.01)
177 m_id = pub.publishTextMarker(j[0], m_id, 0,0,1,1, namespace=
'names', frame_id=j[1], T=j[2], height=0.05)
def jointStatesCallback(data)