# Calculate acceleration/deceleration in the direction of travel from X,Y,Z accelerometer readings from iphone

I am writing an iPhone/iPad app. I need to compute the acceleration and deceleration in the direction of travel of a vehicle traveling in close to a straight horizontal line with erratic acceleration and deceleration. I have the sequence of 3 readings from the X,Y,Z orthogonal accelerometers. But the orientation of the iphone/ipad is arbitrary and the accelerometer readings include vehicle motion and the effect of gravity. The result should be a sequence  single acceleration readings which are positive or negative depending on whether the vehicle is decelerating or accelerating. The positive and negative direction is arbitrary so long as acceleration has the opposite sign to deceleration. Gravity should be factored out of the result. Some amount of variable smoothing of the result would be useful.

The solution should be as simple as possible and must be computationally efficient. The answer should be some kind of pseudo-code algorithm, C code or a sequence of equations which could easily be converted to C code. An iPhone specific solution in Objective C would be fine too.

Thanks Martin
###### Who is Participating?

Commented:
This problem is so simple that the orientation of the cell phone in the vehicle does not matter.
Really.

There is only one unknown -- the magnitude (and sign) of the acceleration along the x-axis.

The complete solution to the problem is:   |Ax|  =  SQRT( Aa²  +  Ab²  +  Ac²  -  g² )
where Aa, Ab,and Ac are the accelerometer readings.

This is the first time you have mentioned filtering.
But please try some calculations with real data.
0

Commented:
Given the constraints, this is easy.

Assume the vehicle is not moving.
Sum the signals from the A, B, and C accelerometers to get a 3D vector.
The magnitude of the vector must be   g = 9.8 m/s²
The direction gives the orientation of the sensor.

Now assume the vehicle is accelerating and decelerating arbitrarily along a straight horizontal line.  Call it the X-axis.
Sum the signals from the A, B, C accelerometers to get another 3D vector.
The magnitude of the vector   M must be GT or EQ to g.
This vector can only have two components:  Az = g   and   the vehicle acceleration Ax.

Ax = sqrt( M² - g²)   the sign is ambiguous.
0

Commented:
The vehicle is constrained to move along a straight, horizontal track.
Align the x-axis with the track, and the z-axis vertically.  The y-axis is irrelevant.

The sensor measures acceleration along three orthogonal axes --  a, b, and c.  The orientation of the sensor is arbitrary and even variable,

Since the vehicle and the sensor accelerate together:
Ax + Ay + Az  =  Aa + Ab + Ac    These are all vectors.

Ax is the only unknown.  Ay = 0,  Az = g,  and  Aa Ab Ac are all measured.

Therefore   (Ax)²  =  (Aa)²  +  (Ab)²  +  (Ac)²  -  g²
0

Author Commented:
Neither of the 'solutions' from d-glitch so far is helpful.

Yes the iphone is sitting in the vehicle. But it's orientation is arbitrary. If I could insist the phone were known and fixed in it's orientation there would really be no problem at all. But it is not known and may change periodically. So none of the accelerometer figures  necessarily corresponds to vertical (g), forward or sideways.

The solution must include specific equations for the vector and or filter math etc.. How do I rotate the X,Y,Z accelerometer data if that is the solution?

Thanks Martin
0

Commented:
Is this question resolved?

A moving average would probably be appropriate for the filtering.  What are the sample rate of the accelerometer and the required response time of the signal?

http://en.wikipedia.org/wiki/Moving_average
0

Software DeveloperCommented:
Hello Martin, after looking at this question I have to agree with dglitch. What you want to measure is acceleration, not direction, not velocity.  Whichever way the device is oriented, one of the accelerometers will detect motion.  The answer dglitch gives calculates the length of the vector given by the accelerometer readings, how much the car is moving.

The only problem is that acceleration has no direction.  You want to know when the car is travelling backwards.  You can do this by comparing the directions of the vectors, a dot product should be enough.  The problem is that you don't have a reference direction.  You could assume that the car starts moving forwards, but that's not 100% safe.  You could use the compass to establish the direction of the device if you know which way the car will be travelling, if it's following a road map for example.

The real issue is that you can't be sure of the orientation of the phone inside the car.  Being able to move the phone around would also cause distortion, rattling of the car and many other factors would interfere.  Depending on the distance involved you could try using GPS readings instead.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.