command_gui_buttons.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #   http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
00016 
00017 
00018 import rospy
00019 from simple_script_server import *
00020 
00021 ## Implements configurable buttons
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         ## Creates the control panel out of configuration from ROS parameter server
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                 #print group_param
00040                 group_param = self.SortDict(group_param)
00041                 #print group_param
00042 
00043                 for group in group_param:
00044                         group_name = group[1]["group_name"]
00045                         component_name = group[1]["component_name"] # \todo check component name with robot_components.yaml files
00046                         button_list = group[1]["buttons"]
00047                         buttons = []
00048                         for button in button_list:
00049                                 #print "button = ",button
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                         # add nav buttons (optional)
00086                         if component_name == "base": # \todo get base name from robot_components.yaml
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                                         #print nav_button_list
00092                                         for button in nav_button_list:
00093                                                 #print "button = ",button
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                 # uniqify lists to not have double entries
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         ## Creates one button with functionality
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         ## Sorts a dictionary alphabetically
00115         def SortDict(self,dictionary):
00116                 keys = sorted(dictionary.iterkeys())
00117                 k=[]
00118                 #print "keys = ", keys
00119                 #for key in keys:
00120                 #       print "values = ", dictionary[key]
00121                 return [[key,dictionary[key]] for key in keys]
00122 
00123         ## Uniqifies a list to not have double entries
00124         def uniqify_list(self,seq, idfun=None):
00125                 # order preserving
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                         # in old Python versions:
00133                         # if seen.has_key(marker)
00134                         # but in new ones:
00135                         if marker in seen: continue
00136                         seen[marker] = 1
00137                         result.append(item)
00138                 return result


cob_command_gui
Author(s): Florian Weisshardt
autogenerated on Sun Jun 9 2019 20:20:15