Skip to the lesson
FixTheRobot Explore Robotics / Robot Foundations / Module 8 / Lesson 2

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.

  • Lesson 2 of 4
  • Beginner
  • No code
  • About 10 minutes

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:

Differences between a real robot and a typical simulator
Real robotOften missing in the simulator
Delay: sensing, computing and the motors take timeInstant response, or a delay smaller than the real one
Wheel slip and uneven floorsPerfect grip on a flat floor
Sensor noise that is not neatly random: reflections, glare, dropoutsTidy, bell-shaped noise
Wear, temperature, a low batteryA robot that is always brand new
Things nobody thought of: a cable, a dog, a person in a high-visibility vestOnly 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.

The same controller with three delaysA plot of the distance still to go against time over 10 seconds, starting at 2 metres. With no delay the error falls smoothly to zero. With a delay of 0.3 seconds the error overshoots below zero, swings back and forth with shrinking size, and settles. With a delay of 0.6 seconds it swings back and forth with growing size, reaching the top and bottom of the plot.0246810-3.2-2.4-1.6-0.800.81.62.43.2time (s)error: distance still to go (m) no delaydelay 0.3 sdelay 0.6 s
One controller (gain 3), three worlds. Blue: no delay, as in a simulator. Green: a delay of 0.3 s. Orange: 0.6 s. The code is identical in all three.
The result of the same controller (gain 3) with three delays
DelayGain × delayFarthest swing past the goalSize of the swings in the last 2 sWhat happens
0 s00 m0 mSettles smoothly
0.3 s0.90.83 m0 mOvershoots, then settles
0.6 s1.810.14 m10.57 mSwings 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.

  1. 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.
  2. 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.
  3. 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.
  4. 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

  1. A simulator is safe, fast, repeatable and cheap, and it can produce failures on demand.
  2. It leaves out delay, slip, messy sensor noise, wear and surprises. The difference is the reality gap.
  3. For a simple feedback loop, gain × delay decides stability: above about 1.6 the swings grow.
  4. 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.

Next lesson

When Things Go Wrong: Failing Safely

List common ways a robot fails and the simple defences against each: limits, timeouts and a stop that always works.

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.