如何使用Accelerometer计算速度?

时间:2017-07-31 10:49:28

标签: android accelerometer sensor

您好我正在尝试使用加速计传感器来计算加速度& Android中的速度。一切都很好,但速度总是在增加。我得到了正确的加速度。为了获得实际的加速度,我用地球的当前加速度(大约是9.8)减去它。这是我的MainActivity类:

    public class MainActivity extends AppCompatActivity {
Handler handler = new Handler();

SensorManager sensorManager;
TextView myTextView;
double intialVelocity = 0.0;
double finalAcceleration = 0.0;
double appliedAcceleration = 0;
double currentAcceleration = 0;
double velocity = 0;
double initialAcceleration = 0;
Date lastUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myTextView = (TextView)findViewById(R.id.myTextView);
    lastUpdate = new Date(System.currentTimeMillis());

    sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensorManager.registerListener(sensorEventListener,
            accelerometer,
            SensorManager.SENSOR_DELAY_FASTEST);

}

private void updateVelocity() {
    // Calculate how long this acceleration has been applied.
    Date timeNow = new Date(System.currentTimeMillis());
    long timeDelta = timeNow.getTime()-lastUpdate.getTime();
    lastUpdate.setTime(timeNow.getTime());

    // Calculate the change in velocity at the
    // current acceleration since the last update.
    double deltaVelocity = appliedAcceleration * (timeDelta/1000);
    appliedAcceleration = currentAcceleration;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //Log.d("init_velocity", String.valueOf(intialVelocity));
            if(initialAcceleration < currentAcceleration){
                if(currentAcceleration > 9.9 || currentAcceleration < 9.4){
                    finalAcceleration = currentAcceleration;
                }else{
                    finalAcceleration = 9.80665;
                }
                double acceleration = finalAcceleration - 9.80665;
                Log.d("acc1", String.valueOf(acceleration));
                velocity = (intialVelocity + (acceleration));
                myTextView.setText(String.valueOf(velocity));
                intialVelocity = velocity;
                initialAcceleration = currentAcceleration;
            }else if(initialAcceleration > currentAcceleration){
                if(currentAcceleration > 9.9 || currentAcceleration < 9.4){
                    finalAcceleration = currentAcceleration;
                }else{
                    finalAcceleration = 9.80665;
                }
                double acceleration = finalAcceleration - 9.80665;
                Log.d("acc2", String.valueOf(acceleration));
                velocity = (intialVelocity - (acceleration));
                myTextView.setText(String.valueOf(velocity));
                intialVelocity = velocity;
                initialAcceleration = currentAcceleration;
            }

        }
    });
}

//private final SensorListener sensorListener = new SensorListener() {
private final SensorEventListener sensorEventListener = new SensorEventListener() {
    double calibration = Double.NaN;



    public void onSensorChanged(SensorEvent event) {
        double x = event.values[0];
        double y = event.values[1];
        double z = event.values[2];

        double a = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));

        if (calibration == Double.NaN)
            calibration = a;
        else {
            updateVelocity();
            currentAcceleration = (float)a;
        }
    }
    public void onAccuracyChanged(Sensor sensor, int accuracy) { }
};
}

有人可以帮我吗?

0 个答案:

没有答案