00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import rospy
00019 from simple_script_server import *
00020
00021
00022 class command_gui_buttons:
00023 def __init__(self):
00024 self.sss = simple_script_server()
00025 self.panels = []
00026 self.stop_buttons = []
00027 self.init_buttons = []
00028 self.recover_buttons = []
00029 self.halt_buttons = []
00030 self.CreateControlPanel()
00031
00032
00033 def CreateControlPanel(self):
00034 param_prefix = "~control_buttons"
00035 if not rospy.has_param(param_prefix):
00036 rospy.logerr("parameter %s does not exist on ROS Parameter Server, aborting...",param_prefix)
00037 return False
00038 group_param = rospy.get_param(param_prefix)
00039
00040 group_param = self.SortDict(group_param)
00041
00042
00043 for group in group_param:
00044 group_name = group[1]["group_name"]
00045 component_name = group[1]["component_name"]
00046 button_list = group[1]["buttons"]
00047 buttons = []
00048 for button in button_list:
00049
00050 if button[1] == "move":
00051 buttons.append(self.CreateButton(button[0],self.sss.move,component_name,button[2]))
00052 elif button[1] == "move_base_rel":
00053 buttons.append(self.CreateButton(button[0],self.sss.move_base_rel,component_name,button[2]))
00054 elif button[1] == "trigger_action":
00055 buttons.append(self.CreateButton(button[0],self.sss.trigger_action,component_name,button[2],True))
00056 elif button[1] == "string_action":
00057 buttons.append(self.CreateButton(button[0],self.sss.string_action,component_name,button[2],True))
00058 elif button[1] == "trigger":
00059 buttons.append(self.CreateButton(button[0],self.sss.trigger,component_name,button[2]))
00060 if button[2] == "stop":
00061 self.stop_buttons.append(component_name)
00062 if button[2] == "init":
00063 self.init_buttons.append(component_name)
00064 if button[2] == "recover":
00065 self.recover_buttons.append(component_name)
00066 if button[2] == "halt":
00067 self.halt_buttons.append(component_name)
00068 elif button[1] == "stop":
00069 buttons.append(self.CreateButton(button[0],self.sss.stop,component_name))
00070 self.stop_buttons.append(component_name)
00071 elif button[1] == "init":
00072 buttons.append(self.CreateButton(button[0],self.sss.init,component_name))
00073 self.init_buttons.append(component_name)
00074 elif button[1] == "recover":
00075 buttons.append(self.CreateButton(button[0],self.sss.recover,component_name))
00076 self.recover_buttons.append(component_name)
00077 elif button[1] == "halt":
00078 buttons.append(self.CreateButton(button[0],self.sss.halt,component_name))
00079 self.halt_buttons.append(component_name)
00080 else:
00081 rospy.logerr("Function <<%s>> not known to command gui",button[1])
00082 return False
00083 group = (group_name,buttons)
00084
00085
00086 if component_name == "base":
00087 param_prefix = "~nav_buttons"
00088 if rospy.has_param(param_prefix):
00089 nav_buttons_param = rospy.get_param(param_prefix)
00090 nav_button_list = nav_buttons_param["buttons"]
00091
00092 for button in nav_button_list:
00093
00094 buttons.append(self.CreateButton(button[0],self.sss.move,component_name,button[2]))
00095 else:
00096 rospy.logwarn("parameter %s does not exist on ROS Parameter Server, no nav buttons will be available.",param_prefix)
00097 self.panels.append(group)
00098
00099
00100 self.stop_buttons = self.uniqify_list(self.stop_buttons)
00101 self.init_buttons = self.uniqify_list(self.init_buttons)
00102 self.recover_buttons = self.uniqify_list(self.recover_buttons)
00103 self.halt_buttons = self.uniqify_list(self.halt_buttons)
00104
00105
00106
00107 def CreateButton(self,button_name,function,component_name,parameter_name=None,blocking=False):
00108 if parameter_name == None:
00109 button = (button_name,function,(component_name,blocking))
00110 else:
00111 button = (button_name,function,(component_name,parameter_name,blocking))
00112 return button
00113
00114
00115 def SortDict(self,dictionary):
00116 keys = sorted(dictionary.iterkeys())
00117 k=[]
00118
00119
00120
00121 return [[key,dictionary[key]] for key in keys]
00122
00123
00124 def uniqify_list(self,seq, idfun=None):
00125
00126 if idfun is None:
00127 def idfun(x): return x
00128 seen = {}
00129 result = []
00130 for item in seq:
00131 marker = idfun(item)
00132
00133
00134
00135 if marker in seen: continue
00136 seen[marker] = 1
00137 result.append(item)
00138 return result