dispatcher_gui.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002     
00003 # yliu Jul 27, 2011
00004   
00005 import roslib; roslib.load_manifest('camera_pose_toolkits')
00006 import rospy
00007 import rosservice
00008 import socket
00009 import wx
00010 from camera_pose_toolkits.srv import *
00011 from std_msgs.msg import String
00012 from calibration_msgs.msg import *
00013 
00014 
00015 class MainWindow(wx.Frame):
00016     def __init__(self, output_ns):
00017         
00018         # ..._dispatcher_gui on ... ?
00019     
00020    
00021         title = '[' + output_ns + ']' + ' dispatcher gui on %s' % socket.gethostname()
00022         self.output_ns = output_ns
00023 
00024         wx.Frame.__init__(self, None, wx.ID_ANY, title, pos=(200, 200), size=(400, 100))
00025 
00026         # Create menu
00027         self.menubar = wx.MenuBar()
00028         self.filemenu = wx.Menu()
00029         self.filemenu.Append(wx.ID_EXIT, 'E&xit', 'Exit the program')
00030         wx.EVT_MENU(self, wx.ID_EXIT, self.on_exit)
00031         self.menubar.Append(self.filemenu, '&File')
00032         self.SetMenuBar(self.menubar)
00033 
00034         camera_ns_list = ['zero', 'one', 'two', 'three', 'four', 'five','six', 'seven', 'eight']
00035         self.combobox = wx.ComboBox(self, wx.ID_ANY, choices=camera_ns_list, style=wx.CB_READONLY)
00036 
00037         self.statusbar = self.CreateStatusBar()
00038         self.statusbar.SetStatusText('[' + self.output_ns + ']' +' now relays:  ')
00039         #self.sub = None 
00040         self.sub = rospy.Subscriber(self.output_ns+"/selected", String, self.cb_func)
00041         self.features_sub = rospy.Subscriber(self.output_ns+'/features', CalibrationPattern, self.features_cb)
00042         self.cb_in_sight = 0
00043 
00044         self.Bind(wx.EVT_COMBOBOX, self.on_combobox)
00045 
00046         self.sizer = wx.BoxSizer(wx.VERTICAL)
00047         self.sizer.Add(self.combobox, 0, wx.EXPAND)
00048         self.SetSizer(self.sizer)
00049 
00050 
00051         # Refresh camera_ns list periodically
00052         self.timer1 = wx.Timer(self, wx.ID_ANY)
00053         self.Bind(wx.EVT_TIMER, self.on_timer1, self.timer1)
00054         self.timer1.Start(500, False)
00055         # default is oneShot=False, timer keeps restarting
00056 
00057         # Check every 1s if node has shutdown (and close GUI)
00058         self.timer2 = wx.Timer(self, wx.ID_ANY)
00059         self.Bind(wx.EVT_TIMER, self.on_timer2, self.timer2)
00060         self.timer2.Start(1000, False)
00061         
00062         self.selected_cam_ns = ""
00063 
00064 
00065 
00066     def cb_func(self, msg):
00067         self.selected_cam_ns = msg.data
00068 
00069     def features_cb(self, msg):
00070         self.cb_in_sight = msg.success
00071 
00072     def on_timer2(self, event):
00073         #print 'time out 2'
00074         if rospy.is_shutdown():
00075             self.Close(True)
00076             self.Refresh()
00077 
00078     def on_timer1(self, event):
00079         #print 'time out 1'
00080         #self.camera_ns_list = [  s[:-len('/image_rect')]  for s in rosservice.get_service_list() if s.endswith('/image_rect')] 
00081         topic_list=[ tn_tt[0]  for tn_tt in rospy.client.get_published_topics()]  #  [topic name, topic type]
00082         self.camera_ns_list = [ tn[:-len('/image_rect')] for tn in topic_list if tn.endswith('/image_rect')]
00083         self.camera_ns_list = [ ns for ns in self.camera_ns_list if not ( ns.startswith(self.output_ns) or ns.startswith('/'+self.output_ns) ) ]
00084         self.combobox.SetItems(self.camera_ns_list)
00085 
00086         for cam_ns in self.camera_ns_list:
00087             if cam_ns == self.selected_cam_ns:
00088                 #wx.CallAfter(self.statusbar.SetStatusText,  'current camera: ' + msg.data)
00089                 self.statusbar.SetStatusText('[' + self.output_ns + ']' + ' now relays:  ' + self.selected_cam_ns)
00090                 break
00091         else: 
00092             self.statusbar.SetStatusText('[' + self.output_ns + ']' + ' now relays:  ')        
00093             #wx.CallAfter(self.statusbar.SetStatusText, 'current camera: ')
00094        
00095         #print self.combobox.GetValue()
00096         print self.camera_ns_list
00097         print self.output_ns
00098         print self.selected_cam_ns
00099         
00100 
00101     def on_timer(self, event):
00102         if rospy.is_shutdown():
00103             self.Close(True)
00104             self.Refresh()
00105 
00106     def on_exit(self, e):
00107         self.Close(True)
00108         self.Refresh()
00109 
00110     def on_error(self):
00111         self.Raise()
00112 
00113     def on_combobox(self, event):
00114         print 'combobox event'
00115 
00116         if self.cb_in_sight == 0 :
00117             print str(self.combobox.GetValue())
00118             #print rosservice.get_service_list()
00119             for s in rosservice.get_service_list(): 
00120                 if s.endswith(self.output_ns+ '/switch'): #'camera_dispatcher/switch'
00121                     switch_cam = rospy.ServiceProxy(self.output_ns+ '/switch', Switch) # 'camera_dispatcher/switch' | camera_pose_toolkits.srv.Switch
00122                     resp = switch_cam ( str(self.combobox.GetValue()))
00123                     break
00124             else:
00125                 wx.MessageBox('Service not ready. Try again later.', 'Info!')
00126         else:
00127             wx.MessageBox('Remove checkerboard from '+ self.output_ns +'\' field of view before switching camera', 'Info!') 
00128         
00129         #rospy.wait_for_service('camera_dispatcher/switch')
00130         
00131         
00132         
00133 
00134 
00135 if __name__ == '__main__':
00136     argv = rospy.myargv()
00137     if len(argv) <2:
00138         print 'Usage:  '
00139         exit(1)
00140     
00141     print argv[0]
00142     print argv[1]
00143 
00144 
00145 
00146     app = wx.PySimpleApp(clearSigInt=False)
00147     rospy.init_node('dispatcher_gui', anonymous=True)
00148 
00149     frame = MainWindow(argv[1])
00150     frame.Show()
00151 
00152     print 'dispatcher_gui started'
00153 
00154     try:
00155         app.MainLoop()
00156     except KeyboardInterrupt, e:
00157         pass
00158     print 'exiting'
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


camera_pose_toolkits
Author(s): Yiping Liu
autogenerated on Thu Aug 15 2013 12:02:52