knoeppkes.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 thread
00019 import pygtk
00020 pygtk.require('2.0')
00021 import gtk
00022 import os
00023 import sys
00024 import signal
00025 
00026 import rospy
00027 import roslib
00028 from cob_msgs.msg import EmergencyStopState
00029 from simple_script_server import *
00030 from command_gui_buttons import *
00031 
00032 planning_enabled = False
00033 base_diff_enabled = False
00034 confirm_commands_enabled = True
00035 
00036 initialized = False
00037 
00038 #Initializing the gtk's thread engine
00039 #gtk.gdk.threads_init()
00040 
00041 ## Executes a button click in a new thread
00042 def start(func, args):
00043   global planning_enabled
00044   global base_diff_enabled
00045   global confirm_commands_enabled
00046   execute_command = True
00047 
00048   largs = list(args)
00049   if confirm_commands_enabled and ((func.__name__ != "stop") and (largs[1] != 'stop')):
00050     confirm_dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, "Execute Command?")
00051     if confirm_dialog.run() == gtk.RESPONSE_NO:
00052       execute_command = False
00053     confirm_dialog.destroy()
00054   if execute_command:
00055     if(largs[0] == "arm"):
00056       if(planning_enabled):
00057         largs.append("planned")
00058     if(largs[0] == "base"):
00059       if(base_diff_enabled):
00060         largs.append("diff")
00061     #print("Args", tuple(largs))
00062     #print("func ", func)
00063     thread.start_new_thread(func,tuple(largs))  # exits silently without evaluating result
00064 
00065 ## use this function in order to evaluate result of action_handle, i.e. show pop-up or similar
00066 def call_thread(func,args):
00067   result = func(*args)
00068   if not result.success:    # action_handle returns with failure
00069     result_dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR,
00070                                                gtk.BUTTONS_OK,
00071                                                "Executing " + func.__name__ + "(" + args[0] + ") failed!")
00072     gtk.gdk.threads_enter()
00073     result_dialog.format_secondary_text(result.message)
00074     gtk.gdk.threads_leave()
00075     gtk.gdk.threads_enter()
00076     result_dialog.run()
00077     gtk.gdk.threads_leave()
00078     gtk.gdk.threads_enter()
00079     result_dialog.destroy()
00080     gtk.gdk.threads_leave()
00081 
00082 def startGTK(widget, data):
00083   data()
00084 
00085 ## Class for general gtk panel implementation
00086 class GtkGeneralPanel(gtk.Frame):
00087   def __init__(self,buttons):
00088     global initialized
00089     self.sss = simple_script_server()
00090     gtk.Frame.__init__(self)
00091     self.em_stop = False
00092     self.set_label("general")
00093     self.set_shadow_type(gtk.SHADOW_IN)
00094     self.vbox = gtk.VBox(False, 0)
00095     self.add(self.vbox)
00096     #hbox=gtk.HBox(True, 0)
00097     #image = gtk.Image()
00098     #image.set_from_file(roslib.packages.get_pkg_dir("cob_command_gui") + "/common/files/icons/batti-040.png")
00099     #hbox.pack_start(image, False, False, 0)
00100     #label = gtk.Label("40 %")
00101     #hbox.pack_start(label, False, False, 0)
00102     #self.vbox.pack_start(hbox, False, False, 5)
00103     hbox=gtk.HBox(True, 0)
00104     self.status_image = gtk.Image()
00105     #self.status_image.set_from_file(roslib.packages.get_pkg_dir("cob_command_gui") + "/common/files/icons/weather-clear.png")
00106     hbox.pack_start(self.status_image, False, False, 0)
00107     self.status_label = gtk.Label("Status OK")
00108     hbox.pack_start(self.status_label, False, False, 0)
00109     self.vbox.pack_start(hbox, False, False, 5)
00110 
00111     butstop = gtk.Button("Stop all")
00112     butstop.connect("clicked", lambda w: self.stop_all(buttons.stop_buttons))
00113     self.vbox.pack_start(butstop, False, False, 5)
00114 
00115     butinit = gtk.Button("Init all")
00116     butinit.connect("clicked", lambda w: self.init_all(buttons.init_buttons))
00117     self.vbox.pack_start(butinit, False, False, 5)
00118 
00119     butrec = gtk.Button("Recover all")
00120     butrec.connect("clicked", lambda w: self.recover_all(buttons.recover_buttons))
00121     self.vbox.pack_start(butrec, False, False, 5)
00122 
00123     butrec = gtk.Button("Halt all")
00124     butrec.connect("clicked", lambda w: self.halt_all(buttons.halt_buttons))
00125     self.vbox.pack_start(butrec, False, False, 5)
00126 
00127     plan_check = gtk.CheckButton("Planning")#
00128     plan_check.connect("toggled", self.planned_toggle)
00129     self.vbox.pack_start(plan_check, False, False, 5)
00130 
00131     base_mode_check = gtk.CheckButton("Base Diff")
00132     base_mode_check.connect("toggled", self.base_mode_toggle)
00133     self.vbox.pack_start(base_mode_check, False, False, 5)
00134 
00135     confirm_com_check = gtk.CheckButton("Confirm Commands")
00136     confirm_com_check.set_active(confirm_commands_enabled)
00137     confirm_com_check.connect("toggled", self.confirm_com_toggle)
00138     self.vbox.pack_start(confirm_com_check, False, False, 5)
00139 
00140     but = gtk.Button(stock=gtk.STOCK_QUIT )
00141     but.connect("clicked", lambda w: gtk.main_quit())
00142     self.vbox.pack_start(but, False, False, 5)
00143     initialized = True
00144 
00145   def stop_all(self,component_names):
00146     for component_name in component_names:
00147       self.sss.stop(component_name,blocking=False)
00148 
00149   def init_all(self,component_names):
00150     for component_name in component_names:
00151       self.sss.init(component_name,False)
00152 
00153   def recover_all(self,component_names):
00154     for component_name in component_names:
00155       self.sss.recover(component_name,False)
00156 
00157   def halt_all(self,component_names):
00158     for component_name in component_names:
00159       self.sss.halt(component_name,False)
00160 
00161 
00162   def setEMStop(self, em):
00163     if(em):
00164       #print("Emergency Stop Active")
00165       gtk.threads_enter()
00166       self.status_image.set_from_file(roslib.packages.get_pkg_dir("cob_command_gui") + "/common/files/icons/error.png")
00167       self.status_label.set_text("EM Stop !")
00168       gtk.threads_leave()
00169       if(self.em_stop == False):
00170         self.em_stop = True
00171     else:
00172       #print("Status OK")
00173       self.status_image.set_from_file(roslib.packages.get_pkg_dir("cob_command_gui") + "/common/files/icons/ok.png")
00174       gtk.threads_enter()
00175       self.status_label.set_text("Status OK")
00176       gtk.threads_leave()
00177       if(self.em_stop == True):
00178         self.em_stop = False
00179 
00180   def planned_toggle(self, b):
00181     global planning_enabled
00182     if(planning_enabled):
00183       planning_enabled = False
00184     else:
00185       planning_enabled = True
00186 
00187   def base_mode_toggle(self, b):
00188     global base_diff_enabled
00189     if(base_diff_enabled):
00190       base_diff_enabled = False
00191     else:
00192       base_diff_enabled = True
00193 
00194   def confirm_com_toggle(self, b):
00195     global confirm_commands_enabled
00196     if(confirm_commands_enabled):
00197       confirm_commands_enabled = False
00198     else:
00199       confirm_commands_enabled = True
00200 
00201 ## Class for gtk panel implementation
00202 class GtkPanel(gtk.Frame):
00203   def __init__(self, master=None, labeltext=""):
00204     gtk.Frame.__init__(self)
00205     self.set_label(labeltext)
00206     self.set_shadow_type(gtk.SHADOW_IN)
00207     self.vbox = gtk.VBox(False, 0)
00208     self.add(self.vbox)
00209 
00210   def addButton(self, text, command):
00211     but = gtk.Button(text)
00212     but.connect("clicked", startGTK, command)
00213     #but.set_size_request(120,-1)
00214     self.vbox.pack_start(but, False, False, 5)
00215 
00216 ## Implementation of knoeppkes command gui
00217 class Knoeppkes():
00218   def delete_event(self, widget, event, data=None):
00219     gtk.main_quit()
00220     return False
00221 
00222   def emcb(self, msg):
00223     global initialized
00224     if(initialized):
00225         self.gpanel.setEMStop(msg.emergency_state)
00226 
00227   def __init__(self):
00228     # init ros node
00229     rospy.init_node('cob_knoeppkes')
00230     rospy.Subscriber("/emergency_stop_state", EmergencyStopState, self.emcb)
00231 
00232     self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
00233     self.window.connect("delete_event", self.delete_event)
00234     self.window.set_title("cob command gui")
00235     self.window.set_size_request(1000, 500)
00236     vbox = gtk.VBox(False, 1)
00237     self.hbox = gtk.HBox(True, 10)
00238     vbox.pack_start(self.hbox, True, True, 0)
00239     b = command_gui_buttons()
00240     self.gpanel = GtkGeneralPanel(b)
00241     self.hbox.pack_start(self.gpanel,True, True, 3)
00242     panels = b.panels
00243     for pname, actions in panels:
00244       panel = GtkPanel(self, pname)
00245       for aname, func, args in actions:
00246         panel.addButton(text=aname, command=lambda f=func, a=args: start(f, a))
00247       self.hbox.pack_start(panel,True, True, 3)
00248 
00249     self.status_bar = gtk.Statusbar()
00250     context_id = self.status_bar.get_context_id("Statusbar")
00251     string = "Connected to $ROS_MASTER_URI=" + os.environ.get("ROS_MASTER_URI")
00252     self.status_bar.push(context_id, string)
00253     vbox.pack_start(self.status_bar, False, False, 0)
00254     self.window.add(vbox)
00255     self.window.show_all()
00256     gtk.gdk.threads_init()
00257     gtk.gdk.threads_enter()
00258     gtk.main()
00259     gtk.gdk.threads_leave()
00260 
00261 def signal_handler(signal, frame):
00262   #print("You pressed Ctrl+C!")
00263   gtk.main_quit()
00264 
00265 if __name__ == "__main__":
00266   signal.signal(signal.SIGINT, signal_handler)
00267   app = Knoeppkes()
00268 


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