Skip to content

Commit d2364ed

Browse files
committed
ddynamic reconfigure based joint state publisher, alternative to the qt gui version
1 parent 8bc3167 commit d2364ed

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

joint_state_publisher_gui/package.xml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<buildtool_depend>catkin</buildtool_depend>
1818

19+
<exec_depend>ddynamic_reconfigure_python</exec_depend>
1920
<exec_depend>joint_state_publisher</exec_depend>
2021
<exec_depend>python_qt_binding</exec_depend>
2122
<exec_depend>rospy</exec_depend>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env python
2+
3+
# Software License Agreement (BSD License)
4+
#
5+
# Copyright (c) 2021, Lucas Walter
6+
# All rights reserved.
7+
#
8+
# Redistribution and use in source and binary forms, with or without
9+
# modification, are permitted provided that the following conditions
10+
# are met:
11+
#
12+
# * Redistributions of source code must retain the above copyright
13+
# notice, this list of conditions and the following disclaimer.
14+
# * Redistributions in binary form must reproduce the above
15+
# copyright notice, this list of conditions and the following
16+
# disclaimer in the documentation and/or other materials provided
17+
# with the distribution.
18+
# * Neither the name of Willow Garage, Inc. nor the names of its
19+
# contributors may be used to endorse or promote products derived
20+
# from this software without specific prior written permission.
21+
#
22+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28+
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32+
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33+
# POSSIBILITY OF SUCH DAMAGE.
34+
35+
import math
36+
import random
37+
38+
import rospy
39+
from ddynamic_reconfigure_python.ddynamic_reconfigure import DDynamicReconfigure
40+
from math import pi
41+
from sensor_msgs.msg import JointState
42+
from threading import Lock
43+
44+
import joint_state_publisher
45+
46+
47+
class JointStatePublisherDR():
48+
def __init__(self, jsp):
49+
self.jsp = jsp
50+
self.ddynrec = DDynamicReconfigure("")
51+
52+
for name in self.jsp.joint_list:
53+
if name not in self.jsp.free_joints:
54+
continue
55+
joint = self.jsp.free_joints[name]
56+
57+
if joint['min'] == joint['max']:
58+
continue
59+
60+
self.ddynrec.add_variable(name, "", float(joint['zero']),
61+
float(joint['min']), float(joint['max']))
62+
63+
# there cannot be a joint named 'center' or 'randomize'
64+
self.ddynrec.add_variable("center", "Set all joints to zero positions", False)
65+
# Set zero positions read from parameters
66+
# self.center()
67+
68+
self.ddynrec.add_variable("randomize", "Set all joints to random positions", False)
69+
70+
self.jsp.set_source_update_cb(self.source_update_cb)
71+
72+
self.config = None
73+
self.ddynrec.start(self.dyn_rec_callback)
74+
75+
def source_update_cb(self):
76+
rospy.logwarn("TBD handle jsp updates")
77+
78+
def dyn_rec_callback(self, config, level):
79+
if config.center:
80+
config = self.center(config)
81+
config.center = False
82+
if config.randomize:
83+
config = self.randomize(config)
84+
config.randomize = False
85+
for name in self.jsp.free_joints.keys():
86+
if name not in config.keys():
87+
rospy.logerr(name + " not in config: " + str(config.keys()))
88+
continue
89+
self.jsp.free_joints[name]['position'] = config[name]
90+
91+
self.config = config
92+
return config
93+
94+
def update_config(self):
95+
config = {}
96+
for name in self.jsp.free_joints.keys():
97+
config[name] = self.jsp.free_joints[name]['position']
98+
self.ddynrec.dyn_rec_srv.update_configuration(config)
99+
100+
def center(self, config):
101+
rospy.loginfo("Centering")
102+
for name in self.jsp.free_joints.keys():
103+
if name in config.keys():
104+
config[name] = self.jsp.free_joints[name]['zero']
105+
return config
106+
107+
def randomize(self, config):
108+
rospy.loginfo("Randomizing")
109+
for name in self.jsp.free_joints.keys():
110+
if name in config.keys():
111+
config[name] = random.uniform(self.jsp.free_joints[name]['min'],
112+
self.jsp.free_joints[name]['max'])
113+
return config
114+
115+
if __name__ == '__main__':
116+
try:
117+
rospy.init_node('joint_state_publisher_dr')
118+
jsp_dr = JointStatePublisherDR(joint_state_publisher.JointStatePublisher())
119+
# TODO(lucasw) threading.Thread here?
120+
jsp_dr.jsp.loop()
121+
except rospy.ROSInterruptException:
122+
pass

0 commit comments

Comments
 (0)