load_passthrough_controllers.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # ####################################################################
00004 # Copyright (c) 2013, Shadow Robot Company, All rights reserved.
00005 #
00006 # This library is free software; you can redistribute it and/or
00007 # modify it under the terms of the GNU Lesser General Public
00008 # License as published by the Free Software Foundation; either
00009 # version 3.0 of the License, or (at your option) any later version.
00010 #
00011 # This library is distributed in the hope that it will be useful,
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014 # Lesser General Public License for more details.
00015 #
00016 # You should have received a copy of the GNU Lesser General Public
00017 # License along with this library.
00018 # ####################################################################
00019 
00020 import rospy
00021 from time import sleep
00022 
00023 from controller_manager_msgs.srv import LoadController, ListControllers, SwitchController, SwitchControllerRequest
00024 
00025 class LoadPassthroughControllers(object):
00026     """
00027     Load the passthrough controllers for all the RoNeXes present on the bus.
00028     """
00029 
00030     def __init__(self):
00031         """
00032         """
00033         ronex_ids = self.find_ronexes()
00034 
00035         if len(ronex_ids) > 0:
00036             self.set_param(ronex_ids)
00037             self.load_and_start_ctrl(ronex_ids)
00038 
00039     def find_ronexes(self):
00040         """
00041         Find the ronexes present on the system
00042 
00043         @return a list of ronex_ids
00044         """
00045         ronex_ids = []
00046 
00047         # retrieve all the ronex ids from the parameter server
00048         # wait until there's one ronex
00049         attempts = 50
00050         rospy.loginfo("Waiting for the ronex to be loaded properly.")
00051         while attempts and not rospy.has_param("/ronex/devices/0/ronex_id"):
00052             sleep(0.1)
00053             attempts -= 1
00054 
00055         if attempts > 0:
00056             ronex_param = rospy.get_param("/ronex/devices")
00057             for key in ronex_param:
00058                 ronex_ids.append([ronex_param[key]["ronex_id"], ronex_param[key]["product_name"]])
00059         else:
00060             rospy.loginfo("Failed to find ronex devices in parameter server")
00061 
00062         return ronex_ids
00063 
00064     def set_param(self, ronex_ids):
00065         """
00066         Load the parameters for the present ronexes into the parameter server
00067 
00068         @param ronex_ids the ids of the ronexes
00069         """
00070         for ronex_id in ronex_ids:
00071             if ronex_id[1] == "general_io":
00072                 rospy.set_param("/ronex_" + ronex_id[0] + "_passthrough/type", "sr_ronex_controllers/GeneralIOPassthroughController")
00073                 rospy.set_param("/ronex_" + ronex_id[0] + "_passthrough/ronex_id", ronex_id[0])
00074             elif ronex_id[1] == "spi":
00075                 rospy.set_param("/ronex_" + ronex_id[0] + "_passthrough/type", "sr_ronex_controllers/SPIPassthroughController")
00076                 rospy.set_param("/ronex_" + ronex_id[0] + "_passthrough/ronex_id", ronex_id[0])
00077             else:
00078                 rospy.logwarn("RoNeX["+ronex_id[0]+"] type not recognized: "+ronex_id[1]+" -> not loading any controllers for that RoNeX")
00079 
00080     def load_and_start_ctrl(self, ronex_ids):
00081         """
00082         Load and start the passthrough controllers for the list of given ronexes.
00083 
00084         @param ronex_ids the ids of the ronexes
00085         """
00086         # building a list of controller names
00087         controllers_list = []
00088         for ronex_id in ronex_ids:
00089             controllers_list.append("ronex_" + ronex_id[0] + "_passthrough")
00090 
00091         # calling the services to load and switch the controllers on
00092         rospy.loginfo("Waiting for controller_manager services: list/load/switch")
00093         rospy.wait_for_service('controller_manager/list_controllers')
00094         rospy.wait_for_service('controller_manager/load_controller')
00095         rospy.wait_for_service('controller_manager/switch_controller')
00096 
00097         list_controllers = rospy.ServiceProxy('controller_manager/list_controllers', ListControllers)
00098         load_controller = rospy.ServiceProxy('controller_manager/load_controller', LoadController)
00099         switch_controller = rospy.ServiceProxy('controller_manager/switch_controller', SwitchController)
00100 
00101         rospy.loginfo("Starting controllers: " + str(controllers_list))
00102         
00103         # first list the available controllers
00104         available_controllers = None
00105         try:
00106             available_controllers = list_controllers()
00107         except rospy.ServiceException, e:
00108             print "Service did not process request: %s" % str(e)
00109 
00110         # load the ones that don't exist
00111         for ctrl in controllers_list:
00112             if ctrl not in available_controllers.controller:
00113                 try:
00114                     resp1 = load_controller(ctrl)
00115                 except rospy.ServiceException, e:
00116                     print "Service did not process request: %s" % str(e)
00117 
00118         # start the controllers
00119         try:
00120             resp1 = switch_controller(controllers_list, [], SwitchControllerRequest.BEST_EFFORT)
00121         except rospy.ServiceException, e:
00122             print "Service did not process request: %s" % str(e)
00123 
00124 
00125 if __name__ == "__main__":
00126     rospy.sleep(2.5)
00127     rospy.init_node("load_passthrough_controllers")
00128     load_passthrough = LoadPassthroughControllers()


sr_ronex_controllers
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless
autogenerated on Fri Aug 28 2015 13:12:37