38 __author__ =
'Antons Rebguns, Cody Jorgensen, Cara Slutter' 39 __copyright__ =
'Copyright (c) 2010-2011 Antons Rebguns, Cody Jorgensen, Cara Slutter' 42 __maintainer__ =
'Antons Rebguns' 43 __email__ =
'anton@email.arizona.edu' 46 from threading
import Thread, Lock
54 from diagnostic_msgs.msg
import DiagnosticArray
55 from diagnostic_msgs.msg
import DiagnosticStatus
56 from diagnostic_msgs.msg
import KeyValue
58 from dynamixel_controllers.srv
import StartController
59 from dynamixel_controllers.srv
import StartControllerResponse
60 from dynamixel_controllers.srv
import StopController
61 from dynamixel_controllers.srv
import StopControllerResponse
62 from dynamixel_controllers.srv
import RestartController
63 from dynamixel_controllers.srv
import RestartControllerResponse
67 rospy.init_node(
'dynamixel_controller_manager', anonymous=
True)
78 manager_namespace = rospy.get_param(
'~namespace')
79 serial_ports = rospy.get_param(
'~serial_ports')
81 for port_namespace,port_config
in serial_ports.items():
82 port_name = port_config[
'port_name']
83 baud_rate = port_config[
'baud_rate']
84 readback_echo = port_config[
'readback_echo']
if 'readback_echo' in port_config
else False 85 min_motor_id = port_config[
'min_motor_id']
if 'min_motor_id' in port_config
else 0
86 max_motor_id = port_config[
'max_motor_id']
if 'max_motor_id' in port_config
else 253
87 update_rate = port_config[
'update_rate']
if 'update_rate' in port_config
else 5
91 if 'diagnostics' in port_config:
92 if 'error_level_temp' in port_config[
'diagnostics']:
93 error_level_temp = port_config[
'diagnostics'][
'error_level_temp']
94 if 'warn_level_temp' in port_config[
'diagnostics']:
95 warn_level_temp = port_config[
'diagnostics'][
'warn_level_temp']
107 serial_proxy.connect()
114 rospy.Service(
'%s/%s/start_controller' % (manager_namespace, port_namespace), StartController, self.
start_controller)
115 rospy.Service(
'%s/%s/stop_controller' % (manager_namespace, port_namespace), StopController, self.
stop_controller)
116 rospy.Service(
'%s/%s/restart_controller' % (manager_namespace, port_namespace), RestartController, self.
restart_controller)
127 rospy.Service(
'%s/meta/start_controller' % manager_namespace, StartController, self.
start_controller)
128 rospy.Service(
'%s/meta/stop_controller' % manager_namespace, StopController, self.
stop_controller)
129 rospy.Service(
'%s/meta/restart_controller' % manager_namespace, RestartController, self.
restart_controller)
135 for serial_proxy
in self.serial_proxies.values():
136 serial_proxy.disconnect()
139 diag_msg = DiagnosticArray()
142 while not rospy.is_shutdown():
144 diag_msg.header.stamp = rospy.Time.now()
146 for controller
in self.controllers.values():
148 joint_state = controller.joint_state
149 temps = joint_state.motor_temps
150 max_temp = max(temps)
152 status = DiagnosticStatus()
153 status.name =
'Joint Controller (%s)' % controller.joint_name
154 status.hardware_id =
'Robotis Dynamixel %s on port %s' % (str(joint_state.motor_ids), controller.port_namespace)
155 status.values.append(KeyValue(
'Goal', str(joint_state.goal_pos)))
156 status.values.append(KeyValue(
'Position', str(joint_state.current_pos)))
157 status.values.append(KeyValue(
'Error', str(joint_state.error)))
158 status.values.append(KeyValue(
'Velocity', str(joint_state.velocity)))
159 status.values.append(KeyValue(
'Load', str(joint_state.load)))
160 status.values.append(KeyValue(
'Moving', str(joint_state.is_moving)))
161 status.values.append(KeyValue(
'Temperature', str(max_temp)))
162 status.level = DiagnosticStatus.OK
163 status.message =
'OK' 165 diag_msg.status.append(status)
169 self.diagnostics_pub.publish(diag_msg)
173 controllers_still_waiting = []
176 if not set(deps).issubset(self.controllers.keys()):
178 rospy.logwarn(
'[%s] not all dependencies started, still waiting for %s...' % (controller_name, str(list(set(deps).difference(self.controllers.keys())))))
180 dependencies = [self.
controllers[dep_name]
for dep_name
in deps]
181 controller = kls(controller_name, dependencies)
183 if controller.initialize():
190 port_name = req.port_name
191 package_path = req.package_path
192 module_name = req.module_name
193 class_name = req.class_name
194 controller_name = req.controller_name
196 self.start_controller_lock.acquire()
199 self.start_controller_lock.release()
200 return StartControllerResponse(
False,
'Controller [%s] already started. If you want to restart it, call restart.' % controller_name)
203 if module_name
not in sys.modules:
205 package_module = __import__(package_path, globals(), locals(), [module_name], -1)
208 package_module = reload(sys.modules[package_path])
209 controller_module = getattr(package_module, module_name)
210 except ImportError, ie:
211 self.start_controller_lock.release()
212 return StartControllerResponse(
False,
'Cannot find controller module. Unable to start controller %s\n%s' % (module_name, str(ie)))
213 except SyntaxError, se:
214 self.start_controller_lock.release()
215 return StartControllerResponse(
False,
'Syntax error in controller module. Unable to start controller %s\n%s' % (module_name, str(se)))
217 self.start_controller_lock.release()
218 return StartControllerResponse(
False,
'Unknown error has occured. Unable to start controller %s\n%s' % (module_name, str(e)))
220 kls = getattr(controller_module, class_name)
222 if port_name ==
'meta':
223 self.waiting_meta_controllers.append((controller_name,req.dependencies,kls))
225 self.start_controller_lock.release()
226 return StartControllerResponse(
True,
'')
228 if port_name !=
'meta' and (port_name
not in self.
serial_proxies):
229 self.start_controller_lock.release()
230 return StartControllerResponse(
False,
'Specified port [%s] not found, available ports are %s. Unable to start controller %s' % (port_name, str(self.serial_proxies.keys()), controller_name))
232 controller = kls(self.
serial_proxies[port_name].dxl_io, controller_name, port_name)
234 if controller.initialize():
239 self.start_controller_lock.release()
241 return StartControllerResponse(
True,
'Controller %s successfully started.' % controller_name)
243 self.start_controller_lock.release()
244 return StartControllerResponse(
False,
'Initialization failed. Unable to start controller %s' % controller_name)
247 controller_name = req.controller_name
248 self.stop_controller_lock.acquire()
253 self.stop_controller_lock.release()
254 return StopControllerResponse(
True,
'controller %s successfully stopped.' % controller_name)
256 self.self.stop_controller_lock.release()
257 return StopControllerResponse(
False,
'controller %s was not running.' % controller_name)
262 return RestartControllerResponse(response1.success
and response2.success,
'%s\n%s' % (response1.reason, response2.reason))
264 if __name__ ==
'__main__':
268 except rospy.ROSInterruptException:
pass
def diagnostics_processor(self)
def stop_controller(self, req)
def start_controller(self, req)
def restart_controller(self, req)