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 roslib
00050 roslib.load_manifest('dynamixel_controllers')
00051
00052 from roslib.packages import InvalidROSPkgException
00053
00054 import rospy
00055 from dynamixel_controllers.srv import StartController
00056 from dynamixel_controllers.srv import StopController
00057 from dynamixel_controllers.srv import RestartController
00058
00059
00060 parser = OptionParser()
00061
00062 def manage_controller(controller_name, port_namespace, controller_type, command, deps, start, stop, restart):
00063 try:
00064 controller = rospy.get_param(controller_name + '/controller')
00065 package_path = roslib.packages.get_pkg_dir(controller['package'])
00066 package_path = os.path.join(package_path, 'src', controller['package'])
00067 module_name = controller['module']
00068 class_name = controller['type']
00069 except KeyError as ke:
00070 rospy.logerr('[%s] configuration error: could not find controller parameters on parameter server' % controller_name)
00071 sys.exit(1)
00072 except InvalidROSPkgException as pe:
00073 rospy.logerr('[%s] configuration error: %s' % (controller_name, pe))
00074 sys.exit(1)
00075 except Exception as e:
00076 rospy.logerr('[%s]: %s' % (controller_name, e))
00077 sys.exit(1)
00078
00079 if command.lower() == 'start':
00080 try:
00081 response = start(port_namespace, package_path, module_name, class_name, controller_name, deps)
00082 if response.success: rospy.loginfo(response.reason)
00083 else: rospy.logerr(response.reason)
00084 except rospy.ServiceException, e:
00085 rospy.logerr('Service call failed: %s' % e)
00086 elif command.lower() == 'stop':
00087 try:
00088 response = stop(controller_name)
00089 if response.success: rospy.loginfo(response.reason)
00090 else: rospy.logerr(response.reason)
00091 except rospy.ServiceException, e:
00092 rospy.logerr('Service call failed: %s' % e)
00093 elif command.lower() == 'restart':
00094 try:
00095 response = restart(port_namespace, package_path, module_name, class_name, controller_name, deps)
00096 if response.success: rospy.loginfo(response.reason)
00097 else: rospy.logerr(response.reason)
00098 except rospy.ServiceException, e:
00099 rospy.logerr('Service call failed: %s' % e)
00100 else:
00101 rospy.logerr('Invalid command.')
00102 parser.print_help()
00103
00104 if __name__ == '__main__':
00105 try:
00106 rospy.init_node('controller_spawner', anonymous=True)
00107
00108 parser.add_option('-m', '--manager', metavar='MANAGER',
00109 help='specified serial port is managed by MANAGER')
00110 parser.add_option('-p', '--port', metavar='PORT',
00111 help='motors of specified controllers are connected to PORT')
00112 parser.add_option('-t', '--type', metavar='TYPE', default='simple', choices=('simple','meta'),
00113 help='type of controller to be loaded (simple|meta) [default: %default]')
00114 parser.add_option('-c', '--command', metavar='COMMAND', default='start', choices=('start','stop','restart'),
00115 help='command to perform on specified controllers: start, stop or restart [default: %default]')
00116
00117 (options, args) = parser.parse_args(rospy.myargv()[1:])
00118
00119 if len(args) < 1:
00120 parser.error('specify at least one controller name')
00121
00122 manager_namespace = options.manager
00123 port_namespace = options.port
00124 controller_type = options.type
00125 command = options.command
00126 joint_controllers = args
00127
00128 if controller_type == 'meta': port_namespace = 'meta'
00129
00130 start_service_name = '%s/%s/start_controller' % (manager_namespace, port_namespace)
00131 stop_service_name = '%s/%s/stop_controller' % (manager_namespace, port_namespace)
00132 restart_service_name = '%s/%s/restart_controller' % (manager_namespace, port_namespace)
00133
00134 parent_namespace = 'global' if rospy.get_namespace() == '/' else rospy.get_namespace()
00135 rospy.loginfo('%s controller_spawner: waiting for controller_manager %s to startup in %s namespace...' % (port_namespace, manager_namespace, parent_namespace))
00136
00137 rospy.wait_for_service(start_service_name)
00138 rospy.wait_for_service(stop_service_name)
00139 rospy.wait_for_service(restart_service_name)
00140
00141 start_controller = rospy.ServiceProxy(start_service_name, StartController)
00142 stop_controller = rospy.ServiceProxy(stop_service_name, StopController)
00143 restart_controller = rospy.ServiceProxy(restart_service_name, RestartController)
00144
00145 rospy.loginfo('%s controller_spawner: All services are up, spawning controllers...' % port_namespace)
00146
00147 if controller_type == 'simple':
00148 for controller_name in joint_controllers:
00149 manage_controller(controller_name, port_namespace, controller_type, command, [], start_controller, stop_controller, restart_controller)
00150 elif controller_type == 'meta':
00151 controller_name = joint_controllers[0]
00152 dependencies = joint_controllers[1:]
00153 manage_controller(controller_name, port_namespace, controller_type, command, dependencies, start_controller, stop_controller, restart_controller)
00154 except rospy.ROSInterruptException: pass
00155