Meet your virtual robot
Every robot runs the same loop: sense, decide, act. Meet a small two-wheel robot that lives in your browser, and drive it to a goal.
In 60 seconds
Meet your virtual robot
Every robot runs the same loop: sense, decide, act. Meet a small two-wheel robot that lives in your browser, and drive it to a goal.
- 1
Sense
Read the sensors: Is there a line under me? How far is the wall? - 2
Decide
Your code turns those readings into a choice: go straight, turn, stop. - 3
Act
The motors spin the wheels at the speeds you chose. - 4
Repeat
Do it again, many times per second. Small, frequent corrections beat big, rare ones.
Why start with a virtual robot?
Your robot
- It lives in a square room, 200 cm by 200 cm, seen from above.
xgoes to the right,ygoes up. - It has two wheels, 10 cm apart, and is round, 5 cm in radius. A third free-rolling wheel (a caster) keeps it balanced.
headingis the direction it faces, in radians: 0 means facing right, and it grows as the robot turns left (anticlockwise). A full turn is about 6.28 radians; a right angle is about 1.57.- Each step is one tenth of a second. At full speed a wheel moves 20 cm per second.
Steering with two wheels
| Left wheel | Right wheel | What the robot does |
|---|---|---|
| Forward, same speed | Forward, same speed | Drives straight |
| Slow | Fast | Curves to the left |
| Fast | Slow | Curves to the right |
| Backward | Forward | Spins on the spot, to the left |
| Stopped | Stopped | Stands still |
# Wheel speeds go from -1 (full backward) to 1 (full forward).
VMAX, BASE, DT = 20.0, 10.0, 0.1 # top speed cm/s, wheel gap cm, seconds per step
for left, right in [(1, 1), (0.3, 0.8), (0.8, 0.3), (-0.5, 0.5), (0, 0)]:
forward = (left + right) / 2 * VMAX # cm per second, straight ahead
turn = (right - left) * VMAX / BASE # radians per second, + means left
print(f"left={left:5} right={right:5} -> forward {forward:5.1f} cm/s,"
f" turn {turn:5.1f} rad/s ({turn * DT:.2f} rad per step)")What you write: the decide function
decide(s). The simulator calls it once per step. s is a dictionary of sensor readings. Your function returns two numbers: the left and right wheel speeds. The simulator moves the robot and tells you if it crashed.def decide(s):
# s["x"], s["y"], s["heading"]: where the robot is (only the simulator knows this exactly)
# s["left"], s["right"]: 1 if that line sensor sees black tape, else 0
# s["offset"]: how far the tape is to the left (+) or right (-), in cm
# s["distance"]: cm to the nearest thing straight ahead (max 100)
# s["step"]: which step this is: 0, 1, 2 ...
return 0.5, 0.5 # (left wheel, right wheel), each from -1 to 1
print(decide({"step": 0}))x, y, heading). Real small robots usually do not: wheels slip and small errors add up. That is why the next lessons use sensors that look at the world instead.Watch and read more
Lab
A decide function that turns your virtual robot towards a goal, drives there and stops.
The problem
This runs in your browser. The robot starts at (30, 30) facing right. The goal is at (160, 160).
- Run the starter. The robot drives straight into the wall. Look at the picture.
- Change
decideso the robot turns towards the goal, drives there and stops within 10 cm of it, without touching a wall. - Two ways to do it: (a) use
s["step"]: spin for a few steps with(-0.2, 0.2)(each step then turns 0.08 radians), then drive, then return(0, 0); or (b) each step, work out the angle to the goal withnp.arctan2and steer towards it. - Write one sentence: why would way (a) be harder on a real robot?
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1Your step-counting solution works in the simulator. Why might the real robot miss the goal by a lot?Reveal
Counting steps assumes each step moves and turns the robot by exactly the same amount. Real wheels slip, motors are not identical, batteries get weaker and the floor is uneven. Small errors in the first turn grow as the robot drives further. Real robots correct themselves using sensors that look at the world: a line, a wall or a camera.
Q2When you steer by the angle to the goal, why must you "wrap" the angle difference into the range -π to π?Reveal
Angles repeat every full turn. If the goal is at +3.1 radians and the robot faces -3.1, the raw difference is 6.2, which would make it spin almost a full turn left, while the goal is really just 0.08 radians to the right. Wrapping, for example (err + np.pi) % (2 * np.pi) - np.pi, gives the short way round.
Questions people ask
Do I need to know Python already?
A little helps: variables, if and functions. The labs are short and you only change one small function. If you are new, try the lab anyway and read the error messages slowly; they usually say which line is wrong.
Why radians and not degrees?
Python's maths functions such as np.cos and np.arctan2 use radians. One full turn is 2π radians, about 6.28. To convert, multiply degrees by π/180, or use np.radians(90).
Is this how real robot software works?
The loop is the same. Real software adds many things: reading sensors at different speeds, safety checks, logging and handling errors. But "read sensors, decide, set motors, repeat" is the core of most small robots.
Can the simulator be wrong?
Yes. It is a simple model: no wheel slip, no bumps, perfect motors. Code that works here is a good start, not a guarantee. The lesson Simulation and sim-to-real explains this gap in more depth.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.