如何根据加速度计传感器值计算每秒钟安卓设备的速度

时间:2014-02-28 13:15:19

标签: android accelerometer

我的问题是,当我摇动Android设备时,我想计算android设备的速度。我试过了,但都失败了。通过使用基于android api的加速计传感器需要计算速度。在这里,我有x,y,z轴值基于我有加速度转换为速度和更新每秒的速度,但没有用。我获得了请通过以下代码并提供我出错的解决方案。

public class Speedometer extends Activity {

      Handler handler = new Handler();


      SensorManager sensorManager;
        TextView myTextView,tv1_x,tv2_y,tv3_z,tv5;

        float appliedAcceleration = 0;
        float currentAcceleration = 0;
        float velocity = 0;
        Date lastUpdate;    

        @Override
        public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          setContentView(R.layout.main);

          myTextView = (TextView)findViewById(R.id.myTextView);
          tv1_x=(TextView)findViewById(R.id.textView1);
            tv2_y=(TextView)findViewById(R.id.textView2);
            tv3_z=(TextView)findViewById(R.id.textView3);
            tv5=(TextView)findViewById(R.id.textView5);

          lastUpdate = new Date(System.currentTimeMillis());
Log.i("TIME", ""+lastUpdate);
          sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
          Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            sensorManager.registerListener(sensorEventListener,
                                           accelerometer,
                                           SensorManager.SENSOR_DELAY_UI);


          Timer updateTimer = new Timer("velocityUpdate");
          updateTimer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
              updateGUI();
            }
          }, 0, 1000);
        }

        private void updateGUI() {
          // Convert from meters per second to miles per hour.
          final double V_mph = (Math.round(100*velocity / 1.6 * 3.6))/100;



          // Update the GUI
          handler.post(new Runnable() {
          public void run() {
              myTextView.setText(String.valueOf(V_mph) + "mph");
              //tv5.setText(String.valueOf(distance_val));
          }
        });
        }

      private void updateVelocity() {
          // Calculate how long this acceleration has been applied.
          Date timeNow = new Date(System.currentTimeMillis());
          Log.i("TIME_now", ""+timeNow);
          long timeDelta = timeNow.getTime()-lastUpdate.getTime();
          lastUpdate.setTime(timeNow.getTime());
          Log.i("last update time TIME", ""+lastUpdate);
          // Calculate the change in velocity at the 
          // current acceleration since the last update. 

          appliedAcceleration = currentAcceleration;
          float deltaVelocity = appliedAcceleration * (timeDelta/1000);

          // Add the velocity change to the current velocity.
          velocity += deltaVelocity;
          Log.i("last update time velocity", ""+velocity);
        }

      //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];

            tv1_x.setText(""+x);
            tv2_y.setText(""+y);
            tv3_z.setText(""+z);

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

          Log.i("TIME_in sensor", "Double val a"+a);

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

提供每秒在设备屏幕上更新速度的解决方案。 提前谢谢。

0 个答案:

没有答案