Introduction to robot simulation

Test the robot on the computer, before it ever moves

Writing software for a robot is different from writing a website or a phone app. A robot’s software moves real motors in the real world, so a mistake can break something. Robot simulation solves this: the robot is built as a model inside the computer, put into a pretend world with pretend physics and pretend sensors, and tested there first. This page explains the idea step by step. Every step has a simulator to play with: a program that lies in its log, a race between a real robot and a simulation, a robot model builder, a 3D viewer, a physics sandbox and a sensor simulator. No real robot is needed.

Words used on this page (open this if a word is new)
simulation, simulatorA computer program that copies how something works in the real world. A robot simulator copies the robot and the world around it.
ROS 2A free software kit that helps people build robot software. It can also describe a robot to the computer.
hardwareThe real, physical parts of a robot: motors, wheels, sensors, batteries and the body.
log messageA line of text that a program prints to say what it is doing, like “goal reached”.
bug, debuggingA bug is a mistake in a program. Debugging means finding and fixing the mistakes.
robot modelA description of a robot that a computer can read: its shape, its parts and how they are joined.
structureThe shape and size of each part of the robot.
componentsThe parts that do jobs, such as wheels, motors and sensors.
connections (joints)How two parts are joined. A joint can be fixed, or it can move, like a wheel that spins.
visualizationShowing something on a screen so it can be seen and studied. A tool called RViz does this for ROS 2 robots.
physics simulatorA simulator that copies the rules of physics. A tool called Gazebo does this for ROS 2 robots.
gravityThe pull that makes things fall down. On Earth it is about 9.81 metres per second squared.
frictionThe grip between two surfaces rubbing together. Ice has little friction. Rubber on a dry road has a lot.
collisionTwo things hitting each other. Without collisions in a simulator, things pass straight through each other.
sensorA part that lets the robot measure the world around it.
laser scannerA sensor that sends out light beams all around and measures how far each one travels before it hits something.
sonarA sensor that sends out sound and listens for the echo. It gives one rough distance from a wide cone.
cameraA sensor that takes pictures.
01

Why robot software is different

Software for a website or a phone app can be written and tested on one computer. Nothing physical is involved. Robot software is different: it controls real hardware, so it must be tested on a real robot, and the way the robot moves in its surroundings has to be watched. The usual way to debug is to read log messages, but those are often not enough. A program can print “goal reached” while the robot is actually stuck against a wall. Pick a bug below and see how far the log alone can go.

Key idea. Normal debugging, such as reading log messages, is often not enough for a robot. It may also be needed to see the robot move, to see what the sensors report, and to see how the robot meets its surroundings.
02

Testing on a real robot takes time, money and care

Testing on a real robot is harder than testing software alone. Every test needs time, because the robot must be set up, charged and put in the right place. It needs money, for the robot, the sensors and the motors, and for repairs when something breaks. It needs hardware, and it needs safety, because a robot with a bug can hit things and people. Here the same program with the same hidden bugs is tested on a real robot and in a simulator. Both do exactly the same tests in the same order.

Key idea. Real robot testing is expensive, slow and sometimes risky. A simulator runs the same tests without those costs.
03

ROS 2 can describe a robot as a model

To simulate a robot, the computer first needs to know what the robot is. ROS 2 can describe a robot with three kinds of information. The structure is the shape of each part. The components are the parts that do jobs: wheels, sensors and so on. The connections say how the parts are joined, for example that a wheel is attached to the body and spins. Together this is the robot model. The model can then be shown on a screen (visualization) and placed in a pretend world (simulation). Build a robot below by adding and removing parts.

Key idea. A robot model is a description: structure, components and connections. The same description can be used for visualization and for simulation.
04

Visualization: studying the robot on the screen

A robot model can be shown on the computer. This is called visualization, and a tool named RViz does it for ROS 2. It lets people inspect the structure of the robot, its components and its sensors, and look at it from any side, without the real robot being in front of them. Turn the robot around below. The robot is the same one that was built in the last section.

Key idea. Robot model leads to visualization. The robot can be studied without physically having it in front of anyone.
05

Physics simulation: a pretend world with real rules

The robot model can also be placed inside a physics simulator, a program that copies the rules of the real world. A tool named Gazebo does this for ROS 2. The simulator can reproduce movement, gravity, friction and collisions, and also how the robot interacts with its surroundings. Try it below: drive a robot up a ramp toward a wall, or drop it from a height. Change gravity, friction and bounciness and watch what changes.

Key idea. A physics simulator reproduces gravity, friction, collisions and movement, so a robot can be tested in a world with real rules.
06

Sensor simulation: fake sensors with real-looking data

Many robot sensors can be simulated too, so the robot software can receive simulated sensor information without any real sensor. The PDF names three: the laser scanner, the sonar and the camera. A robot plus an environment plus physics plus sensors makes a simulated robot system. Drag the robot around the room below and watch what each sensor reports.

Key idea. Sensors such as cameras, laser scanners and sonars can be simulated, so software can be tested against realistic sensor data.
07

Why simulate first?

The reason is simple: test and debug the robot’s behaviour before using real hardware. Simulation reduces hardware damage, testing cost, development time and risk. It helps students, who can learn and try ideas without buying a robot, and it helps professionals, who can find errors and unsafe behaviour before the real robot is switched on. Simulation is not the real world, though, and some problems only appear on the real robot. Slide the numbers below to see the trade.

Key idea. Simulation first: test and debug, then reduce the risk before using the real robot.
08

Summary and quiz

Key takeaways

  1. Robotics software often needs to interact with physical hardware.
  2. Testing on a real robot can be expensive and time-consuming.
  3. ROS 2 allows a model of a robot to be created.
  4. Simulation can include physics and sensors.
  5. Simulation allows developers to test and debug before using real hardware.

Good to know

  • The robot model has structure, components and connections.
  • The model can be visualized on a computer, and also placed in a physics simulator.
  • Simulation can reproduce movement, gravity, friction and collisions.
  • Cameras, laser scanners and sonars can be simulated.
  • Simulation is useful for learning, development and debugging before deploying to a real robot.

Quiz