knoeppkes.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 from threading import Thread
19 import gi
20 from gi import pygtkcompat
21 pygtkcompat.enable()
22 pygtkcompat.enable_gtk(version='3.0')
23 gi.require_version('Gdk', '3.0')
24 gi.require_version('Gtk', '3.0')
25 
26 import gtk
27 import os
28 import sys
29 import signal
30 
31 import rospy
32 import roslib
33 from cob_msgs.msg import EmergencyStopState
34 from simple_script_server import *
35 from command_gui_buttons import command_gui_buttons
36 
37 planning_enabled = False
38 base_diff_enabled = False
39 confirm_commands_enabled = True
40 
41 initialized = False
42 
43 #Initializing the gtk's thread engine
44 #gtk.gdk.threads_init()
45 
46 ## Executes a button click in a new thread
47 def start(func, args):
48  global planning_enabled
49  global base_diff_enabled
50  global confirm_commands_enabled
51  execute_command = True
52 
53  largs = list(args)
54  if confirm_commands_enabled and ((func.__name__ != "stop") and (largs[1] != 'stop')):
55  confirm_dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, "Execute Command?")
56  if confirm_dialog.run() == gtk.RESPONSE_NO:
57  execute_command = False
58  confirm_dialog.destroy()
59  if execute_command:
60  if(largs[0] == "arm"):
61  if(planning_enabled):
62  largs.append("planned")
63  if(largs[0] == "base"):
64  if(base_diff_enabled):
65  largs.append("diff")
66  #print("Args", tuple(largs))
67  #print("func ", func)
68  Thread(target=func, args=tuple(largs)).start() # exits silently without evaluating result
69 
70 ## use this function in order to evaluate result of action_handle, i.e. show pop-up or similar
71 def call_thread(func,args):
72  result = func(*args)
73  if not result.success: # action_handle returns with failure
74  result_dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR,
75  gtk.BUTTONS_OK,
76  "Executing " + func.__name__ + "(" + args[0] + ") failed!")
77  gtk.gdk.threads_enter()
78  result_dialog.format_secondary_text(result.message)
79  gtk.gdk.threads_leave()
80  gtk.gdk.threads_enter()
81  result_dialog.run()
82  gtk.gdk.threads_leave()
83  gtk.gdk.threads_enter()
84  result_dialog.destroy()
85  gtk.gdk.threads_leave()
86 
87 def startGTK(widget, data):
88  data()
89 
90 ## Class for general gtk panel implementation
91 class GtkGeneralPanel(gtk.Frame):
92  def __init__(self,buttons):
93  global initialized
95  gtk.Frame.__init__(self)
96  self.em_stop = False
97  self.set_label("general")
98  self.set_shadow_type(gtk.SHADOW_IN)
99  self.vbox = gtk.VBox(False, 0)
100  self.add(self.vbox)
101  #hbox=gtk.HBox(True, 0)
102  #image = gtk.Image()
103  #image.set_from_file(roslib.packages.get_pkg_dir("cob_command_gui") + "/common/files/icons/batti-040.png")
104  #hbox.pack_start(image, False, False, 0)
105  #label = gtk.Label("40 %")
106  #hbox.pack_start(label, False, False, 0)
107  #self.vbox.pack_start(hbox, False, False, 5)
108  hbox=gtk.HBox(True, 0)
109  self.status_image = gtk.Image()
110  #self.status_image.set_from_file(roslib.packages.get_pkg_dir("cob_command_gui") + "/common/files/icons/weather-clear.png")
111  hbox.pack_start(self.status_image, False, False, 0)
112  self.status_label = gtk.Label("Status OK")
113  hbox.pack_start(self.status_label, False, False, 0)
114  self.vbox.pack_start(hbox, False, False, 5)
115 
116  butstop = gtk.Button("Stop all")
117  butstop.connect("clicked", lambda w: self.stop_all(buttons.stop_buttons))
118  self.vbox.pack_start(butstop, False, False, 5)
119 
120  butinit = gtk.Button("Init all")
121  butinit.connect("clicked", lambda w: self.init_all(buttons.init_buttons))
122  self.vbox.pack_start(butinit, False, False, 5)
123 
124  butrec = gtk.Button("Recover all")
125  butrec.connect("clicked", lambda w: self.recover_all(buttons.recover_buttons))
126  self.vbox.pack_start(butrec, False, False, 5)
127 
128  butrec = gtk.Button("Halt all")
129  butrec.connect("clicked", lambda w: self.halt_all(buttons.halt_buttons))
130  self.vbox.pack_start(butrec, False, False, 5)
131 
132  plan_check = gtk.CheckButton("Planning")#
133  plan_check.connect("toggled", self.planned_toggle)
134  self.vbox.pack_start(plan_check, False, False, 5)
135 
136  base_mode_check = gtk.CheckButton("Base Diff")
137  base_mode_check.connect("toggled", self.base_mode_toggle)
138  self.vbox.pack_start(base_mode_check, False, False, 5)
139 
140  confirm_com_check = gtk.CheckButton("Confirm Commands")
141  confirm_com_check.set_active(confirm_commands_enabled)
142  confirm_com_check.connect("toggled", self.confirm_com_toggle)
143  self.vbox.pack_start(confirm_com_check, False, False, 5)
144 
145  but = gtk.Button(stock=gtk.STOCK_QUIT )
146  but.connect("clicked", lambda w: gtk.main_quit())
147  self.vbox.pack_start(but, False, False, 5)
148  initialized = True
149 
150  def stop_all(self,component_names):
151  for component_name in component_names:
152  self.sss.stop(component_name,blocking=False)
153 
154  def init_all(self,component_names):
155  for component_name in component_names:
156  self.sss.init(component_name,False)
157 
158  def recover_all(self,component_names):
159  for component_name in component_names:
160  self.sss.recover(component_name,False)
161 
162  def halt_all(self,component_names):
163  for component_name in component_names:
164  self.sss.halt(component_name,False)
165 
166 
167  def setEMStop(self, em):
168  if(em):
169  #print("Emergency Stop Active")
170  gtk.gdk.threads_enter()
171  self.status_image.set_from_file(roslib.packages.get_pkg_dir("cob_command_gui") + "/common/files/icons/error.png")
172  self.status_label.set_text("EM Stop !")
173  gtk.gdk.threads_leave()
174  if(self.em_stop == False):
175  self.em_stop = True
176  else:
177  #print("Status OK")
178  self.status_image.set_from_file(roslib.packages.get_pkg_dir("cob_command_gui") + "/common/files/icons/ok.png")
179  gtk.gdk.threads_enter()
180  self.status_label.set_text("Status OK")
181  gtk.gdk.threads_leave()
182  if(self.em_stop == True):
183  self.em_stop = False
184 
185  def planned_toggle(self, b):
186  global planning_enabled
187  if(planning_enabled):
188  planning_enabled = False
189  else:
190  planning_enabled = True
191 
192  def base_mode_toggle(self, b):
193  global base_diff_enabled
194  if(base_diff_enabled):
195  base_diff_enabled = False
196  else:
197  base_diff_enabled = True
198 
199  def confirm_com_toggle(self, b):
200  global confirm_commands_enabled
201  if(confirm_commands_enabled):
202  confirm_commands_enabled = False
203  else:
204  confirm_commands_enabled = True
205 
206 ## Class for gtk panel implementation
207 class GtkPanel(gtk.Frame):
208  def __init__(self, master=None, labeltext=""):
209  gtk.Frame.__init__(self)
210  self.set_label(labeltext)
211  self.set_shadow_type(gtk.SHADOW_IN)
212  self.vbox = gtk.VBox(False, 0)
213  self.add(self.vbox)
214 
215  def addButton(self, text, command):
216  but = gtk.Button(text)
217  but.connect("clicked", startGTK, command)
218  #but.set_size_request(120,-1)
219  self.vbox.pack_start(but, False, False, 5)
220 
221 ## Implementation of knoeppkes command gui
222 class Knoeppkes():
223  def delete_event(self, widget, event, data=None):
224  gtk.main_quit()
225  return False
226 
227  def emcb(self, msg):
228  global initialized
229  if(initialized):
230  self.gpanel.setEMStop(msg.emergency_state)
231 
232  def __init__(self):
233  # init ros node
234  rospy.init_node('cob_knoeppkes')
235  rospy.Subscriber("/emergency_stop_state", EmergencyStopState, self.emcb)
236 
237  self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
238  self.window.connect("delete_event", self.delete_event)
239  self.window.set_title("cob command gui")
240  self.window.set_size_request(1000, 500)
241  vbox = gtk.VBox(False, 1)
242  self.hbox = gtk.HBox(True, 10)
243  vbox.pack_start(self.hbox, True, True, 0)
244  b = command_gui_buttons()
246  self.hbox.pack_start(self.gpanel,True, True, 3)
247  panels = b.panels
248  for pname, actions in panels:
249  panel = GtkPanel(self, pname)
250  for aname, func, args in actions:
251  panel.addButton(text=aname, command=lambda f=func, a=args: start(f, a))
252  self.hbox.pack_start(panel,True, True, 3)
253 
254  self.status_bar = gtk.Statusbar()
255  context_id = self.status_bar.get_context_id("Statusbar")
256  string = "Connected to $ROS_MASTER_URI=" + os.environ.get("ROS_MASTER_URI")
257  self.status_bar.push(context_id, string)
258  vbox.pack_start(self.status_bar, False, False, 0)
259  self.window.add(vbox)
260  self.window.show_all()
261  gtk.gdk.threads_init()
262  gtk.gdk.threads_enter()
263  gtk.main()
264  gtk.gdk.threads_leave()
265 
266 def signal_handler(signal, frame):
267  #print("You pressed Ctrl+C!")
268  gtk.main_quit()
269 
270 if __name__ == "__main__":
271  signal.signal(signal.SIGINT, signal_handler)
272  app = Knoeppkes()
273 
def base_mode_toggle(self, b)
Definition: knoeppkes.py:192
def recover_all(self, component_names)
Definition: knoeppkes.py:158
def signal_handler(signal, frame)
Definition: knoeppkes.py:266
def confirm_com_toggle(self, b)
Definition: knoeppkes.py:199
Implementation of knoeppkes command gui.
Definition: knoeppkes.py:222
def __init__(self, buttons)
Definition: knoeppkes.py:92
def halt_all(self, component_names)
Definition: knoeppkes.py:162
def planned_toggle(self, b)
Definition: knoeppkes.py:185
def __init__(self, master=None, labeltext="")
Definition: knoeppkes.py:208
def startGTK(widget, data)
Definition: knoeppkes.py:87
def start(func, args)
Executes a button click in a new thread.
Definition: knoeppkes.py:47
def __init__(self)
Definition: knoeppkes.py:232
Class for gtk panel implementation.
Definition: knoeppkes.py:207
def stop_all(self, component_names)
Definition: knoeppkes.py:150
def setEMStop(self, em)
Definition: knoeppkes.py:167
def call_thread(func, args)
use this function in order to evaluate result of action_handle, i.e.
Definition: knoeppkes.py:71
Class for general gtk panel implementation.
Definition: knoeppkes.py:91
def addButton(self, text, command)
Definition: knoeppkes.py:215
def delete_event(self, widget, event, data=None)
Definition: knoeppkes.py:223
def emcb(self, msg)
Definition: knoeppkes.py:227
def init_all(self, component_names)
Definition: knoeppkes.py:154


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