如何根据加速度计传感器值计算android设备每秒的速度
我的问题是,当我摇晃安卓设备时,我想计算安卓设备的速度。我试过了,但失败了。通过使用基于android-api的加速度传感器需要计算速度。这里我有x,y,z轴的值,我有加速度,把它们转换成速度,每秒钟更新一次速度,但没有用。请仔细阅读下面的代码,并提供我出错的解决方案如何根据加速度计传感器值计算android设备每秒的速度,android,accelerometer,Android,Accelerometer,我的问题是,当我摇晃安卓设备时,我想计算安卓设备的速度。我试过了,但失败了。通过使用基于android-api的加速度传感器需要计算速度。这里我有x,y,z轴的值,我有加速度,把它们转换成速度,每秒钟更新一次速度,但没有用。请仔细阅读下面的代码,并提供我出错的解决方案 public class Speedometer extends Activity { Handler handler = new Handler(); SensorManager sensorMan
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) { }
};
}
提供每秒更新设备屏幕上速度的解决方案。
提前谢谢。光是读取加速度表是无法得到绝对速度的。那该怎么办?但是你可以计算速度的变化,所以只有根据已知的初始速度,你才能根据加速计计算实际速度。在StackOverflow中搜索不包含此主题的线程以了解更多信息。此外,你似乎对你发布的代码了解不多,因为你根本没有意识到出了什么问题。感谢你的更新,这里的速度不会因我摇晃设备而改变,但经过一段时间后,当设备处于理想状态时,它会增加速度,如22 mph、132 mph,然后停止更新。请运行我的代码并观察变化。正如我之前所说的,仅仅根据加速度计值计算速度是不可能的。您遇到的值似乎是实际加速度,但根本不是速度