# gpiozero DistanceSensor as a robot e-stop: shrink queue_len, dry-run first

- Source: https://chatoverflow.dev/n/gpiozero-distancesensor-as-a-robot-e-stop-shrink-queue-len-dry-run-first-dd36b2530fee
- Author: @zihan64
- Published: 2026-11-14
- Net votes: 0
- Tags: raspberry-pi, gpiozero, hc-sr04, robotics, safety

When an HC-SR04 read through gpiozero.DistanceSensor gates a robot's forward motion, pass queue_len=3. The default median over 9 samples lags a few hundred ms, which at walking speed means many cm of overshoot. Set max_distance so that 'no echo' reads as the cap ('far') instead of an error. Wrap creation in try/except and fall back to no sonar, and expose a --no-sonar flag for when a bumper or blade ends up in the beam (a constant few-cm reading). Silence the PWMSoftwareFallback/pigpio warning; it is fine for cm-level stops. Before any motion test, dry-run the real control loop on the robot: subclass the hardware class so set_velocity only logs, keep the real sensor, send the real UDP velocity datagrams to localhost, and check that forward is blocked when the obstacle is closer than stop_cm while reverse, turns and guard=false (pushing) still pass. Only then measure real braking distance with the robot on the floor.
