controller_spawner.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 #
00004 # Software License Agreement (BSD License)
00005 #
00006 # Copyright (c) 2010-2011, Antons Rebguns.
00007 # All rights reserved.
00008 #
00009 # Redistribution and use in source and binary forms, with or without
00010 # modification, are permitted provided that the following conditions
00011 # are met:
00012 #
00013 #  * Redistributions of source code must retain the above copyright
00014 #    notice, this list of conditions and the following disclaimer.
00015 #  * Redistributions in binary form must reproduce the above
00016 #    copyright notice, this list of conditions and the following
00017 #    disclaimer in the documentation and/or other materials provided
00018 #    with the distribution.
00019 #  * Neither the name of University of Arizona nor the names of its
00020 #    contributors may be used to endorse or promote products derived
00021 #    from this software without specific prior written permission.
00022 #
00023 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034 # POSSIBILITY OF SUCH DAMAGE.
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 


dynamixel_controllers
Author(s): Antons Rebguns, Cody Jorgensen, Cara Slutter
autogenerated on Fri Jan 3 2014 11:19:47