如何使用三轴加速器 计算速度
大虾:
请教一下:如何使用三轴加速器 计算速度 与 方向
android 7.1
我们使用的是 mpu6881 六轴的芯片,从android取的数据来看,只能取 Sensor.TYPE_ACCELEROMETER 的x y z 值
我尝试这样取速度 ,速度没有变化
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
double x = event.values[0];
double y = event.values[1];
double z = event.values[2];
double xyz = (double)(x*x + y*y + z*z);
yz_plane = (double)(Math.sqrt((y)*(y)+(z)*(z)));
xz_plane = (double)(Math.sqrt((x)*(x)+(z)*(z)));
xy_plane = (double)(Math.sqrt((x)*(x)+(y)*(y)));
if(curTime-lastUpdate > TIME){
long diffTime = curTime-lastUpdate;
if(diffTime <= 0)diffTime = 1;
lastUpdate = curTime;
x = event.values[SensorManager.DATA_X];
y = event.values[SensorManager.DATA_X];
z = event.values[SensorManager.DATA_X];
double speed = Math.abs(x+y+z-last_x-last_y-last_z)/diffTime*10000;
Log.i("lock"," speed "+speed);
if(speed > SHAKE_THRESHOLD){
Log.i("lock","speed = "+speed);
lastUpdate += 10000;//判断成功的话,10秒内不再判断
// handler.sendEmptyMessage(MessageID.MESSAGE_SENSOR);
}
last_x = x;
last_y = y;
last_z = z;
}