buttons.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #################################################################
00003 ##\file
00004 #
00005 # \note
00006 #   Copyright (c) 2010 \n
00007 #   Fraunhofer Institute for Manufacturing Engineering
00008 #   and Automation (IPA) \n\n
00009 #
00010 #################################################################
00011 #
00012 # \note
00013 #   Project name: care-o-bot
00014 # \note
00015 #   ROS stack name: cob_apps
00016 # \note
00017 #   ROS package name: cob_command_gui
00018 #
00019 # \author
00020 #   Author: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de
00021 # \author
00022 #   Supervised by: Florian Weisshardt, email:florian.weisshardt@ipa.fhg.de
00023 #
00024 # \date Date of creation: Aug 2010
00025 #
00026 # \brief
00027 #   Implementation of ROS node for command gui.
00028 #
00029 #################################################################
00030 #
00031 # Redistribution and use in source and binary forms, with or without
00032 # modification, are permitted provided that the following conditions are met:
00033 #
00034 #     - Redistributions of source code must retain the above copyright
00035 #       notice, this list of conditions and the following disclaimer. \n
00036 #     - Redistributions in binary form must reproduce the above copyright
00037 #       notice, this list of conditions and the following disclaimer in the
00038 #       documentation and/or other materials provided with the distribution. \n
00039 #     - Neither the name of the Fraunhofer Institute for Manufacturing
00040 #       Engineering and Automation (IPA) nor the names of its
00041 #       contributors may be used to endorse or promote products derived from
00042 #       this software without specific prior written permission. \n
00043 #
00044 # This program is free software: you can redistribute it and/or modify
00045 # it under the terms of the GNU Lesser General Public License LGPL as 
00046 # published by the Free Software Foundation, either version 3 of the 
00047 # License, or (at your option) any later version.
00048 # 
00049 # This program is distributed in the hope that it will be useful,
00050 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00051 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00052 # GNU Lesser General Public License LGPL for more details.
00053 # 
00054 # You should have received a copy of the GNU Lesser General Public 
00055 # License LGPL along with this program. 
00056 # If not, see <http://www.gnu.org/licenses/>.
00057 #
00058 #################################################################
00059 
00060 import rospy
00061 from simple_script_server import *
00062 
00063 ## Implements configurable buttons
00064 class buttons:
00065         def __init__(self):
00066                 self.sss = simple_script_server()
00067                 self.panels = []
00068                 self.stop_buttons = []
00069                 self.init_buttons = []
00070                 self.recover_buttons = []
00071                 self.CreateControlPanel()
00072 
00073         ## Creates the control panel out of configuration from ROS parameter server
00074         def CreateControlPanel(self):
00075                 param_prefix = "~control_buttons"
00076                 if not rospy.has_param(param_prefix):
00077                         rospy.logerr("parameter %s does not exist on ROS Parameter Server, aborting...",param_prefix)
00078                         return False
00079                 group_param = rospy.get_param(param_prefix)
00080                 #print group_param
00081                 group_param = self.SortDict(group_param)
00082                 #print group_param
00083                 
00084                 for group in group_param:
00085                         group_name = group[1]["group_name"]
00086                         component_name = group[1]["component_name"] # \todo check component name with robot_components.yaml files
00087                         button_list = group[1]["buttons"]
00088                         buttons = []
00089                         for button in button_list:
00090                                 #print "button = ",button
00091                                 if button[1] == "move":
00092                                         buttons.append(self.CreateButton(button[0],self.sss.move,component_name,button[2]))
00093                                 elif button[1] == "move_base_rel":
00094                                         buttons.append(self.CreateButton(button[0],self.sss.move_base_rel,component_name,button[2]))
00095                                 elif button[1] == "trigger":
00096                                         buttons.append(self.CreateButton(button[0],self.sss.trigger,component_name,button[2]))
00097                                         if button[2] == "stop":
00098                                                 self.stop_buttons.append(component_name)
00099                                         if button[2] == "init":
00100                                                 self.init_buttons.append(component_name)
00101                                         if button[2] == "recover":
00102                                                 self.recover_buttons.append(component_name)
00103                                 elif button[1] == "stop":
00104                                         buttons.append(self.CreateButton(button[0],self.sss.stop,component_name,button[2]))
00105                                         self.stop_buttons.append(component_name)
00106                                 elif button[1] == "mode":
00107                                         buttons.append(self.CreateButton(button[0],self.sss.set_operation_mode,component_name,button[2]))
00108                                 else:
00109                                         rospy.logerr("Function <<%s>> not known to command gui",button[1])
00110                                         return False
00111                         group = (group_name,buttons)
00112                         
00113                         # add nav buttons (optional)
00114                         if component_name == "base": # \todo get base name from robot_components.yaml
00115                                 param_prefix = "~nav_buttons"
00116                                 if rospy.has_param(param_prefix):
00117                                         nav_buttons_param = rospy.get_param(param_prefix)
00118                                         nav_button_list = nav_buttons_param["buttons"]
00119                                         #print nav_button_list
00120                                         for button in nav_button_list:
00121                                                 #print "button = ",button
00122                                                 buttons.append(self.CreateButton(button[0],self.sss.move,component_name,button[2]))
00123                                 else:
00124                                         rospy.logwarn("parameter %s does not exist on ROS Parameter Server, no nav buttons will be available.",param_prefix)
00125                         self.panels.append(group)
00126                 
00127                 # uniqify lists to not have double entries
00128                 self.stop_buttons = self.uniqify_list(self.stop_buttons)
00129                 self.init_buttons = self.uniqify_list(self.init_buttons)
00130                 self.recover_buttons = self.uniqify_list(self.recover_buttons)
00131                 
00132         
00133         ## Creates one button with functionality
00134         def CreateButton(self,button_name,function,component_name,parameter_name):
00135                 button = (button_name,function,(component_name,parameter_name,False))
00136                 return button
00137         
00138         ## Sorts a dictionary alphabetically
00139         def SortDict(self,dictionary):
00140                 keys = sorted(dictionary.iterkeys())
00141                 k=[]
00142                 #print "keys = ", keys
00143                 #for key in keys:
00144                 #       print "values = ", dictionary[key]
00145                 return [[key,dictionary[key]] for key in keys]
00146                 
00147         ## Uniqifies a list to not have double entries
00148         def uniqify_list(self,seq, idfun=None): 
00149                 # order preserving
00150                 if idfun is None:
00151                         def idfun(x): return x
00152                 seen = {}
00153                 result = []
00154                 for item in seq:
00155                         marker = idfun(item)
00156                         # in old Python versions:
00157                         # if seen.has_key(marker)
00158                         # but in new ones:
00159                         if marker in seen: continue
00160                         seen[marker] = 1
00161                         result.append(item)
00162                 return result


cob_command_gui
Author(s): Florian Weisshardt
autogenerated on Sun Oct 5 2014 23:01:03