How Offroad Buddy Calculates Pitch and Roll
Why This Was Harder Than I Expected
When I started building Offroad Buddy, I assumed measuring pitch and roll from an iPhone would be trivial. The hardware is there — accelerometer, gyroscope, all of it. I figured I'd read the values, display them, done.
The first version I shipped used the accelerometer alone. On pavement, it worked great. The moment I took it off-road — even on a mild gravel road — the readings were a flickering mess. Every bump sent the numbers jumping ±5 degrees. It was useless. I pulled over, opened a notes app, and started researching why.
What I found is that accurate angle measurement in a moving vehicle is a well-studied problem, and the solution isn't complicated once you understand it — but it's not obvious.
The Two Sensors and Why Each One Fails Alone
The Accelerometer
In a perfectly still environment, the accelerometer measures exactly one thing: gravity. Since gravity always points straight down, you can calculate the tilt of the phone (and therefore the vehicle) very precisely from that measurement. Static accuracy is excellent.
The problem is that off-road driving is never still. Every rock, every rut, every bump adds its own acceleration vector on top of gravity. The accelerometer can't distinguish between "the vehicle is tilted" and "the vehicle just hit something." In rough terrain, those forces are constant and large. The result is readings that are accurate on average but jittery in a way that makes them unreadable in real time.
The Gyroscope
The gyroscope measures rotation rate — how fast the phone is spinning on each axis. By integrating that rate over time, you can calculate the current angle. This produces beautifully smooth readings that don't react to bumps at all.
The catch is drift. Gyroscopes accumulate small measurement errors over time, and since you're integrating, those errors add up. A tiny consistent error in rotation rate becomes a degree of angle error every few minutes. After 20 minutes of recording, the gyroscope-only angle can be several degrees off from reality with no way to self-correct.
I actually ran some tests on this: gyroscope-only tracking on a 30-minute off-road loop drifted about 4 degrees by the end of the session. Not catastrophic, but noticeable and unacceptable for a tool people use to make safety judgments.
The Complementary Filter
The solution I settled on is called a complementary filter. The idea is to use each sensor where it's reliable and filter out where it isn't:
- For rapid, short-term changes — trust the gyroscope. It's smooth and doesn't get confused by bumps.
- For long-term correction — trust the accelerometer. When things settle, it knows where "down" actually is.
The filter continuously blends the two signals using a tuning constant. I spent a few evenings on a dirt road testing different values — too much accelerometer weight and the readings got jittery again; too little and drift started showing up on long sessions. The value I landed on gives smooth readings in rough terrain while keeping drift negligible across a full day of use.
I looked at Kalman filters as an alternative (more sophisticated, theoretically more optimal) but the implementation complexity wasn't worth it for this use case. The complementary filter is simpler to understand, simpler to tune, and for vehicle telemetry the results are effectively equivalent.
Why Calibration Matters
Before every session, the app asks you to place your phone flat and calibrate. This does two things: it zeros out the current orientation as the baseline, and it resets any accumulated gyroscope state from before you started.
The most common complaint I hear about inaccurate readings is from people who skip calibration. Without it, the app is measuring angles relative to whatever position the phone was in when you last used it — which might be sitting on your kitchen counter at a slight angle. The zero reference being wrong makes every reading wrong by the same fixed offset.
Two seconds of calibration at the trailhead fixes this. It's worth making it a habit.
Accuracy in Practice
In typical off-road conditions, the filter achieves about ±1 degree on both pitch and roll axes. I validated this by mounting a bubble inclinometer on my dash alongside the phone during a test run and comparing readings manually at various points. The match was consistently within 1 degree except on very rough washboard at speed, where vibration pushed the error to about 2 degrees temporarily.
For very high-vibration environments — sustained washboard at 40 mph, for instance — accuracy does degrade. The filter leans more heavily on the gyroscope in those conditions, which means drift can accumulate faster. A mid-trip recalibration when you stop for a break clears it cleanly.
One degree of accuracy is comparable to a decent aftermarket inclinometer. The advantage of doing it in software is that the numbers are also being recorded continuously alongside your GPS track, so you can review them after the run instead of trying to read a gauge while you're driving.
Ready to Track Your Adventures?
Download Offroad Buddy today and start recording professional-grade telemetry.
Download Now