ordered_hand_arm_joint_states_merger.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright 2011 Shadow Robot Company Ltd.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 # more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 import rospy
18 from sensor_msgs.msg import JointState
19 from sr_utilities.srv import getJointState
20 import thread
21 
22 RATE = 100
23 
24 
25 class MergeMessages(object):
26  def __init__(self):
27  rospy.init_node('arm_and_hand_joint_state_merger', anonymous=True)
28 
29  self.msg_1_received = False
30  self.msg_2_received = False
31 
32  self.subs_1 = rospy.Subscriber("/sh/joint_states", JointState, self.callback1)
33  self.subs_2 = rospy.Subscriber("/sa/joint_states", JointState, self.callback2)
34  self.serv = rospy.Service('/getJointState', getJointState, self.getJointStateCB)
35 
36  self.pub = rospy.Publisher("/joint_states", JointState)
37 
38  self.joint_state_msg = JointState()
39  self.joint_state_msg_1 = JointState()
40  self.joint_state_msg_2 = JointState()
41 
42  self.mutex = thread.allocate_lock()
43 
44  r = rospy.Rate(RATE)
45  while not rospy.is_shutdown():
46  self.publish()
47  r.sleep()
48 
49  def callback1(self, data):
50  self.msg_1_received = True
51  self.joint_state_msg_1 = data
52 
53  def callback2(self, data):
54  self.msg_2_received = True
55  self.joint_state_msg_2 = data
56 
57  def publish(self):
58  if self.msg_1_received and self.msg_2_received:
59  self.joint_state_msg.header.stamp = rospy.Time.now()
60  self.joint_state_msg.name = self.joint_state_msg_1.name + self.joint_state_msg_2.name
61  self.joint_state_msg.position = self.joint_state_msg_1.position + self.joint_state_msg_2.position
62  self.joint_state_msg.effort = self.joint_state_msg_1.effort + self.joint_state_msg_2.effort
63  self.joint_state_msg.velocity = self.joint_state_msg_1.velocity + self.joint_state_msg_2.velocity
64 
65  self.pub.publish(self.joint_state_msg)
66  self.msg_1_received = False
67  self.msg_2_received = False
68 
69  def getJointStateCB(self, req):
70  res = self.joint_state_msg
71  return res
72 
73 
74 if __name__ == '__main__':
75  merger = MergeMessages()


sr_utilities
Author(s): Ugo Cupcic
autogenerated on Mon Feb 28 2022 23:52:19