Robot Foundations · Module 8: The Robot as a System, and Your Next Step
Simulation and Real Robots
A simulator is a safe, fast, repeatable pretend world. It is always simpler than the real one, and the differences are where robots fail.
Every lab on this site is a simulation. Your code drove a pretend robot through a pretend room, and nothing broke when it crashed. That is exactly what simulation is for. But there is a catch that every robotics engineer learns, usually the hard way: a program that works perfectly in simulation can fail the first minute on a real robot. This lesson says why, and shows one example you can compute for yourself.
What you will be able to do
- Say what a simulator is good for.
- Name things a simulator usually leaves out.
- Show with a delay how the same controller can work in one world and fail in another.
- Describe how engineers close the gap between simulation and the real robot.
The idea
A simulator is a program that plays the part of the world: it moves a pretend robot with a pretend motion model, and makes pretend sensor readings from a pretend room. The robot's software cannot tell, provided the simulator publishes the same messages the real sensors would (the previous lesson).
What simulation is good for
- Safety. A bug that would crash a robot into a wall costs nothing.
- Speed. It can run faster than real time, so a ten-minute mission takes seconds. In the labs, grading runs many worlds in a moment.
- Repeatability. A seeded world can be replayed exactly. A failure that happens once in a hundred runs on a floor can be hunted down.
- Cost. No hardware, no battery, no repair. A thousand tests run in an afternoon.
- Making the rare happen. A simulator can drop a sensor, add a wall or make the floor slippery on demand. The labs have sliders for slip and noise for exactly this.
What simulation leaves out
A simulator is a model, and every model leaves things out. The difference between the model and the real world is called the reality gap. The usual missing pieces:
| Real robot | Often missing in the simulator |
|---|---|
| Delay: sensing, computing and the motors take time | Instant response, or a delay smaller than the real one |
| Wheel slip and uneven floors | Perfect grip on a flat floor |
| Sensor noise that is not neatly random: reflections, glare, dropouts | Tidy, bell-shaped noise |
| Wear, temperature, a low battery | A robot that is always brand new |
| Things nobody thought of: a cable, a dog, a person in a high-visibility vest | Only what the author put in |
None of these is a flaw in simulators. They are the price of a model that is simple enough to run and to understand. The danger is only in forgetting that they exist.
One gap you can compute: delay
Take the proportional rule of the lesson Feedback: Measure, Compare, Correct: speed = gain × error. In a simulator the wheels respond at once. On a real robot there is a delay between measuring the error and the wheels doing anything about it: the scanner's update interval, the computer's thinking time, the motor's slow start. The effect on the error is:
The error shrinks at a rate set by what the error was one delay ago, not by what it is now.
rate of change of error = − gain × error(now − delay)
With no delay this is the smooth shrinking you met in module 3.
| Delay | Gain × delay | Farthest swing past the goal | Size of the swings in the last 2 s | What happens |
|---|---|---|---|---|
| 0 s | 0 | 0 m | 0 m | Settles smoothly |
| 0.3 s | 0.9 | 0.83 m | 0 m | Overshoots, then settles |
| 0.6 s | 1.8 | 10.14 m | 10.57 m | Swings grow: unstable |
The program did not change. Only the world it ran in did. With no delay it is a model controller. With a modest delay it overshoots and wobbles. With a longer delay it gets worse with every swing. The pattern is general: the product of the gain and the delay decides it. A rule of thumb for this simple loop is that when the product is more than about 1.6 (π ÷ 2), the swings grow. A robot tuned in simulation with a bold gain can be perfectly stable there and unstable on the floor. (A real robot saturates, so the swings stop growing at the speed limit. It then wobbles for ever, which is no better.)
A simulation proves that the code works in the simulator's world. It says only as much about the real robot as the simulator has in common with it.
Closing the gap
- Put the missing things in. Add delay, noise and slip to the simulator, with values measured on the real robot. The sliders of the labs are the small version of this.
- Test with margin. If the controller is stable with twice the delay you measured, a surprise costs less.
- Move to the real robot in stages. Slowly first, then faster. With a person watching and a stop button in reach. On a lifted stand before the floor.
- Record real runs and replay them. The messages from a real run, played into the software, reproduce a field failure at a desk.
The Robot Learning track on this site has a whole module on this gap, because it is the hardest problem for robots that learn in simulation. The idea is the same: expect the difference, and design for it.
Words to know
- Simulator
- A program that stands in for the world, the robot's body and its sensors. On Pip: the pretend rooms of the labs.
- Reality gap
- The difference between a simulator and the real world. On Pip: the delay and slip a lab leaves out.
- Delay (latency)
- The time between a measurement and the robot's response to it. On Pip: sensor update, computing time and motor response add up.
- Stable
- Settling to its goal after a disturbance, instead of swinging or running away. On Pip: the blue curve and the green one.
- Margin
- Room to spare: designing so that the system still works if conditions are worse than expected. On Pip: stable with twice the measured delay.
- Replay
- Feeding recorded messages back into the software to reproduce a run. On Pip: used to find a rare failure from the floor.
A worked example: the same gain in three worlds
The controller of lesson Feedback: Measure, Compare, Correct has a gain of 3 per second. The robot starts 2 m from its goal. Use the product of gain and delay to predict, then check against the table.
- No delay. Product 0. With no delay each correction is based on the current error, and the error falls smoothly. Table: farthest swing past the goal 0 m: none.
- A delay of 0.3 s. Product 0.9. The robot is correcting for where it was 0.3 s ago, and is still moving that way, so it overshoots by 0.83 m. The product is below 1.6, so the swings shrink: by the last two seconds they are down to 0 m.
- A delay of 0.6 s. Product 1.8, above 1.6. Each correction arrives so late that it makes the next error worse. The swings grow: in the last two seconds they are 10.6 m, larger than the starting distance.
- The fix. Keep the gain times the delay below about 1. For a delay of 0.6 s that means a gain of at most about 1.7 per second, a third of what worked in the simulator. The slower, gentler controller is the right one for a world with delay.
The simulator, with no delay, would have approved all three gains. Only a simulation that includes the delay, or the real robot, shows the problem.
Try it yourself
Try this with a sink or a shower. Turn the hot tap until the water is the right temperature, but wait to feel the result for several seconds after every change. (a) What do you do if you react to the temperature at once? (b) What happens if you make big changes and the water takes a few seconds to respond? (c) What do you do to stop it?
What to expect
(a) With instant feedback you can find the temperature quickly and settle. (b) With a delay you overshoot: too hot, you turn it down, still hot because the change has not arrived, so you turn it down further, and it becomes cold, and you go back and forth. That is the green and orange curves. (c) Make smaller changes and wait. A smaller gain. Everyone who has used a shower in an old building has built a controller that works in a world with delay.
Common mistakes
"It works in simulation, so it works."
It works in the simulator's world. Whether it works on the robot depends on how much the simulator left out, and a controller that is tuned to the edge of instability in simulation will fall over on the floor.
"A more detailed simulator closes the gap."
It narrows it. Nothing closes it. There is always something missing, which is why a test on the real robot is never skipped.
"The labs' sliders for slip and noise are optional extras."
They are the point. A solution that only works with slip 0 and noise 0 is one you would not take to a real floor.
Remember this
- A simulator is safe, fast, repeatable and cheap, and it can produce failures on demand.
- It leaves out delay, slip, messy sensor noise, wear and surprises. The difference is the reality gap.
- For a simple feedback loop, gain × delay decides stability: above about 1.6 the swings grow.
- Close the gap by adding measured delay, noise and slip, testing with margin, moving to the robot in stages and replaying real runs.
Check yourself
Three questions. Write an answer in your own words, then reveal the explanation. Nothing you type leaves your browser.
Question 1
A controller with a gain of 2 per second works in simulation. On the robot the delay is 1 s. Is it likely to be stable?
Explanation
The product is 2 × 1 = 2, above the limit of about 1.6, so no: it is likely to swing with growing size. Lowering the gain to about 1 per second or less, so that the product is near 1, should restore stability.
Question 2
A robot passes every test in simulation and fails on the first morning on a real floor. Give two reasons that are not a bug in the code.
Explanation
Any two of: delay in the real sensors and motors that the simulator did not model, wheel slip or an uneven floor, sensor errors such as glare or dropouts, a world containing things the simulator never had. The code can be correct and the model of the world still incomplete.
Question 3
Why are the sliders for wheel slip and encoder noise in lab 5 useful for learning even though the robot is not real?
Explanation
They put some of the missing parts of the real world back into the simulator. Solutions that work only when slip and noise are zero show that they depend on the simulator being perfect, and the sliders reveal that. Testing across a range is a cheap version of testing with margin.
What’s next
Delay is only one of the ways a real robot surprises its builder. The next lesson looks at the failures every robot should be ready for, and the simple defences that make a robot fail safely.
Where this goes on the site
Nothing below is needed to continue this course. These pages take the topic of this module further, and most assume more mathematics or Python than this course does.
- LaterThe Navigation Stack at a Glance
The programs of a real mobile robot and the messages between them.
- LaterBuild a CBF Safety Filter
A safety layer that edits unsafe commands.
- LaterLinear Algebra for Robotics
The mathematics the specialised tracks use.