Robot simulation (Webots, Gazebo)
What you’ll learn
- Why engineers simulate robots
- The main free 3D robot simulators
- The sim-to-real gap
Why simulate?
- Test ideas before buying parts.
- Crash without breaking anything.
- Run thousands of tests overnight (and train AI).
- Everyone can work on the robot at once.
RoboLearn’s robot simulator is a simple 2D version of the same idea.
3D simulators
| Simulator | Notes |
|---|---|
| Webots | Free and open source; easy to start; many ready robot models; Python, C, C++ controllers; works with ROS 2 |
| Gazebo | Free and open source; the classic partner for ROS 2 |
| NVIDIA Isaac Sim | Photorealistic; needs a powerful NVIDIA GPU; used for AI training |
| CoppeliaSim | Flexible; free educational licence |
They simulate physics (mass, friction, collisions), sensors (cameras, lidar, distance) and actuators (motors, joints).
The sim-to-real gap
Simulations are simplified. Real wheels slip, real sensors are noisy, batteries sag and floors aren’t flat. Code that works perfectly in simulation can fail on the real robot.
Reduce the gap by:
- Adding noise to simulated sensors
- Using realistic masses, motor limits and friction
- Testing on the real robot early and often
- Writing robust code (thresholds with margins, timeouts, fail-safes)
Example: a Webots controller (Python)
from controller import Robot
robot = Robot()step = int(robot.getBasicTimeStep())left = robot.getDevice("left wheel motor")right = robot.getDevice("right wheel motor")for m in (left, right): m.setPosition(float("inf")) # velocity control modeds = robot.getDevice("ds_front")ds.enable(step)
while robot.step(step) != -1: if ds.getValue() > 800: # sensor-specific units: close left.setVelocity(-2); right.setVelocity(2) else: left.setVelocity(4); right.setVelocity(4)(Device names depend on the robot model you load.)
Example: the gap in action
In the RoboLearn simulator the line follower never misreads the line. On a real track, a shiny patch of floor read as “black” made it turn suddenly. The fix, requiring two readings in a row before turning, also works in simulation, so test it there first.
Sim vs realEasy
List three things that are perfect in RoboLearn’s simulator but messy in real life. For example: sensor readings (no noise), wheel grip (no slipping), identical motors, and a battery voltage that never drops.
Add noiseMedium
In the robot simulator, make your obstacle avoider robust to a noisy sensor: only turn when two readings in a row are below the threshold.
let close = 0; function loop() { close = distance() < 25 ? close + 1 : 0; if (close >= 2) motors(-40, 40); else motors(60, 60); }
Try WebotsHard
Install Webots (free), open one of its example worlds with a two-wheeled robot, and change its controller to avoid obstacles.
Quick check
Finished this lesson?