Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the direction of movement using an accelerometer?

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.

like image 522
Marcos Passos Avatar asked May 07 '12 04:05

Marcos Passos


People also ask

Can accelerometer detect direction?

Single- and multi-axis accelerometers can detect both the magnitude and the direction of the proper acceleration, as a vector quantity, and can be used to sense orientation (because the direction of weight changes), coordinate acceleration, vibration, shock, and falling in a resistive medium (a case in which the proper ...

How does an accelerometer determine position?

Re: Get a Position from Gyroscope and Accelerometer You need a magnetometer to calculate the attitude (rotation). Use the attitude to filter out the gravity acceleration from the raw accelerometer values so you're left with linear acceleration. Integrate it twice to get velocity and position and voila!

How does an IMU know which way it's facing?

As the name might suggest an IMU is capable of measuring orientation data and to achieve this it uses a combination of three sensors, namely Accelerometer, Gyroscope, and Magnetometer.

Which direction is the accelerometer measurement axis facing?

The X axis is parallel with the device's screen, aligned with the top and bottom edges, in the left-right direction. The Y axis is parallel with the device's screen, aligned with the left and right edges, in the top-bottom direction. The Z axis is perpendicular to the device's screen, pointing up.


2 Answers

Yes.

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

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.

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.

like image 138
Louth Avatar answered Sep 19 '22 09:09

Louth


From what I've read, with the sensors one can detect only accelerations and phone orientation. So you can easily detect the start of the movement (and in which direction) and the stop of the movement, since the velocity is changing so there is acceleration (when stoping acceleration is against the velocity direction).

If the phone is moving with constant velocity, the accelerometers will give zero values (linear acceleration which subtracts gravity). So in order to know if the phone is moving you should compute the velocity at each instant, by

V(t)=V(t-1)+a*dt

in which:

  • V(t-1) is the known velocity at previous instant,
  • V(t) is the velocity at current instant,
  • a is the acceleration (consider the acceleration in previous instant or mean acceleration between previous and current instant).

The problem is that due to the uncertainty of the sensor values, you might end up summing small errors at each instant, which will lead to erroneous velocity values. Probably you'll have to adjust a low pass filter to the values.

like image 39
renatogsousa Avatar answered Sep 23 '22 09:09

renatogsousa