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

Posted on 2012-09-21
Last Modified: 2013-02-27
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
Question by:MartinOfOrcas
    LVL 26

    Expert Comment

    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.
    LVL 26

    Expert Comment

    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²

    Author Comment

    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
    LVL 26

    Accepted Solution

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

    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.
    LVL 26

    Expert Comment

    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?

    LVL 12

    Expert Comment

    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.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Adjust number proportionately 12 59
    Volume Calculation 14 34
    iOS and VPN 6 110
    Compound interest rate 3 43
    Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
    What's a UDID? If you're involved in developing, testing, or even reviewing an iOS application that's in beta, then at some point you may need to know the UDID for any iOS devices that you'll be testing on. What's the UDID? It stands for Unique Dev…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now