21 from controller_manager_msgs.srv
import ListControllers
22 from controller_manager_msgs.srv
import SwitchController, LoadController
23 from std_msgs.msg
import Bool
31 controller_to_start =
'sh_rh_grasp_controller' 35 rospy.wait_for_service(
'controller_manager/load_controller', self.
service_timeout)
36 load_controllers = rospy.ServiceProxy(
'controller_manager/load_controller', LoadController)
37 loaded_controllers = load_controllers(controller_to_start)
38 except rospy.ServiceException:
40 if not loaded_controllers.ok:
44 rospy.wait_for_service(
'controller_manager/switch_controller', self.
service_timeout)
45 switch_controllers = rospy.ServiceProxy(
'controller_manager/switch_controller', SwitchController)
46 switched_controllers = switch_controllers([controller_to_start],
None,
47 SwitchController._request_class.BEST_EFFORT)
48 except rospy.ServiceException:
51 if not switched_controllers.ok:
55 rospy.logerr(
"Failed to launch grasp controller!")
67 wait_for_topic_result = [
None]
69 def wait_for_topic_cb(msg):
70 wait_for_topic_result[0] = msg
71 rospy.logdebug(
"Heard from wait-for topic: %s" % str(msg.data))
72 rospy.Subscriber(topic_name, Bool, wait_for_topic_cb)
73 started_waiting = rospy.Time.now().to_sec()
76 warned_about_not_hearing_anything =
False 77 while not wait_for_topic_result[0]:
79 if rospy.is_shutdown():
81 if not warned_about_not_hearing_anything:
82 if rospy.Time.now().to_sec() - started_waiting > timeout:
83 warned_about_not_hearing_anything =
True 84 rospy.logwarn(
"Controller Spawner hasn't heard anything from its \"wait for\" topic (%s)" %
86 while not wait_for_topic_result[0].data:
88 if rospy.is_shutdown():
93 if __name__ ==
"__main__":
94 rospy.init_node(
"grasp_controller_spawner")
95 wait_for_topic = rospy.get_param(
"~wait_for",
"")
96 timeout = rospy.get_param(
"~timeout", 30.0)
97 service_timeout = rospy.get_param(
"~service_timeout", 60.0)
100 if grasp_spawner.wait_for_topic(wait_for_topic, timeout):
101 grasp_spawner.set_controller()
def wait_for_topic(topic_name, timeout)
def __init__(self, service_timeout)