简体   繁体   English

互补滤波器陀螺加速度计

[英]Complementary filter Gyro accelerometer

I'm using complementary filter for gyro and accelerometer just for azimuth... I've got it from this site : http://www.thousand-thoughts.com/2012/03/android-sensor-fusion-tutorial/1/ 我正在为陀螺仪和加速度计使用辅助滤波器,仅用于方位角...我已经从以下站点获得了它: http : //www.thousand-thoughts.com/2012/03/android-sensor-fusion-tutorial/1 /

the core of filter is : 过滤器的核心是:

         /*
         * Fix for 179° <--> -179° transition problem:
         * Check whether one of the two orientation angles (gyro or accMag) is negative while the other one is positive.
         * If so, add 360° (2 * math.PI) to the negative value, perform the sensor fusion, and remove the 360° from the result
         * if it is greater than 180°. This stabilizes the output in positive-to-negative-transition cases.
         */

        // azimuth
        if (gyroOrientation[0] < -0.5 * Math.PI && accMagOrientation[0] > 0.0) {
            fusedOrientation[0] = (float) (FILTER_COEFFICIENT * (gyroOrientation[0] + 2.0 * Math.PI) + oneMinusCoeff * accMagOrientation[0]);
            fusedOrientation[0] -= (fusedOrientation[0] > Math.PI) ? 2.0 * Math.PI : 0;
            Log.d("test","gyro Is Negative");
        }
        else if (accMagOrientation[0] < -0.5 * Math.PI && gyroOrientation[0] > 0.0) {
            fusedOrientation[0] = (float) (FILTER_COEFFICIENT * gyroOrientation[0] + oneMinusCoeff * (accMagOrientation[0] + 2.0 * Math.PI));
            fusedOrientation[0] -= (fusedOrientation[0] > Math.PI)? 2.0 * Math.PI : 0;
            Log.d("test","accel Is Negative");

        }
        else {
            fusedOrientation[0] = FILTER_COEFFICIENT * gyroOrientation[0] + oneMinusCoeff * accMagOrientation[0];
        }
        gyroMatrix = getRotationMatrixFromOrientation(fusedOrientation);
        System.arraycopy(fusedOrientation, 0, gyroOrientation, 0, 3);

I want to compare this with Real gyro data that has drift ... for this, I've used gyroOreintationReal... and add some codes to save gyroOreintation, like this: 我想将其与具有漂移的Real陀螺仪数据进行比较...为此,我使用了gyroOreintationReal ...并添加了一些代码来保存gyroOreintation,如下所示:

 if(initState) {
        float[] initMatrix = new float[9];
        initMatrix = getRotationMatrixFromOrientation(accMagOrientation);
        float[] test = new float[3];
        SensorManager.getOrientation(initMatrix, test);
        gyroMatrix = matrixMultiplication(gyroMatrix, initMatrix);
        gyroMatrixReal = matrixMultiplication(gyroMatrixReal, initMatrix);

        initState = false;
    }

    // copy the new gyro values into the gyro array
    // convert the raw gyro data into a rotation vector
    float[] deltaVector = new float[4];
    float[] deltaVectorReal = new float[4];

    if(timestamp != 0) {
        final float dT = (event.timestamp - timestamp) * NS2S;


    System.arraycopy(event.values, 0, gyro, 0, 3);
    System.arraycopy(event.values, 0, gyroReal, 0, 3);


    getRotationVectorFromGyro(gyro, deltaVector, dT / 2.0f);
    getRotationVectorFromGyro(gyroReal, deltaVectorReal, dT / 2.0f);

    }

    // measurement done, save current time for next interval
    timestamp = event.timestamp;

    // convert rotation vector into rotation matrix
    float[] deltaMatrix = new float[9];
    float[] deltaMatrixReal = new float[9];

    SensorManager.getRotationMatrixFromVector(deltaMatrix, deltaVector);
    SensorManager.getRotationMatrixFromVector(deltaMatrixReal, deltaVectorReal);


    // apply the new rotation interval on the gyroscope based rotation matrix
    gyroMatrix = matrixMultiplication(gyroMatrix, deltaMatrix);
    gyroMatrixReal = matrixMultiplication(gyroMatrixReal, deltaMatrixReal);


    // get the gyroscope based orientation from the rotation matrix
    SensorManager.getOrientation(gyroMatrix, gyroOrientation);
    SensorManager.getOrientation(gyroMatrixReal, gyroOrientationReal);     

and I saved the result and ploted them with matlab ... but the plot shows that gyro's orientation is negative ... but fusedOreientation is less than +150 and accel orientation is a little more than +150 ... 我保存了结果并用matlab对其进行了绘制...但是该图显示了陀螺仪的方向为负...但是fusedOreientation小于+150而accel方向略大于+150 ...

how can I fix the problem?? 我该如何解决这个问题? I add some codes to core of complementary filter : 我在补充过滤器的核心添加一些代码:

//RealGyro
       if (gyroOrientationReal[0] < -0.5 * Math.PI && accMagOrientation[0] > 0.0) {
            gyroOrientationReal[0] =  (float) (gyroOrientation[0] + 2.0 * Math.PI);
            gyroOrientationReal[0] -= (gyroOrientationReal[0] > Math.PI) ? 2.0 * Math.PI : 0;
       }        

it's Ok sometimes But I don't know what can I do if I have negative accel data and positive gyro data? 有时候没关系,但是我不知道加速度数据为负而陀螺数据为正怎么办?

when I tried to rotate the phone 360 degree .. I got this plot from matlab : http://www.uppic.com/uploads/14205374821.jpg 当我尝试将手机旋转360度时。.我从matlab获得此图: http : //www.uppic.com/uploads/14205374821.jpg

the green lone is for Gyro, red for fused Orientation and the blue one is for accel. 绿色的代表陀螺仪,红色的代表融合方向,蓝色的代表加速度。

  • why Gyro's time has offset?? 为什么陀螺仪的时间抵消了? the reason is drift ?? 原因是漂移? the plot is correct? 情节是正确的吗?
  • how can I use 0-360 period instead of 0_180 and -180_0? 如何使用0-360周期而不是0_180和-180_0?

The link to the image you posted does not work, so I couldn't check the plot from matlab. 您发布的图片的链接不起作用,因此我无法从matlab中查看绘图。 Gyro drift does not add any time offset to the data. 陀螺仪漂移不会向数据添加任何时间偏移。 Maybe the offset is caused by some internal delay (unprobable) or, more likely by a delay in the algorithm. 偏移可能是由某些内部延迟(不太可能)引起的,或更可能是由算法中的延迟引起的。 I can't tell exactly because I cannot see, how big the time offset is. 我无法确切说出时间偏移量,因为我看不到。

If you ounly want the gyro data, the piece you added to the complementary filter is unnecessary. 如果您只需要陀螺仪数据,则无需添加到互补滤波器中。 All gyro data should be already in the gyroOrientationReal vector. 所有陀螺仪数据都应该已经在gyroOrientationReal向量中。 No further processing needed. 无需进一步处理。 The real gyro data should not be influenced by accMagOrientation (which you do, by changing gyroOrientationReal if accMagOrientation[0] > 0.0 . 实际的陀螺仪数据不应受accMagOrientation影响(如果accMagOrientation[0] > 0.0 ,则可以通过更改gyroOrientationReal accMagOrientation[0] > 0.0

If you want to go from a [0, 360] period to a [0, 180, -180, 0] period you just need to subtract 180 from your result, and you're done. 如果您要从[0,360]周期变为[0,180,-180,0]周期,则只需要从结果中减去180,就可以完成。

I hope I could help you with this. 希望我能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM