Pitch and Roll from LSM303 DLH

Hi - and apologies in advance - I'm just getting into the Arduino experience and hence there may be plenty of schoolboy errors.

I've got a Uno board and a LSM303 DLH Accelerometer and Magnometer. I've got it to work using the great download libraries (tilt-compensated compass, calibration, raw output), but what I'd like is to calculate the pitch and roll angles if possible.

I've done a fair amount of searching (although not always knowing what I'm looking for), and an application note suggests that it's possible, but the formulas are confusing me.

I've also seen a video of it running here...


...but it's in Russian without further information. I think an issue I'm having is that I'm not entirely sure what the "raw output" function is actually giving me and what I can do with that.

I don't mind putting the work in, but I've hit a bit of a wall on where to look and what to look for. Any help gratefully received.

Simon (edit to tidy youtube embed)

You can probably find out more about the raw data in the datasheet: http://www.pololu.com/file/download/LSM303DLM.pdf?file_id=0J514

Once you get the X, Y and Z accelerations into floats you just need some trig to calculate Pitch and Roll (in radians):

pitch = atan (x / sqrt(y^2 + z^2)); roll = atan (y / sqrt(z^2 + z^2));

Fabulous, thank you for that. I've looked through the datasheet, but it's still a bit confusing so far (I come from a programming background).

However, I think the serial script in the supplied library gives you access to the raw accelerometer data, so I'll brush up my trig and give that a go.

Thanks once again.


Finally got some results from this. I'd been getting lots of zeros for the pitch/roll, until I realised that I'd been declaring pitch and roll in integers and the results would return radians. Using floats and converting to degrees afterwards gave me great results.

The problem I'm having now is that the roll appears to be +-90 degrees. That is, when I roll the unit past 90degrees, the roll goes back to zero. I managed to get some software working that interpreted the serial output into a visual representation, which worked fine until I got over 90.

I can understand it with pitch, since once you're over 90 degrees, the heading would change, but roll I'm not so sure on. Ideally I'm looking to calculate whether the unit is inverted

Is this a limitation of the hardware I'm using, the calculations, or am I just not understanding the situation properly?

I think the problem is that the math throws away the quadrant information since y*y and -y*-y result in the same value. You probably have to check the sign of X, Y, and Z to determine the actual angle.

Ahh, thank you again. That's given me plenty to look into.

Cessquill: Ahh, thank you again. That's given me plenty to look into.

Tell us please how you do that?