Sensor Fusion Explained: How to Build a High-Accuracy Fitness Rep Counter
Source: Dev.to
The Problem with Single‑Sensor Tracking
Fitness apps often miscount repetitions, especially for complex movements. Accelerometers in phones are noisy, and gyroscopes drift over time, leading to “phantom” reps or missed sets.
Why One Sensor Isn’t Enough
Accelerometer
- Measures proper acceleration (including gravity)
- Stable over long periods
- Highly sensitive to vibrations and sudden shakes
Gyroscope
- Measures rotational speed
- Excellent for short‑term precision
- Suffers from drift, causing orientation errors over time
Sensor Fusion with a Complementary Filter
By combining the accelerometer’s long‑term stability with the gyroscope’s short‑term accuracy, we obtain a reliable orientation signal.
Formula
FusedAngle = α * (GyroAngle) + (1 - α) * (AccelAngle)
- Typical α (alpha) = 0.98 → trust the gyroscope 98 % of the time, using the accelerometer to correct drift.
Comparison of Sensors and Fusion
| Feature | Accelerometer | Gyroscope | Fused Result |
|---|---|---|---|
| Short‑term Accuracy | Low (Noisy) | High | High |
| Long‑term Stability | High | Low (Drift) | High |
| Vibration Resistance | Low | High | Balanced |
Counting Reps with a State Machine
Instead of counting every signal peak, a finite‑state machine tracks the phases of an exercise (e.g., a bicep curl):
| State | Description |
|---|---|
| IDLE | User is at rest. |
| GOING_UP | Angle crosses an entry threshold (e.g., 30°). |
| GOING_DOWN | User reaches the peak and begins the return movement. |
| REPETITION | Return completes → increment count and reset to IDLE. |
This approach filters out jitter and mid‑rep pauses, preventing false counts.
Adapting the Framework to Other Exercises
- Kettlebell Swings – Track torso pitch angle.
- Squats – Use vertical movement and thigh orientation.
The same fusion and state‑machine logic can be tuned for various motions.
Conclusion
Real‑time, accurate health feedback depends on reliable sensor data. Mastering sensor fusion lets developers create fitness tools users can trust for daily progress.
For a full Python implementation and step‑by‑step guide, see WellAlly’s comprehensive rep‑counter tutorial.