command_gui_buttons.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 import rospy
19 from simple_script_server import *
20 
21 ## Implements configurable buttons
23  def __init__(self):
25  self.panels = []
26  self.stop_buttons = []
27  self.init_buttons = []
28  self.recover_buttons = []
29  self.halt_buttons = []
30  self.CreateControlPanel()
31 
32  ## Creates the control panel out of configuration from ROS parameter server
33  def CreateControlPanel(self):
34  param_prefix = "~control_buttons"
35  if not rospy.has_param(param_prefix):
36  rospy.logerr("parameter %s does not exist on ROS Parameter Server, aborting...",param_prefix)
37  return False
38  group_param = rospy.get_param(param_prefix)
39  #print group_param
40  group_param = self.SortDict(group_param)
41  #print group_param
42 
43  for group in group_param:
44  group_name = group[1]["group_name"]
45  component_name = group[1]["component_name"] # \todo check component name with robot_components.yaml files
46  button_list = group[1]["buttons"]
47  buttons = []
48  for button in button_list:
49  #print "button = ",button
50  if button[1] == "move":
51  buttons.append(self.CreateButton(button[0],self.sss.move,component_name,button[2]))
52  elif button[1] == "move_base_rel":
53  buttons.append(self.CreateButton(button[0],self.sss.move_base_rel,component_name,button[2]))
54  elif button[1] == "trigger_action":
55  buttons.append(self.CreateButton(button[0],self.sss.trigger_action,component_name,button[2],True))
56  elif button[1] == "string_action":
57  buttons.append(self.CreateButton(button[0],self.sss.string_action,component_name,button[2],True))
58  elif button[1] == "trigger":
59  buttons.append(self.CreateButton(button[0],self.sss.trigger,component_name,button[2]))
60  if button[2] == "stop":
61  self.stop_buttons.append(component_name)
62  if button[2] == "init":
63  self.init_buttons.append(component_name)
64  if button[2] == "recover":
65  self.recover_buttons.append(component_name)
66  if button[2] == "halt":
67  self.halt_buttons.append(component_name)
68  elif button[1] == "stop":
69  buttons.append(self.CreateButton(button[0],self.sss.stop,component_name))
70  self.stop_buttons.append(component_name)
71  elif button[1] == "init":
72  buttons.append(self.CreateButton(button[0],self.sss.init,component_name))
73  self.init_buttons.append(component_name)
74  elif button[1] == "recover":
75  buttons.append(self.CreateButton(button[0],self.sss.recover,component_name))
76  self.recover_buttons.append(component_name)
77  elif button[1] == "halt":
78  buttons.append(self.CreateButton(button[0],self.sss.halt,component_name))
79  self.halt_buttons.append(component_name)
80  else:
81  rospy.logerr("Function <<%s>> not known to command gui",button[1])
82  return False
83  group = (group_name,buttons)
84 
85  # add nav buttons (optional)
86  if component_name == "base": # \todo get base name from robot_components.yaml
87  param_prefix = "~nav_buttons"
88  if rospy.has_param(param_prefix):
89  nav_buttons_param = rospy.get_param(param_prefix)
90  nav_button_list = nav_buttons_param["buttons"]
91  #print nav_button_list
92  for button in nav_button_list:
93  #print "button = ",button
94  buttons.append(self.CreateButton(button[0],self.sss.move,component_name,button[2]))
95  else:
96  rospy.logwarn("parameter %s does not exist on ROS Parameter Server, no nav buttons will be available.",param_prefix)
97  self.panels.append(group)
98 
99  # uniqify lists to not have double entries
100  self.stop_buttons = self.uniqify_list(self.stop_buttons)
101  self.init_buttons = self.uniqify_list(self.init_buttons)
103  self.halt_buttons = self.uniqify_list(self.halt_buttons)
104 
105 
106  ## Creates one button with functionality
107  def CreateButton(self,button_name,function,component_name,parameter_name=None,blocking=False):
108  if parameter_name == None:
109  button = (button_name,function,(component_name,blocking))
110  else:
111  button = (button_name,function,(component_name,parameter_name,blocking))
112  return button
113 
114  ## Sorts a dictionary alphabetically
115  def SortDict(self,dictionary):
116  keys = sorted(dictionary.keys())
117  k=[]
118  #print "keys = ", keys
119  #for key in keys:
120  # print "values = ", dictionary[key]
121  return [[key,dictionary[key]] for key in keys]
122 
123  def idfun(self,x):
124  return x
125 
126  ## Uniqifies a list to not have double entries
127  def uniqify_list(self,seq, idfun=None):
128  # order preserving
129  if idfun is None:
130  idfun=self.idfun
131  seen = {}
132  result = []
133  for item in seq:
134  marker = idfun(item)
135  # in old Python versions:
136  # if seen.has_key(marker)
137  # but in new ones:
138  if marker in seen: continue
139  seen[marker] = 1
140  result.append(item)
141  return result
def SortDict(self, dictionary)
Sorts a dictionary alphabetically.
def CreateControlPanel(self)
Creates the control panel out of configuration from ROS parameter server.
def uniqify_list(self, seq, idfun=None)
Uniqifies a list to not have double entries.
def CreateButton(self, button_name, function, component_name, parameter_name=None, blocking=False)
Creates one button with functionality.


cob_command_gui
Author(s): Florian Weisshardt
autogenerated on Wed Apr 7 2021 03:03:08