The sense–think–act loop in time
Looking closely at each stage of the robot's loop — perception, decision and action — and at the timing that decides whether the loop works at all.
Lesson 4 of 6 · 30 min · Self-paced · Free
A loop that takes time
In the first lesson of this module you met the idea that a robot senses, thinks and acts in a loop. That picture is correct but incomplete, because it hides something every robot engineer worries about: time. Each stage takes time, the loop repeats at some rate, and the world keeps moving while the robot is busy. A robot that makes the right decision too late has made the wrong decision.
This lesson opens up each stage of the loop, then puts numbers on the timing so you can see when a loop is fast enough and when it is not.
Sensing is really perception
A sensor produces raw data: a voltage, a count of encoder ticks, a grid of pixel brightness values. Raw data is rarely what the robot needs. A robot needs to know things like "the wall is 30 cm ahead" or "the cup is tipped over". Turning raw data into useful facts is called perception.
Perception has two parts worth separating:
- Measurement: the sensor converts a physical quantity into a number, with some noise and some delay.
- Interpretation: software turns those numbers into a description of the situation, often called the robot's state estimate. A state estimate might be as simple as one distance or as rich as a map with the robot's position on it.
Interpretation can be slow — finding an object in a camera image might take tens of milliseconds — and the result always describes the world as it was when the measurement was taken.
Thinking is choosing an action
The decision stage takes the state estimate and a goal and picks what to do next. Engineers use different words for different kinds of decision:
- Control answers "how hard should I push right now?" — for example, how much voltage to send to a motor to hold a joint at an angle. It is usually fast, simple and repeated very often.
- Planning answers "what sequence of moves will get me to the goal?" — for example, a route across a room or a path for an arm around an obstacle. It is usually slower and repeated less often.
Many robots run both at once: a planner chooses where to go every second or so, and a controller steers towards the planner's current target hundreds of times per second. You will study this layering in the module on system architecture.
Acting changes the world
Actuators carry out the decision. Like sensors, they are not instant. A motor has to accelerate its own rotor and whatever it is moving, and a command sent over a wire takes time to arrive. When the actuator moves, the world changes: the robot is in a new place, an object has shifted, a force has appeared.
Why the loop closes through the physical world
Here is the idea that makes robotics different from pure software. There is no wire from the robot's motors back to its sensors. The only connection is the physical world itself. The robot pushes on the world; the world responds according to physics — mass, friction, springiness, gravity; and the sensors measure the result.
This has two consequences. First, the robot cannot simply assume its action worked. A wheel can slip; an object can be heavier than expected. The only way to know is to measure again. Second, the behaviour of the whole loop depends on the physics of whatever the robot is touching. The same controller can behave beautifully lifting a light box and badly lifting a heavy one, because the world part of the loop has changed.
Putting numbers on timing
Two timing quantities matter most:
- Loop period (T): the time between one pass of the loop and the next. Its reciprocal is the loop rate. A loop that runs every 10 ms has a rate of 1 / 0.010 s = 100 Hz.
- Latency (L): the delay between something happening in the world and the robot acting on it. It includes the sensor's own delay, data transfer, perception and computing time.
While the robot waits for its next loop and for its data to arrive, it keeps moving. A useful rule of thumb is that in the worst case, the robot reacts to an event up to T + L seconds late. If it is moving at speed v, it travels about v · (T + L) before responding.
Worked example
A robot drives towards a wall at 0.8 m/s. It should stop when its distance sensor reads 300 mm. Suppose its loop period is 200 ms and its readings are 80 ms old by the time they are used.
worst-case delay = T + L = 0.200 + 0.080 = 0.280 s
distance travelled = 0.8 m/s × 0.280 s = 0.224 m = 224 mmSo the robot could stop as close as 300 − 224 = 76 mm from the wall instead of 300 mm. That is a big difference for a robot trying not to scuff the paint.
The short program below simulates the same robot with different loop periods and latencies. Time is counted in whole milliseconds and distance in whole millimetres so the arithmetic is exact.
# A robot drives at 0.8 m/s (0.8 mm per ms) towards a wall 1200 mm away.
# It commands "stop" as soon as a reading shows 300 mm or less.
# Whole millimetres and milliseconds keep the arithmetic exact.
def stopping_gap(loop_ms, latency_ms):
t = 0
while True:
measured_at = max(t - latency_ms, 0) # the reading is this old
reading = 1200 - 8 * measured_at // 10 # distance it reports, mm
if reading <= 300:
return 1200 - 8 * t // 10 # true gap when it stops
t += loop_ms # wait for the next loop
for loop_ms, latency_ms in [(10, 0), (100, 0), (100, 80), (200, 80)]:
gap = stopping_gap(loop_ms, latency_ms)
print(f"loop {loop_ms:3d} ms, latency {latency_ms:2d} ms: stops {gap:3d} mm from the wall")Output:
loop 10 ms, latency 0 ms: stops 296 mm from the wall
loop 100 ms, latency 0 ms: stops 240 mm from the wall
loop 100 ms, latency 80 ms: stops 160 mm from the wall
loop 200 ms, latency 80 ms: stops 80 mm from the wallThe fast loop with no latency stops within 4 mm of its target. The slow loop with latency stops 220 mm late — close to the 224 mm worst case we calculated. (This model even assumes the robot stops instantly once commanded; a real robot also needs braking distance, which makes things worse.)
How fast is fast enough?
There is no single correct loop rate. What matters is how quickly the relevant part of the world changes compared with how quickly the robot responds. A greenhouse robot checking whether soil is dry can run its loop once a minute, because soil dries slowly. A drone keeping itself level needs hundreds of corrections per second, because it starts to tip within a fraction of a second. A practical way to think about it: the faster things change, or the faster the robot moves, the shorter T + L must be.
When you cannot make the loop faster, you have other options: move more slowly, leave a larger safety margin, or predict where things will be by the time the action lands. All three are common engineering choices.
Explorer 02 — Feedback control
Measure, compare, correct
- Setpoint r = 1
- Closed-loop output
- Open loop (u = r/K)
- ±2 % band
Gains
Applies the command the model says is right, u = r / K, without ever measuring.
More controls
Plant
Like motor speed or an oven: τ·ẏ = −y + K·u. K = 1.
Real-world effects
Noise only affects the closed loop: the derivative term amplifies it.
- Rise 10–90 %
- 0.72 s
- Overshoot
- 0.0 %
- Settling 2 %
- 1.30 s
- Steady-state err
- 0.333
- theory r/(1+Kp·K) = 0.333
State
P control, Kp 2.0 on a first-order plant with τ 1.0 s. Rise time 0.72 s, overshoot 0.0 %, 2 % settling 1.30 s, steady-state error 0.333 (theory for P: 0.333). Peak control effort 2.00. Open loop: steady-state error 0.000.
P control, Kp 2.0 on a first-order plant with τ 1.0 s. Rise time 0.72 s, overshoot 0.0 %, 2 % settling 1.30 s, steady-state error 0.333 (theory for P: 0.333). Peak control effort 2.00. Open loop: steady-state error 0.000.
How to read this
The top panel shows the setpoint (dashed line at 1) and the plant's true output after a step command at t = 0. The solid line is the controlled system; the dotted line is open loop. The bottom panel shows the control effort u the actuator is asked to deliver.
Rise time is how long the output takes to go from 10 % to 90 % of where it ends up. Overshoot is how far it passes that value. Settling time is when it stays inside ±2 % for good. Steady-state error is the gap left at the end: P control always leaves one on these plants; integral action removes it.
Assumptions & limitations
- Linear plant models with known, constant parameters (K = 1). Real motors and heaters have friction, dead zones and parameters that drift with temperature and wear.
- Controller runs every 10 ms and holds its output between samples; the plant is integrated with 4th-order Runge–Kutta at the same step. There is no transport delay, which real systems almost always have.
- The derivative acts on the measurement (not the error) through a 50 ms low-pass filter, a common practical choice that avoids a spike when the setpoint jumps.
- Sensor noise is white Gaussian and seeded for repeatability; it is added only to the measurement, never to the true output.
- Metrics are computed on the true output up to the disturbance time (or 10 s); a response still moving at the end of the window is reported as not settled.
- Open loop uses the exact plant gain to pick its command. That flatters it: any modelling error would show up as steady-state error.
Knowledge check
Check your understanding
Lesson materials · free to download
Sign in to track your progress. Reading is free without an account.