joint_states_listener.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 #spins off a thread to listen for joint_states messages
00004 #and provides the same information (or subsets of) as a service
00005 
00006 import roslib
00007 roslib.load_manifest('coverage_3d_tools')
00008 import rospy
00009 from coverage_3d_tools.srv import *
00010 from sensor_msgs.msg import JointState
00011 import threading
00012 
00013 
00014 #holds the latest states obtained from joint_states messages
00015 class LatestJointStates:
00016 
00017     def __init__(self):
00018         rospy.init_node('joint_states_listener')
00019         self.lock = threading.Lock()
00020         self.name = []
00021         self.position = []
00022         self.velocity = []
00023         self.effort = []
00024         self.thread = threading.Thread(target=self.joint_states_listener)
00025         self.thread.start()
00026 
00027         s = rospy.Service('return_joint_states', ReturnJointStates, self.return_joint_states)
00028         
00029 
00030     #thread function: listen for joint_states messages
00031     def joint_states_listener(self):
00032         rospy.Subscriber('joint_states', JointState, self.joint_states_callback)
00033         rospy.spin()
00034 
00035 
00036     #callback function: when a joint_states message arrives, save the values
00037     def joint_states_callback(self, msg):
00038         self.lock.acquire()
00039         self.name = msg.name
00040         self.position = msg.position
00041         self.velocity = msg.velocity
00042         self.effort = msg.effort
00043         self.lock.release()
00044 
00045 
00046     #returns (found, position, velocity, effort) for the joint joint_name 
00047     #(found is 1 if found, 0 otherwise)
00048     def return_joint_state(self, joint_name):
00049 
00050         #no messages yet
00051         if self.name == []:
00052             rospy.logerr("No robot_state messages received!\n")
00053             return (0, 0., 0., 0.)
00054 
00055         #return info for this joint
00056         self.lock.acquire()
00057         if joint_name in self.name:
00058             index = self.name.index(joint_name)
00059             position = self.position[index]
00060             velocity = self.velocity[index]
00061             effort = self.effort[index]
00062 
00063         #unless it's not found
00064         else:
00065             rospy.logerr("Joint %s not found!", (joint_name,))
00066             self.lock.release()
00067             return (0, 0., 0., 0.)
00068         self.lock.release()
00069         return (1, position, velocity, effort)
00070 
00071 
00072     #server callback: returns arrays of position, velocity, and effort
00073     #for a list of joints specified by name
00074     def return_joint_states(self, req):
00075         joints_found = []
00076         positions = []
00077         velocities = []
00078         efforts = []
00079         for joint_name in req.name:
00080             (found, position, velocity, effort) = self.return_joint_state(joint_name)
00081             joints_found.append(found)
00082             positions.append(position)
00083             velocities.append(velocity)
00084             efforts.append(effort)
00085         return ReturnJointStatesResponse(joints_found, positions, velocities, efforts)
00086 
00087 
00088 #run the server
00089 if __name__ == "__main__":
00090 
00091     latestjointstates = LatestJointStates()
00092 
00093     print "joints_states_listener server started, waiting for queries"
00094     rospy.spin()
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


coverage_3d_tools
Author(s): Juergen Hess
autogenerated on Wed Dec 26 2012 15:25:37