tactile_data_gui.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #
00003 #
00004 #
00005 #
00006 #
00007 #
00008 #
00009 #
00010 
00011 import roslib; roslib.load_manifest('cob_tactiletools')
00012 import rospy
00013 from schunk_sdh.msg import TactileMatrix, TactileSensor
00014 import pygtk
00015 pygtk.require('2.0')
00016 import gtk, gobject, cairo
00017 import threading
00018 import time
00019 
00020 #Initializing the gtk's thread engine
00021 gtk.gdk.threads_init()
00022 
00023 class Screen(gtk.DrawingArea):
00024 
00025     # Draw in response to an expose-event
00026     __gsignals__ = { "expose-event": "override" }
00027     sizex = 3
00028     sizey = 1
00029     def __init__(self):
00030         gtk.DrawingArea.__init__(self)
00031         self.tactile_array = (0,0,0)
00032 
00033     # Handle the expose-event by drawing
00034     def do_expose_event(self, event):
00035         # Create the cairo context
00036         cr = self.window.cairo_create()
00037         #print "Width: ", self.allocation.width
00038         #print "Height: ", self.allocation.height
00039         # Restrict Cairo to the exposed area; avoid extra work
00040         cr.rectangle(event.area.x, event.area.y, event.area.width, event.area.height)
00041         cr.clip()
00042         self.draw(cr, self.allocation.width,self.allocation.height )
00043 
00044     def getColorCode(self, value):
00045         if(value < 0):
00046             #schwarz
00047             return [1,1,1]
00048         if(value > 4000):
00049             #rot
00050             return [1,0,0]
00051         elif (value > 3429):
00052             #orange
00053             return [248./256,171./256,21./256]
00054         elif (value > 2857):
00055             #gelb
00056             return [248./256,245./256,21./256]
00057         elif (value > 2286):
00058             #gruen
00059             return [1./256,181./256,22./256.]
00060         elif (value > 1714):
00061             #gruen hell
00062             return [118./256,255./256,86./256]
00063         elif (value > 1143):
00064             #tuerkis
00065             return [85./256.,249./256.,251./256.]
00066         elif (value > 571):
00067             #blauig
00068             return [39./256.,126./256.,167./256.]
00069         else:
00070             #blau
00071             return [1./256.,7./256.,255./256.]
00072 
00073     def draw(self, cr, width, height):
00074         # Fill the background with gray
00075         color = 0.5
00076         xw = width/(self.sizex)
00077         yw = height/(self.sizey)
00078         #print "SIZE+++++ ", len(self.tactile_array)
00079         for j in range(0,self.sizey):
00080             for i in range(0,self.sizex):
00081                 #print (i+1)*(j+1)
00082                 [colorr, colorg, colorb] = self.getColorCode(self.tactile_array[self.sizex*j+i])
00083                 #print colorr, colorg, colorb
00084                 cr.set_source_rgb(colorr, colorg, colorb)
00085                 cr.rectangle((i)*xw, (j)*yw, xw, yw)
00086                 cr.fill()
00087 
00088     def setMatrixSize(self, matrixx, matrixy):
00089         self.sizex = matrixx
00090         self.sizey = matrixy
00091 
00092     def updateTactileMatrix(self, array):
00093         #print "Got something: ", array
00094         self.tactile_array = array
00095         self.queue_draw()
00096 
00097 
00098             
00099 def roscb(data):
00100     global sc1
00101     global sc2
00102     global sc3
00103     global sc4
00104     global sc5
00105     global sc6
00106     global testv
00107     matrices = data.tactile_matrix
00108     gtk.gdk.threads_enter()
00109     for mat in matrices:
00110         if(mat.matrix_id == 1):
00111             sc1.setMatrixSize(mat.cells_x,mat.cells_y)
00112             sc1.updateTactileMatrix(mat.tactile_array)
00113         if(mat.matrix_id == 3):
00114             sc2.setMatrixSize(mat.cells_x,mat.cells_y)
00115             sc2.updateTactileMatrix(mat.tactile_array)
00116         if(mat.matrix_id == 5):
00117             sc3.setMatrixSize(mat.cells_x,mat.cells_y)
00118             sc3.updateTactileMatrix(mat.tactile_array)
00119         if(mat.matrix_id == 0):
00120             sc4.setMatrixSize(mat.cells_x,mat.cells_y)
00121             sc4.updateTactileMatrix(mat.tactile_array)
00122         if(mat.matrix_id == 2):
00123             sc5.setMatrixSize(mat.cells_x,mat.cells_y)
00124             sc5.updateTactileMatrix(mat.tactile_array)
00125         if(mat.matrix_id == 4):
00126             sc6.setMatrixSize(mat.cells_x,mat.cells_y)
00127             sc6.updateTactileMatrix(mat.tactile_array)
00128     gtk.gdk.threads_leave()
00129 
00130 
00131 # GTK mumbo-jumbo to show the widget in a window and quit when it's closed
00132 def main_quit(obh, obb):
00133     #Stopping the thread and the gtk's main loop
00134     gtk.main_quit()
00135 
00136 try:
00137     window = gtk.Window()
00138     winwidth = 200
00139     winheight = 300
00140     window.set_size_request(winwidth,winheight)
00141     window.set_title("TactileSensorData")
00142     testv = 0
00143     sc1 = Screen()
00144     sc1.set_size_request((winwidth/3)-10, (winheight/2)-5)
00145     sc2 = Screen()
00146     sc2.set_size_request((winwidth/3)-10, (winheight/2)-5)
00147     sc3 = Screen()
00148     sc3.set_size_request((winwidth/3)-10, (winheight/2)-5)
00149     sc4 = Screen()
00150     sc4.set_size_request((winwidth/3)-10, (winheight/2)-5)
00151     sc5 = Screen()
00152     sc5.set_size_request((winwidth/3)-10, (winheight/2)-5)
00153     sc6 = Screen()
00154     sc6.set_size_request((winwidth/3)-10, (winheight/2)-5)
00155     hbox1=gtk.HBox(False ,1 )
00156     hbox1.pack_start(sc1,False, True, 0)
00157     hbox1.pack_start(sc2,False, True, 0)
00158     hbox1.pack_start(sc3,False, True, 0)
00159     hbox2=gtk.HBox(False,1)
00160     hbox2.pack_start(sc4,False, True, 0)
00161     hbox2.pack_start(sc5,False, True, 0)
00162     hbox2.pack_start(sc6,False, True, 0)
00163     vbox=gtk.VBox(True,0)
00164     vbox.pack_start(hbox1,False, False, 0)
00165     vbox.pack_start(hbox2,False, False, 0)
00166     window.add(vbox)
00167     window.show_all()
00168     window.connect("delete-event", main_quit)
00169     window.present()
00170     #ROS Subscribes
00171     rospy.init_node('TactileSensorView', anonymous=True)
00172     rospy.Subscriber("/sdh_controller/tactile_data", TactileSensor, roscb)
00173     gtk.main()
00174 except KeyboardInterrupt:
00175     main_quit("tut", "tut")


cob_tactiletools
Author(s): Alexander Bubeck
autogenerated on Wed Aug 26 2015 11:01:17