Programming · Sensors
Make sensors trustworthy
Encoders, IMU, distance, and touch sensors—how to read, verify, and keep them honest.
What this is
- Quick patterns for reading and validating common FTC sensors.
- Checks to catch wiring, config, or orientation issues early.
- Tips to keep sensor data stable for auto and TeleOp aids.
When you need it
- Sensor readings are zero/NaN or jumpy.
- Encoders disagree or drift in straight drives.
- IMU heading looks wrong after init or impacts.
How it works
- Encoders: read currentPosition each loop; reset before auto; use consistent ticks-per-rev and wheel diameter.
- IMU: initialize once before start; use remapped axes only if needed; read heading in radians or degrees consistently.
- Distance/Touch: debounce or average a few samples; check for saturation (too close/too far).
- Validation: compare left/right encoder deltas for straight moves; ensure heading stays near zero when stationary.
Common mistakes
- Mixing units (ticks vs inches, radians vs degrees).
- Not resetting encoders/IMU before auto start.
- Loose encoder cables or reversed motor directions causing negative counts.
- Ignoring sensor init failures in logs/telemetry.
How to test it
- Spin one wheel and confirm only that side’s encoder changes and sign is correct.
- Rotate robot 90°; heading should change ~90 with correct sign.
- Move toward/away from an object; distance sensor should decrease/increase smoothly.
- Press touch sensor repeatedly; no bounce after minimal debouncing.
Related problems
- Heading jumps after bump → re-initialize IMU, check mounting, reduce vibration.
- Encoder mismatch → check wheel slip, gear ratio constants, motor direction, cable seating.
- Distance sensor stuck → target too close/far or angled; add range checks and fallback.
