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 if __name__ == '__main__':
00060 parser = OptionParser()
00061 parser.add_option('-p', '--port', metavar='PORT', default='/dev/ttyUSB0',
00062 help='motors of specified controllers are connected to PORT [default: %default]')
00063 parser.add_option('-c', '--command', metavar='COMMAND', default='start',
00064 help='command to perform on specified controllers: start, stop or restart [default: %default]')
00065
00066 (options, args) = parser.parse_args(rospy.myargv()[1:])
00067
00068 if len(args) < 1:
00069 print 'Specify at least one controller name to manage'
00070
00071 port = options.port
00072 command = options.command
00073 joint_controllers = args
00074
00075 device_namespace = port[port.rfind('/') + 1:]
00076 start_service_name = 'start_controller/%s' % device_namespace
00077 stop_service_name = 'stop_controller/%s' % device_namespace
00078 restart_service_name = 'restart_controller/%s' % device_namespace
00079
00080 parent_namespace = 'global' if rospy.get_namespace() == '/' else rospy.get_namespace()
00081 rospy.loginfo('%s controller_spawner: waiting for controller_manager to startup in %s namespace...' % (device_namespace, parent_namespace))
00082
00083 rospy.wait_for_service(start_service_name)
00084 rospy.wait_for_service(stop_service_name)
00085 rospy.wait_for_service(restart_service_name)
00086
00087 start_controller = rospy.ServiceProxy(start_service_name, StartController)
00088 stop_controller = rospy.ServiceProxy(stop_service_name, StopController)
00089 restart_controller = rospy.ServiceProxy(restart_service_name, RestartController)
00090
00091 rospy.loginfo('%s controller_spawner: All services are up, spawning controllers...' % device_namespace)
00092
00093 for controller_name in joint_controllers:
00094 try:
00095 controller = rospy.get_param(controller_name + '/controller')
00096 package_path = roslib.packages.get_pkg_dir(controller['package'])
00097 package_path = os.path.join(package_path, 'src', controller['package'])
00098 module_name = controller['module']
00099 class_name = controller['type']
00100 except KeyError as ke:
00101 rospy.logerr('[%s] configuration error: could not find controller parameters on parameter server' % controller_name)
00102 sys.exit(1)
00103 except InvalidROSPkgException as pe:
00104 rospy.logerr('[%s] configuration error: %s' % (controller_name, pe))
00105 sys.exit(1)
00106 except Exception as e:
00107 rospy.logerr('[%s]: %s' % (controller_name, e))
00108 sys.exit(1)
00109
00110 if command.lower() == 'start':
00111 try:
00112 response = start_controller(port, package_path, module_name, class_name, controller_name)
00113 if response.success: rospy.loginfo(response.reason)
00114 else: rospy.logerr(response.reason)
00115 except rospy.ServiceException, e:
00116 rospy.logerr('Service call failed: %s' % e)
00117 elif command.lower() == 'stop':
00118 try:
00119 response = stop_controller(controller_name)
00120 if response.success: rospy.loginfo(response.reason)
00121 else: rospy.logerr(response.reason)
00122 except rospy.ServiceException, e:
00123 rospy.logerr('Service call failed: %s' % e)
00124 elif command.lower() == 'restart':
00125 try:
00126 response = restart_controller(port, package_path, module_name, class_name, controller_name)
00127 if response.success: rospy.loginfo(response.reason)
00128 else: rospy.logerr(response.reason)
00129 except rospy.ServiceException, e:
00130 rospy.logerr('Service call failed: %s' % e)
00131 else:
00132 rospy.logerr('Invalid command.')
00133 parser.print_help()
00134