加速计计算

Accelerometer calculation

我找不到这段代码有什么问题。我正在尝试获取加速度计数据,但是当我尝试在设备上 运行 它时,它正在工作但不是正确的读数我认为计算中的问题,我用于陀螺仪的相同逻辑并采取了正确的读数但加速度计输出显示 Accelx :3.56023e-09 Accely :1.76423e-42 Accelz :1.77404e-42

    accelX = accelerometer->Loop_Accelx();
    accelY = accelerometer->Loop_Accely();
    accelZ = accelerometer->Loop_Accelz();

    for(int i = 0; i<2; i++) //averaging of accelerometer data upto 3 readings
    {
        float accelX1 = accelerometer->Loop_Accelx(); //storing the Accelx value from DmpMPU6050_Demo class
        float accelY1 = accelerometer->Loop_Accely(); //storing the Accely value from DmpMPU6050_Demo class
        float accelZ1 = accelerometer->Loop_Accelz(); //storing the Accelz value from DmpMPU6050_Demo class

        accelX = accelX + accelX1;
        accelY = accelY + accelY1;
        accelZ = accelZ + accelZ1;

        delay(1);
    }

    accelX = accelX/3;
    accelY = accelY/3;
    accelZ = accelZ/3;

    if(numbercount == number){

    emit Accelx_Data(accelX);
    emit Accely_Data(accelY);
    emit Accelz_Data(accelZ);

    cout<<"Accelx :"<<accelX<<"  Accely :"<<accelY<<"  Accelz :"<<accelZ<<endl;

dmpmpu_demo 加速度 x 的文件:

float DmpMPU6050_Demo::Loop_Accelx()
{
    if (!dmpReady)
    {
        return 1;
    }

    fifoCount = mpu.getFIFOCount();

    if (fifoCount == 1024)
    {
        mpu.resetFIFO();
        printf("FIFO overflow!\n");
    }

    else if (fifoCount >= 42 )
    {
        mpu.getFIFOBytes(fifoBuffer, packetSize);

        #ifdef OUTPUT_READABLE_REALACCEL

        mpu.dmpGetQuaternion(&q, fifoBuffer);
        mpu.dmpGetAccel(&aa, fifoBuffer);
        mpu.dmpGetGravity(&gravity, &q);
        mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);

        #endif

        printf("\n");

    }

}

必须感受到地球的重力加速度。这意味着你得到的向量的垂直分量必须明确定义,你得到的向量显然是零向量。这是错误的,表明您没有从加速度计获得正确的值。检查一下。

下次,最好 post 完成代码,因为全局 accelXaccelYaccelZ 声明在代码中的任何地方都看不到,问题的真正根源是什么...我们也没有 accelerometer->LoopAccel...() 原型的定义。

最后,不要像评论中所说的那样摇动它来感知数据。加速度计感应地球的引力,你只需改变它的方向就可以看到加速度矢量的变化。

在 dmpmpu.cpp 文件中我添加了 return

    #ifdef OUTPUT_READABLE_REALACCEL

    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetAccel(&aa, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);

    return 3;
    #endif

    printf("\n");

用于计算没有数字计数的加速度值

        accelX = accelerometer->Loop_Accelx();
    accelY = accelerometer->Loop_Accely();
    accelZ = accelerometer->Loop_Accelz();

    for(int i = 0; i<2; i++) //averaging of accelerometer data upto 3 readings
    {
        float accelX1 = accelerometer->Loop_Accelx(); //storing the Accelx value from DmpMPU6050_Demo class
        float accelY1 = accelerometer->Loop_Accely(); //storing the Accely value from DmpMPU6050_Demo class
        float accelZ1 = accelerometer->Loop_Accelz(); //storing the Accelz value from DmpMPU6050_Demo class

        accelX = accelX + accelX1;
        accelY = accelY + accelY1;
        accelZ = accelZ + accelZ1;

        delay(1);
    }

    accelX = accelX/ 0.16384;
    accelY = accelY/ 0.16384;
    accelZ = accelZ/ 0.16384;

最后我得到了类似

的输出
yaw :8.3854  pitch :8.3854  roll :8.3854
Accelx :54.9316  Accely :54.9316  Accelz :54.9316
yaw :-5.5964  pitch :-5.5964  roll :-5.5964
Accelx :42.7246  Accely :36.6211  Accelz :30.5176
yaw :-129.255  pitch :-129.255  roll :-91.2928
Accelx :36.6214  Accely :48.8281  Accelz :30.5176