卡尔曼实现

Kalman implementation

也许我的问题看起来很愚蠢,但我是 Android 编码的新手。我有一个陀螺仪代码,如果我将 phone 旋转 60 度角,它就会振动。因此,如果我转动 phone 超过 -30 度和 +30 度,phone 就会振动。但是我有噪音问题,我的传感器不能正常工作,如果我快速移动 phone,即使没有完成限制 +-30 度,phone 也会振动。

下面是我的代码,如果有人能帮助我,如何在该代码中实现卡尔曼滤波器?
我是否必须添加一些新的 class 并以某种方式调用它?如果有人有例子,我将不胜感激。

public class AccessGyroscope extends Activity implements SensorEventListener
{

    private TextView tv;
    //the Sensor Manager
    private SensorManager sManager;

    private float valueX, valueY, valueZ;

    public Vibrator v;

    private float vibrateThreshold = 0;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //get the TextView from the layout file
        tv = (TextView) findViewById(R.id.tv);

        //get a hook to the sensor service
        sManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        vibrateThreshold = 30;

        //initialize vibration
        v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
    }

    //when this Activity starts
    @Override
    protected void onResume() 
    {
        super.onResume();
        /*register the sensor listener to listen to the gyroscope sensor, use the 
         * callbacks defined in this class, and gather the sensor information as  
         * quick as possible*/
        sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
    }

    //When this Activity isn't visible anymore
    @Override
    protected void onStop() 
    {
        //unregister the sensor listener
        sManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) 
    {
        //Do nothing
    }

    @Override
    public void onSensorChanged(SensorEvent event) 
    {
        //if sensor is unreliable, return void
        if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
        {
            return;
        }

        valueX = event.values[2];
        valueY = event.values[1];
        valueZ = event.values[0];


        //else it will output the Roll, Pitch and Yawn values
        tv.setText("Orientation X (Roll) :"+ Float.toString(valueX) +"\n"+
                   "Orientation Y (Pitch) :"+ Float.toString(valueY) +"\n"+
                   "Orientation Z (Yaw) :"+ Float.toString(valueZ));



        vibrate(); //here I'm calling vibration calculation


    }

    public void vibrate(){
        if(valueX > vibrateThreshold || valueX < -vibrateThreshold){
            v.vibrate(50);
        }
    }
}

我会使用免费的滤波器将加速度传感器和磁传感器与陀螺仪融合在一起,以帮助改进您的测量。卡尔曼滤波器对于你想要完成的事情来说可能有点矫枉过正。陀螺仪测量的质量在很大程度上取决于特定设备,但一般而言,陀螺仪对大量快速移动或振动反应不佳。

This project 演示了如何完成免费的过滤器传感器融合。在大多数情况下,它会显着提高测量的可靠性。具体来说,您需要 FusedGyroscopeSensor class。您可以通过几行代码修改项目来完成您的要求。

如果您需要一个工作示例,您可以从 google 硬纸板 api 查看卡尔曼滤波器。 HeadTrackerclass使用卡尔曼滤波器,CardboardViewclass:

使用卡尔曼滤波器

https://github.com/raasun/cardboard/blob/master/src/com/google/vrtoolkit/cardboard/sensors/internal/OrientationEKF.java