使用加速度计读取Android手机的x y z坐标 [英] Read x y z coordinates of android phone using accelerometer

查看:32
本文介绍了使用加速度计读取Android手机的x y z坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将开发 Android 应用程序,该应用程序需要在 3D 空间中读取手机的 x、y、z 坐标.

I am going to develop Android application which needs to read x,y,z coordinates of phone on 3D space.

我想写一个简单的代码并在设备上测试..

I would like to write a simple code and test on the device..

我在设备和模拟器上都使用姜饼.

I am using ginger bread on both the device and emulator.

推荐答案

要从加速度中获得位置,您需要对其进行两次积分.

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 上,我使用 Google 的线性加速度计复合传感器在 5 秒内将位置漂移降低到 0.02 m.

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

代码:

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;
}

现在您有了 3d 空间中的位置,请记住,它假定手机在开始采样时是静止的.

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.

这篇关于使用加速度计读取Android手机的x y z坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆