使用加速度计读取Android手机的xyz坐标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11068671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read x y z coordinates of android phone using accelerometer
提问by Ruwantha
I am going to develop Android application which needs to read x,y,z coordinates of phone on 3D space.
我将开发需要在 3D 空间上读取手机的 x、y、z 坐标的Android 应用程序。
I would like to write a simple code and test on the device..
我想写一个简单的代码并在设备上测试..
I am using ginger bread on both the device and emulator.
我在设备和模拟器上都使用姜饼。
采纳答案by spontus
To get position from acceleration you need to integrate it twice.
要从加速度获得位置,您需要将其积分两次。
Integrating acceleration gives you velocity and integrating the velocity gives you the position.
积分加速度为您提供速度,积分速度为您提供位置。
Keep in mind that integrating noise creates drift and integrating drift creates A LOT of drift, the android sensors tend to generate quite a lot of noise.
请记住,积分噪声会产生漂移,积分漂移会产生大量漂移,Android 传感器往往会产生大量噪声。
On my Galaxy S3 I have been able to get the drift in position down to 0.02 m in 5 seconds using Google's Linear Accelerometer composite sensor.
在我的 Galaxy S3 上,使用 Google 的线性加速度计复合传感器,我已经能够在 5 秒内将位置漂移降低到 0.02 m。
I am not sure if you can use the linear accelerometer sensor on gingerbread. If you can't you will have to remove the gravity before integrating.
我不确定您是否可以在姜饼上使用线性加速度计传感器。如果不能,则必须在积分前去除重力。
If you haven't already, read everything here http://developer.android.com/guide/topics/sensors/sensors_motion.html
如果您还没有,请阅读此处的所有内容 http://developer.android.com/guide/topics/sensors/sensors_motion.html
A great talk about the motion sensors in android
关于 android 中的运动传感器的精彩演讲
http://www.youtube.com/watch?v=C7JQ7Rpwn2k
http://www.youtube.com/watch?v=C7JQ7Rpwn2k
Code:
代码:
static final float NS2S = 1.0f / 1000000000.0f;
float[] last_values = null;
float[] velocity = null;
float[] position = null;
long last_timestamp = 0;
@Override
public void onSensorChanged(SensorEvent event) {
if(last_values != null){
float dt = (event.timestamp - last_timestamp) * NS2S;
for(int index = 0; index < 3;++index){
velocity[index] += (event.values[index] + last_values[index])/2 * dt;
position[index] += velocity[index] * dt;
}
}
else{
last_values = new float[3];
velocity = new float[3];
position = new float[3];
velocity[0] = velocity[1] = velocity[2] = 0f;
position[0] = position[1] = position[2] = 0f;
}
System.arraycopy(event.values, 0, last_values, 0, 3);
last_timestamp = event.timestamp;
}
Now you have the position in 3d space, keep in mind it assumes that the phone is stationary when it starts sampling.
现在您有了 3d 空间中的位置,请记住,它假定手机在开始采样时是静止的。
If you don't remove gravity it will soon be very far away.
如果你不去除重力,它很快就会很远。
This doesn't filter the data at all and will generate a lot of drift.
这根本不会过滤数据,并且会产生很多漂移。
回答by Eight
Read this tutorial.
阅读本教程。
brief summary of the above given tutorial ::
上面给出的教程的简要总结::
first get an instance of SensorManagerand Sensor.
Inside onCreate()
::
首先获取SensorManager和Sensor的实例。
里面onCreate()
::
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
after this, override onSensorChanged(SensorEvent event)
and use event.values[]
to get the co-ordinates.
在此之后,覆盖onSensorChanged(SensorEvent event)
并使用event.values[]
来获取坐标。
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
}