controller_spawner.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Software License Agreement (BSD License)
5 #
6 # Copyright (c) 2010-2011, Antons Rebguns.
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above
16 # copyright notice, this list of conditions and the following
17 # disclaimer in the documentation and/or other materials provided
18 # with the distribution.
19 # * Neither the name of University of Arizona nor the names of its
20 # contributors may be used to endorse or promote products derived
21 # from this software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 # POSSIBILITY OF SUCH DAMAGE.
35 
36 
37 __author__ = 'Antons Rebguns'
38 __copyright__ = 'Copyright (c) 2010-2011 Antons Rebguns'
39 
40 __license__ = 'BSD'
41 __maintainer__ = 'Antons Rebguns'
42 __email__ = 'anton@email.arizona.edu'
43 
44 
45 import sys
46 import os
47 from optparse import OptionParser
48 
49 import rospy
50 
51 from dynamixel_controllers.srv import StartController
52 from dynamixel_controllers.srv import StopController
53 from dynamixel_controllers.srv import RestartController
54 
55 
56 parser = OptionParser()
57 
58 def manage_controller(controller_name, port_namespace, controller_type, command, deps, start, stop, restart):
59  try:
60  controller = rospy.get_param(controller_name + '/controller')
61  package_path = controller['package']
62  module_name = controller['module']
63  class_name = controller['type']
64  except KeyError as ke:
65  rospy.logerr('[%s] configuration error: could not find controller parameters on parameter server' % controller_name)
66  sys.exit(1)
67  except Exception as e:
68  rospy.logerr('[%s]: %s' % (controller_name, e))
69  sys.exit(1)
70 
71  if command.lower() == 'start':
72  try:
73  response = start(port_namespace, package_path, module_name, class_name, controller_name, deps)
74  if response.success: rospy.loginfo(response.reason)
75  else: rospy.logerr(response.reason)
76  except rospy.ServiceException, e:
77  rospy.logerr('Service call failed: %s' % e)
78  elif command.lower() == 'stop':
79  try:
80  response = stop(controller_name)
81  if response.success: rospy.loginfo(response.reason)
82  else: rospy.logerr(response.reason)
83  except rospy.ServiceException, e:
84  rospy.logerr('Service call failed: %s' % e)
85  elif command.lower() == 'restart':
86  try:
87  response = restart(port_namespace, package_path, module_name, class_name, controller_name, deps)
88  if response.success: rospy.loginfo(response.reason)
89  else: rospy.logerr(response.reason)
90  except rospy.ServiceException, e:
91  rospy.logerr('Service call failed: %s' % e)
92  else:
93  rospy.logerr('Invalid command.')
94  parser.print_help()
95 
96 if __name__ == '__main__':
97  try:
98  rospy.init_node('controller_spawner', anonymous=True)
99 
100  parser.add_option('-m', '--manager', metavar='MANAGER',
101  help='specified serial port is managed by MANAGER')
102  parser.add_option('-p', '--port', metavar='PORT',
103  help='motors of specified controllers are connected to PORT')
104  parser.add_option('-t', '--type', metavar='TYPE', default='simple', choices=('simple','meta'),
105  help='type of controller to be loaded (simple|meta) [default: %default]')
106  parser.add_option('-c', '--command', metavar='COMMAND', default='start', choices=('start','stop','restart'),
107  help='command to perform on specified controllers: start, stop or restart [default: %default]')
108 
109  (options, args) = parser.parse_args(rospy.myargv()[1:])
110 
111  if len(args) < 1:
112  parser.error('specify at least one controller name')
113 
114  manager_namespace = options.manager
115  port_namespace = options.port
116  controller_type = options.type
117  command = options.command
118  joint_controllers = args
119 
120  if controller_type == 'meta': port_namespace = 'meta'
121 
122  start_service_name = '%s/%s/start_controller' % (manager_namespace, port_namespace)
123  stop_service_name = '%s/%s/stop_controller' % (manager_namespace, port_namespace)
124  restart_service_name = '%s/%s/restart_controller' % (manager_namespace, port_namespace)
125 
126  parent_namespace = 'global' if rospy.get_namespace() == '/' else rospy.get_namespace()
127  rospy.loginfo('%s controller_spawner: waiting for controller_manager %s to startup in %s namespace...' % (port_namespace, manager_namespace, parent_namespace))
128 
129  rospy.wait_for_service(start_service_name)
130  rospy.wait_for_service(stop_service_name)
131  rospy.wait_for_service(restart_service_name)
132 
133  start_controller = rospy.ServiceProxy(start_service_name, StartController)
134  stop_controller = rospy.ServiceProxy(stop_service_name, StopController)
135  restart_controller = rospy.ServiceProxy(restart_service_name, RestartController)
136 
137  rospy.loginfo('%s controller_spawner: All services are up, spawning controllers...' % port_namespace)
138 
139  if controller_type == 'simple':
140  for controller_name in joint_controllers:
141  manage_controller(controller_name, port_namespace, controller_type, command, [], start_controller, stop_controller, restart_controller)
142  elif controller_type == 'meta':
143  controller_name = joint_controllers[0]
144  dependencies = joint_controllers[1:]
145  manage_controller(controller_name, port_namespace, controller_type, command, dependencies, start_controller, stop_controller, restart_controller)
146  except rospy.ROSInterruptException: pass
147 
def manage_controller(controller_name, port_namespace, controller_type, command, deps, start, stop, restart)


dynamixel_controllers
Author(s): Antons Rebguns, Cody Jorgensen, Cara Slutter
autogenerated on Tue May 12 2020 03:10:59