back to writing

Building Lean With It: motorcycle lean from an iPhone

·

Lean With It began with a question that sounded simple: if an iPhone is rigidly mounted to a motorcycle, can it record a believable lean angle for the ride?

The phone already has the sensors. Core Motion provides a fused attitude from the accelerometer, gyroscope, and magnetometer. Core Location provides speed, course, and the route. The hard part is turning those observations into motorcycle motion without presenting false precision.

It is not just phone roll

My first estimator treated the phone's starting attitude as the reference and extracted roll from the relative quaternion. That worked on the bench. It failed on the first real ride.

As the motorcycle changed compass heading, the relative rotation contained yaw as well as bank. The estimator increasingly interpreted heading as lean and eventually produced values near plus or minus 180 degrees. The recording itself was healthy—more than an hour of continuous 50 Hz motion with no gaps over 200 milliseconds—but the derived result was wrong.

That distinction became a central design rule: observations are source data; lean is an estimate. The app keeps the original motion and route streams so an estimator failure can be fixed and replayed without repeating the ride.

Finding upright while moving

The phone's coordinate system is not the motorcycle's coordinate system, and the bike often starts on its side stand. Treating the stationary starting posture as upright would bake a large offset into every result.

The replacement estimator calculates bank against gravity around the motorcycle's forward axis, which makes it invariant to compass heading. Lean With It then learns a riding zero from sustained, GPS-confirmed straight riding. The side-stand posture remains the mount reference; the neutral posture while moving becomes the zero used for lean estimates.

The first version learned that zero too quickly. On a follow-up ride it committed after roughly two seconds of qualifying motion and ended up 2.84 degrees away from the more stable full-ride baseline.

The next version waits for evidence across multiple straight route intervals, checks the spread of the candidate observations, and persists the accepted solution with its quality data. On its first on-device test, it accepted after 39.1 seconds and landed within 0.22 degrees of an independent full-ride baseline. Two later ride legs differed by 0.52 and 1.04 degrees.

Those comparisons do not prove absolute lean accuracy. They do show that the app can establish a repeatable internal reference instead of silently changing its answer after the ride.

Fast IMU, slow GPS

The IMU responds quickly but has to be interpreted carefully. GPS course is slower and noisier, but it provides an independent clue about whether the motorcycle is turning left or right.

That makes the two sensors complementary. Motion drives the responsive lean curve. GPS helps identify straight riding, constrain direction, and segment the route into corners.

Real recordings exposed a timing problem: the reported GPS course change can arrive after the IMU lean event. On two recent ride legs, comparing the signals with no timing adjustment produced correlations of 0.78 and 0.70. Comparing GPS with IMU observations roughly 1.0 to 1.25 seconds earlier raised those correlations to about 0.90 and 0.91, with direction agreement above 90 percent.

Lean With It now estimates a quality-gated GPS/IMU timing lead for each ride. Source timestamps never change. The alignment is versioned derived analysis used to compare the streams and search a bounded earlier window for the lean peak associated with a GPS-defined corner.

That recovered one corner card's peak from a truncated 19.5-degree result to 35.0 degrees without weakening the rules that decide whether the corner itself is credible.

Build for after the ride

The live screen has a large gauge, recording state, elapsed time, and speed, but the product is intentionally a recorder first. It should not require attention while the motorcycle is moving.

The useful surface is the ride detail view after parking. It includes:

  • duration, distance, speed, and credible left/right peaks;
  • synchronized lean and speed charts;
  • a route replay colored by estimated lean magnitude;
  • scrubbing that keeps time, lean, speed, and map position aligned;
  • conservative corner cards with synchronized replay; and
  • explicit archiving, deletion, and raw-file export.

The app avoids lean scores, traction-margin claims, and prompts to push harder. A phone-only estimate cannot say how much grip remains or whether an angle is safe.

What is validated

Five road recordings have now produced continuous 50 Hz motion capture. Locked-screen operation was explicitly established on the first three. The current riding-zero approach has reproduced stable live baselines, and GPS and IMU have shown strong directional agreement after accounting for course latency.

There is still important work left. Motorcycle vibration and movement in the Quad Lock dampener need broader analysis. Absolute angle accuracy needs a credible reference. Road camber and low-speed events need better confidence treatment, and physical left/right direction needs more rider-confirmed validation.

For now, Lean With It remains a personal iPhone app and a sensor-feasibility project. That is the point of the current phase: keep the evidence, label the uncertainty, and make the estimator earn trust one recording at a time.

See the Lean With It project overview →