What happens when a robot is moving toward a wall, but its controller is still waiting for a sensor reading?
That question became the starting point for Project #05 of From Lab to Code: a small, interactive experiment about real-time control, network latency, and one surprisingly expensive operation—waiting.
In this project, I built an independent robot-control simulator in Python. It puts two controller designs under the same conditions: one measures speed with a blocking wait, while the other uses periodic, non-blocking telemetry. The result is an animated dashboard, a reproducible experiment, and a concrete way to see why a correct braking formula is not enough if the decision comes too late.
Source code: GitHub — Real-Time Robot Control Lab
Project series: From Lab to Code · Project website
1. The Problem: Stopping Is a Timing Problem
Imagine a robot traveling at 10 m/s toward a wall. It can decelerate at 4 m/s², but it needs sensor data from a remote system before deciding when to brake.
Even in an ideal world, braking takes distance:
Braking distance = velocity² / (2 × deceleration)
10² / (2 × 4) = 12.5 meters
But 12.5 meters is not the whole answer. The robot keeps moving while the controller is waiting for a measurement, processing its decision, and sending the brake command.
At 10 m/s, an additional 100 ms of delay means another 1 meter traveled before braking even starts.
The important question is therefore not only “Can the robot calculate the braking distance?” It is also “Can it calculate and apply that decision in time?”
This is one of the ideas behind real-time system design: the timing of a result can be as important as the result itself.
2. What I Built
Instead of relying on the source code or infrastructure of a university laboratory, I created a self-contained educational implementation with three parts:
- Robot physics: a one-dimensional moving robot with bounded acceleration and braking.
- Virtual network and controllers: sensor requests, delayed responses, brake commands, and two different scheduling strategies.
- Interactive browser dashboard: animation, live telemetry, configurable delays, events, and side-by-side comparisons.
The Python backend uses FastAPI, while the interface uses plain HTML, CSS, and JavaScript Canvas. No cloud service or university robot server is required.
The simulated robot starts at position 8 m, has a 1 m radius, and moves toward a fixed wall at 52 m. That leaves an initial clearance of 43 m. Its movement is updated in fixed 10 ms simulation steps.
This is deliberately a 1D control experiment, not a full robot navigation or realistic vehicle-dynamics simulator. Keeping the geometry simple makes it easier to isolate the effects of timing.
3. One Braking Rule, Two Control Strategies
Both controllers use the same simplified stopping threshold:
critical_distance =
velocity² / (2 × braking_deceleration)
+ velocity × reaction_budget
+ safety_margin
The configured reaction budget is:
reaction_budget =
(network_RTT_ms + jitter_ms + poll_interval_ms) / 1000
This is a design heuristic, not a proven worst-case response-time guarantee. In particular, the blocking controller does not include its additional speed-measurement wait in that budget. That omission is intentional: the experiment asks what happens when the control logic underestimates how long it remains unable to act.
For a 10 m/s robot, 4 m/s² braking deceleration, 50 ms configured RTT, 20 ms additional jitter setting, 50 ms polling interval, and 1.2 m margin:
Braking distance: 12.50 m
Reaction allowance: 1.20 m
Safety margin: 1.20 m
---------------------------------
Critical distance: 14.90 m
Strategy A: Blocking Speed Measurement
The first controller estimates speed using two odometer readings with a 500 ms waiting interval between them.
Conceptually:
first = read_odometer()
wait(0.5) # No new control decision during this interval
second = read_odometer()
estimated_speed = (second - first) / elapsed_time
check_whether_to_brake()
The implementation also includes simulated request and response delays. The problem is not the speed formula itself; estimating distance over time is entirely reasonable.
The problem is when the next decision is allowed to happen. A robot can travel several meters during a measurement cycle.
Strategy B: Non-Blocking Periodic Telemetry
The second controller periodically requests telemetry without inserting a separate half-second waiting phase. Once a fresh pair of readings is available, it estimates speed from their timestamps and odometer values:
interval = current["sampled_at"] - previous["sampled_at"]
if interval > 0:
estimated_speed = (
current["odometer"] - previous["odometer"]
) / interval
This controller can keep receiving measurements and checking the stopping threshold on a much shorter schedule.
An additional detail matters: readings have timestamps, and the simulation rejects out-of-order readings that would corrupt the speed estimate.
Non-blocking does not mean zero latency. Network delays still exist, measurements can still become stale, and a controller can still make a late decision. It simply avoids adding an unnecessary fixed waiting phase to each speed estimate.
4. Modeling the Network
The dashboard lets you change the configured network round-trip time (RTT) and additional jitter.
Rather than measuring the real internet, I implemented a deterministic virtual network. Each request and response leg gets half the configured baseline RTT plus a separately sampled nonnegative extra delay. Event delivery is scheduled with a priority queue, and delays are rounded to the 10 ms simulation clock.
That distinction matters:
- Configured RTT is an input to the simulator.
- Observed simulated RTT can be higher because of extra delay and clock quantization.
- Neither number is a measurement of a physical network.
Brake commands also incur a simulated one-way network delay before the robot receives them. Even then, the robot does not stop instantly—it decelerates under the physics model.
Using a fixed random seed makes runs reproducible. However, because the two strategies produce different request sequences, they do not receive identical individual packet delays, even when they use the same seed and settings.
5. The Experiment: Does the Robot Stop?
Here is one reproducible scenario:
| Parameter | Value |
|---|---|
| Target speed | 10 m/s |
| Acceleration | 3 m/s² |
| Braking deceleration | 4 m/s² |
| Configured network RTT | 50 ms |
| Additional jitter setting | 20 ms |
| Poll interval | 50 ms |
| Blocking measurement wait | 500 ms |
| Safety margin | 1.2 m |
| Random seed | 2026 |
I ran both strategies to completion with these settings.
The results were:
| Metric | Blocking | Non-blocking |
|---|---|---|
| Outcome | Collision | Safe stop |
| Final wall clearance | 0.00 m | 1.47 m |
| Brake requested at | 4.84 s | 4.53 s |
| Brake applied at | 4.88 s | 4.57 s |
| Mean observed simulated RTT | 74.29 ms | 70.77 ms |
| Time spent in blocking measurement phases | 4,540 ms | 0 ms |
The non-blocking controller requested braking about 310 ms earlier. In this particular scenario, that was enough to stop before the wall.
The 4,540 ms figure is the cumulative time spent in blocking measurement phases, counted in virtual 10 ms steps. It is not a measured OS scheduling delay or a formal count of missed hard-real-time deadlines.
These are simulation results, not measurements from a physical robot or evidence that one algorithm is universally safe.
6. Changing the Latency
One successful run is interesting, but it is not much of an experiment by itself. So I added a script that repeats the comparison across configured RTT values from 0 to 400 ms.
You can reproduce the data with:
python -m experiments.latency_sweep
This generates:
-
results/latency_sweep.json— numerical results for every tested RTT. -
results/latency_sweep.svg— a chart of final wall clearance.
View the latency-sweep chart on GitHub
A few rows from the generated experiment:
| Configured RTT | Blocking | Non-blocking |
|---|---|---|
| 0 ms | Safe stop, 0.37 m | Safe stop, 1.37 m |
| 25 ms | Collision | Safe stop, 1.47 m |
| 50 ms | Collision | Safe stop, 1.47 m |
| 100 ms | Collision | Safe stop, 1.37 m |
| 200 ms | Collision | Safe stop, 1.37 m |
| 400 ms | Collision | Safe stop, 1.37 m |
For this chosen model, controller policy, and random seed, the blocking design fails at the tested nonzero RTT values, while the non-blocking design stops successfully.
That is not a reliability curve. There is only one deterministic run per setting; a serious robustness study would require multiple seeds, different delay distributions, sensor noise, and more realistic dynamics. It would also need to compare controllers with appropriately designed latency budgets.
7. Try It Yourself
The project is open source:
GitHub: From Lab to Code #05 — Real-Time Robot Control Lab
It requires Python 3.10 or newer. From the project directory:
python -m venv .venv
Activate the environment, install dependencies, run the tests, and start the app:
pip install -r requirements.txt
python -m unittest discover -s tests -v
python app.py
Then open http://127.0.0.1:8000.
Try RUN BOTH STRATEGIES first, then adjust the network RTT, jitter, braking deceleration, and the advanced controller settings. The interface lets you inspect the speed estimate, true simulated speed, stopping threshold, and event stream.
The repository also contains an optional, separate localhost TCP demonstration using a small JSON-lines protocol. Unlike the browser experiment's virtual transport, that example uses actual local sockets:
# Terminal 1
python -m robot_lab.tcp_demo server --port 30352
# Terminal 2
python -m robot_lab.tcp_demo client --port 30352 --speed 10
It is an independently implemented demo—not a client for the university arena—and its real loopback timing should not be directly compared with the virtual experiment.
The repository includes 19 automated tests, covering physics, validation, deterministic replay, API behavior, controller outcomes, and a local TCP exchange.
8. What This Experiment Taught Me
The key lesson was not simply that “non-blocking code is better.” That would be too broad.
It was that a controller needs to account for the entire time between sensing a dangerous situation and actually changing the physical system.
A useful stopping-distance equation can still fail if its reaction budget ignores a long measurement cycle. And a responsive controller can still fail if its sensor data is stale, its network delay is underestimated, or its braking model is wrong.
This project has important limitations:
- Motion is one-dimensional, with constant acceleration and deceleration.
- There is no turning, tire friction, sensor noise, obstacle uncertainty, or actuator lag.
- The main comparison uses simulated network delays and a virtual 10 ms clock.
- Neither Python nor the browser provides a certified hard-real-time guarantee.
- A single deterministic run does not establish safety or statistical reliability.
Those limitations are intentional. The goal was to build a compact experiment where timing effects are visible, testable, and easy to reason about.
For a next iteration, I would be interested in adding noisy sensor data, a better estimate of worst-case communication delay, and a multi-seed robustness study.
From Lab to Code is my ongoing series of small, independent engineering projects: taking a concept from theory, implementing it, visualizing the result, and being explicit about what the experiment does—and does not—prove.
If you would like to explore or modify the simulator, the complete source, tests, and generated results are available here:
Thanks for reading!


Top comments (0)