使用机器人加速度计确定恒定速度
Determining constant velocity using androids accelerometer
我正在尝试编写一个以 phone 的速度更新屏幕的小应用程序。它使用加速度计计算当前速度并将其写在屏幕上。问题是移动 phone 后速度不会回到零速度。它会稳定在速度的最高点。我正在使用 LINEAR_ACCELEROMETER
这是代码:
public class AccelerometerUpSensor extends SensorAbstract{
private ExerciseFragment fragment;
private double v0 = 0;
private float lastX;
private float lastY;
private float lastZ;
private long interval;
private long lastEvent = System.currentTimeMillis();
public AccelerometerUpSensor(SensorManager sensorManager, ExerciseFragment fragment, int[] sensorTypes){
super(sensorManager,sensorTypes);
this.fragment = fragment;
}
@Override
public final void onSensorChanged(SensorEvent event) {
lastX = event.values[0];
lastY = event.values[1];
lastZ = event.values[2];
long now = System.currentTimeMillis();
interval = (now - lastEvent);
lastEvent = now;
double acceleration = lastX+lastY+lastZ;
double velocity = v0 + (acceleration*(interval/(double)1000));
v0 = velocity;
System.out.println(velocity);
}
有com.google.android.gms
ActivityRecognition
,可以追踪不同种类的移动。
Accelerometer
mGravity = event.values.clone();
float x = mGravity[0];
float y = mGravity[1];
float z = mGravity[2];
mAccelLast = mAccelCurrent;
mAccelCurrent =(float) Math.sqrt(x * x + y * y + z * z);
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;
// Make this higher or lower according to how much motion you want to detect
if (Math.abs(mAccel) > 2) {
private void calculateVelocity(float x, float y, float z){
long now = System.currentTimeMillis();
interval = (now - lastEvent);
if(interval > 100){
lastEvent = now;
double acceleration = x+y+z-lastX-lastY-lastZ;
double velocity = v0 + (acceleration*(interval/(double)1000));
velocities.add(Math.abs(velocity));
v0= velocity;
lastX = x;
lastY = y;
lastZ = z;
}
}
这是我用来计算每 100 毫秒速度的代码。 velocities-array 用于计算每秒的平均速度,您可以忽略它。该方法的输入是 event.values
我正在尝试编写一个以 phone 的速度更新屏幕的小应用程序。它使用加速度计计算当前速度并将其写在屏幕上。问题是移动 phone 后速度不会回到零速度。它会稳定在速度的最高点。我正在使用 LINEAR_ACCELEROMETER
这是代码:
public class AccelerometerUpSensor extends SensorAbstract{
private ExerciseFragment fragment;
private double v0 = 0;
private float lastX;
private float lastY;
private float lastZ;
private long interval;
private long lastEvent = System.currentTimeMillis();
public AccelerometerUpSensor(SensorManager sensorManager, ExerciseFragment fragment, int[] sensorTypes){
super(sensorManager,sensorTypes);
this.fragment = fragment;
}
@Override
public final void onSensorChanged(SensorEvent event) {
lastX = event.values[0];
lastY = event.values[1];
lastZ = event.values[2];
long now = System.currentTimeMillis();
interval = (now - lastEvent);
lastEvent = now;
double acceleration = lastX+lastY+lastZ;
double velocity = v0 + (acceleration*(interval/(double)1000));
v0 = velocity;
System.out.println(velocity);
}
有
com.google.android.gms
ActivityRecognition
,可以追踪不同种类的移动。Accelerometer
mGravity = event.values.clone(); float x = mGravity[0]; float y = mGravity[1]; float z = mGravity[2]; mAccelLast = mAccelCurrent; mAccelCurrent =(float) Math.sqrt(x * x + y * y + z * z); float delta = mAccelCurrent - mAccelLast; mAccel = mAccel * 0.9f + delta; // Make this higher or lower according to how much motion you want to detect if (Math.abs(mAccel) > 2) {
private void calculateVelocity(float x, float y, float z){
long now = System.currentTimeMillis();
interval = (now - lastEvent);
if(interval > 100){
lastEvent = now;
double acceleration = x+y+z-lastX-lastY-lastZ;
double velocity = v0 + (acceleration*(interval/(double)1000));
velocities.add(Math.abs(velocity));
v0= velocity;
lastX = x;
lastY = y;
lastZ = z;
}
}
这是我用来计算每 100 毫秒速度的代码。 velocities-array 用于计算每秒的平均速度,您可以忽略它。该方法的输入是 event.values