00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 __author__ = 'Antons Rebguns'
00038 __copyright__ = 'Copyright (c) 2010-2011 Antons Rebguns'
00039
00040 __license__ = 'BSD'
00041 __maintainer__ = 'Antons Rebguns'
00042 __email__ = 'anton@email.arizona.edu'
00043
00044
00045 import sys
00046 import os
00047 from optparse import OptionParser
00048
00049 import rospy
00050
00051 from dynamixel_controllers.srv import StartController
00052 from dynamixel_controllers.srv import StopController
00053 from dynamixel_controllers.srv import RestartController
00054
00055
00056 parser = OptionParser()
00057
00058 def manage_controller(controller_name, port_namespace, controller_type, command, deps, start, stop, restart):
00059 try:
00060 controller = rospy.get_param(controller_name + '/controller')
00061 package_path = controller['package']
00062 module_name = controller['module']
00063 class_name = controller['type']
00064 except KeyError as ke:
00065 rospy.logerr('[%s] configuration error: could not find controller parameters on parameter server' % controller_name)
00066 sys.exit(1)
00067 except Exception as e:
00068 rospy.logerr('[%s]: %s' % (controller_name, e))
00069 sys.exit(1)
00070
00071 if command.lower() == 'start':
00072 try:
00073 response = start(port_namespace, package_path, module_name, class_name, controller_name, deps)
00074 if response.success: rospy.loginfo(response.reason)
00075 else: rospy.logerr(response.reason)
00076 except rospy.ServiceException, e:
00077 rospy.logerr('Service call failed: %s' % e)
00078 elif command.lower() == 'stop':
00079 try:
00080 response = stop(controller_name)
00081 if response.success: rospy.loginfo(response.reason)
00082 else: rospy.logerr(response.reason)
00083 except rospy.ServiceException, e:
00084 rospy.logerr('Service call failed: %s' % e)
00085 elif command.lower() == 'restart':
00086 try:
00087 response = restart(port_namespace, package_path, module_name, class_name, controller_name, deps)
00088 if response.success: rospy.loginfo(response.reason)
00089 else: rospy.logerr(response.reason)
00090 except rospy.ServiceException, e:
00091 rospy.logerr('Service call failed: %s' % e)
00092 else:
00093 rospy.logerr('Invalid command.')
00094 parser.print_help()
00095
00096 if __name__ == '__main__':
00097 try:
00098 rospy.init_node('controller_spawner', anonymous=True)
00099
00100 parser.add_option('-m', '--manager', metavar='MANAGER',
00101 help='specified serial port is managed by MANAGER')
00102 parser.add_option('-p', '--port', metavar='PORT',
00103 help='motors of specified controllers are connected to PORT')
00104 parser.add_option('-t', '--type', metavar='TYPE', default='simple', choices=('simple','meta'),
00105 help='type of controller to be loaded (simple|meta) [default: %default]')
00106 parser.add_option('-c', '--command', metavar='COMMAND', default='start', choices=('start','stop','restart'),
00107 help='command to perform on specified controllers: start, stop or restart [default: %default]')
00108
00109 (options, args) = parser.parse_args(rospy.myargv()[1:])
00110
00111 if len(args) < 1:
00112 parser.error('specify at least one controller name')
00113
00114 manager_namespace = options.manager
00115 port_namespace = options.port
00116 controller_type = options.type
00117 command = options.command
00118 joint_controllers = args
00119
00120 if controller_type == 'meta': port_namespace = 'meta'
00121
00122 start_service_name = '%s/%s/start_controller' % (manager_namespace, port_namespace)
00123 stop_service_name = '%s/%s/stop_controller' % (manager_namespace, port_namespace)
00124 restart_service_name = '%s/%s/restart_controller' % (manager_namespace, port_namespace)
00125
00126 parent_namespace = 'global' if rospy.get_namespace() == '/' else rospy.get_namespace()
00127 rospy.loginfo('%s controller_spawner: waiting for controller_manager %s to startup in %s namespace...' % (port_namespace, manager_namespace, parent_namespace))
00128
00129 rospy.wait_for_service(start_service_name)
00130 rospy.wait_for_service(stop_service_name)
00131 rospy.wait_for_service(restart_service_name)
00132
00133 start_controller = rospy.ServiceProxy(start_service_name, StartController)
00134 stop_controller = rospy.ServiceProxy(stop_service_name, StopController)
00135 restart_controller = rospy.ServiceProxy(restart_service_name, RestartController)
00136
00137 rospy.loginfo('%s controller_spawner: All services are up, spawning controllers...' % port_namespace)
00138
00139 if controller_type == 'simple':
00140 for controller_name in joint_controllers:
00141 manage_controller(controller_name, port_namespace, controller_type, command, [], start_controller, stop_controller, restart_controller)
00142 elif controller_type == 'meta':
00143 controller_name = joint_controllers[0]
00144 dependencies = joint_controllers[1:]
00145 manage_controller(controller_name, port_namespace, controller_type, command, dependencies, start_controller, stop_controller, restart_controller)
00146 except rospy.ROSInterruptException: pass
00147