Education › Robotics › Stage 4: Build real robots

Simulation — build robots for free

Gazebo and Webots: design and test a whole robot in software before spending on parts.

Intermediate→Advanced ~32 min read Module 14 of 16

Simulation lets you build, break and test a robot without buying a single part or risking your hardware. Modern robotics leans on it heavily: you can develop your whole software stack against a virtual robot, run a risky manoeuvre a thousand times, and even train learning-based controllers where real trials would be too slow or dangerous. This lesson explains what a robot simulator does, how it connects to the same code that runs your real robot, the famous 'reality gap', and how to use simulation well so the skills transfer to real hardware.

After this module you can
  • Explain what a physics-based robot simulator provides
  • Describe how the same ROS 2 code can drive a simulated or real robot
  • Define the reality gap and ways to narrow it
  • Decide when to use simulation and when to move to hardware

What a simulator gives you

A robot simulator like Gazebo (or Isaac Sim, Webots) is a virtual world with a physics engine. You give it a model of your robot — its shape, mass, joints, and where its sensors sit — and a model of the environment, and it computes how the robot moves under gravity, friction and motor forces, and what its virtual sensors would see. This buys you enormous freedom: no hardware to damage, no batteries to charge, the ability to pause and inspect, to teleport the robot to a tricky spot, to run faster than real time, and to test dangerous scenarios safely. For anyone learning without a robot in hand, a simulator is the single most valuable tool — you can complete this whole track's practicals virtually.

Tip

You do not need a physical robot to learn robotics seriously. A laptop running Gazebo with ROS 2 gives you a full robot to program — sensors, motors, physics — and everything you learn transfers to hardware later. Start in simulation and buy parts once your software works.

The same code, virtual or real

The reason simulation is so powerful in the ROS world is that your control code does not know or care whether it is driving a real robot or a simulated one. Recall from the ROS lesson that nodes talk over topics: your navigation node publishes velocity commands to a topic and subscribes to sensor topics. A simulator publishes the same sensor topics and subscribes to the same command topics a real robot would. So you can develop your entire stack against Gazebo, and when the hardware is ready, you swap the simulator for real drivers publishing those same topics — and your code runs unchanged. This clean separation between 'the robot's software' and 'where the robot's body comes from' is one of the best reasons to build on ROS 2.

bash
# Same navigation node, two different 'bodies' underneath.

# In simulation: Gazebo provides /scan, /odom and consumes /cmd_vel
ros2 launch my_robot sim.launch.py
ros2 run my_nav navigator            # publishes /cmd_vel, reads /scan

# On hardware: real drivers provide /scan, /odom and consume /cmd_vel
ros2 launch my_robot hardware.launch.py
ros2 run my_nav navigator            # the SAME node, unchanged

The reality gap

Simulation is never a perfect copy of reality, and the difference is called the reality gap. A simulator approximates friction, its virtual sensors are cleaner than real ones, motor behaviour is idealised, and countless small effects (wheel slip, sensor noise, flex, timing jitter) are simplified or absent. So code that works flawlessly in simulation can misbehave on the real robot. This does not make simulation useless — it makes it a powerful first stage that you must validate on hardware. Two habits narrow the gap. Add realism: turn on sensor noise, model friction better, randomise parameters so your code cannot depend on a perfect world. Domain randomization — deliberately varying masses, frictions, and sensor noise across many simulated runs — forces a controller to be robust, so it survives the messiness of reality. The mindset to carry: trust simulation to develop and shake out logic, but never declare victory until it runs on the physical robot.

Using simulation well

Simulation earns its keep at specific moments. Use it to develop and debug logic where iterating on hardware would be slow — navigation, planning, behaviour trees, perception pipelines. Use it to test the dangerous and the rare — a fall, a collision, an edge case you would never risk on a real arm. Use it to train where real trials are impractical, as reinforcement-learning controllers do, running millions of episodes overnight. And use it as a regression test — a scene your robot must still handle after a code change. When do you move to hardware? Once the logic is solid in sim and you need to confront the real sensor noise, timing, and physics — the things simulation smooths over. The professional flow is a loop: prototype in sim, validate on hardware, feed what you learned back into a more realistic sim.

  • Develop and debug logic fast, without hardware wear.
  • Test dangerous or rare scenarios safely.
  • Train learning-based controllers over many episodes.
  • Use scenes as regression tests after code changes.
  • Move to hardware to confront real noise, timing and physics.
Hands-on practice

Drive a robot in simulation

  1. Launch Gazebo with a provided differential-drive robot model and a simple world.
  2. Run 'ros2 topic list' and identify the sensor topics the simulator publishes and the command topic it consumes.
  3. Teleoperate the robot and watch it move under simulated physics, bumping into obstacles.
  4. Run your line-following or obstacle-avoidance node against the simulator with no code changes.
  5. Turn on sensor noise or change the friction in the world and observe how your code copes with a less perfect environment.
Cheat sheet

Simulation — build robots for free — at a glance

Main things to focus on

  • A simulator is a virtual world with physics that models your robot and its sensors.
  • You can learn the whole track in simulation without buying hardware.
  • ROS code drives sim or real hardware unchanged — both use the same topics.
  • The reality gap is the difference between simulation and the real world.
  • Add noise and domain randomization to make code robust to reality.
  • Prototype in sim, then validate on hardware — never skip the hardware step.

The simulator

Gazebocommon ROS 2 physics simulator
robot modelshape, mass, joints, sensor mounts
physics enginecomputes motion under forces
virtual sensorssimulated lidar, camera, odom

Sim ↔ real

/cmd_velvelocity command topic (both)
/scan, /odomsensor topics the sim or driver publishes
same nodeunchanged code drives either body
swap launch filesim.launch vs hardware.launch

Reality gap

reality gapsim differs from the real world
sensor noiseadd it so code doesn't assume clean data
domain randomizationvary params for robustness
validate on hardwarethe non-negotiable final step

When to use

develop logiciterate fast without hardware
test danger/rarefalls, collisions, edge cases
train controllersmany episodes overnight
regression scenesmust-still-pass tests

Common pitfalls

  • Believing a robot works because it works in simulation, then skipping hardware testing.
  • Running a spotless simulation with no noise, so code silently depends on perfect data.
  • Building sim-only code that hard-codes assumptions the real world breaks.
  • Ignoring domain randomization, leaving a controller brittle to real variation.
  • Not reusing the same ROS topics, so sim code cannot move to hardware unchanged.
Quiz

Check your understanding

5 questions · 4 to pass · answers are explained as you go. Your best score is saved on this device only.

Progress and quiz scores are saved in this browser only. Back up or restore on the hub.

Was this lesson useful? Tell me what to improve →