Wireless Lean Sensor
Part of BEATS, GT Animal Lab
Firmware that turns a matchbox-sized M5Stack Capsule into a wireless tilt sensor. It fuses gyroscope and accelerometer data into a steady orientation estimate 100 times a second and streams roll, pitch and yaw over its own Wi-Fi network to a live plot.
- 100 Hzsensor fusion rate
- 50 Hzwireless stream to the plot
- ~2 sgyro calibration at power-on
The problem
The camera-based Lean Tracker needs a clear view of the rider. A small sensor worn on the body measures lean directly, whatever the camera angle or lighting. It has to be wireless, because a rider on a moving horse can’t be plugged into a laptop.
How it works
- Sensor fusion. A gyroscope is smooth but drifts over time. An accelerometer doesn’t drift but is noisy and gets thrown off by the horse’s movement. A Madgwick filter combines the two at 100 Hz into one steady orientation.
- Ignoring bumps. When the measured acceleration is far from 1 g (more than 0.15 g off), the board is being jolted. The filter then ignores the accelerometer and trusts the gyroscope until things settle.
- Calibration. At power-on, with the Capsule lying still, it measures the gyroscope’s bias for about two seconds and subtracts it from every reading after.
- Its own network. The Capsule creates its own Wi-Fi access point and broadcasts roll, pitch and yaw over UDP to Teleplot at 50 Hz, so any laptop that joins sees a live graph. No router is needed at the barn.
What I built
- The firmware: IMU reads over I2C, gyro calibration, the Madgwick filter, accelerometer gating, and the Wi-Fi access point plus UDP streaming.
- A PlatformIO setup for building and uploading from VS Code, with a serial plotter for wired debugging.
What I learned
Every sensor is wrong in its own way, and the job is to combine them. On its own, the gyroscope drifted slowly and the accelerometer jumped with every bump. Sensor fusion came down to two decisions:
- How much to trust each sensor. The Madgwick filter’s gain sets the balance. I tuned it down to 0.03, so the gyroscope gives smooth readings and the accelerometer slowly corrects the drift.
- When not to trust a sensor at all. On a moving horse, the accelerometer measures the horse’s motion, not just gravity. Ignoring it whenever the total acceleration was more than 0.15 g from 1 g stopped those jolts from showing up as fake leans.
I also learned to design for the field, not the desk. A barn has no router and no outlet next to the rider, so the Capsule runs on its battery and hosts its own Wi-Fi network.
What I’d do differently
- Log to a file, not just a live plot. Teleplot is great for watching data, but the study needs recordings. Next, I’d timestamp and save every reading so sessions can be analyzed and compared with the camera-based Lean Tracker.
- Measure against a known angle. I’d check the readings against fixed tilts (for example, a board propped at 10°) and report the error, instead of judging accuracy by eye.