12

How would one convert an inclinometers (Pitch, Yaw and Roll) into the gravitational pull expected on the system in [X,Y,Z]?

A system at rest in a certain Pitch, Yaw and Roll angle should be pulled to earth at a certain [X*g,Y*g,Z*g], lets say this is for simulation purposes. I want to make a function whoose input is Pitch, Yaw and Roll and the output is a Vector3(X,Y,Z) of the downard moment.

Meaning a object at rest with it's back downwards would output something like [0,-1,0] from the accelerometers and a [pitch,yaw,roll]->[0,-1,0], where [0,-1,0] minus [0,-1,0] resulting in [0,0,0]. or if we pull it left at the speed 1g we have a accelerometer showing [1,-1,0] making the new value [1,0,0].

With the system on its back [pitch,yaw,roll]->[0,-1,0] function is what i'm after

Vector3 OriToXYZ(float pitch, float yaw, float roll){
    Vector3 XYZ = Vector.Zero;
    //Simulate what the XYZ should be on a object in this orientation
    //oriented against gravity
    ...
    return XYZ;
}

Yes I know as the explanation below shows I'm not able to detect if the systems upside down or not based on the roll as roll only gives (-90 to 90) but that's a different problem).

This is how the orientation is laid out. inclometer decriptor

For extra information about why, what and how to use this information keep reading.

The plan is to use the incinometer as an alternative to the gyrometer for removing the gravity component to the accelerometer data, by simulating/calculating the expected value of gravity at orientation (Pitch,Yaw,Roll).

As the accelerometer(XYZ) is a combination of two components gravity(XYZ) and movement(XYZ), I'm assuming that gravity(XYZ)-calc_g(XYZ) = 0, allowing me to perform accelerometer(XYZ)- calc_g(XYZ) =movement(XYZ)

to show why i think this is possible. when i graph the values from the phone and move the phone sideways hard in a somewhat pendulum motion the lines that looks like sine/cosine motions are inclinometre and the other lines are XYZ accelerometer:

  • red = (Pitch & accell-X)
  • green = (Yaw & accell-Y)
  • blue = (Roll & accell-Z)

Acceleration value is multiplied by 90 as it ranges from (-2 to 2) so that it in the drawing ranges from -180 to 180, Pitch yaw and roll ranges as seen in the instructable above. The middle of the image is Y = 0, left side is X=0 (X=time)

Sensor measurements

Solved Solution by Romasz

VectorX = Cos(Pitch)*Sin(Roll);
VectorY = -Sin(Pitch);
VectorZ = -Cos(Pitch)*Cos(Roll);

Result enter image description here

*The graphs are not from the same measurement.

Community
  • 1
  • 1
Thomas Andreè Wang
  • 3,379
  • 6
  • 37
  • 53
  • http://www.blitzbasic.com/Community/posts.php?topic=82995 looks relevant – Stachu Jan 30 '14 at 17:04
  • 3
    This is a math problem and nothing to do with programming specifically. – Ashigore Jan 30 '14 at 17:05
  • 6
    This question appears to be off-topic because it is about Physics – 123 456 789 0 Jan 30 '14 at 17:06
  • I'd say it was more about maths personally... – Chris Jan 30 '14 at 17:10
  • 13
    It's a problem that is probably faced more than once by PROGRAMMERS within the realm of their regular tasks. This is very much on-topic, and likely will have relevance to someone in the future. – Stachu Jan 30 '14 at 17:12
  • This discussion appears to be pointless...vote to close or don't? – Liam Jan 30 '14 at 17:12
  • i need to figgure this out so that i can remove the gravity from a accelerometer. and in that way get access to the "true" acceleration values with gravity excluded. I can not be the only person attempting this. using Accel/Gyro is a no go as it is uselessly errorus. – Thomas Andreè Wang Jan 30 '14 at 17:14
  • @ThomasAndreèLian: Currently this question is too vague to answer, and it probably won't do what you want anyway -- there's no simple way to separate gravity from acceleration. – tom10 Jan 30 '14 at 17:47
  • if we simulate/calculate the gravity that is imposed on a object with a certain XYZ direction we should be able to remove that gravity. as the inclinometer dont have drift it will keep true, where as a gyrometer will drift and become worse and worse, atleas that how ive understood it. Im researching the usefullness of a inclinometer for this particular solution. – Thomas Andreè Wang Jan 30 '14 at 17:52
  • Do you really want the gravitational force removed? In a rotating frame of reference (eg the earth) then a stationary object (eg your phone on a table) will have no acceleration despite a gravitational force. Does an accelerometer really not report 0 in that situation? – Chris Jan 30 '14 at 17:54
  • @Chris the accelerometer will report a constant acceleration downwards. What that is as a XYZ vector, depends on the orientation of the phone. Now if we can simulate the gravity only, then hopefully it can be removed. Meaning that of you move we won't get your movement(XYZ) + gravity(XYZ) (where the gravity(XYZ) component is hidden in the movement(XYZ) component) but your movement(XYZ) + gravity(XYZ)- Calc_gravity(XYZ) and gravity(XYZ)- Calc_gravity(XYZ) hopefully is = 0. – Thomas Andreè Wang Jan 30 '14 at 17:56
  • 2
    Blimey. I assumed they'd be calibrated to avoid that. Still think that the actual question is mathematical rather than programming, even if it can be used for programming. If nothing else I suspect you are likely to get better and probably faster answers on http://math.stackexchange.com/ than you would here. – Chris Jan 30 '14 at 17:58
  • the really is no way to calibrate your way out of gravity. As the saying goes "gravity is a bi*ch" – Thomas Andreè Wang Jan 30 '14 at 18:01
  • Sure, if you knew the inclination you could remove gravity, but you don't and can't. **There is fundamentally no way to separate gravity and acceleration, and an inclometer will be thrown off in the same way the accelerometer is.** – tom10 Jan 30 '14 at 18:58
  • @tom10 by graphing the roll, pitch, yaw, accelX,accelY and accelZ i have determined that you are wrong. when jerking the phone the accelerometer readings do as expected they go haywhire. while the inclinometer values stay where expected. – Thomas Andreè Wang Jan 30 '14 at 19:38
  • The "inclinometer" may have a low pass filter that makes its response to jerks less apparent, but there's no magic way to know the incline. It just reports the angle of the phone relative to gravity, and therefore folds in acceleration too. If a filtered measurement of gravity works for you, then go for it.. but this wont be "removing the gravity component" any better than could be done directly with the accelerometer and gyroscope. Anyway, you know my opinion on this, so I'll let it go. – tom10 Jan 31 '14 at 00:01
  • 1
    as far as a physics standpoint, acceleration and gravity are the same thing, gravity is just constant acceleration toward the center of mass at the rate of (9.8m/s^2)^2 on earth. the reason you are getting the readings that you are getting on your inclinometer is because there is no time factor in the calculations such as the accelerometer would use. seeing as how this is programming we cant get exact acceleration with digital equipment because it can only sample and calculate the rate as fast as the processor can handle the calculations – CumminUp07 Jan 31 '14 at 04:07
  • Actually i would like to add a new graph to the picture. the graph of the calculated gravity(XYZ vector) of an object in a certain position(pitch,yaw,roll). I'm assuming that if i sum the calculated gravity it should be a flat line, but each of the components should be ever changing with movement, and then subtract-able from the accell XYZ making it more reliable. I'm not after a perfect solution here just something less unreliable, a proof of concept. – Thomas Andreè Wang Jan 31 '14 at 15:07
  • 1
    This question appears to be off-topic because it is a physics question, not a programming question. – Sander Feb 01 '14 at 12:14
  • 2
    This is very much a programming problem. The physics is easy. The programming is not. Voting to reopen. – David Hammen Feb 01 '14 at 13:59

2 Answers2

4

(Edited (completely) after comments)

If you want to calculate a gravity components in the direction of inclination then you will need only Pitch and Roll (in WP convention) - the rotation about Z (Yaw) doesn't have influance on accelerometers. The formula should look like this:

VectorX = Cos(Pitch)*Sin(Roll);
VectorY = -Sin(Pitch);
VectorZ = -Cos(Pitch)*Cos(Roll);

(Similar formula you can find for example here or here)

There can be some problems with accuracy from many reasons:

  • inclination is single precision, acceleration double
  • it may take a while for inclinometers to stabilize
  • different timing while performing readouts from Inclinometer and Accelerometer (lookout becasue those sensors have different minimum report interval)
  • accelerometers have different accuracy dependant on their position

Also watch out because accelerometers can be overloaded (their range is +-2g) - for example if you snap the phone.


To test it I have writted a simple App (which you can download here) - comparing Values indicated by Accelerometers and those calculated via inclination. Because values of accelerometers are relative to gravity, its strightforward:
In XAML - few TextBlocks:

<Grid x:Name="LayoutRoot" Background="Transparent" Margin="20">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" VerticalAlignment="Center" Orientation="Horizontal">
            <TextBlock Text="Incliation:" FontSize="16"/>
            <TextBlock Name="incXTB" Margin="10"/>
            <TextBlock Name="incYTB" Margin="10"/>
        </StackPanel>
        <StackPanel Grid.Row="1" VerticalAlignment="Center" Orientation="Horizontal">
            <TextBlock Text="Accelerometers:" FontSize="16"/>
            <TextBlock Name="accXTB" Margin="10"/>
            <TextBlock Name="accYTB" Margin="10"/>
            <TextBlock Name="accZTB" Margin="10"/>
        </StackPanel>
        <StackPanel Grid.Row="2" VerticalAlignment="Center" Orientation="Horizontal">
            <TextBlock Text="Through Inc:" FontSize="16"/>
            <TextBlock Name="accincXTB" Margin="10"/>
            <TextBlock Name="accincYTB" Margin="10"/>
            <TextBlock Name="accincZTB" Margin="10"/>
        </StackPanel>
</Grid>

In code behind:

public partial class MainPage : PhoneApplicationPage
{
    private Inclinometer myIncMeter = null;
    private float inclX = 0;
    private float inclY = 0;

    private Accelerometer myAccel = null;
    private double accX = 0;
    private double accY = 0;
    private double accZ = 0;

    public MainPage()
    {
        InitializeComponent();
        this.DataContext = this;
        myIncMeter = Inclinometer.GetDefault();
        myIncMeter.ReportInterval = myIncMeter.MinimumReportInterval;
        myAccel = Accelerometer.GetDefault();
        myAccel.ReportInterval = myIncMeter.MinimumReportInterval;

        CompositionTarget.Rendering += CompositionTarget_Rendering;
    }

    private void CompositionTarget_Rendering(object sender, EventArgs e)
    {
        InclinometerReading incRead = myIncMeter.GetCurrentReading();
        AccelerometerReading accRead = myAccel.GetCurrentReading();

        accX = accRead.AccelerationX;
        accY = accRead.AccelerationY;
        accZ = accRead.AccelerationZ;

        inclX = incRead.RollDegrees;
        inclY = incRead.PitchDegrees;

        incXTB.Text = "X: " + inclX.ToString("0.00");
        incYTB.Text = "Y: " + inclY.ToString("0.00");

        accXTB.Text = "X: " + accX.ToString("0.00");
        accYTB.Text = "Y: " + accY.ToString("0.00");
        accZTB.Text = "Z: " + accZ.ToString("0.00");

        accincXTB.Text = "X: " + ((Math.Cos(inclY * Math.PI / 180) * Math.Sin(inclX * Math.PI / 180))).ToString("0.00");
        accincYTB.Text = "Y: " + (-Math.Sin(inclY * Math.PI / 180)).ToString("0.00");
        accincZTB.Text = "Z: " + (-(Math.Cos(inclX * Math.PI / 180) * Math.Cos(inclY * Math.PI / 180))).ToString("0.00");
    }
}
Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • And nothing you said was new information. Well of course i cant calculate gravity in itself with inclinometer. But i should be able to calculate the amount of grav force expected in the position at any time as i know the overall gravitational pull (9.81). Look at it this way you have a pen and its tilted in balance the gravitational pull (accell) should be equal or 0 in two or three directions. now if we jerk the pen to the right it falls over because the accell is now increased to the right meaning that the pen will move in (pith, yaw and roll) changing the gravity value imposed on it. – Thomas Andreè Wang Jan 31 '14 at 13:28
  • 1
    @ThomasAndreèLian So then sorry, but I don't understand where the problem lies. Unless the accelerometer isn't self calibrating and inclinometer is fixed you should be able to do acc - 9.81*incl = acc_mov. As for me the biggest problem may be as I've said with accuracy and timing, as movement can be vary fast. – Romasz Jan 31 '14 at 13:38
  • 1
    @ThomasAndreèLian As I think now - then sorry but I probably had misunderstood your question :). It seems that you want to calculate the gravity component in specified direction. Then forget about my answer (I'll delete it within a day). Just be aware of mentioned problems (if your are not already), and one more: from my experience some accelerometers (I don't know if phones) have different accuracy depending on their position. – Romasz Jan 31 '14 at 14:02
  • 1
    @ThomasAndreèLian I've rebuild my answer showing how to determine the vector components of gravity with inclination + code example. Maybe it will help. – Romasz Feb 02 '14 at 21:26
  • This starts to look like what I'm after (I'm eager to test). Ohh I don't need one component, when i modeled with my hand i suddenly realized yea rotation around the Z axis is useless as grav is the same. ty need to verify but I think you'll get the accepted answer. – Thomas Andreè Wang Feb 02 '14 at 21:54
  • 1
    That worked like a charm. the calculated and measured gravity perfectly overlap in the graph. You had trouble with timing? think using a graphed line of each calc and measured. then take the first split second of data to finding the deviation and the correct for that. worked for me. only ran it for 5 minutes but i did not detect any shift over time there. – Thomas Andreè Wang Feb 02 '14 at 22:10
  • 1
    @ThomasAndreèLian I'm glad that I've helped. I thought about timing problems while you need to perform calculations on the fly. If you had recorded data (and then work with it), then you are right that correcting the deviation would solve the problem. But it can also be that I've exaggerated the problem. – Romasz Feb 02 '14 at 22:29
  • I'm using sampled data on the fly. it collects about 100 samples per second. and i use the previous data to fix the next data as when a new sample (s101) comes inn the calculation is ran over sample 2-101 then 3-102 and so on meaning that the last reading is always in the set. – Thomas Andreè Wang Feb 02 '14 at 22:41
  • 1
    @ThomasAndreèLian Nice, I will have to try it one day (if I only had more time :) ). I'm only curious about this 100 Hz, if you check in VS reportInterval then minimum for Inclinometer is 16ms and accelerometer 10 ms. As it's said http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.sensors.accelerometer.reportinterval the sensivity can vary. I'm curious if this interval is connected only with reporting event or with data collection by sensor. – Romasz Feb 02 '14 at 22:54
  • sorry im wrong. im keeping 10 s of data. and i have about 10 measurements per second. thanks for catching that. – Thomas Andreè Wang Feb 03 '14 at 01:49
  • @ThomasAndreèLian I've moved this discusstion to chat: http://chat.stackoverflow.com/rooms/46657/discussion-between-romasz-and-thomas-andree-lian – Romasz Feb 03 '14 at 07:09
2

First things first: accelerometers do not measure gravity. They measure acceleration due to every real force but gravity. (That's the Newtonian explanation. The relativistic explanation is even easier: Accelerometers measure acceleration due to all of the real forces acting on the accelerometer. Gravity is a fictitious force in general relativity.)

The first clue that accelerometers don't sense gravity is to look at the output of an accelerometer resting of the surface of the Earth. It registers an acceleration of about 1g upwards. The forces acting on the accelerometer are gravity, about 1g directed downwards, and the normal force, about 1g directed upwards. If accelerometers did sense gravity an accelerometer at rest on the surface of the Earth would register close to zero acceleration. They don't. All they are sensitive to is that 1g upwards normal force.

Another clue: Strap an accelerometer to a skydiver. While the skydiver is standing in the plane and waiting for the plane to reach the drop spot. The floor of the plane pushes the skydiver upwards, and that force propagates throughout the skydiver's body to the accelerometer. The accelerometer will register about 1g upwards. When the skydiver jumps, the accelerometer will suddenly register a sideways acceleration because the only force acting on the skydiver is the horizontal wind. There will be no upward or downward component to the registered acceleration. The drag force will shift to upwards as the skydiver falls and picks up vertical speed, making the accelerometer's output shift from sideways to upwards. The accelerometer output will spike when the skydiver pulls the ripcord and then drop as the skydiver reaches a steady speed. The accelerometer's output has changed drastically even though the gravitational force has hardly changed a bit.


So how to accomplish what you want? Because accelerometers do not sense gravitation, you need some kind of model for gravitation in your software. Depending on need, this model can range in complexity from

  • The very simple models used in game controllers. Those game control programmers might not even know that they're building a model of the Earth's local gravity field. The model doesn't have to be all that sophisticated as the controller isn't moving much.

  • The somewhat more sophisticated models used in systems for vehicles that might drive across or fly around town. Some require the operator to start the system and not move until the software says it's okay. During that startup time, the software is calibrating the local gravity field.

  • The even more sophisticated models used in longer range vehicle systems. Now the curvature of the Earth means "down" changes direction, and also means that the gravitational acceleration varies in magnitude.

  • The even more sophisticated models used to detect oil fields and such by variations in the gravitational field.

  • The very sophisticated models used in military planes and satellites that dock with other satellites.

You didn't say what level of sophistication you need. It's probably not the final category; if it was, you wouldn't be asking. You would have taken classes on the subject. You can probably get by with a system that learns the local gravity field. A simple averaging scheme at startup might suffice, or you might need a Kalman filter. The application is going to dictate the accuracy needed, and that in turn will dictated the required sophistication.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • Yes accelerometers don't measure gravity but they do measure acceleration and the earth pulls the accelerometer downwards at 9.81 m/s^2 distributed on the ZYX components of the accelerometer aka (gravity as we know it on earth). You are actually just confirming how the world works, and the need for simulating the "gravity" the plan is to compare this solution against the complementary and Kalman filter which removes some of the "gravity" over time. – Thomas Andreè Wang Feb 01 '14 at 11:19
  • Accelerometers **do not** measure the upward normal force. They are just little masses and springs: *gravity pulls the mass downward*, and they measure this deflection. This also happens to be exactly the same downward deflection that the mass would experience if the device were accelerating *upwards* at 1g, so these states give the same readings. In free-fall, gravity still accelerates the mass downward, but the rest of the device is also accelerating downwards, so there's no relative deflection and it measures 0. – tom10 Feb 01 '14 at 23:38
  • @tom10 - That's wrong. Gravity isn't a force. It is a force in Newtonian mechanics, but Newtonian mechanics isn't the best description of reality. Think of the equivalence principle. You wake up in a windowless elevator car. You feel the normal pull of gravity on your body. How can you tell whether you're standing on the surface of the Earth or are a hostage in some alien spacecraft accelerating at 1g? You can't. Gravity is not a force. It's a fictitious force. Accelerometers aren't sensitive to fictitious forces because they're fictitious. – David Hammen Feb 02 '14 at 00:00
  • @tom10 - The Newtonian explanation works quite nicely also, so long as you say that accelerometer are sensitive to all real forces except gravity. Drop an accelerometer attached to a parachute from a building. The accelerometer reading gradually increases from zero as the parachute+accelerometer system speeds up and encounters wind resistance. Attach an accelerometer to a helium balloon and let it go. The accelerometer reading exceeds 1g upward as the balloon ascends. …. Continued – David Hammen Feb 02 '14 at 00:13
  • In all cases, you can draw a free body diagram of all of the forces acting on the the accelerometer. Sum all of those forces *except for gravity*, divide by the accelerometer's mass, and voila! you have the accelerometer reading (sans noise of course, which accelerometers are wont to produce). – David Hammen Feb 02 '14 at 00:14
  • An accelerometer has two parts, the body and the sensor mass. When sitting on the ground, and the body is supported by the ground, the sensor mass is deflected downward (a model-independent physical fact). What is deflecting the mass downward? In my explanation: (classical) gravity is pulling the sensor mass downward; (GR) the body is held in place by the ground and the sensor mass is trying to follow the geodesic (free fall) but is held back by the spring. How about your model? – tom10 Feb 02 '14 at 01:10
  • @tom10 - My model is that an ideal accelerometer is a device that measures *proper acceleration*, which is acceleration due to all real forces (gravity is not a real force in GR). A physically realizable accelerometer comes very close to this ideal. The geodesic that the test mass would follow were it not for the measuring device that physically restrains the test mass defines the (GR) local inertial frame. Inertial frames in GR are not the same as inertial frames in Newtonian mechanics. ... – David Hammen Feb 03 '14 at 16:50
  • … You need a different model for how accelerometers work in a gravity field versus how they work far removed from one. The Newtonian model that accelerometers measure acceleration to all real forces except gravitation -- that works everywhere. The GR model that that accelerometers measure acceleration to all real forces, period -- that also works everywhere. – David Hammen Feb 03 '14 at 16:53
  • One last point: The acceleration registered by an accelerometer at rest on the surface of the Earth (a) points in the wrong direction and (b) except at the poles, has a magnitude different from acceleration due to gravity. The net force is not zero. The Earth is rotating, which means the accelerometer is undergoing circular motion. The net force is just that needed to make F=ma explain this circular motion. – David Hammen Feb 03 '14 at 16:56
  • There's no advantage to looking at it the way you describe when the standard way works just fine (and also explains everything, like why the little accelerometer mass is deflected downwards -- a question you always avoid, why the gravity reads as an upward acceleration, airplanes, parachutes, etc). I'd recommend learning and/or going back to the standard approach, as it answers everything in a consistent way. Anyway, this isn't the place for such a discussion, so I'm signing off here. – tom10 Feb 03 '14 at 18:22
  • There's an immense advantage to the way I describe it: It always works. It's not just my description. It is the standard description. Accelerometers ideally measure proper acceleration, which is acceleration due to real forces (GR perspective) / all real forces but gravitation (Newtonian perspective). – David Hammen Feb 04 '14 at 23:50