22 from controller_manager_msgs.srv
import ListControllers
23 from controller_manager_msgs.srv
import SwitchController, LoadController
24 from sr_utilities.hand_finder
import HandFinder
28 JOINT_NAMES = [
"rh_FFJ1",
"rh_FFJ2",
"rh_FFJ3",
"rh_FFJ4",
"rh_LFJ1",
"rh_LFJ2",
"rh_LFJ3",
"rh_LFJ4",
"rh_LFJ5",
29 "rh_MFJ1",
"rh_MFJ2",
"rh_MFJ3",
"rh_MFJ4",
"rh_RFJ1",
"rh_RFJ2",
"rh_RFJ3",
"rh_RFJ4",
"rh_THJ1",
30 "rh_THJ2",
"rh_THJ3",
"rh_THJ4",
"rh_THJ5",
"rh_WRJ1",
"rh_WRJ2",
31 "lh_FFJ1",
"lh_FFJ2",
"lh_FFJ3",
"lh_FFJ4",
"lh_LFJ1",
"lh_LFJ2",
"lh_LFJ3",
"lh_LFJ4",
"lh_LFJ5",
32 "lh_MFJ1",
"lh_MFJ2",
"lh_MFJ3",
"lh_MFJ4",
"lh_RFJ1",
"lh_RFJ2",
"lh_RFJ3",
"lh_RFJ4",
"lh_THJ1",
33 "lh_THJ2",
"lh_THJ3",
"lh_THJ4",
"lh_THJ5",
"lh_WRJ1",
"lh_WRJ2"]
35 def __init__(self, config_file_path, service_timeout):
40 self.
_joints = hand_finder.get_hand_joints()
43 for joint
in self.
_joints[side]:
54 except EnvironmentError
as error:
55 rospy.logerr(
"Failed to load controller spawner configuration from '{}'".format(self.
_config_file_path))
66 if "controller_configs" in self.
_config.keys():
67 for side
in self.
_config[
"controller_configs"]:
70 side_config = self.
_config[
"controller_configs"][side]
71 rospy.logwarn(side_config)
72 for controller
in side_config:
74 resolved_config_path = self.
resolve_path(side_config[controller],
76 with open(resolved_config_path)
as controller_config_yaml:
77 controller_config = yaml.load(controller_config_yaml)
79 for key
in controller_config:
80 rospy.set_param(key, controller_config[key])
81 except EnvironmentError
as error:
82 rospy.logerr(
"Failed to load {} controller configuration from '{}'".format(controller,
91 matches = re.findall(
r'%rospack_find_(.+)%', path)
93 package_name = matches[0]
94 ros_pack = rospkg.RosPack()
96 package_path = ros_pack.get_path(package_name)
97 path = re.sub(
r'%rospack_find_(.+)%', package_path, path)
98 except rospkg.common.ResourceNotFound
as e:
99 rospy.logerr(
"Package '{}' in controller spawner config doesn't exist.".format(package_name))
100 if path.startswith(
'/'):
103 if local_path
is None:
106 return "{}/{}".format(local_path, path)
109 if joint_name
is not None:
110 string = re.sub(
r'%joint_name%', joint_name, string)
114 if "controller_groups" not in self.
_config.keys():
115 rospy.logwarn(
"No controller groups specified in controller spawner config ({})".format(
120 for controller_group_name
in self.
_config[
"controller_groups"]:
121 controller_group = []
122 for side
in self.
_config[
"controller_groups"][controller_group_name]:
123 side_controllers = self.
_config[
"controller_groups"][controller_group_name][side]
126 necessary_if_joint_present = []
127 if "necessary_if_joint_present" in side_controllers:
128 necessary_if_joint_present = side_controllers[
"necessary_if_joint_present"]
129 for joint_name
in self.
_joints[side]:
130 if "common" in side_controllers:
131 for controller_raw
in side_controllers[
"common"]:
132 controller = self.
resolve_string(controller_raw, joint_name=joint_name.lower())
133 if (joint_name
not in self.
_excluded_joints)
or (controller
in necessary_if_joint_present):
134 if controller
not in controller_group:
135 controller_group.append(controller)
138 if joint_name
in side_controllers:
139 for controller_raw
in side_controllers[joint_name]:
140 controller = self.
resolve_string(controller_raw, joint_name=joint_name.lower())
141 if (joint_name
not in self.
_excluded_joints)
or (controller
in necessary_if_joint_present):
142 if controller
not in controller_group:
143 controller_group.append(controller)
146 elif "default" in side_controllers:
147 for controller_raw
in side_controllers[
"default"]:
148 controller = self.
resolve_string(controller_raw, joint_name=joint_name.lower())
149 if (joint_name
not in self.
_excluded_joints)
or (controller
in necessary_if_joint_present):
150 if controller
not in controller_group:
151 controller_group.append(controller)
162 rospy.logerr(
"There is no controller group named '{}'".format(controller_group_name))
166 rospy.wait_for_service(
'controller_manager/list_controllers', self.
_service_timeout)
167 list_controllers = rospy.ServiceProxy(
168 'controller_manager/list_controllers', ListControllers)
169 current_controllers = list_controllers().controller
170 except rospy.ServiceException:
172 rospy.logerr(
"Failed to get currently running controllers.")
174 loaded_controllers = [controller.name
for controller
in current_controllers]
175 running_controllers = [controller.name
for controller
in current_controllers
if controller.state ==
"running"]
176 controllers_to_stop = [controller
for controller
in running_controllers
if 177 controller
in self.
_all_controllers and controller
not in desired_controllers]
178 controllers_to_load = [controller
for controller
in desired_controllers
if controller
not in loaded_controllers]
179 controllers_to_start = [controller
for controller
in desired_controllers
if 180 controller
not in running_controllers]
184 for controller
in controllers_to_load:
186 rospy.wait_for_service(
'controller_manager/load_controller', self.
_service_timeout)
187 load_controllers = rospy.ServiceProxy(
'controller_manager/load_controller', LoadController)
188 load_success = load_controllers(controller).ok
190 rospy.logerr(
"Failed to load controller '{}'".format(controller))
192 except rospy.ServiceException
as error:
193 rospy.logerr(
"Failed to load controller '{}'".format(controller))
199 rospy.wait_for_service(
'controller_manager/switch_controller', self.
_service_timeout)
200 switch_controllers = rospy.ServiceProxy(
'controller_manager/switch_controller', SwitchController)
202 SwitchController._request_class.BEST_EFFORT,
False, 0).ok
203 if not start_success:
204 rospy.logerr(
"Failed to stop controllers {} and/or start controllers {}".format(controllers_to_stop,
205 controllers_to_start))
207 except rospy.ServiceException
as error:
208 rospy.logerr(
"Failed to stop controllers {} and/or start controllers {}".format(controllers_to_stop,
209 controllers_to_start))
213 rospy.loginfo(
"Controllers spawned successfully.")
215 rospy.logerr(
"There was an error spawning controllers")
220 joints_lower = [joint.lower()
for joint
in joints]
221 for key
in config.keys():
222 if key.lower()
in joints_lower:
225 if isinstance(config[key], dict):
226 ControllerSpawner.remove_joints(config[key], joints)
227 if isinstance(config[key], list):
228 for joint
in joints_lower:
229 match_indices = [i
for i, item
in enumerate(config[key])
if joint
in item.lower()]
230 for index
in sorted(match_indices, reverse=
True):
231 del config[key][index]
234 if __name__ ==
"__main__":
235 rospy.init_node(
"sr_controller_spawner")
236 ros_pack = rospkg.RosPack()
237 sr_robot_launch_path = ros_pack.get_path(
'sr_robot_launch')
238 wait_for_topic = rospy.get_param(
"~wait_for",
"")
239 controller_group = rospy.get_param(
"~controller_group",
"trajectory")
240 config_file_path = rospy.get_param(
"~config_file_path",
"{}/config/controller_spawner.yaml".format(
241 sr_robot_launch_path))
242 service_timeout = rospy.get_param(
"~service_timeout", 60.0)
244 if not controller_spawner.load_config():
245 rospy.logerr(
"Failed to load controller spawner config.")
247 if (rospy.has_param(
"~wait_for")):
248 rospy.loginfo(
"Shadow controller spawner is waiting for topic '{}'...".format(rospy.get_param(
"~wait_for")))
249 rospy.wait_for_message(rospy.get_param(
"~wait_for"), rospy.AnyMsg)
250 controller_spawner.switch_controllers(controller_group)
def parse_controllers(self)
def remove_joints(config, joints=[])
def load_controller_configs(self)
def resolve_path(self, path, joint_name=None, local_path=None)
def __init__(self, config_file_path, service_timeout)
def resolve_string(self, string, joint_name=None)
def switch_controllers(self, controller_group_name)