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
00009 self.load = rospy.ServiceProxy('pr2_controller_manager/load_controller', pmm.LoadController)
00010
00011
00012 self.unload = rospy.ServiceProxy('pr2_controller_manager/unload_controller', pmm.UnloadController)
00013
00014
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
00031 for idx, controller in enumerate(con.controllers):
00032 if controller in start_con:
00033
00034 if con.state[idx] != 'running':
00035
00036 valid_start.append(controller)
00037
00038
00039
00040 if controller in stop_con:
00041 if con.state[idx] != 'stopped':
00042 valid_stop.append(controller)
00043
00044
00045
00046
00047 for n in start_con:
00048 if not n in con.controllers:
00049
00050 self.load(n)
00051 valid_start.append(n)
00052
00053 if len(start_con) > 0:
00054
00055 resp = self._switch_controller(valid_start, valid_stop, pmm.SwitchControllerRequest.STRICT)
00056 return resp.ok, valid_start, valid_stop
00057 else:
00058
00059 return None, valid_start, valid_stop
00060
00061 def joint_mode(self, arm):
00062
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
00071 return self.switch([self.cart_controllers['l']], [self.joint_controllers['l']])
00072 if arm == 'right' or arm == 'both':
00073
00074 return self.switch([self.cart_controllers['r']], [self.joint_controllers['r']])