Robotics from zero · Lab
How does a robot stop before an obstacle?
The sensor sees the obstacle at a set distance, the threshold. The robot keeps rolling while the stop command is on its way, then while it brakes. It stops in time only when the threshold is longer than that stopping distance.
A lab for Chapter 4: explore sensing delay and physical stopping limits. Return to the parent chapter →
Stops 8.75 cm before the obstacle
- Sensor sees it at
- 20 cm
- Rolled during the delay
- 5 cm
- Rolled while braking
- 6.25 cm
- Stopping distance
- 11.25 cm
- Gap left
- 8.75 cm
This is a model. Its numbers explain an idea. They are not safety settings for a real robot.
Under the hood
Stopping takes two stretches of floor. The robot covers the first while the stop command is still on its way, and the second while its wheels brake.
stopping distance = speed × delay + speed² ÷ (2 × braking rate)
- Speed: how fast the robot drives, in metres per second (m/s).
- Delay: the time from the moment the obstacle is inside the threshold until the motors get “stop”. It adds the sensor's reading time and one pass of the control loop, in seconds (s).
- Braking rate: how quickly the robot loses speed once it brakes, in metres per second squared (m/s²). This model uses 2 m/s².
Worked example with the model's starting values
- During the delay: 0.5 m/s × 0.1 s = 5 cm
- While braking: 0.5² ÷ (2 × 2) = 6.25 cm
- Stopping distance: 5 cm + 6.25 cm = 11.25 cm
- The threshold is 20 cm, longer than 11.25 cm, so the robot stops 8.75 cm before the obstacle.
Why speed counts twice
Double the speed and the distance rolled during the delay doubles. The braking distance grows four times, because speed is squared. At 1 m/s the same robot needs 25 cm just to brake.
Real-world catch
- Floors and tyres change the braking rate. A smooth floor or a dusty wheel can make the real stop longer than the model's.
- Motors do not stop at once. The motor driver, the gearbox and the robot's own mass keep it rolling for a moment.
- The delay is not fixed. A busy control loop or a slow sensor reading can make one stop late, even when most stops are on time.
- The sensor can miss what it should see: a thin chair leg, a soft cushion or an angled surface. The ultrasonic sensor lesson shows why.
Check yourself
Predict first, then open the answer. Nothing is graded.
The robot drives at 1 m/s, the delay is 100 ms and the threshold is 20 cm. Does it stop in time?
Show the answer
No. It rolls 10 cm during the delay and 25 cm while braking, 35 cm in total. That is more than the 20 cm threshold, so it hits the obstacle. Set the same values in the model to watch it.
At 0.5 m/s you halve the delay from 100 ms to 50 ms. Which part of the stopping distance changes?
Show the answer
Only the distance rolled during the delay: it drops from 5 cm to 2.5 cm. The braking distance stays 6.25 cm, because braking still starts at the same speed.
At high speed, which helps more: a faster control loop or better brakes?
Show the answer
Better brakes. The delay part grows in step with speed, but the braking part grows with the square of speed. At 1 m/s with a 100 ms delay, the robot rolls 10 cm during the delay and 25 cm while braking.
From model to hardware
On a real robot the same rule is a few lines inside a loop. This is logic to read, not wiring instructions.
every 20 ms: # repeat every 20 ms
distance = read_ultrasonic() # distance from the sensor, in cm
if distance < THRESHOLD_CM: # the obstacle is inside the threshold
motors.stop() # tell the motor driver to stop
else:
motors.forward(SPEED) # otherwise keep driving
Each pass of the loop takes time, and so does each sensor reading. The datasheet of a common ultrasonic sensor, the HC-SR04, suggests a measurement cycle of more than 60 ms. With a 60 ms reading and a 20 ms loop, the delay can reach 80 ms.
That is why correct code can still hit an obstacle: the rule is right, but it runs too late for the speed.
Parts that do this job: an ultrasonic distance sensor, a microcontroller board, a motor driver, two DC gear motors and a battery pack.
Terms in this lesson
- Threshold
- The distance at which the robot decides to stop.
- Delay (latency)
- The time between the obstacle crossing the threshold and the motors getting the command.
- Reaction distance
- How far the robot rolls during the delay.
- Braking distance
- How far the robot rolls once it brakes.
- Control loop
- The cycle of read, decide and act that the robot repeats many times a second.
Return to the parent chapter →