ordered_position_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 import thread
20 
21 
23  def __init__(self):
24  rospy.init_node('position_joint_state_merger', anonymous=True)
25  self.subs_1 = rospy.Subscriber("/srh/position/joint_states", JointState, self.callback1)
26  self.subs_2 = rospy.Subscriber("/sr_arm/position/joint_states", JointState, self.callback2)
27 
28  self.pub = rospy.Publisher("/positions/joint_states", JointState)
29 
30  self.msg_1_received = False
31  self.msg_2_received = False
32 
33  self.joint_state_msg = JointState()
34 
35  self.mutex = thread.allocate_lock()
36 
37  rospy.spin()
38 
39  def callback1(self, data):
40  self.mutex.acquire()
41  if self.msg_1_received:
42  self.mutex.release()
43  return
44 
45  self.msg_1_received = True
46 
47  self.joint_state_msg.header.stamp = rospy.Time.now()
48 
49  tmp = data.name
50  self.joint_state_msg.name = tmp
51 
52  tmp = data.position
53  self.joint_state_msg.position = tmp
54 
55  tmp = data.velocity
56  self.joint_state_msg.velocity = tmp
57 
58  tmp = data.effort
59  self.joint_state_msg.effort = tmp
60  self.mutex.release()
61 
62  def callback2(self, data):
63  self.mutex.acquire()
64 
65  self.msg_2_received = True
66 
67  self.joint_state_msg.header.stamp = rospy.Time.now()
68 
69  tmp = self.joint_state_msg.name
70  tmp += data.name
71  self.joint_state_msg.name = tmp
72 
73  tmp = self.joint_state_msg.position
74  tmp += data.position
75  self.joint_state_msg.position = tmp
76 
77  tmp = self.joint_state_msg.velocity
78  tmp += data.velocity
79  self.joint_state_msg.velocity = tmp
80 
81  tmp = self.joint_state_msg.effort
82  tmp += data.effort
83  self.joint_state_msg.effort = tmp
84 
85  if self.msg_1_received:
86  self.pub.publish(self.joint_state_msg)
87  self.msg_1_received = False
88  self.msg_2_received = False
89  self.joint_state_msg = JointState()
90 
91  self.mutex.release()
92 
93 
94 if __name__ == '__main__':
95  merger = MergeMessages()


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