__init__.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2010, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import math
34 import sys
35 import xml.dom.minidom
36 
37 import rospy
38 import sensor_msgs.msg
39 
40 
41 def get_param(name, value=None):
42  private = "~%s" % name
43  if rospy.has_param(private):
44  return rospy.get_param(private)
45  elif rospy.has_param(name):
46  return rospy.get_param(name)
47  else:
48  return value
49 
50 
52  def init_collada(self, robot):
53  robot = robot.getElementsByTagName('kinematics_model')[0].getElementsByTagName('technique_common')[0]
54  for child in robot.childNodes:
55  if child.nodeType is child.TEXT_NODE:
56  continue
57  if child.localName == 'joint':
58  name = child.getAttribute('name')
59  if child.getElementsByTagName('revolute'):
60  joint = child.getElementsByTagName('revolute')[0]
61  else:
62  rospy.logwarn("Unknown joint type %s", child)
63  continue
64 
65  if joint:
66  limit = joint.getElementsByTagName('limits')[0]
67  minval = float(limit.getElementsByTagName('min')[0].childNodes[0].nodeValue)
68  maxval = float(limit.getElementsByTagName('max')[0].childNodes[0].nodeValue)
69  if minval == maxval: # this is fixed joint
70  continue
71 
72  self.joint_list.append(name)
73  joint = {'min':minval*math.pi/180.0, 'max':maxval*math.pi/180.0, 'zero':0, 'position':0, 'velocity':0, 'effort':0}
74  self.free_joints[name] = joint
75 
76  def init_urdf(self, robot):
77  robot = robot.getElementsByTagName('robot')[0]
78  # Find all non-fixed joints
79  for child in robot.childNodes:
80  if child.nodeType is child.TEXT_NODE:
81  continue
82  if child.localName == 'joint':
83  jtype = child.getAttribute('type')
84  if jtype in ['fixed', 'floating', 'planar']:
85  continue
86  name = child.getAttribute('name')
87  self.joint_list.append(name)
88  if jtype == 'continuous':
89  minval = -math.pi
90  maxval = math.pi
91  else:
92  try:
93  limit = child.getElementsByTagName('limit')[0]
94  minval = float(limit.getAttribute('lower'))
95  maxval = float(limit.getAttribute('upper'))
96  except:
97  rospy.logwarn("%s is not fixed, nor continuous, but limits are not specified!" % name)
98  continue
99 
100  safety_tags = child.getElementsByTagName('safety_controller')
101  if self.use_small and len(safety_tags) == 1:
102  tag = safety_tags[0]
103  if tag.hasAttribute('soft_lower_limit'):
104  minval = max(minval, float(tag.getAttribute('soft_lower_limit')))
105  if tag.hasAttribute('soft_upper_limit'):
106  maxval = min(maxval, float(tag.getAttribute('soft_upper_limit')))
107 
108  mimic_tags = child.getElementsByTagName('mimic')
109  if self.use_mimic and len(mimic_tags) == 1:
110  tag = mimic_tags[0]
111  entry = {'parent': tag.getAttribute('joint')}
112  if tag.hasAttribute('multiplier'):
113  entry['factor'] = float(tag.getAttribute('multiplier'))
114  if tag.hasAttribute('offset'):
115  entry['offset'] = float(tag.getAttribute('offset'))
116 
117  self.dependent_joints[name] = entry
118  continue
119 
120  if name in self.dependent_joints:
121  continue
122 
123  if self.zeros and name in self.zeros:
124  zeroval = self.zeros[name]
125  elif minval > 0 or maxval < 0:
126  zeroval = (maxval + minval)/2
127  else:
128  zeroval = 0
129 
130  joint = {'min': minval, 'max': maxval, 'zero': zeroval}
131  if self.pub_def_positions:
132  joint['position'] = zeroval
133  if self.pub_def_vels:
134  joint['velocity'] = 0.0
135  if self.pub_def_efforts:
136  joint['effort'] = 0.0
137 
138  if jtype == 'continuous':
139  joint['continuous'] = True
140  self.free_joints[name] = joint
141 
142  def __init__(self):
143  description = get_param('robot_description')
144  if description is None:
145  raise RuntimeError('The robot_description parameter is required and not set.')
146 
147  self.free_joints = {}
148  self.joint_list = [] # for maintaining the original order of the joints
149  self.dependent_joints = get_param("dependent_joints", {})
150  self.use_mimic = get_param('use_mimic_tags', True)
151  self.use_small = get_param('use_smallest_joint_limits', True)
152 
153  self.zeros = get_param("zeros")
154 
155  self.pub_def_positions = get_param("publish_default_positions", True)
156  self.pub_def_vels = get_param("publish_default_velocities", False)
157  self.pub_def_efforts = get_param("publish_default_efforts", False)
158 
159  robot = xml.dom.minidom.parseString(description.encode('utf-8'))
160  if robot.getElementsByTagName('COLLADA'):
161  self.init_collada(robot)
162  else:
163  self.init_urdf(robot)
164 
165  # The source_update_cb will be called at the end of self.source_cb.
166  # The main purpose it to allow external observes (such as the
167  # joint_state_publisher_gui) to be notified when things are updated.
168  self.source_update_cb = None
169 
170  source_list = get_param("source_list", [])
171  self.sources = []
172  for source in source_list:
173  self.sources.append(rospy.Subscriber(source, sensor_msgs.msg.JointState, self.source_cb))
174 
175  self.pub = rospy.Publisher('joint_states', sensor_msgs.msg.JointState, queue_size=5)
176 
177  def source_cb(self, msg):
178  for i in range(len(msg.name)):
179  name = msg.name[i]
180  if name not in self.free_joints:
181  continue
182 
183  if msg.position:
184  position = msg.position[i]
185  else:
186  position = None
187  if msg.velocity:
188  velocity = msg.velocity[i]
189  else:
190  velocity = None
191  if msg.effort:
192  effort = msg.effort[i]
193  else:
194  effort = None
195 
196  joint = self.free_joints[name]
197  if position is not None:
198  joint['position'] = position
199  if velocity is not None:
200  joint['velocity'] = velocity
201  if effort is not None:
202  joint['effort'] = effort
203 
204  if self.source_update_cb is not None:
205  self.source_update_cb()
206 
207  def set_source_update_cb(self, user_cb):
208  self.source_update_cb = user_cb
209 
210  def loop(self):
211  hz = get_param("rate", 10) # 10hz
212  r = rospy.Rate(hz)
213 
214  delta = get_param("delta", 0.0)
215 
216  # Publish Joint States
217  while not rospy.is_shutdown():
218  msg = sensor_msgs.msg.JointState()
219  msg.header.stamp = rospy.Time.now()
220 
221  if delta > 0:
222  self.update(delta)
223 
224  # Initialize msg.position, msg.velocity, and msg.effort.
225  has_position = len(self.dependent_joints.items()) > 0
226  has_velocity = False
227  has_effort = False
228  for name, joint in self.free_joints.items():
229  if not has_position and 'position' in joint:
230  has_position = True
231  if not has_velocity and 'velocity' in joint:
232  has_velocity = True
233  if not has_effort and 'effort' in joint:
234  has_effort = True
235  num_joints = (len(self.free_joints.items()) +
236  len(self.dependent_joints.items()))
237  if has_position:
238  msg.position = num_joints * [0.0]
239  if has_velocity:
240  msg.velocity = num_joints * [0.0]
241  if has_effort:
242  msg.effort = num_joints * [0.0]
243 
244  for i, name in enumerate(self.joint_list):
245  msg.name.append(name)
246  joint = None
247 
248  # Add Free Joint
249  if name in self.free_joints:
250  joint = self.free_joints[name]
251  factor = 1
252  offset = 0
253  # Add Dependent Joint
254  elif name in self.dependent_joints:
255  param = self.dependent_joints[name]
256  parent = param['parent']
257  factor = param.get('factor', 1)
258  offset = param.get('offset', 0)
259  # Handle recursive mimic chain
260  recursive_mimic_chain_joints = [name]
261  while parent in self.dependent_joints:
262  if parent in recursive_mimic_chain_joints:
263  error_message = "Found an infinite recursive mimic chain"
264  rospy.logerr("%s: [%s, %s]", error_message, ', '.join(recursive_mimic_chain_joints), parent)
265  sys.exit(1)
266  recursive_mimic_chain_joints.append(parent)
267  param = self.dependent_joints[parent]
268  parent = param['parent']
269  offset += factor * param.get('offset', 0)
270  factor *= param.get('factor', 1)
271  joint = self.free_joints[parent]
272 
273  if has_position and 'position' in joint:
274  msg.position[i] = joint['position'] * factor + offset
275  if has_velocity and 'velocity' in joint:
276  msg.velocity[i] = joint['velocity'] * factor
277  if has_effort and 'effort' in joint:
278  msg.effort[i] = joint['effort']
279 
280  if msg.name or msg.position or msg.velocity or msg.effort:
281  # Only publish non-empty messages
282  self.pub.publish(msg)
283  try:
284  r.sleep()
285  except rospy.exceptions.ROSTimeMovedBackwardsException:
286  pass
287 
288  def update(self, delta):
289  for name, joint in self.free_joints.iteritems():
290  forward = joint.get('forward', True)
291  if forward:
292  joint['position'] += delta
293  if joint['position'] > joint['max']:
294  if joint.get('continuous', False):
295  joint['position'] = joint['min']
296  else:
297  joint['position'] = joint['max']
298  joint['forward'] = not forward
299  else:
300  joint['position'] -= delta
301  if joint['position'] < joint['min']:
302  joint['position'] = joint['min']
303  joint['forward'] = not forward
def get_param(name, value=None)
Definition: __init__.py:41


joint_state_publisher
Author(s): David V. Lu!! , Jackie Kay
autogenerated on Sun Dec 13 2020 03:38:38