pycontroller_manager.py
Go to the documentation of this file.
00001 import roslib; roslib.load_manifest('pycontroller_manager')
00002 import rospy
00003 import pr2_mechanism_msgs.srv as pmm
00004 
00005 class ControllerManager:
00006 
00007     def __init__(self):
00008         # LoadController        
00009         self.load = rospy.ServiceProxy('pr2_controller_manager/load_controller', pmm.LoadController)
00010 
00011         # UnloadController        
00012         self.unload = rospy.ServiceProxy('pr2_controller_manager/unload_controller', pmm.UnloadController)
00013 
00014         # SwitchController
00015         self._switch_controller = rospy.ServiceProxy('pr2_controller_manager/switch_controller', pmm.SwitchController)
00016 
00017         self.list_controllers = rospy.ServiceProxy('pr2_controller_manager/list_controllers', pmm.ListControllers)
00018 
00019         self.joint_controllers = {}
00020         self.cart_controllers = {}
00021         for arm in ['l', 'r']:
00022             self.joint_controllers[arm] = arm + '_arm_controller'
00023             self.cart_controllers[arm] = cart_controller_name = arm + '_cart'
00024 
00025     def switch(self, start_con, stop_con):
00026         con = self.list_controllers()
00027         valid_start = []
00028         valid_stop = []
00029 
00030         #Add the ones that are loaded but not running.
00031         for idx, controller in enumerate(con.controllers):
00032             if controller in start_con:
00033                 #print 'found', controller, 'it\'s state is', con.state[idx], len(con.state[idx]), con.state[idx].__class__
00034                 if con.state[idx] != 'running':
00035                     #print 'ControllerManager: adding controller', controller
00036                     valid_start.append(controller)
00037                 #else:
00038                 #    print 'ControllerManager: not adding', controller
00039 
00040             if controller in stop_con:
00041                 if con.state[idx] != 'stopped':
00042                     valid_stop.append(controller)
00043 
00044             #print controller, con.state[idx]
00045 
00046         #Add all controllers not loaded! But load them first
00047         for n in start_con:
00048             if not n in con.controllers:
00049                 #print 'loading controller', n
00050                 self.load(n)
00051                 valid_start.append(n)
00052 
00053         if len(start_con) > 0:
00054             #print 'ControllerManager: starting', valid_start, 'stopping', valid_stop
00055             resp = self._switch_controller(valid_start, valid_stop, pmm.SwitchControllerRequest.STRICT)
00056             return resp.ok, valid_start, valid_stop
00057         else:
00058             #print 'ControllerManager: not starting or stopping any controllsers'
00059             return None, valid_start, valid_stop
00060 
00061     def joint_mode(self, arm):
00062         #get current state
00063         if arm == 'left' or arm == 'both':
00064             return self.switch([self.joint_controllers['l']], [self.cart_controllers['l']])
00065         if arm == 'right' or arm == 'both':
00066             return self.switch([self.joint_controllers['r']], [self.cart_controllers['r']])
00067 
00068     def cart_mode(self, arm):
00069         if arm == 'left' or arm == 'both':
00070             #print 'switchleft'
00071             return self.switch([self.cart_controllers['l']], [self.joint_controllers['l']])
00072         if arm == 'right' or arm == 'both':
00073             #print 'switchright'
00074             return self.switch([self.cart_controllers['r']], [self.joint_controllers['r']])


pycontroller_manager
Author(s): Hai
autogenerated on Thu Dec 12 2013 12:09:22