21 from xml.etree
import ElementTree
as ET
22 from controller_manager_msgs.srv
import ListControllers
24 from sr_robot_msgs.srv
import ForceController, SetEffortControllerGains, SetMixedPositionVelocityPidGains, SetPidGains
31 Parses xml file and reads controller settings 32 Creates lists for headers, fingers, motors 35 def __init__(self, xml_path, controller_type, joint_prefix):
39 xml_file = open(xml_path)
40 xml_tree = ET.parse(xml_file)
45 for ctrl
in xml_tree.findall(
"controller"):
46 ctrl_name = ctrl.attrib[
'name']
47 if ctrl_name == controller_type:
53 "Couldn't find the settings for the controller " + controller_type)
56 xml_headers = ctrl_tree.find(
"headers")
57 for header
in xml_headers.findall(
"item"):
58 self.headers.append(header.attrib)
63 self.hand_item.extend((self.
nb_columns - 1) * [
""])
68 all_fingers = xml_tree.find(
"fingers")
69 for finger
in all_fingers.findall(
"finger"):
70 finger_row = [finger.attrib[
'name']]
71 finger_row.extend((self.
nb_columns - 1) * [
""])
72 self.fingers.append(finger_row)
74 motors_for_finger = []
75 for motor
in finger.findall(
"motor"):
76 motor_row = [
"", joint_prefix + motor.attrib[
'name']]
78 motors_for_finger.append(motor_row)
80 self.motors.append(motors_for_finger)
86 Handles loading, saving and setting of controller settings 88 CONTROLLER_MANAGER_DETECTION_TIMEOUT = 3.0
93 "Mixed Position/Velocity",
"Effort",
"Muscle Position"]
117 Get the prefix (hand and joint) 118 Check if it matches selected prefix 122 hand_joint_prefixes = []
125 prefix = self.selected_prefix.rstrip(
"/")
127 if rospy.has_param(
"/hand/mapping"):
128 hand_mapping = rospy.get_param(
"/hand/mapping")
129 for _, value
in hand_mapping.items():
132 if value.startswith(prefix):
133 hand_ids.append(value)
134 if len(hand_ids) == 0:
137 rospy.loginfo(
"Using namespace, no prefix")
141 rospy.logwarn(
"Prefix not matching namespace")
144 if len(hand_ids) > 1:
147 "More than one hand found with prefix :" + prefix +
" !\n Not loading controllers")
151 if rospy.has_param(
"/hand/joint_prefix"):
152 hand_joint_prefix_mapping = rospy.get_param(
"/hand/joint_prefix")
153 for _, value
in hand_joint_prefix_mapping.items():
156 hand_joint_prefixes.append(value)
158 if len(hand_joint_prefixes) > 1:
161 "More than one hand found with prefix :" + prefix +
" !\n Not loading controllers")
164 if len(hand_joint_prefixes) == 0:
165 rospy.logwarn(
"No hand found with prefix :" + prefix)
170 rospy.loginfo(
"using joint_prefix " + self.
joint_prefix)
175 Retrieve currently running controllers 176 return ["Motor Force", "Position"] 182 ctrl_srv_name = self.
prefix +
'controller_manager/list_controllers' 184 rospy.wait_for_service(
187 except rospy.ROSException:
191 ctrl_srv_name =
'controller_manager/list_controllers' 193 rospy.wait_for_service(
196 rospy.loginfo(
"Detected single loop")
197 except rospy.ROSException, e:
199 "Controller manager not running: %s" % str(e))
200 rospy.loginfo(
"Running controller tuner in edit-only mode")
206 controllers = rospy.ServiceProxy(ctrl_srv_name, ListControllers)
210 except rospy.ServiceException, e:
211 rospy.logerr(
"Service did not process request: %s" % str(e))
213 running_ctrls.append(
"Motor Force")
215 for controller
in resp.controller:
216 if controller.state ==
"running":
219 '[tfmrlw][fhr]j[0-5]_', controller.name)
225 if len(splitted) >= 2:
226 ctrl_type_tmp = splitted[1]
228 ctrl_type_tmp_splitted = ctrl_type_tmp.split(
"_")
230 if ctrl_type_tmp_splitted[0].lower()
in defined_ctrl_type.lower():
231 running_ctrls.append(defined_ctrl_type)
235 rospy.loginfo(
"No controllers currently running")
236 rospy.loginfo(
"Running controller tuner in edit-only mode")
242 Sets all the controllers to defined type for edit-only mode 247 running_ctrls.append(defined_ctrl_type)
252 Effectively change control mode on the realtime loop 255 'sr_hand_robot/' + self.
prefix +
'default_control_mode',
'FORCE')
259 Parses a file containing the controller settings 260 and their min and max values, and returns them. 269 Load the parameters from the yaml file. 273 if controller_type ==
"Motor Force":
277 param_name = self.
prefix + \
279 self.joint_prefix.rstrip(
"/") +
"_").lower() +
"/pid" 280 elif controller_type ==
"Position":
282 joint_name.lower() +
"_position_controller/pid" 283 elif controller_type ==
"Muscle Position":
285 joint_name.lower() +
"_muscle_position_controller/pid" 286 elif controller_type ==
"Velocity":
288 joint_name.lower() +
"_velocity_controller/pid" 289 elif controller_type ==
"Mixed Position/Velocity":
291 "_mixed_position_velocity_controller/position_pid",
293 "_mixed_position_velocity_controller/velocity_pid"]
294 elif controller_type ==
"Effort":
296 joint_name.lower() +
"_effort_controller" 298 return self.pid_loader.get_settings(param_name)
302 Sets the controller settings calling the proper service with the correct syntax for controller type. 308 if controller_type ==
"Motor Force":
313 service_name =
"sr_hand_robot/" + self.
prefix + \
314 "change_force_PID_" + joint_name[-4:].upper()
315 pid_service = rospy.ServiceProxy(service_name, ForceController)
317 elif controller_type ==
"Position":
320 joint_name.lower() +
"_position_controller/set_gains" 321 pid_service = rospy.ServiceProxy(service_name, SetPidGains)
323 elif controller_type ==
"Muscle Position":
326 joint_name.lower() +
"_muscle_position_controller/set_gains" 327 pid_service = rospy.ServiceProxy(service_name, SetPidGains)
329 elif controller_type ==
"Velocity":
332 joint_name.lower() +
"_velocity_controller/set_gains" 333 pid_service = rospy.ServiceProxy(service_name, SetPidGains)
335 elif controller_type ==
"Mixed Position/Velocity":
338 joint_name.lower() + \
339 "_mixed_position_velocity_controller/set_gains" 340 pid_service = rospy.ServiceProxy(
341 service_name, SetMixedPositionVelocityPidGains)
343 elif controller_type ==
"Effort":
346 joint_name.lower() +
"_effort_controller/set_gains" 347 pid_service = rospy.ServiceProxy(
348 service_name, SetEffortControllerGains)
352 "", controller_type,
" is not a recognized controller type.")
354 contrlr_settings_converted = {}
355 for param
in controller_settings.items():
356 contrlr_settings_converted[param[0]] = float(param[1])
358 if controller_type ==
"Motor Force":
360 for setting
in [
"torque_limit",
"torque_limiter_gain"]:
361 if setting
not in contrlr_settings_converted:
362 contrlr_settings_converted[setting] = 0
363 pid_service(int(contrlr_settings_converted[
"max_pwm"]),
364 int(contrlr_settings_converted[
"sgleftref"]),
365 int(contrlr_settings_converted[
"sgrightref"]),
366 int(contrlr_settings_converted[
"f"]),
367 int(contrlr_settings_converted[
"p"]), int(
368 contrlr_settings_converted[
"i"]),
369 int(contrlr_settings_converted[
"d"]), int(
370 contrlr_settings_converted[
"imax"]),
371 int(contrlr_settings_converted[
"deadband"]),
372 int(contrlr_settings_converted[
"sign"]),
373 int(contrlr_settings_converted[
"torque_limit"]),
374 int(contrlr_settings_converted[
"torque_limiter_gain"]))
375 except rospy.ServiceException:
378 elif controller_type ==
"Position":
381 float(contrlr_settings_converted[
"p"]), float(
382 contrlr_settings_converted[
"i"]),
383 float(contrlr_settings_converted[
"d"]), float(
384 contrlr_settings_converted[
"i_clamp"]),
385 float(contrlr_settings_converted[
"max_force"]), float(
386 contrlr_settings_converted[
387 "position_deadband"]),
388 int(contrlr_settings_converted[
"friction_deadband"]))
389 except rospy.ServiceException:
392 elif controller_type ==
"Muscle Position":
395 float(contrlr_settings_converted[
"p"]), float(
396 contrlr_settings_converted[
"i"]),
397 float(contrlr_settings_converted[
"d"]), float(
398 contrlr_settings_converted[
"i_clamp"]),
399 float(contrlr_settings_converted[
"max_force"]), float(
400 contrlr_settings_converted[
401 "position_deadband"]),
402 int(contrlr_settings_converted[
"friction_deadband"]))
403 except rospy.ServiceException:
406 elif controller_type ==
"Velocity":
409 float(contrlr_settings_converted[
"p"]), float(
410 contrlr_settings_converted[
"i"]),
411 float(contrlr_settings_converted[
"d"]), float(
412 contrlr_settings_converted[
"i_clamp"]),
413 float(contrlr_settings_converted[
"max_force"]), float(
414 contrlr_settings_converted[
415 "velocity_deadband"]),
416 int(contrlr_settings_converted[
"friction_deadband"]))
417 except rospy.ServiceException:
420 elif controller_type ==
"Mixed Position/Velocity":
423 float(contrlr_settings_converted[
"pos/p"]), float(
424 contrlr_settings_converted[
"pos/i"]),
425 float(contrlr_settings_converted[
"pos/d"]), float(
426 contrlr_settings_converted[
"pos/i_clamp"]),
427 float(contrlr_settings_converted[
"pos/min_velocity"]), float(
428 contrlr_settings_converted[
429 "pos/max_velocity"]),
430 float(contrlr_settings_converted[
431 "pos/position_deadband"]),
432 float(contrlr_settings_converted[
"vel/p"]), float(
433 contrlr_settings_converted[
"vel/i"]),
434 float(contrlr_settings_converted[
"vel/d"]), float(
435 contrlr_settings_converted[
"vel/i_clamp"]),
436 float(contrlr_settings_converted[
"vel/max_force"]),
437 int(contrlr_settings_converted[
"vel/friction_deadband"]))
438 except rospy.ServiceException:
441 elif controller_type ==
"Effort":
443 pid_service(int(contrlr_settings_converted[
"max_force"]), int(
444 contrlr_settings_converted[
"friction_deadband"]))
445 except rospy.ServiceException:
449 "", controller_type,
" is not a recognized controller type.")
453 def save_controller(self, joint_name, controller_type, controller_settings, filename):
455 Saves the controller settings calling the proper service with the correct syntax for controller type 459 if controller_type ==
"Motor Force":
460 param_name = [
"" + joint_name[-4:].lower(),
"pid"]
461 elif controller_type ==
"Position":
463 joint_name.lower() +
"_position_controller",
"pid"]
464 elif controller_type ==
"Muscle Position":
466 joint_name.lower() +
"_muscle_position_controller",
"pid"]
467 elif controller_type ==
"Velocity":
469 joint_name.lower() +
"_velocity_controller",
"pid"]
470 elif controller_type ==
"Mixed Position/Velocity":
472 joint_name.lower() +
"_mixed_position_velocity_controller",
"pid"]
473 elif controller_type ==
"Effort":
475 joint_name.lower() +
"_effort_controller"]
477 pid_saver.save_settings(param_name, controller_settings)
float CONTROLLER_MANAGER_DETECTION_TIMEOUT
def get_controller_settings(self, controller_type)
def __init__(self, xml_path)
def refresh_control_mode(self)
def save_controller(self, joint_name, controller_type, controller_settings, filename)
def __init__(self, xml_path, controller_type, joint_prefix)
def set_controller(self, joint_name, controller_type, controller_settings)
def load_parameters(self, controller_type, joint_name)
def set_edit_only(self, running_ctrls)