# Detecting the movement direction using an accelerometer

• marcospassos
In summary, the conversation is about using an accelerometer to detect movement of an object on a table. The main issue is accurately detecting changes in direction, as the current algorithm only works well for slower, more gradual movements. The conversation delves into different formulas and patterns that could be used for better detection, but the main focus is on integrating the acceleration to get the velocity, and using that to determine the direction of movement. The conversation also includes sample code for the algorithm and suggestions for improving it.
marcospassos
I'm a few days trying to get the direction of movement using an accelerometer. For my application I need to put an object on the table (z axis fixed) and detect the movements. I know perhaps the accelerometer is not the best sensor for this kind of application but is the sensor that is available on the devices that I need to use.

For example:

- I move it to the right, left, right and then left quickly.
- The expected information: right, left, right, left

The pattern for the movement start is simple: I get the signal and compute the difference between the current value and last value (ignoring small values) and if is negative, than it is moving to the left, if is positive it is moving to the right. So, I've created a stop condition. When I've obtained X numbers of zeros of the difference I understand it is stoped. This algorithm works great and is very accurate.

The big problem is when I'm movig to right and I quickly move to the left. The algorithm cannot detect the change because he is waiting the stop and it doesn't happens.

I've analyzed the data several times, but I can not find a pattern to the end of the movement in order to create an new algorithm.

Bellow are some graphics that I've generated using the data obtained from accelerometer.

http://img31.imageshack.us/img31/9544/graphsjk.jpg

The chart on the left side is a simple and slow movement from the right to the left. In the right side is the problematic situation where the movement is from the right to the left and to the right again.

I would be grateful if you could help me.

Marcos

Last edited by a moderator:
Without a gyroscope, this will work only if the orientation (all axes) of the device is fixed. You have to know the initial velocity of the object when you start recording the acceleration (presumably zero, if the user is advised to hold the device still for a moment). Then you have to integrate the acceleration to get the velocity. There should be plenty of sample code for this online in any language used on mobile devices.

The pattern for the movement start is simple: I get the signal and compute the difference between the current value and last value (ignoring small values) and if is negative, than it is moving to the left, if is positive it is moving to the right.
That sounds like differentiation. What you have to do is something like this:

current_velocity = velocity_dt_ago + (acceleration_dt_ago + current_acceleration) / (2 * dt)

There a better integration algorithms though. Google for "runge kutta 4".

The velocity is not important to me. I just want to know the direction. In fact, I have the direction using my algorithm but it doesn't work when I change quickly. I believe there is a formula or pattern to identify when it occurs through the graph peaks.

http://img403.imageshack.us/img403/831/graphs2.jpg

Observing the following graph I can suggest that at point 1 the object starts moving to the left, because the graphic was stable and started decreasing. At point 2, I believe the movement from the left definately has stopped and started moving to the right, once the values started increasing. At point 3 the object is stopped again.

As it is possible recognize looking at, I believe is possible create a algorithm for this, using some formula or pattern.

Am I wrong?

Last edited by a moderator:
marcospassos said:
The velocity is not important to me. I just want to know the direction.
Velocity is a vector. It tells you the direction as well. You have to apply the intergation to all three components (x,y,z).
marcospassos said:
I believe is possible create a algorithm for this, using some formula or pattern.
Yes, integration.

A.T,

I'm trying get it work. What I'm doing:

- First, the algorithm "calibrates" the sensor getting 50 samples and using the average as the offset
- Subtracts the offset from the accelerometer value
- Stores the current value of accelerometer
- Makes comparisons using the speed in order to detect the direction

I think I'm doing something wrong, because doesn't works well. I moved the device from the left to right and then I stoped, it shows: left, right, stoped. Sometimes it shows always right or left.

Can you help me?

My algorithm:

// calibration... offsetX = SUM(50 samples)/50

accX[1] = (x - offsetX);

// Mechanical Filtering (remove some noise)
if((accX[1] < 0.2) && (accX[1] > -0.2)) {
accX[1] = 0;
}

// First X integration:
velocityX[1] = velocityX[0] + accX[0] + (accX[1] -accX[0]);
// Second X integration:
positionX[1] = positionX[0] + velocityX[0] + (velocityX[1] - velocityX[0]);

accX[0] = accX[1];
velocityX[0] = velocityX[1];

if(velocityX[0] < 0)
debug = "left";
else if(velocityX[0] > 0) {
debug = "right" ;
} else {
debug = "stoped";
}

marcospassos said:
// First X integration:
velocityX[1] = velocityX[0] + accX[0] + (accX[1] -accX[0]);
// Second X integration:
positionX[1] = positionX[0] + velocityX[0] + (velocityX[1] - velocityX[0]);
Ahhm... you are adding and subtracting the same values there (accX[0] and velocityX[0]).

Simple intergation works like this:
PHP:
velocityX[0] = 0 // we assume the device was static when we started recording acc
positionX[0] = 0

for every sample i > 0:
dt = time[i-1] - time[i] // if sampling rate is constant simply use 1 / frequency here
velocityX[i] = velocityX[i-1] + (accX[i] + accX[i-1]) / (2 * dt)
positionX[i] = positionX[i-1] + (velocityX[i] + velocityX[i-1]) / (2 * dt)

## 1. What is an accelerometer and how does it work?

An accelerometer is a sensor that measures acceleration, or the rate of change of velocity, in a given direction. It typically consists of a mass attached to a spring, which produces an electrical signal when the mass is moved due to acceleration. This signal can then be used to determine the direction and magnitude of the acceleration.

## 2. How does an accelerometer detect movement direction?

An accelerometer uses the principles of Newton's laws of motion to detect movement direction. When an object accelerates, it experiences a force in the direction of the acceleration. This force causes the mass in the accelerometer to move, which then produces an electrical signal that can be used to determine the direction of the acceleration.

## 3. What factors can affect the accuracy of an accelerometer's movement direction detection?

There are several factors that can affect the accuracy of an accelerometer's movement direction detection. These include the sensitivity of the sensor, external forces acting on the device, and the orientation of the device relative to the direction of movement. Additionally, any noise or interference in the signal can also impact the accuracy of the detected movement direction.

## 4. Are there different types of accelerometers used for detecting movement direction?

Yes, there are different types of accelerometers that can be used for detecting movement direction. Some common types include piezoelectric, capacitive, and MEMS (microelectromechanical systems) accelerometers. Each type has its own advantages and disadvantages, and the choice of which type to use depends on the specific application and requirements.

## 5. What are some real-world applications of detecting movement direction using an accelerometer?

There are many real-world applications for detecting movement direction using an accelerometer. These include navigation and tracking systems, such as in smartphones and fitness trackers, as well as in vehicles for airbag deployment and stability control. They are also used in robotics for movement control and in industrial machinery for vibration monitoring and analysis.

• Mechanics
Replies
11
Views
22K
• Mechanics
Replies
2
Views
10K
• Mechanics
Replies
3
Views
936
• Mechanics
Replies
8
Views
3K
• Mechanics
Replies
4
Views
8K
• Mechanics
Replies
10
Views
2K
• Mechanics
Replies
2
Views
859
• Introductory Physics Homework Help
Replies
27
Views
2K
• Mechanics
Replies
1
Views
1K
• Mechanics
Replies
1
Views
2K