43 from copy
import deepcopy
46 import generate_robot_srdf
47 import sr_moveit_hand_config.generate_moveit_config
as hand_config
51 s_indent =
"\n".join((numspaces *
" ") + i
for i
in in_str.splitlines())
57 paramlist = rosparam.load_str(upload_str,
"generated",
58 default_namespace=ns_)
59 for params, namespace
in paramlist:
60 rosparam.upload_params(namespace, params)
61 if output_path
is not None:
62 file_writer = open(output_path,
"wb")
63 file_writer.write(upload_str)
68 output_str =
"controller_list:\n" 69 for manipulator
in robot_config.manipulators:
70 if manipulator.has_arm:
72 arm_yaml_path = manipulator.arm.moveit_path +
"/" +
"fake_controllers.yaml" 73 with open(arm_yaml_path,
'r') as stream: 74 arm_yamldoc = yaml.load(stream) 76 output_str += " - name: fake_" + manipulator.arm.prefix +
"controller" +
"\n" 77 output_str +=
" joints:\n" 78 for joint
in arm_yamldoc[
"controller_list"][0][
"joints"]:
79 output_str +=
" - " + manipulator.arm.prefix + joint +
"\n" 80 if manipulator.has_hand
and not manipulator.hand.is_lite:
81 output_str +=
" - " + manipulator.hand.prefix +
"WRJ2" +
"\n" 82 output_str +=
" - " + manipulator.hand.prefix +
"WRJ1" +
"\n" 84 if manipulator.has_hand:
86 for group
in robot.groups:
87 if group.name == manipulator.hand.internal_name:
90 output_str +=
" - name: fake_" + manipulator.hand.prefix +
"controller\n" 91 output_str +=
" joints:\n" 92 if len(group.joints) == 0:
95 for joint
in group.joints:
96 if joint.name[-3:] !=
"tip":
97 if manipulator.has_arm:
98 if joint.name[len(manipulator.hand.prefix):]
not in [
"WRJ1",
"WRJ2"]:
99 output_str +=
" - " + joint.name +
"\n" 101 output_str +=
" - " + joint.name +
"\n" 108 output_str =
"controller_list:\n" 109 for manipulator
in robot_config.manipulators:
110 if manipulator.has_arm:
112 arm_yaml_path = manipulator.arm.moveit_path +
"/" +
"controllers.yaml" 113 with open(arm_yaml_path,
'r') as stream: 114 arm_yamldoc = yaml.load(stream) 116 output_str += " - name: " + manipulator.arm.prefix +
"trajectory_controller\n" 117 output_str +=
" action_ns: follow_joint_trajectory\n" 118 output_str +=
" type: FollowJointTrajectory\n" 119 output_str +=
" default: true\n" 120 output_str +=
" joints:\n" 121 for joint
in arm_yamldoc[
"controller_list"][0][
"joints"]:
122 output_str +=
" - " + manipulator.arm.prefix + joint +
"\n" 123 if manipulator.has_hand
and not manipulator.hand.is_lite:
124 output_str +=
" - " + manipulator.hand.prefix +
"WRJ2" +
"\n" 125 output_str +=
" - " + manipulator.hand.prefix +
"WRJ1" +
"\n" 127 if manipulator.has_hand:
129 for group
in robot.groups:
130 if group.name == manipulator.hand.internal_name:
133 controller_name =
" - name: " + manipulator.hand.prefix +
"trajectory_controller\n" 134 output_str += controller_name
135 output_str +=
" action_ns: follow_joint_trajectory\n" 136 output_str +=
" type: FollowJointTrajectory\n" 137 output_str +=
" default: true\n" 138 output_str +=
" joints:\n" 139 if len(group.joints) == 0:
140 output_str +=
" []\n" 142 for joint
in group.joints:
143 if joint.name[-3:] !=
"tip":
144 if manipulator.has_arm:
145 if joint.name[len(manipulator.hand.prefix):]
not in [
"WRJ1",
"WRJ2"]:
146 output_str +=
" - " + joint.name +
"\n" 148 output_str +=
" - " + joint.name +
"\n" 155 output_path=
None, ns_=
None):
156 with open(hand_template_path,
'r') as stream: 157 hand_yamldoc = yaml.load(stream) 159 output_str +=
"planner_configs:\n" 160 output_str +=
yaml_reindent(yaml.dump(hand_yamldoc[
"planner_configs"],
161 default_flow_style=
False, allow_unicode=
True), 2)
164 for manipulator
in robot_config.manipulators:
165 if manipulator.has_arm:
166 arm_yaml_path = manipulator.arm.moveit_path +
"/" +
"ompl_planning.yaml" 167 with open(arm_yaml_path,
'r') as stream: 168 arm_yamldoc = yaml.load(stream) 169 if manipulator.arm.extra_groups_config_path:
170 arm_yaml_extra_groups_path = (manipulator.arm.extra_groups_config_path +
"/" +
171 "ompl_planning_extra_groups.yaml")
172 with open(arm_yaml_extra_groups_path,
'r') as stream: 173 arm_yamldoc_extra_groups = yaml.load(stream) 174 prefix = manipulator.arm.prefix 175 for group
in robot.groups:
176 group_name = group.name
177 if group_name == manipulator.arm.internal_name:
178 group_name = manipulator.arm.main_group
179 group_prefix = prefix
180 elif manipulator.arm.internal_name
in group_name:
181 group_prefix, group_name = group_name.split(
"_", 1)
182 group_prefix = prefix
184 group_name = group.name[len(prefix):]
185 group_prefix = group.name[:len(prefix)]
187 if group_name
in arm_yamldoc
and group_prefix == prefix:
188 output_str += group.name +
":\n" 189 group_config = arm_yamldoc[group_name]
192 if "projection_evaluator" in group_config:
193 proj_eval = group_config[
"projection_evaluator"]
195 proj_eval_striped = re.split(
'\W+', proj_eval)
196 joints = [word
for word
in proj_eval_striped
if word
not in [
"joints",
""]]
197 proj_eval_new =
"joints(" 199 proj_eval_new = proj_eval_new + prefix + joint +
"," 200 proj_eval_new = proj_eval_new[:-1] +
")" 201 group_config[
"projection_evaluator"] = proj_eval_new
202 group_dump = yaml.dump(group_config,
203 default_flow_style=
False,
207 elif group_name
in arm_yamldoc_extra_groups:
208 output_str += group.name +
":\n" 209 group_config = arm_yamldoc_extra_groups[group_name]
212 if "projection_evaluator" in group_config:
213 proj_eval = group_config[
"projection_evaluator"]
215 proj_eval_striped = re.split(
'\W+', proj_eval)
216 joints = [word
for word
in proj_eval_striped
if word
not in [
"joints",
""]]
217 proj_eval_new =
"joints(" 219 proj_eval_new = proj_eval_new + prefix + joint +
"," 220 proj_eval_new = proj_eval_new[:-1] +
")" 221 group_config[
"projection_evaluator"] = proj_eval_new
222 group_dump = yaml.dump(group_config,
223 default_flow_style=
False,
228 if manipulator.has_hand:
229 with open(hand_template_path,
'r') as stream: 230 hand_yamldoc = yaml.load(stream) 231 prefix = manipulator.hand.prefix 233 proj_eval_re = re.compile(
r'joints\(([TFMRLW][FHR]J[0-5]),([TFMRLW][FHR]J[0-5])\)')
234 for group
in robot.groups:
235 group_name = group.name
236 if group_name == manipulator.hand.internal_name:
238 group_prefix = prefix
240 group_name = group.name[len(prefix):]
241 group_prefix = group.name[:len(prefix)]
242 if group_name
in hand_yamldoc
and group_prefix == prefix:
243 output_str += group.name +
":\n" 244 group_config = hand_yamldoc[group_name]
247 if "projection_evaluator" in group_config:
248 proj_eval = group_config[
"projection_evaluator"]
250 proj_eval_new = proj_eval_re.sub(
r'joints(' +
256 group_config[
"projection_evaluator"] = proj_eval_new
257 group_dump = yaml.dump(group_config,
258 default_flow_style=
False,
268 def generate_kinematics(robot, robot_config, hand_template_path="kinematics_template.yaml",
269 output_path=
None, ns_=
None):
271 while not rospy.has_param(
'/robot_description'):
273 rospy.loginfo(
"waiting for robot_description")
274 urdf_str = rospy.get_param(
'/robot_description')
275 robot_urdf = URDF.from_xml_string(urdf_str)
277 for manipulator
in robot_config.manipulators:
278 if manipulator.has_arm:
279 arm_yaml_path = manipulator.arm.moveit_path +
"/" +
"kinematics.yaml" 280 with open(arm_yaml_path,
'r') as stream: 281 arm_yamldoc = yaml.load(stream) 282 if manipulator.arm.extra_groups_config_path:
283 arm_yaml_extra_groups_path = (manipulator.arm.extra_groups_config_path +
"/" +
284 "kinematics_extra_groups.yaml")
285 with open(arm_yaml_extra_groups_path,
'r') as stream: 286 arm_yamldoc_extra_groups = yaml.load(stream) 287 prefix = manipulator.arm.prefix 288 for group
in robot.groups:
289 group_name = group.name
290 if group_name == manipulator.arm.internal_name:
291 group_name = manipulator.arm.main_group
292 group_prefix = prefix
293 elif manipulator.arm.internal_name
in group_name:
294 group_prefix, group_name = group_name.split(
"_", 1)
295 group_prefix = prefix
297 group_name = group.name[len(prefix):]
298 group_prefix = group.name[:len(prefix)]
300 if group_name
in arm_yamldoc
and group_prefix == prefix:
301 kinematics_config = arm_yamldoc[group_name]
303 if "tip_name" in kinematics_config:
304 tip_name = kinematics_config[
"tip_name"]
305 kinematics_config[
"tip_name"] = prefix + tip_name
306 if "root_name" in kinematics_config:
307 root_name = kinematics_config[
"root_name"]
308 kinematics_config[
"root_name"] = prefix + root_name
310 output_str += group.name +
":\n" 312 default_flow_style=
False,
313 allow_unicode=
True), 2)
315 elif group_name
in arm_yamldoc_extra_groups:
316 kinematics_config = arm_yamldoc_extra_groups[group_name]
318 if "tip_name" in kinematics_config:
319 tip_name = kinematics_config[
"tip_name"]
320 kinematics_config[
"tip_name"] = prefix + tip_name
321 if "root_name" in kinematics_config:
322 root_name = kinematics_config[
"root_name"]
323 kinematics_config[
"root_name"] = prefix + root_name
325 output_str += group.name +
":\n" 327 default_flow_style=
False,
328 allow_unicode=
True), 2)
331 if manipulator.has_hand:
333 with open(hand_template_path,
'r') as stream: 334 hand_yamldoc = yaml.load(stream) 336 if 'kinematics_template' in hand_template_path:
337 default_solver_for_fixed_joint =
"trac_ik" 338 fixed_joint_template_path = rospkg.RosPack().get_path(
339 'sr_moveit_hand_config') +
"/config/kinematics_" + default_solver_for_fixed_joint +
"_template.yaml" 340 with open(fixed_joint_template_path,
'r') as stream: 341 hand_yamldoc_fixed_joint = yaml.load(stream) 343 hand_yamldoc_fixed_joint = deepcopy(hand_yamldoc)
345 prefix = manipulator.hand.prefix
346 finger_prefixes = [
"FF",
"MF",
"RF",
"LF",
"TH"]
349 is_fixed = {
"first_finger":
False,
"middle_finger":
False,
"ring_finger":
False,
350 "little_finger":
False,
"thumb":
False}
351 finger_with_fixed_joint = [
False,
False,
False,
False,
False]
352 for joint
in robot_urdf.joints:
353 joint_name = joint.name[len(prefix):]
354 for index, finger_prefix
in enumerate(finger_prefixes):
355 if joint_name[0:2].upper() == finger_prefix
and joint_name[-3:] !=
"tip" and joint.type ==
"fixed":
356 finger_with_fixed_joint[index] =
True 357 is_fixed[
'first_finger'] = finger_with_fixed_joint[0]
358 is_fixed[
'middle_finger'] = finger_with_fixed_joint[1]
359 is_fixed[
'ring_finger'] = finger_with_fixed_joint[2]
360 is_fixed[
'little_finger'] = finger_with_fixed_joint[3]
361 is_fixed[
'thumb'] = finger_with_fixed_joint[4]
363 for group
in robot.groups:
364 kinematics_config =
None 365 group_name = group.name
366 if group_name == manipulator.hand.internal_name:
368 group_prefix = prefix
370 group_name = group.name[len(prefix):]
371 group_prefix = group.name[:len(prefix)]
373 if group_name
in hand_yamldoc
and group_prefix == prefix:
374 if is_fixed.get(group_name):
375 kinematics_config = hand_yamldoc_fixed_joint[group_name]
377 kinematics_config = hand_yamldoc[group_name]
379 if kinematics_config
is not None:
381 if "tip_name" in kinematics_config:
382 tip_name = kinematics_config[
"tip_name"]
383 kinematics_config[
"tip_name"] = prefix + tip_name
384 if "root_name" in kinematics_config:
385 root_name = kinematics_config[
"root_name"]
386 kinematics_config[
"root_name"] = prefix + root_name
388 output_str += group.name +
":\n" 390 default_flow_style=
False,
391 allow_unicode=
True), 2)
398 output_path=
None, ns_=
None):
400 output_str +=
"joint_limits:\n" 401 for manipulator
in robot_config.manipulators:
402 if manipulator.has_arm:
404 arm_yaml_path = manipulator.arm.moveit_path +
"/" +
"joint_limits.yaml" 405 with open(arm_yaml_path,
'r') as stream: 406 arm_yamldoc = yaml.load(stream) 407 for joint
in arm_yamldoc[
"joint_limits"]:
408 joint_limits_config = arm_yamldoc[
"joint_limits"][joint]
409 output_str +=
" " + manipulator.arm.prefix + joint +
":\n" 410 joint_limits_dump = yaml.dump(
412 default_flow_style=
False,
416 if manipulator.has_hand:
417 with open(hand_template_path,
'r') as stream: 418 hand_yamldoc = yaml.load(stream) 419 group_name = manipulator.hand.internal_name 420 if group_name
is not None:
422 for joint
in robot.group_map[group_name].joints:
423 joint_name = joint.name[-4:]
424 if joint_name
in hand_yamldoc[
"joint_limits"]:
425 joint_limits_config = hand_yamldoc[
"joint_limits"][joint_name]
426 output_str +=
" " + joint.name +
":\n" 427 joint_limits_dump = yaml.dump(
429 default_flow_style=
False,
438 if __name__ ==
'__main__':
440 PARSER = argparse.ArgumentParser(usage=
'Load an SRDF file')
441 PARSER.add_argument(
'file', type=argparse.FileType(
'r'), nargs='?',
443 help=
'File to load. Use - for stdin')
444 ARGS = PARSER.parse_args()
446 if ARGS.file
is not None:
448 ROBOT = SRDF.from_xml_string(ARGS.file.read())
450 output_path=
"fake_controllers.yaml")
452 output_path=
"controllers.yaml")
454 "ompl_planning_template.yaml",
455 output_path=
"ompl_planning.yaml")
457 "kinematics_template.yaml",
458 output_path=
"kinematics.yaml")
460 "joint_limits_template.yaml",
461 output_path=
"joint_limits.yaml")
463 rospy.logerr(
"No SRDF file provided")
def generate_kinematics(robot, robot_config, hand_template_path="kinematics_template.yaml", output_path=None, ns_=None)
def upload_output_params(upload_str, output_path=None, upload=True, ns_=None)
def generate_ompl_planning(robot, robot_config, hand_template_path="ompl_planning_template.yaml", output_path=None, ns_=None)
def generate_real_controllers(robot, robot_config, output_path=None, ns_=None)
def generate_fake_controllers(robot, robot_config, output_path=None, ns_=None)
def yaml_reindent(in_str, numspaces)
def generate_joint_limits(robot, robot_config, hand_template_path="joint_limits_template.yaml", output_path=None, ns_=None)