Search code examples
androidimplementationkalman-filter

Kalman implementation


Maybe my question seems stupid but I'm new to Android coding. I have code for a gyroscope which vibrates if I turn the phone an angle of 60 degrees. So if I turn the phone more then -30 degrees and +30 degrees the phone vibrates. But I have problem with noise and my sensor doesn't work properly, if I move the phone quickly the phone vibrates even the if limit +-30 degrees is not completed .

Here below is my code, and if anyone can help me, how to implement a Kalman filter in that code?
Do I have to add some new class and call it in some way?? I'll be thankful for some example, if anyone has one.

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);
        }
    }
}

Solution

  • I would use a complimentary filter to fuse the acceleration and magnetic sensors with the gyroscope to help improve your measurements. A Kalman filter is probably overkill for what you want to accomplish. The quality of the gyroscope measurements depend heavily on the specific device, but in general gyroscopes do not respond well to lots of quick movements or vibrations.

    This project demonstrates how to accomplish the complimentary filter sensor fusion. Under most circumstances, it will improve the reliability of the measurement significantly. Specifically, you want the FusedGyroscopeSensor class. You can modify the project with a few lines of code to accomplish your requirements.