def __init__( self , buildDir="", motecom=None, tkRoot=None, comm=None ) : #first, import all types, msgs, and enums from the nesc app self.app = nescDecls.nescApp(buildDir, "Oscope") #use the user's comm and tkroot, if they passed it in if tkRoot == None: self.tkRoot = Tk.Tk() else : self.tkRoot = tkRoot if comm == None: comm = Comm() comm.connect( motecom ) #defaults to MOTECOM env variable if undefined self.comm = comm self.initializeGui() #create a queue for receiving oscope messages and register it for messages oscopeMsgQueue = MessageQueue(1) self.comm.register( deepcopy(self.app.msgs.OscopeMsg) , oscopeMsgQueue ) #start a thread to process the messages (make daemon so it dies when tk is killed) msgThread = threading.Thread(target=self.processMessages, args=(oscopeMsgQueue,)) msgThread.setDaemon(True) msgThread.start() # start the GUI thread if we own it if tkRoot == None: self.tkRoot.mainloop() print "Oscope.py exited normally"
def __init__(self, buildDir="", motecom=None, tkRoot=None, comm=None): #first, import all types, msgs, and enums from the nesc app self.app = nescDecls.nescApp(buildDir, "Oscope") #use the user's comm and tkroot, if they passed it in if tkRoot == None: self.tkRoot = Tk.Tk() else: self.tkRoot = tkRoot if comm == None: comm = Comm() comm.connect( motecom) #defaults to MOTECOM env variable if undefined self.comm = comm self.initializeGui() #create a queue for receiving oscope messages and register it for messages oscopeMsgQueue = MessageQueue(1) self.comm.register(deepcopy(self.app.msgs.OscopeMsg), oscopeMsgQueue) #start a thread to process the messages (make daemon so it dies when tk is killed) msgThread = threading.Thread(target=self.processMessages, args=(oscopeMsgQueue, )) msgThread.setDaemon(True) msgThread.start() # start the GUI thread if we own it if tkRoot == None: self.tkRoot.mainloop() print "Oscope.py exited normally"
def __init__( self , moteComStr ) : self.__msgQueue = MessageQueue( 5 ) self.__nextMessageType = None # connect to the mote self.__moteComStr = moteComStr self.__comm = Comm() self.__comm.connect( self.__moteComStr ) # When receiving these messages, queue them up self.__comm.register( msgs[ "MotorState" ] , self.__msgQueue ) self.__comm.register( msgs[ "MotorTrim" ] , self.__msgQueue ) self.__comm.register( msgs[ "MotorMovement" ] , self.__msgQueue )
def __init__( self , motecom=None, tkRoot=None, comm=None ) : #use the user's tk root, if they passed it in if tkRoot == None: self.tkRoot = Tk.Tk() else: self.tkRoot = tkRoot #use the user's comm, if they passed it in if comm != None: self.comm = comm #if no comm passed, create one else: # connect using motecom first, then MOTECOM environment var self.comm = Comm() if motecom != None : self.comm.connect( motecom ) elif "MOTECOM" in os.environ : self.comm.connect( os.environ["MOTECOM"] ) else: sys.stderr.write("No serial port specified\n") #register a listener for my message self.comm.register( tinyos.oscope.OscopeMsg() , self ) # start/stop state self.started = False # create the frame where all the widgets will go self.frame = Tk.Frame( self.tkRoot ) self.frame.pack() # create the matplotlib figure for displaying the oscope msg payload #self.fig = Figure() #self.axes = self.fig.add_subplot(111) self.fig = figure() self.axes = subplot(111) self.axes.plot([0],[0],'b') #we create a line that holds sensor data #so that we can update it later self.line, = self.axes.lines self.line.set_data([],[]) #remember the current axis limits self.xlim = self.axes.get_xlim() self.ylim = self.axes.get_ylim() # start/stop button self.startButton = Tk.Button( self.frame , text="start" , command = self.toggleStart ) self.startButton.pack( side = Tk.LEFT ) # reset button self.resetButton = Tk.Button( self.frame , text="reset" , command = self.reset ) self.resetButton.pack( side = Tk.LEFT ) #container object for the figure instance self.canvas = FigureCanvasTkAgg( self.fig , master = self.tkRoot ) self.canvas.show() self.canvas.get_tk_widget().pack( side = Tk.TOP , fill = Tk.BOTH , expand = 1 ) self.toolbar = NavigationToolbar2TkAgg( self.canvas , self.tkRoot ) self.toolbar.update() self.canvas._tkcanvas.pack( side=Tk.TOP , fill=Tk.BOTH , expand=1) # start the GUI if I created it if tkRoot == None: self.tkRoot.mainloop()
class Oscope( object ) : """Oscope is a class that opens a window and prints out data being passed in by a mote running the tinyos-1.x/apps/Oscilloscope application. Usage: from the shell command line: python Oscope.py serial@COM1:telos or export MOTECOM = serial@COM1:telos python Oscope.py from the python command line: (note, it's slow and loses packets as a module... why?) from Oscope import Oscope oscope = Oscope('serial@COM1:telos') or from pytos.Comm import Comm myComm = Comm() myComm.connect('serial@COM1:telos') oscope = Oscope(comm=myComm) and/or (you can use combinations of comm and tkRoot) import Tkinter as Tk root = Tk.Tk() oscope = Oscope(rkRoot = root) root.mainloop() """ def __init__( self , motecom=None, tkRoot=None, comm=None ) : #use the user's tk root, if they passed it in if tkRoot == None: self.tkRoot = Tk.Tk() else: self.tkRoot = tkRoot #use the user's comm, if they passed it in if comm != None: self.comm = comm #if no comm passed, create one else: # connect using motecom first, then MOTECOM environment var self.comm = Comm() if motecom != None : self.comm.connect( motecom ) elif "MOTECOM" in os.environ : self.comm.connect( os.environ["MOTECOM"] ) else: sys.stderr.write("No serial port specified\n") #register a listener for my message self.comm.register( tinyos.oscope.OscopeMsg() , self ) # start/stop state self.started = False # create the frame where all the widgets will go self.frame = Tk.Frame( self.tkRoot ) self.frame.pack() # create the matplotlib figure for displaying the oscope msg payload #self.fig = Figure() #self.axes = self.fig.add_subplot(111) self.fig = figure() self.axes = subplot(111) self.axes.plot([0],[0],'b') #we create a line that holds sensor data #so that we can update it later self.line, = self.axes.lines self.line.set_data([],[]) #remember the current axis limits self.xlim = self.axes.get_xlim() self.ylim = self.axes.get_ylim() # start/stop button self.startButton = Tk.Button( self.frame , text="start" , command = self.toggleStart ) self.startButton.pack( side = Tk.LEFT ) # reset button self.resetButton = Tk.Button( self.frame , text="reset" , command = self.reset ) self.resetButton.pack( side = Tk.LEFT ) #container object for the figure instance self.canvas = FigureCanvasTkAgg( self.fig , master = self.tkRoot ) self.canvas.show() self.canvas.get_tk_widget().pack( side = Tk.TOP , fill = Tk.BOTH , expand = 1 ) self.toolbar = NavigationToolbar2TkAgg( self.canvas , self.tkRoot ) self.toolbar.update() self.canvas._tkcanvas.pack( side=Tk.TOP , fill=Tk.BOTH , expand=1) # start the GUI if I created it if tkRoot == None: self.tkRoot.mainloop() def reset( self ) : self.comm.send(65535, tinyos.oscope.OscopeResetMsg() ) self.line.set_data([],[]) self.canvas.draw() def toggleStart( self ) : self.started = not self.started if self.started : self.startButton["text"] = "stop" else : self.startButton["text"] = "start" def messageReceived( self , addr , msg ) : # Because callbacks via jpype give awful errors, catch # and print errors here try: if self.started : if isinstance( msg , tinyos.oscope.OscopeMsg ) : self.receivedOscopeMsg( msg ) else : sys.stderr.write("message of unknown type received\n") except Exception,inst : print inst sys.exit(1)
class RoboMote( object ) : def __init__( self , moteComStr ) : self.__msgQueue = MessageQueue( 5 ) self.__nextMessageType = None # connect to the mote self.__moteComStr = moteComStr self.__comm = Comm() self.__comm.connect( self.__moteComStr ) # When receiving these messages, queue them up self.__comm.register( msgs[ "MotorState" ] , self.__msgQueue ) self.__comm.register( msgs[ "MotorTrim" ] , self.__msgQueue ) self.__comm.register( msgs[ "MotorMovement" ] , self.__msgQueue ) def __query( self , queryType , returnMsgName ) : self.__nextMessageName = returnMsgName motorQueryMsg = msgs[ "MotorQuery" ] motorQueryMsg.type = queryType self.__comm.send( enums.TOS_BCAST_ADDR , motorQueryMsg ); def getNextMsg( self , timeout = 1 ) : try: (addr,msg) = self.__msgQueue.get( True , timeout ) except Queue.Empty : return None if msg.nescType == self.__nextMessageName : return msg return None def destroy( self ) : self.stopMovement() self.disableMotors() self.__comm.disconnect( self.__moteComStr ) def sendKeepAlive( self , timeout = 0 ) : """Timeout is in seconds; 0 is interpreted (on the mote side) as use the previous value""" m = msgs[ "MotorKeepAlive" ] m.stayAliveMillis = timeout*1000.0 self.__comm.send( enums.TOS_BCAST_ADDR , m ) def enableMotors( self ) : m = msgs[ "MotorState" ] m.motorState = enums.MOTORSTATE_ENABLED self.__comm.send( enums.TOS_BCAST_ADDR , m ) def disableMotors( self ) : m = msgs[ "MotorState" ] m.motorState = enums.MOTORSTATE_DISABLED self.__comm.send( enums.TOS_BCAST_ADDR , m ) def stopMovement( self ): self.setMovement( 0.0 , 0.0 , 0.0 , 0.0 ) def setMovement( self , turnA = 0.0 , turnB = 0.0 , speedA = 0.0 , speedB = 0.0 ) : """all values between -1 and 1""" m = msgs[ "MotorMovement" ] m.turnA = moteRange(turnA) m.turnB = moteRange(turnB) m.speedA = moteRange(speedA) m.speedB = moteRange(speedB) self.__comm.send( enums.TOS_BCAST_ADDR , m ) def setTrim( self , turnA = 0.0 , turnB = 0.0 , speedA = 0.0 , speedB = 0.0 ): """all values between -1 and 1""" m = msgs[ "MotorTrim" ] m.turnATrim = moteRange(turnA) m.turnBTrim = moteRange(turnB) m.speedATrim = moteRange(speedA) m.speedBTrim = moteRange(speedB) self.__comm.send( enums.TOS_BCAST_ADDR , m ) def getStateDict( self ) : enabled = self.isEnabled() if not isinstance( enabled , bool ) : return None return { "enabled" : enabled } def isEnabled( self ) : self.__query( enums.MOTORQUERY_STATE , "MotorState" ) msg = self.getNextMsg() if not msg : return None if msg.motorState == enums.MOTORSTATE_DISABLED : return False return True def getTrim( self ) : self.__query( enums.MOTORQUERY_TRIM , "MotorTrim" ) msg = self.getNextMsg() if not msg : return None trim = ( userRange( msg.turnATrim ) , userRange( msg.turnBTrim ) , userRange( msg.speedATrim ) , userRange( msg.speedBTrim ) ) return trim def getTrimDict( self ) : trim = self.getTrim() if not trim : return None trimDict = { "turnATrim" : trim[0] , "turnBTrim" : trim[1] , "speedATrim" : trim[2] , "speedBTrim" : trim[3] } return trimDict def getMovement( self ) : self.__query( enums.MOTORQUERY_MOVEMENT , "MotorMovement" ) msg = self.getNextMsg() if not msg : return None movement = ( userRange( msg.turnA ) , userRange( msg.turnB ) , userRange( msg.speedA ) , userRange( msg.speedB ) ) return movement def getMovementDict( self ) : movement = self.getMovement() if not movement : return None movementDict = { "turnA" : movement[0] , "turnB" : movement[1] , "speedA" : movement[2] , "speedB" : movement[3] } return movementDict def getAllDict( self ) : d = {} for newDict in ( self.getStateDict() , self.getTrimDict() , self.getMovementDict() ) : if newDict : d.update( newDict ) return d
def __init__(self, motecom=None, tkRoot=None, comm=None): #use the user's tk root, if they passed it in if tkRoot == None: self.tkRoot = Tk.Tk() else: self.tkRoot = tkRoot #use the user's comm, if they passed it in if comm != None: self.comm = comm #if no comm passed, create one else: # connect using motecom first, then MOTECOM environment var self.comm = Comm() if motecom != None: self.comm.connect(motecom) elif "MOTECOM" in os.environ: self.comm.connect(os.environ["MOTECOM"]) else: sys.stderr.write("No serial port specified\n") #register a listener for my message self.comm.register(tinyos.oscope.OscopeMsg(), self) # start/stop state self.started = False # create the frame where all the widgets will go self.frame = Tk.Frame(self.tkRoot) self.frame.pack() # create the matplotlib figure for displaying the oscope msg payload #self.fig = Figure() #self.axes = self.fig.add_subplot(111) self.fig = figure() self.axes = subplot(111) self.axes.plot([0], [0], 'b') #we create a line that holds sensor data #so that we can update it later self.line, = self.axes.lines self.line.set_data([], []) #remember the current axis limits self.xlim = self.axes.get_xlim() self.ylim = self.axes.get_ylim() # start/stop button self.startButton = Tk.Button(self.frame, text="start", command=self.toggleStart) self.startButton.pack(side=Tk.LEFT) # reset button self.resetButton = Tk.Button(self.frame, text="reset", command=self.reset) self.resetButton.pack(side=Tk.LEFT) #container object for the figure instance self.canvas = FigureCanvasTkAgg(self.fig, master=self.tkRoot) self.canvas.show() self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) self.toolbar = NavigationToolbar2TkAgg(self.canvas, self.tkRoot) self.toolbar.update() self.canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) # start the GUI if I created it if tkRoot == None: self.tkRoot.mainloop()
class Oscope(object): """Oscope is a class that opens a window and prints out data being passed in by a mote running the tinyos-1.x/apps/Oscilloscope application. Usage: from the shell command line: python Oscope.py serial@COM1:telos or export MOTECOM = serial@COM1:telos python Oscope.py from the python command line: (note, it's slow and loses packets as a module... why?) from Oscope import Oscope oscope = Oscope('serial@COM1:telos') or from pytos.Comm import Comm myComm = Comm() myComm.connect('serial@COM1:telos') oscope = Oscope(comm=myComm) and/or (you can use combinations of comm and tkRoot) import Tkinter as Tk root = Tk.Tk() oscope = Oscope(rkRoot = root) root.mainloop() """ def __init__(self, motecom=None, tkRoot=None, comm=None): #use the user's tk root, if they passed it in if tkRoot == None: self.tkRoot = Tk.Tk() else: self.tkRoot = tkRoot #use the user's comm, if they passed it in if comm != None: self.comm = comm #if no comm passed, create one else: # connect using motecom first, then MOTECOM environment var self.comm = Comm() if motecom != None: self.comm.connect(motecom) elif "MOTECOM" in os.environ: self.comm.connect(os.environ["MOTECOM"]) else: sys.stderr.write("No serial port specified\n") #register a listener for my message self.comm.register(tinyos.oscope.OscopeMsg(), self) # start/stop state self.started = False # create the frame where all the widgets will go self.frame = Tk.Frame(self.tkRoot) self.frame.pack() # create the matplotlib figure for displaying the oscope msg payload #self.fig = Figure() #self.axes = self.fig.add_subplot(111) self.fig = figure() self.axes = subplot(111) self.axes.plot([0], [0], 'b') #we create a line that holds sensor data #so that we can update it later self.line, = self.axes.lines self.line.set_data([], []) #remember the current axis limits self.xlim = self.axes.get_xlim() self.ylim = self.axes.get_ylim() # start/stop button self.startButton = Tk.Button(self.frame, text="start", command=self.toggleStart) self.startButton.pack(side=Tk.LEFT) # reset button self.resetButton = Tk.Button(self.frame, text="reset", command=self.reset) self.resetButton.pack(side=Tk.LEFT) #container object for the figure instance self.canvas = FigureCanvasTkAgg(self.fig, master=self.tkRoot) self.canvas.show() self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) self.toolbar = NavigationToolbar2TkAgg(self.canvas, self.tkRoot) self.toolbar.update() self.canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) # start the GUI if I created it if tkRoot == None: self.tkRoot.mainloop() def reset(self): self.comm.send(65535, tinyos.oscope.OscopeResetMsg()) self.line.set_data([], []) self.canvas.draw() def toggleStart(self): self.started = not self.started if self.started: self.startButton["text"] = "stop" else: self.startButton["text"] = "start" def messageReceived(self, addr, msg): # Because callbacks via jpype give awful errors, catch # and print errors here try: if self.started: if isinstance(msg, tinyos.oscope.OscopeMsg): self.receivedOscopeMsg(msg) else: sys.stderr.write("message of unknown type received\n") except Exception, inst: print inst sys.exit(1)