37 __author__ =
'Antons Rebguns' 38 __copyright__ =
'Copyright (c) 2010-2011 Antons Rebguns' 41 __maintainer__ =
'Antons Rebguns' 42 __email__ =
'anton@email.arizona.edu' 47 from optparse
import OptionParser
51 from dynamixel_controllers.srv
import StartController
52 from dynamixel_controllers.srv
import StopController
53 from dynamixel_controllers.srv
import RestartController
56 parser = OptionParser()
58 def manage_controller(controller_name, port_namespace, controller_type, command, deps, start, stop, restart):
60 controller = rospy.get_param(controller_name +
'/controller')
61 package_path = controller[
'package']
62 module_name = controller[
'module']
63 class_name = controller[
'type']
64 except KeyError
as ke:
65 rospy.logerr(
'[%s] configuration error: could not find controller parameters on parameter server' % controller_name)
67 except Exception
as e:
68 rospy.logerr(
'[%s]: %s' % (controller_name, e))
71 if command.lower() ==
'start':
73 response = start(port_namespace, package_path, module_name, class_name, controller_name, deps)
74 if response.success: rospy.loginfo(response.reason)
75 else: rospy.logerr(response.reason)
76 except rospy.ServiceException, e:
77 rospy.logerr(
'Service call failed: %s' % e)
78 elif command.lower() ==
'stop':
80 response = stop(controller_name)
81 if response.success: rospy.loginfo(response.reason)
82 else: rospy.logerr(response.reason)
83 except rospy.ServiceException, e:
84 rospy.logerr(
'Service call failed: %s' % e)
85 elif command.lower() ==
'restart':
87 response = restart(port_namespace, package_path, module_name, class_name, controller_name, deps)
88 if response.success: rospy.loginfo(response.reason)
89 else: rospy.logerr(response.reason)
90 except rospy.ServiceException, e:
91 rospy.logerr(
'Service call failed: %s' % e)
93 rospy.logerr(
'Invalid command.')
96 if __name__ ==
'__main__':
98 rospy.init_node(
'controller_spawner', anonymous=
True)
100 parser.add_option(
'-m',
'--manager', metavar=
'MANAGER',
101 help=
'specified serial port is managed by MANAGER')
102 parser.add_option(
'-p',
'--port', metavar=
'PORT',
103 help=
'motors of specified controllers are connected to PORT')
104 parser.add_option(
'-t',
'--type', metavar=
'TYPE', default=
'simple', choices=(
'simple',
'meta'),
105 help=
'type of controller to be loaded (simple|meta) [default: %default]')
106 parser.add_option(
'-c',
'--command', metavar=
'COMMAND', default=
'start', choices=(
'start',
'stop',
'restart'),
107 help=
'command to perform on specified controllers: start, stop or restart [default: %default]')
109 (options, args) = parser.parse_args(rospy.myargv()[1:])
112 parser.error(
'specify at least one controller name')
114 manager_namespace = options.manager
115 port_namespace = options.port
116 controller_type = options.type
117 command = options.command
118 joint_controllers = args
120 if controller_type ==
'meta': port_namespace =
'meta' 122 start_service_name =
'%s/%s/start_controller' % (manager_namespace, port_namespace)
123 stop_service_name =
'%s/%s/stop_controller' % (manager_namespace, port_namespace)
124 restart_service_name =
'%s/%s/restart_controller' % (manager_namespace, port_namespace)
126 parent_namespace =
'global' if rospy.get_namespace() ==
'/' else rospy.get_namespace()
127 rospy.loginfo(
'%s controller_spawner: waiting for controller_manager %s to startup in %s namespace...' % (port_namespace, manager_namespace, parent_namespace))
129 rospy.wait_for_service(start_service_name)
130 rospy.wait_for_service(stop_service_name)
131 rospy.wait_for_service(restart_service_name)
133 start_controller = rospy.ServiceProxy(start_service_name, StartController)
134 stop_controller = rospy.ServiceProxy(stop_service_name, StopController)
135 restart_controller = rospy.ServiceProxy(restart_service_name, RestartController)
137 rospy.loginfo(
'%s controller_spawner: All services are up, spawning controllers...' % port_namespace)
139 if controller_type ==
'simple':
140 for controller_name
in joint_controllers:
141 manage_controller(controller_name, port_namespace, controller_type, command, [], start_controller, stop_controller, restart_controller)
142 elif controller_type ==
'meta':
143 controller_name = joint_controllers[0]
144 dependencies = joint_controllers[1:]
145 manage_controller(controller_name, port_namespace, controller_type, command, dependencies, start_controller, stop_controller, restart_controller)
146 except rospy.ROSInterruptException:
pass def manage_controller(controller_name, port_namespace, controller_type, command, deps, start, stop, restart)