如何知道 Android 设备是否在 table 上平放
How to know if Android device is Flat on table
我正在使用加速计传感器来检测我的设备是否平放在 table 上。奇怪的是,即使我将 phone 放平或旋转到它的一侧,该值也始终在 90 到 100 之间!这不应该是正确的!我错过了什么吗?这是我的代码:
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float norm_Of_g =(float) Math.sqrt(x * x + y * y + z * z);
// Normalize the accelerometer vector
x = (x / norm_Of_g);
y = (y / norm_Of_g);
z = (z / norm_Of_g);
int inclination = (int) Math.round(Math.toDegrees(Math.acos(y)));
Log.i("tag","incline is:"+inclination);
if (inclination < 25 || inclination > 155)
{
// device is flat
Toast.makeText(this,"device flat - beep!",Toast.LENGTH_SHORT);
}
编辑:我正在使用此代码:How to measure the tilt of the phone in XY plane using accelerometer in Android
我使用了此页面上的代码Motion Sensors
public void onSensorChanged(SensorEvent event){
// In this example, alpha is calculated as t / (t + dT),
// where t is the low-pass filter's time-constant and
// dT is the event delivery rate.
final float alpha = 0.8;
// Isolate the force of gravity with the low-pass filter.
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
// Remove the gravity contribution with the high-pass filter.
linear_acceleration[0] = event.values[0] - gravity[0];
linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];
}
这样做的目的是在加速度计值中排除重力(通过重复测量),只留下每个方向的加速度分量。
重力是沿每个轴测量的,因此一旦您知道哪个轴代表平放的设备,您就可以检查大部分重力是否仅位于该轴上。这意味着设备平放在 table.
上
您还可以查看线性加速度以确保设备没有移动。
您使用的是 y 轴而不是 the answer you linked 中使用的 z 轴。
当参数接近 1 时,acos 的值将接近于零(或接近负 1 时接近 180 度),如图所示:
因此,只有当 y 轴归一化为大约 1 或负 1 时,例如当它与重力平行时,您的倾角才会接近 0(或 180)度,(因此,设备 "standing up").
如果没有其他错误,只需切换:
int inclination = (int) Math.round(Math.toDegrees(Math.acos(y)));
到
int inclination = (int) Math.round(Math.toDegrees(Math.acos(z)));
应该做。
我正在使用加速计传感器来检测我的设备是否平放在 table 上。奇怪的是,即使我将 phone 放平或旋转到它的一侧,该值也始终在 90 到 100 之间!这不应该是正确的!我错过了什么吗?这是我的代码:
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float norm_Of_g =(float) Math.sqrt(x * x + y * y + z * z);
// Normalize the accelerometer vector
x = (x / norm_Of_g);
y = (y / norm_Of_g);
z = (z / norm_Of_g);
int inclination = (int) Math.round(Math.toDegrees(Math.acos(y)));
Log.i("tag","incline is:"+inclination);
if (inclination < 25 || inclination > 155)
{
// device is flat
Toast.makeText(this,"device flat - beep!",Toast.LENGTH_SHORT);
}
编辑:我正在使用此代码:How to measure the tilt of the phone in XY plane using accelerometer in Android
我使用了此页面上的代码Motion Sensors
public void onSensorChanged(SensorEvent event){
// In this example, alpha is calculated as t / (t + dT),
// where t is the low-pass filter's time-constant and
// dT is the event delivery rate.
final float alpha = 0.8;
// Isolate the force of gravity with the low-pass filter.
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
// Remove the gravity contribution with the high-pass filter.
linear_acceleration[0] = event.values[0] - gravity[0];
linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];
}
这样做的目的是在加速度计值中排除重力(通过重复测量),只留下每个方向的加速度分量。
重力是沿每个轴测量的,因此一旦您知道哪个轴代表平放的设备,您就可以检查大部分重力是否仅位于该轴上。这意味着设备平放在 table.
上您还可以查看线性加速度以确保设备没有移动。
您使用的是 y 轴而不是 the answer you linked 中使用的 z 轴。
当参数接近 1 时,acos 的值将接近于零(或接近负 1 时接近 180 度),如图所示:
因此,只有当 y 轴归一化为大约 1 或负 1 时,例如当它与重力平行时,您的倾角才会接近 0(或 180)度,(因此,设备 "standing up").
如果没有其他错误,只需切换:
int inclination = (int) Math.round(Math.toDegrees(Math.acos(y)));
到
int inclination = (int) Math.round(Math.toDegrees(Math.acos(z)));
应该做。