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, Cody Jorgensen, Cara Slutter'
00038 __copyright__ = 'Copyright (c) 2010-2011 Antons Rebguns, Cody Jorgensen, Cara Slutter'
00039
00040 __license__ = 'BSD'
00041 __maintainer__ = 'Antons Rebguns'
00042 __email__ = 'anton@email.arizona.edu'
00043
00044
00045 import sys
00046
00047 import roslib
00048 roslib.load_manifest('dynamixel_controllers')
00049
00050 import rospy
00051
00052 from dynamixel_driver.dynamixel_serial_proxy import SerialProxy
00053
00054 from dynamixel_controllers.srv import StartController
00055 from dynamixel_controllers.srv import StartControllerResponse
00056 from dynamixel_controllers.srv import StopController
00057 from dynamixel_controllers.srv import StopControllerResponse
00058 from dynamixel_controllers.srv import RestartController
00059 from dynamixel_controllers.srv import RestartControllerResponse
00060
00061 class ControllerManager:
00062 def __init__(self):
00063 rospy.init_node('dynamixel_controller_manager', anonymous=False)
00064
00065 port_name = rospy.get_param('~port_name', '/dev/ttyUSB0')
00066 baud_rate = rospy.get_param('~baud_rate', 1000000)
00067 min_motor_id = rospy.get_param('~min_motor_id', 1)
00068 max_motor_id = rospy.get_param('~max_motor_id', 25)
00069 update_rate = rospy.get_param("~update_rate", 5)
00070 namespace = port_name[port_name.rfind('/') + 1:]
00071
00072 self.serial_proxy = SerialProxy(port_name, baud_rate, min_motor_id, max_motor_id, update_rate)
00073 self.serial_proxy.connect()
00074
00075 rospy.on_shutdown(self.serial_proxy.disconnect)
00076
00077 self.controllers = {}
00078
00079 rospy.Service('start_controller/%s' % namespace, StartController, self.start_controller)
00080 rospy.Service('stop_controller/%s' % namespace, StopController, self.stop_controller)
00081 rospy.Service('restart_controller/%s' % namespace, RestartController, self.restart_controller)
00082
00083 def start_controller(self, req):
00084 port_name = req.port_name
00085 package_path = req.package_path
00086 module_name = req.module_name
00087 class_name = req.class_name
00088 controller_name = req.controller_name
00089
00090 if controller_name in self.controllers:
00091 return StartControllerResponse(False, 'Controller [%s] already started. If you want to restart it, call restart.' % controller_name)
00092
00093
00094 if not package_path in sys.path:
00095 sys.path.append(package_path)
00096
00097 try:
00098 if module_name not in sys.modules:
00099
00100 controller_module = __import__(module_name)
00101 else:
00102
00103 controller_module = reload(sys.modules[module_name])
00104 except ImportError, ie:
00105 return StartControllerResponse(False, 'Cannot find controller module. Unable to start controller %s\n%s' % (module_name, str(ie)))
00106 except SyntaxError, se:
00107 return StartControllerResponse(False, 'Syntax error in controller module. Unable to start controller %s\n%s' % (module_name, str(se)))
00108 except Exception, e:
00109 return StartControllerResponse(False, 'Unknown error has occured. Unable to start controller %s\n%s' % (module_name, str(e)))
00110
00111 kls = getattr(controller_module, class_name)
00112 controller = kls(self.serial_proxy.queue_new_packet, controller_name, port_name)
00113
00114 if controller.initialize():
00115 controller.start()
00116 self.controllers[controller_name] = controller
00117 return StartControllerResponse(True, 'Controller %s successfully started.' % controller_name)
00118 else:
00119 return StartControllerResponse(False, 'Initialization failed. Unable to start controller %s' % controller_name)
00120
00121 def stop_controller(self, req):
00122 controller_name = req.controller_name
00123
00124 if controller_name in self.controllers:
00125 self.controllers[controller_name].stop()
00126 del self.controllers[controller_name]
00127 return StopControllerResponse(True, 'controller %s successfully stopped.' % controller_name)
00128 else:
00129 return StopControllerResponse(False, 'controller %s was not running.' % controller_name)
00130
00131 def restart_controller(self, req):
00132 response1 = self.stop_controller(req)
00133 response2 = self.start_controller(req)
00134 return RestartControllerResponse(response1.success and response2.success, '%s\n%s' % (response1.reason, response2.reason))
00135
00136 if __name__ == '__main__':
00137 try:
00138 manager = ControllerManager()
00139 rospy.spin()
00140 except rospy.ROSInterruptException: pass
00141