Accelerometer axes to tilt angles

Where can I find code to convert accelerometer axes values to vehicle tilt angles.

After searching the web I find there are some papers on this topic with the usual trigonometry to solve it but no actual code. Before reinventing the wheel, I am guessing there must be some here.

My goal is to write Rover code to prevent it moving any further forward with an increasing inclination ( pitch or roll ) which will result in the Rover rolling over. My 6WD Wild Thumper enjoys climbing anything, so I have to stop that.
Any ideas, thanks.

Short answer: yes - the code to do that already exists. It’s used extensively in the plane and copter code.

A bit of an explanation is needed though. Your intuition on the accelerometers providing lean angle is correct. By measuring which direction gravity is pulling on your rover you can tell it’s attitude. The problem is that most of the time the accelerometers are measuring much more than just gravity. Centripetal acceleration, motor vibrations, and every bump in the road will cause your accelerometer based attitude estimation to be all over the place.

Enter a branch of mathematics called estimation. Using some fancy math equations it is possible to use information from all the autopilot’s sensors to get a very good idea what the real attitude is. Estimation is rather complex but the good news is all the leg work has been done for you.

Look in libraries/AP_AHRS (attitude and heading reference system). In there is the code which does all the estimation for you. The examples folder should give you a good idea how to use the AHRS. You can also look at the coper and plane code to see how they use it.

Thanks for your note.
I didn’t search too far into the code but instead went ahead and wrote my own algorithm from scratch.
It is working fairly well giving me angles on all 3 axes which appear to be good within +/- 1 degree.
For my purposes ( ie Rover rollover prevention ) I think that is good enough.

I now need to figure out where to inject some throttle control given the known vehicle inclination.
Stay tuned.