コード例 #1
0
ファイル: neblinaCore.py プロジェクト: Motsai/neblina-python
 def sendCommand(self, subSystem, command, enable=True, **kwargs):
     if self.device:
         packet = NebCommandPacket(subSystem, command, enable, **kwargs)
         self.device.sendPacket(packet.stringEncode())
コード例 #2
0
    def testPacketsEncoding(self):
        packetList = []
        # Testing downsample command
        # Downsample to 1Hz example
        downSampleFactor = 1000
        downSampleCommandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.Downsample, downSampleFactor)
        packetString = downSampleCommandPacket.stringEncode()
        packetBytes = bytearray(packetString)
        self.assertEqual(len(packetBytes), 20)
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Motion)
        self.assertEqual(packetBytes[1], 16)
        self.assertEqual(packetBytes[3], Commands.Motion.Downsample)
        self.assertEqual(packetBytes[8], struct.pack('<H', downSampleFactor)[0])
        self.assertEqual(packetBytes[9], struct.pack('<H', downSampleFactor)[1])

        # Make sure these calls dont cause an exception
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.MotionState, True)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.MotionState, False)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.IMU, True)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.IMU, False)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.Quaternion, True)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.Quaternion, False)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.EulerAngle, True)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.EulerAngle, False)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.ExtForce, True)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.ExtForce, False)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.TrajectoryInfo, True)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.TrajectoryInfo, False)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.Pedometer, True)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.Pedometer, False)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.MAG, True)
        commandPacket = NebCommandPacket(SubSystem.Motion, Commands.Motion.MAG, False)

        # Test encoding of recording packets
        recordCommandPacket = NebCommandPacket(SubSystem.Storage, Commands.Storage.EraseAll)
        packetBytes = bytearray(recordCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Storage)
        self.assertEqual(packetBytes[1], 16)
        self.assertEqual(packetBytes[3], Commands.Storage.EraseAll)

        recordCommandPacket = NebCommandPacket(SubSystem.Storage, Commands.Storage.Record, True)
        packetBytes = bytearray(recordCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Storage)
        self.assertEqual(packetBytes[3], Commands.Storage.Record)
        self.assertEqual(packetBytes[8], 0x01)

        recordCommandPacket = NebCommandPacket(SubSystem.Storage, Commands.Storage.Record, False)
        packetBytes = bytearray(recordCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Storage)
        self.assertEqual(packetBytes[3], Commands.Storage.Record)
        self.assertEqual(packetBytes[8], 0x00)

        recordCommandPacket = NebCommandPacket(SubSystem.Storage, Commands.Storage.Playback, True, sessionID=42)
        packetBytes = bytearray(recordCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Storage)
        self.assertEqual(packetBytes[3], Commands.Storage.Playback)
        self.assertEqual(packetBytes[8], 0x01)

        recordCommandPacket = NebCommandPacket(SubSystem.Storage, Commands.Storage.Playback, False,  sessionID=42)
        packetBytes = bytearray(recordCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Storage)
        self.assertEqual(packetBytes[3], Commands.Storage.Playback)
        self.assertEqual(packetBytes[8], 0x00)

        readCommandPacket = NebCommandPacket(SubSystem.EEPROM, Commands.EEPROM.Read, pageNumber=5)
        packetBytes = bytearray(readCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.EEPROM)
        self.assertEqual(packetBytes[3], Commands.EEPROM.Read)
        self.assertEqual(packetBytes[4], 0x05)

        # EEPROM Command packet testing
        writeCommandPacket = NebCommandPacket(SubSystem.EEPROM, Commands.EEPROM.Write,\
          pageNumber=11, dataBytes=b'\xde\xad\xbe\xef\xba\xbe\x92\x74')
        packetBytes = bytearray(writeCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.EEPROM)
        self.assertEqual(packetBytes[3], Commands.EEPROM.Write)
        self.assertEqual(packetBytes[4], 0x0B)
        self.assertEqual(packetBytes[5], 0x00)
        self.assertEqual(packetBytes[6], 0xde)
        self.assertEqual(packetBytes[7], 0xad)
        self.assertEqual(packetBytes[8], 0xbe)
        self.assertEqual(packetBytes[9], 0xef)
        self.assertEqual(packetBytes[10], 0xba)
        self.assertEqual(packetBytes[11], 0xbe)
        self.assertEqual(packetBytes[12], 0x92)
        self.assertEqual(packetBytes[13], 0x74)

        # Debug packet set interface
        switchInterfaceCommandPacket = NebCommandPacket(SubSystem.Debug,\
            Commands.Debug.SetInterface,\
            True)
        packetBytes = bytearray(switchInterfaceCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Debug)
        self.assertEqual(packetBytes[3], Commands.Debug.SetInterface)
        self.assertEqual(packetBytes[8], 0x01)
        switchInterfaceCommandPacket = NebCommandPacket(SubSystem.Debug,\
            Commands.Debug.SetInterface,\
            False)
        packetBytes = bytearray(switchInterfaceCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Debug)
        self.assertEqual(packetBytes[3], Commands.Debug.SetInterface)
        self.assertEqual(packetBytes[8], 0x00)

        flashAndRecorderStateCommandPacket = NebCommandPacket(SubSystem.Debug,\
            Commands.Debug.MotAndFlashRecState)
        packetBytes = bytearray(flashAndRecorderStateCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Debug)
        self.assertEqual(packetBytes[3], Commands.Debug.MotAndFlashRecState)

        # Unit test start command packet
        unitTestStartCommandPacket = NebCommandPacket(SubSystem.Debug,\
            Commands.Debug.StartUnitTestMotion, True)
        packetBytes = bytearray(unitTestStartCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Debug)
        self.assertEqual(packetBytes[3], Commands.Debug.StartUnitTestMotion)
        self.assertEqual(packetBytes[8], 0x01)

        unitTestStartCommandPacket = NebCommandPacket(SubSystem.Debug,\
            Commands.Debug.StartUnitTestMotion, False)
        packetBytes = bytearray(unitTestStartCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Debug)
        self.assertEqual(packetBytes[3], Commands.Debug.StartUnitTestMotion)
        self.assertEqual(packetBytes[8], 0x00)

        # Unit test data command packet
        imuPackets = nebsim.createRandomIMUDataPacketList(50.0, 10, 1.0)
        magPackets = nebsim.createRandomMAGDataPacketList(50.0, 10, 1.0)
        for idx,imuPacket in enumerate(imuPackets):
            magPacket = magPackets[idx]
            imuPacketString = imuPacket.stringEncode()
            magPacketString = magPacket.stringEncode()
            unitTestDataPacket = NebCommandPacket(SubSystem.Debug,\
                Commands.Debug.UnitTestMotionData, timestamp=imuPacket.data.timestamp,\
                accel=imuPacket.data.accel, gyro=imuPacket.data.gyro, mag=magPacket.data.mag)
            testPacketBytes = bytearray(unitTestDataPacket.stringEncode())
            self.assertEqual(testPacketBytes[0], (PacketType.Command << 5)| SubSystem.Debug)
            self.assertEqual(testPacketBytes[3], Commands.Debug.UnitTestMotionData)
            self.assertEqual(testPacketBytes[8], imuPacketString[8])
            self.assertEqual(testPacketBytes[9], imuPacketString[9])
            self.assertEqual(testPacketBytes[10], imuPacketString[10])
            self.assertEqual(testPacketBytes[11], imuPacketString[11])
            self.assertEqual(testPacketBytes[12], imuPacketString[12])
            self.assertEqual(testPacketBytes[13], imuPacketString[13])
            self.assertEqual(testPacketBytes[14], imuPacketString[14])
            self.assertEqual(testPacketBytes[15], imuPacketString[15])
            self.assertEqual(testPacketBytes[16], imuPacketString[16])
            self.assertEqual(testPacketBytes[17], imuPacketString[17])
            self.assertEqual(testPacketBytes[18], imuPacketString[18])
            self.assertEqual(testPacketBytes[19], imuPacketString[19])
            self.assertEqual(testPacketBytes[20], magPacketString[8])
            self.assertEqual(testPacketBytes[21], magPacketString[9])
            self.assertEqual(testPacketBytes[22], magPacketString[10])
            self.assertEqual(testPacketBytes[23], magPacketString[11])
            self.assertEqual(testPacketBytes[24], magPacketString[12])
            self.assertEqual(testPacketBytes[25], magPacketString[13])

        # LED Command Packets
        ledValues = [(0,1),(1,34),(3,0),(4,254),(5,128),(12,11)]
        setLEDValuesPacket = NebCommandPacket(\
            SubSystem.LED, Commands.LED.SetVal, ledValueTupleList=ledValues)
        packetBytes = bytearray(setLEDValuesPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.LED)
        self.assertEqual(packetBytes[3], Commands.LED.SetVal)
        self.assertEqual(packetBytes[4], 6)
        self.assertEqual(packetBytes[5], 0x00)
        self.assertEqual(packetBytes[6], 0x01)
        self.assertEqual(packetBytes[7], 0x01)
        self.assertEqual(packetBytes[8], 34)
        self.assertEqual(packetBytes[9], 3)
        self.assertEqual(packetBytes[10], 0)
        self.assertEqual(packetBytes[11], 4)
        self.assertEqual(packetBytes[12], 254)
        self.assertEqual(packetBytes[13], 5)
        self.assertEqual(packetBytes[14], 128)
        self.assertEqual(packetBytes[15], 12)
        self.assertEqual(packetBytes[16], 11)

        leds = [3,5,1,6,7,2,4]
        getLEDValuesPacket = NebCommandPacket(\
            SubSystem.LED, Commands.LED.GetVal,\
            ledIndices=leds)
        packetBytes = bytearray(getLEDValuesPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.LED)
        self.assertEqual(packetBytes[3], Commands.LED.GetVal)
        self.assertEqual(packetBytes[4], 7)
        self.assertEqual(packetBytes[5], 3)
        self.assertEqual(packetBytes[6], 5)
        self.assertEqual(packetBytes[7], 1)
        self.assertEqual(packetBytes[8], 6)
        self.assertEqual(packetBytes[9], 7)
        self.assertEqual(packetBytes[10], 2)
        self.assertEqual(packetBytes[11], 4)

        # Flash Session Info
        sessionInfoCommandPacket = NebCommandPacket(SubSystem.Storage,\
            Commands.Storage.SessionInfo, sessionID = 10)
        packetBytes = bytearray(sessionInfoCommandPacket.stringEncode())
        self.assertEqual(packetBytes[0], (PacketType.Command << 5)| SubSystem.Storage)
        self.assertEqual(packetBytes[3], Commands.Storage.SessionInfo)
        self.assertEqual(packetBytes[8], 0x0A)
        self.assertEqual(packetBytes[9], 0x00)
コード例 #3
0
 def sendCommand(self, subSystem, command, enable=True, **kwargs):
     if self.device:
         packet = NebCommandPacket(subSystem, command, enable, **kwargs)
         self.device.sendPacket(packet.stringEncode())