阅读使用加速度计的android手机的XYZ坐标加速度计、坐标、手机、android

2023-09-04 10:15:54 作者:沧海一声笑

我要制定机器人应用程序,它需要的读取X,Y,手机的三维空间 Z轴坐标。我搜索的网页天,但无法获得足够数量的细节的写一个简单的code和测试的设备上。 任何一个可以直接我去哪里寻找这一点。

I am going to develop android application which needs to read x,y,z coordinates of phone on 3D space. I searched for days on web but could not get enough amount of details to write a simple code and test on the device. Can any one direct me where to look for that.

期待你的帮助。谢谢你。

Looking forward your kind help. Thank you.

新到Android。 的设备和仿真器上都使用姜饼。 期待创建示例应用程序。

推荐答案

要得到加速位置,你需要两次整合。

To get position from acceleration you need to integrate it twice.

集成加速度为您提供了速度和整合的速度给您的位置。

Integrating acceleration gives you velocity and integrating the velocity gives you the position.

请记住,整合噪音产生漂移,漂移整合创造漂移很多,Android的传感器往往会产生相当多的噪音。

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.

在我的Galaxy S3的我已经能够使用谷歌的线性加速度复合传感器得到的位置漂移小于0.02米5秒。

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.

我不知道是否可以使用线性加速计传感器的姜饼。如果你不能,你将不得不整合之前删除的严重性。

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.

如果您还没有准备好,读这里的一切 http://developer.android.com/guide/topics/sensors/sensors_motion.html

If you haven't already, read everything here http://developer.android.com/guide/topics/sensors/sensors_motion.html

在Android的有关运动传感器一个伟大的谈

A great talk about the motion sensors in android

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.

如果您不删除重心将很快很远的地方。

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.