如何使用加速度计获得运动方向? [英] How can I get the direction of movement using an accelerometer?

查看:25
本文介绍了如何使用加速度计获得运动方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Android 应用程序,我想知道是否可以检测固定一个轴的运动方向.例如,我想将手机放在桌子上,并在移动时检测方向(向左、向右、向上和向下).距离不是必须的,我只想知道准确的方向.

I'm developing a Android application and I would like to know if is possible detect the direction of movement with one axis fixed. For example, I want put my phone on the table and detect the direction when I move it (left, right, up and down). The distance is not necessary, I just want know the accurate direction.

推荐答案

是的.

使用 SensorEventListener.onSensorChanged(SensorEvent event),您可以确定沿 X & 提供的值.Y轴.您需要记录这些值,然后将它们与您在后续调用 onSensorChanged 方法时收到的任何新值进行比较,以获得增量值.如果一个轴上的 delta 值为正,则设备向一侧移动,如果为负,则向相反方向移动.

Using the SensorEventListener.onSensorChanged(SensorEvent event) you can determine the values provided along the X & Y axis. You would need to record these values and then compare them to any new values that you receive on subsequent calls to the onSensorChanged method to get a delta value. If the delta value on one axis is positive then the device is moving one way, if its negative its moving the opposite way.

您可能需要微调您接收加速度计事件的速率和您认为增量值以指示方向变化的阈值.

You will probably need to fine tune both the rate at which you receive accelerometer events and the threshold at which you consider a delta value to indicate a change in direction.

这是我正在谈论的内容的快速代码示例:

Here's a quick code example of what I'm talking about:

public class AccelerometerExample extends Activity implements SensorEventListener {
    TextView textView;
    StringBuilder builder = new StringBuilder();

    float [] history = new float[2];
    String [] direction = {"NONE","NONE"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        textView = new TextView(this);
        setContentView(textView);

        SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        float xChange = history[0] - event.values[0];
        float yChange = history[1] - event.values[1];

        history[0] = event.values[0];
        history[1] = event.values[1];

        if (xChange > 2){
          direction[0] = "LEFT";
        }
        else if (xChange < -2){
          direction[0] = "RIGHT";
        }

        if (yChange > 2){
          direction[1] = "DOWN";
        }
        else if (yChange < -2){
          direction[1] = "UP";
        }

        builder.setLength(0);
        builder.append("x: ");
        builder.append(direction[0]);
        builder.append(" y: ");
        builder.append(direction[1]);

        textView.setText(builder.toString());
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // nothing to do here
    }
}

此代码只会为您提供手机在 X 轴和 Y 轴上移动的大致方向.为了提供更细粒度的方向确定(例如,尝试模仿计算机鼠标的移动),您可能发现手机的加速度计不适合用途.

This code will only provide you with the general direction on the X and Y axis that the phone has moved in. To provide a more fine grained determination of direction (e.g. to attempt to mimic the movement of a computer mouse) you might find that a phone's accelerometer is not fit for purpose.

要尝试此操作,我首先将传感器延迟设置为 SensorManager.SENSOR_DELAY_FAST 并创建多个历史事件的列表,以便我可以随着时间的推移检测运动,而不会受到轻微运动的影响在如此精细的水平上进行加速度计测量时经常发生相反的方向.您还需要测量已经过去的时间量,以帮助计算您评论中建议的准确运动量度.

To attempt this, I would first set the sensor delay to SensorManager.SENSOR_DELAY_FAST and create a list of multiple history events so that I could detect movement over time and not be influenced by slight movements in the opposite direction that often happen when taking accelerometer measurements at such a fine level. You would also need to measure the amount of time that has passed to help calculate the accurate measure of movement as suggested in your comments.

这篇关于如何使用加速度计获得运动方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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