Пример #1
0
    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"
Пример #2
0
    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 )
Пример #3
0
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