Build a mini robotBeginnerLesson 2647 min read

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.

Lesson in motion

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/5
In simple words
Think of riding a bicycle in a busy market. Your eyes look ahead, your brain decides "slow down, go left", and your legs and hands do it. Then you look again. You do this many times every second. A robot does exactly the same: it senses, it decides, it acts, and then it starts again.
A robot is a machine that can notice the world around it and move or act by itself. A washing machine follows a fixed program. A robot vacuum cleaner is different: it feels a wall, decides to turn, and turns. That loop of sense, decide, act is the heart of every robot, from a toy car to a factory arm.
  1. 1

    Sense

    Read the sensors: Is there a line under me? How far is the wall?
  2. 2

    Decide

    Your code turns those readings into a choice: go straight, turn, stop.
  3. 3

    Act

    The motors spin the wheels at the speeds you chose.
  4. 4

    Repeat

    Do it again, many times per second. Small, frequent corrections beat big, rare ones.

Why start with a virtual robot?

Real parts cost money, can break and take time to wire. A simulator is a small program that pretends to be the robot and its room. You can crash it a hundred times for free, change one number and try again in a second. Real robotics teams do this too. But a simulator is always simpler than the real world, so later we test on real hardware as well.

Your robot

  • It lives in a square room, 200 cm by 200 cm, seen from above. x goes to the right, y goes 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.
  • heading is 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

This robot has no steering wheel. It turns by running its two wheels at different speeds. This is called differential drive, and many small robots and robot vacuum cleaners use it.
Left wheelRight wheelWhat the robot does
Forward, same speedForward, same speedDrives straight
SlowFastCurves to the left
FastSlowCurves to the right
BackwardForwardSpins on the spot, to the left
StoppedStoppedStands still
Python: how wheel speeds become motionpython
# 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

In every lab of this track you write one 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.
The shape of every decide functionpython
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}))
Note
In this first lab the robot knows its exact position (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.
Do this
A virtual robot cannot hurt anyone. A real one can pinch fingers in its wheels, fall off a table or knock things over. From lesson 6 on, we always test real robots on the floor, with the wheels lifted first.
Try this. Before you open the lab, predict: if the left wheel is at 0.2 and the right wheel at 0.6, will the robot curve left or right? Then run the code above with those numbers.

Watch and read more

Lab

A decide function that turns your virtual robot towards a goal, drives there and stops.

~20 min

The problem

This runs in your browser. The robot starts at (30, 30) facing right. The goal is at (160, 160).

  1. Run the starter. The robot drives straight into the wall. Look at the picture.
  2. Change decide so the robot turns towards the goal, drives there and stops within 10 cm of it, without touching a wall.
  3. 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 with np.arctan2 and steer towards it.
  4. 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.

Output

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.

Please sign in to continue.

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.

Sign in to take the test and save your progress