Python:访问一个接一个地发送的数据包

时间:2016-02-02 11:27:51

标签: python python-2.7

我正在使用MPU-9150。它一次发送一个加速度计数据,陀螺仪数据,旋转数据等数据包。要访问不同的数据包,我使用if语句if self.type == 0 for accelerometerif self.type == 5 for rotation

现在我的问题是弄清楚如何获取两个数据包,因为后续计算需要两者都可用。

    self.readings = [0,0,0,0,0,0,0,0,0,0,0,0]

    #if (self.type == 0) and (self.type == 5):

    #    print "Both data is available"
    counter = 0

    while counter < 1:

        if self.type == 0:
            print "TESTING FOR ACCELERATION"
            self.readings[0] = self.data[0]
            self.readings[1] = self.data[1]
            self.readings[2] = self.data[2]

        elif self.type == 5:
            print "TESTING FOR ROTATION MATRIX"
            self.readings[3] = self.data[0]
            self.readings[4] = self.data[1]
            self.readings[5] = self.data[2]
            self.readings[6] = self.data[3]
            self.readings[7] = self.data[4]
            self.readings[8] = self.data[5]
            self.readings[9] = self.data[6]
            self.readings[10] = self.data[7]
            self.readings[11] = self.data[8]

        else:
            print "None of the Conditions are True!"
            pass

        counter += 1

    aX = self.readings[0]
    aY = self.readings[1]
    aZ = self.readings[2]

    print 'Acc in X = ', aX
    print 'Acc in Y = ', aY
    print 'Acc in Z = ', aZ

    r1 = self.readings[3]
    r2 = self.readings[4]
    r3 = self.readings[5]
    r4 = self.readings[6]
    r5 = self.readings[7]
    r6 = self.readings[8]
    r7 = self.readings[9]
    r8 = self.readings[10]
    r9 = self.readings[11]

    print 'rotation matrix: \n%7.3f %7.3f %7.3f\n%7.3f %7.3f %7.3f\n%7.3f %7.3f %7.3f' % \
          (r1, r2, r3, \
           r4, r5, r6, \
           r7, r8, r9)

运行此代码检查self.type == 0并给出加速度值但保留零点进行旋转,然后它给出旋转值和零加速度。

当我记录运行代码的结果时,我获得了结果

TESTING FOR ACCELERATION 
Acc in X = -0.08056640625 
Acc in Y = -0.04541015625 
Acc in Z = 1.0068359375 
rotation matrix: 
0.000 0.000 0.000 
0.000 0.000 0.000 
0.000 0.000 0.000 
TESTING FOR ROTATION MATRIX 
Acc in X = 0 
Acc in Y = 0 
Acc in Z = 0 
rotation matrix: 
0.980 0.178 0.085 
-0.181 0.983 0.030 
-0.078 -0.045 0.996 
TESTING FOR ACCELERATION 
Acc in X = -0.080078125 
Acc in Y = -0.044921875 
Acc in Z = 1.0068359375 
rotation matrix: 
0.000 0.000 0.000 
0.000 0.000 0.000 
etc

0 个答案:

没有答案