generate_hand_srdf.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2015, CITEC, Bielefeld University
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
30 
31 # Author: Guillaume Walck <gwalck@techfak.uni-bielefeld.de>
32 
33 """
34  generate the srdf according to the urdf
35  syntax generate_hand_srdf [output filename]
36 """
37 
38 import sys
39 import os
40 from xml.dom.minidom import parse
41 
42 import xacro
43 import rospy
44 import rospkg
45 from rosgraph.names import load_mappings
46 
47 from urdf_parser_py.urdf import URDF
48 
49 
50 class SRDFHandGenerator(object):
51  def __init__(self, urdf_str=None, load=True, save=True):
52  if urdf_str is None:
53  while not rospy.has_param('robot_description'):
54  rospy.sleep(0.5)
55  rospy.loginfo("waiting for robot_description")
56 
57  # load the urdf from the parameter server
58  urdf_str = rospy.get_param('robot_description')
59 
60  robot = URDF.from_xml_string(urdf_str)
61 
62  extracted_prefix = False
63  prefix = ""
64  ff = mf = rf = lf = th = False
65  is_lite = True
66  is_biotac = False
67  hand_name = "right_hand"
68 
69  # Check if hand has the old biotac sensors
70  for key in robot.link_map:
71  link = robot.link_map[key]
72  if link.visual:
73  if hasattr(link.visual.geometry, 'filename'):
74  filename = os.path.basename(link.visual.geometry.filename)
75  if filename == "biotac_decimated.dae":
76  is_biotac = True
77  break
78 
79  for key in robot.joint_map:
80  # any joint is supposed to have the same prefix and a joint name with 4 chars
81  if not extracted_prefix:
82  prefix = key.split("_")[0] + "_"
83  rospy.loginfo("Found prefix:" + prefix)
84  extracted_prefix = True
85  if prefix == "lh_":
86  hand_name = "left_hand"
87 
88  if not ff and key.endswith("FFJ4"):
89  ff = True
90  if not mf and key.endswith("MFJ4"):
91  mf = True
92  if not rf and key.endswith("RFJ4"):
93  rf = True
94  if not lf and key.endswith("LFJ4"):
95  lf = True
96  if not th and key.endswith("THJ4"):
97  th = True
98  if is_lite and key.endswith("WRJ2"):
99  is_lite = False
100 
101  rospy.logdebug("Found fingers (ff mf rf lf th)" + str(ff) + str(mf) + str(rf) + str(lf) + str(th))
102  rospy.logdebug("is_lite: " + str(is_lite))
103  rospy.logdebug("is_biotac: " + str(is_biotac))
104  rospy.logdebug("Hand name: " + str(hand_name))
105 
106  mappings = load_mappings(['prefix:=' + str(prefix),
107  'robot_name:=' + robot.name,
108  'ff:=' + str(int(ff)),
109  'mf:=' + str(int(mf)),
110  'rf:=' + str(int(rf)),
111  'lf:=' + str(int(lf)),
112  'th:=' + str(int(th)),
113  'is_lite:=' + str(int(is_lite)),
114  'is_biotac:=' + str(int(is_biotac)),
115  'hand_name:=' + str(hand_name)
116  ])
117 
118  # the prefix version of the srdf_xacro must be loaded
119  rospack = rospkg.RosPack()
120  package_path = rospack.get_path('sr_moveit_hand_config')
121  srdf_xacro_filename = package_path + "/config/shadowhands_prefix.srdf.xacro"
122  rospy.loginfo("File loaded " + srdf_xacro_filename)
123 
124  # open and parse the xacro.srdf file
125  srdf_xacro_file = open(srdf_xacro_filename, 'r')
126  self.srdf_xacro_xml = parse(srdf_xacro_file)
127 
128  # expand the xacro
129  xacro.process_includes(self.srdf_xacro_xml, os.path.dirname(sys.argv[0]))
130  xacro.process_doc(self.srdf_xacro_xml, mappings=mappings)
131 
132  if len(sys.argv) > 1:
133  OUTPUT_PATH = sys.argv[1]
134  # reject ROS internal parameters and detect termination
135  if (OUTPUT_PATH.startswith("_") or
136  OUTPUT_PATH.startswith("--")):
137  OUTPUT_PATH = None
138  else:
139  OUTPUT_PATH = None
140 
141  if load:
142  rospy.loginfo("Loading SRDF on parameter server")
143  robot_description_param = rospy.resolve_name('robot_description') + "_semantic"
144  rospy.set_param(robot_description_param,
145  self.srdf_xacro_xml.toprettyxml(indent=' '))
146  if save:
147  OUTPUT_PATH = package_path + "/config/generated_shadowhand.srdf"
148  FW = open(OUTPUT_PATH, "wb")
149  FW.write(self.srdf_xacro_xml.toprettyxml(indent=' '))
150  FW.close()
151 
152  OUTPUT_PATH = package_path + "/config/generated_shadowhand.urdf"
153  FW = open(OUTPUT_PATH, "wb")
154  FW.write(urdf_str)
155  FW.close()
156 
157  srdf_xacro_file.close()
158 
159  def get_hand_srdf(self):
160  return self.srdf_xacro_xml
161 
162 if __name__ == '__main__':
163  rospy.init_node('hand_srdf_generator', anonymous=True)
164  srdfGenerator = SRDFHandGenerator()
def process_includes(elt, macros=None, symbols=None)
def process_doc(doc, in_order=False, just_deps=False, just_includes=False, mappings=None, xacro_ns=True, kwargs)
def __init__(self, urdf_str=None, load=True, save=True)


sr_moveit_hand_config
Author(s): MoveIt Setup Assistant , Guillaume Walck
autogenerated on Wed Oct 14 2020 04:05:17