如何减少android加速度计上的噪音

时间:2015-11-07 18:15:26

标签: android accelerometer noise

我正在开发一个应用程序来找到一个Android手机的位置相对于我正在进行的机器人项目的起点。机器人行进的最大范围是12英尺乘12英尺的正方形。我也愿意接受其他方式。目前,我正在使用加速度计来找到机器人在现场的位置,但是机器人上的噪音正在弄乱积分计算。这是我的代码的副本。

   double currentVelocity;
   double currentPosition;
   float noise[] = new float[3];
   float alpha = (float) 0.1;
   private static final double timeInterval = .25;

public void onSensorChanged(SensorEvent sensorEvent) {
    Sensor mySensor = sensorEvent.sensor;

    if (mySensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
        //X,Y,Z values for the acceleration in meters per second squared
        float xAccelCurrent_RAW = sensorEvent.values[0];
        float yAccelCurrent_RAW = sensorEvent.values[1];
        float zAccelCurrent_RAW = sensorEvent.values[2];

        noise[0] = alpha * noise[0] + (1 - alpha) * sensorEvent.values[0];
        noise[1] = alpha * noise[1] + (1 - alpha) * sensorEvent.values[1];
        noise[2] = alpha * noise[2] + (1 - alpha) * sensorEvent.values[2];

        float xAccelCurrent_filtered = xAccelCurrent_RAW - noise[0];
        float yAccelCurrent_filtered = yAccelCurrent_RAW - noise[1];
        float zAccelCurrent_filtered = zAccelCurrent_RAW - noise[2];

        String xPositionString = Double.toString(findPosition(findVelocity(xAccelCurrent_filtered, timeInterval), timeInterval));
        String yPositionString = Double.toString(findPosition(findVelocity(yAccelCurrent_filtered, timeInterval), timeInterval));
        String zPositionString = Double.toString(findPosition(findVelocity(zAccelCurrent_filtered, timeInterval), timeInterval));

        xAccelTextView.setText(xPositionString);
        yAccelTextView.setText(yPositionString);
        zAccelTextView.setText(zPositionString); }}

1 个答案:

答案 0 :(得分:1)

低通滤波器是您的解决方案https://en.wikipedia.org/wiki/Low-pass_filter

public Time(String militaryTime)
{
    //Check to make sure something was entered
    if (militaryTime == null || !militaryTime.matches("^\\d{2}:\\d{2}$"))
    {
        System.out.println(
            "You must enter a valid miliary time." );
    }
    //Check to make sure there are 5  characters
    else
    {
        //Check to make sure the colon is in the right spot

        if (!Character.isDigit(militaryTime.charAt(2)))
        {
            System.out.println(militaryTime +
                " is not a valid miliary time." );
        }
        //Check to make sure all other characters are digits
        else if (!Character.isDigit(militaryTime.charAt(0)))
        {
            System.out.println(militaryTime +
                " is not a valid miliary time." );
        }
        else if (!Character.isDigit(militaryTime.charAt(0)))
        {
            System.out.println(militaryTime +
                " is not a valid miliary time." );
        }
        else if (!Character.isDigit(militaryTime.charAt(0)))
        {
            System.out.println(militaryTime +
                " is not a valid miliary time." );
        }
        else if (!Character.isDigit(militaryTime.charAt(0)))
        {
            System.out.println(militaryTime +
                " is not a valid miliary time." );
        }
        else
        {
            //this separates hours and minutes
            hours = Integer.parseInt(militaryTime.substring(0,2));

            //validate hours and minutes are valid values
            if(hours > 23)
            {
                System.out.println(militaryTime +
                    " is not a valid miliary time." );
            }
            else if(minutes > 59)
            {
                System.out.println(militaryTime +
                    " is not a valid miliary time." );
            }
            //convert military time to conventional time
            //for afternoon times
            else if (hours > 12)
            {
                hours = hours - 12;
                afternoon = true;
                System.out.println(this.toString());
            }
            //account for midnight
            else if (hours == 0)
            {
                hours = 12;
                System.out.println(this.toString());
            }
            //account for noon
            else if (hours == 12)
            {
                afternoon = true;
                System.out.println(this.toString());
            }
            //morning times don't need converting
            else
            {
                System.out.println(this.toString());
            }
        }
    }
}