Driving to a plan

The Maqueen robot drives a square: our own functions for driving and turning, the speed measurements, and why a robot with two motors doesn't drive straight.

the robot drives a square marked with tape on the floor
So far the robot has only made short moves, one move per button. Today it will drive to a plan: forward, turn, forward, turn… until it has driven a square.

For that we’ll make our own blocks, functions, and use the measurements from the last lesson, How fast does the motor spin?.

Today we will:

  • make the functions naprijed and okreni;
  • give numbers names: meet constants;
  • set up an exact quarter turn;
  • drive a square with the repeat block;
  • find out why the robot doesn’t drive perfectly straight, and how to fix it;
  • if there’s time, drive a slalom between cups.

Materials

  • a Maqueen robot with a micro:bit;
  • the speed table from the last lesson;
  • a tape measure and sticky tape;
  • free space on the floor, at least 1 × 1 metre;
  • for the slalom: three plastic cups and a space 2 metres long.

Exercise 1: The function naprijed

Program: Plan, step 1   Technique: a function with a parameter

In the smart crossroads we made the function Svjetlo: a group of blocks with a name, which we give numbers. Today we make a function for driving. It’s called naprijed (Croatian for “forward”), and the number we give it is how long it drives, in milliseconds.

Start a new project, call it Plan and add the maqueen extension. In the Functions category, make the function naprijed with one number, vrijeme (Croatian for “time”).

Plan before you program:
FUNCTION naprijed (vrijeme)
both motors forward, at speed 150
wait vrijeme
stop both motors
wait 0.2 seconds
ON START
stop both motors
ON BUTTON A PRESSED
naprijed (1000)
the function naprijed with the parameter vrijeme, called on button A

Why does the robot wait 0.2 seconds after stopping? After it stops, the robot still rolls a little. If the next move started straight away, the moves would get mixed up.

Predict

Look at your table from the last lesson. How far does the robot drive at speed 150 in 2 seconds? So how far will it drive in 1 second?

Try it

Press A and measure. Did the robot get close to what you predicted? Then work it out: how long does it have to drive to cover 50 cm? Put that number into the function call and check with the tape measure.

Exercise 2: The function okreni

Program: Plan, step 2   Technique: constants and setting up (calibration)

For a square, the robot has to turn by a quarter of a circle, like when you turn a corner.

We carry on in the Plan project. Make the function okreni (Croatian for “turn”) with the number smjer (direction). For the direction we use the agreed numbers from the lesson The micro:bit as a controller: 3 is left, 4 is right.

But when the program says okreni (4), it’s hard to see straight away which way the robot turns. So we give the numbers names. Make the variables LIJEVO and DESNO (Croatian for “left” and “right”) and set them to 3 and 4 at the start of the program. Now the program says okreni (DESNO), and everyone understands it.

We set LIJEVO and DESNO only once, at the start, and never change them anywhere else, because left is always 3. A name for a number that never changes is called a constant. We write the names of constants in capital letters, so it’s clear at once that the program mustn’t change them. MakeCode has no special block for constants, so we use ordinary variables for them.

How long the turn lasts is kept in the variable okret (turn). Add an on button B pressed block that turns the robot right.

Add to the program:
FUNCTION okreni (smjer)
IF smjer = LIJEVO
left motor backward, right forward, at speed 100
ELSE
left motor forward, right backward, at speed 100
wait okret
stop both motors
wait 0.2 seconds
ON START
set LIJEVO to 3
set DESNO to 4
set okret to 400
ON BUTTON B PRESSED
okreni (DESNO)
the function okreni with the parameter smjer, the constants LIJEVO and DESNO, and the variable okret

Set up the turn

400 milliseconds is only a first guess. Put the robot on a line of sticky tape and press B. Did it turn exactly a quarter of a circle, so that it now stands at a right angle to the line?

  • If it turned too little, make okret bigger.
  • If it turned too much, make okret smaller.

Repeat until the turn is right. Write down your number:

A quarter turn takes my robot ______ ms.

Real machines are set up the same way. We call it calibration: you measure, correct the number, then measure again.

Exercise 3: The square

Program: Plan, step 3   Technique: the repeat loop

A square is four equal sides and four equal turns. Instead of putting the same blocks together four times, we use the repeat block from the smart crossroads.

Change the on button A pressed block: put the naprijed call inside repeat 4 times, together with the call okreni (DESNO).

Change:
ON BUTTON A PRESSED
REPEAT 4 TIMES
naprijed (1000)
okreni (DESNO)
button A repeats forward and turn four times
the plan of the square: four sides and four right turns, back to the start

Predict

Mark the robot’s starting spot with tape. When the robot has driven the square, will it stop exactly at the start?

Try it

Run the square and mark where the robot stopped. How far is it from the start? Run it twice more. Does it always stop in the same place?

Why doesn't the robot stop exactly at the start?
Every turn is a little too small or a little too big, and the wheels slip a little. Small errors add up: after four sides and four turns the robot is quite far from the start. A robot that only drives to a plan doesn’t know where it is. That’s why robots need sensors and remote control, and we’ll meet those in the next lessons.

Exercise 4: Drive straight

Program: Plan, step 4   Technique: each wheel its own speed

Stick a straight line of tape on the floor, 1 metre long. Put the robot at the start of the line and let it drive forward. Does it stay on the line?

Almost no robot drives perfectly straight. Two motors are never exactly the same: one turns a little faster than the other, so the robot drifts towards the side of the slower wheel. It’s not a fault. It’s like that with every robot that has two motors.

a robot with one faster wheel drifts off the line, and a robot with corrected speeds drives straight

The fix: each wheel gets its own speed. We carry on in the Plan project. Make the variables lijevaBrzina and desnaBrzina (left speed and right speed) and set them to 150. Change the function naprijed: instead of one block for both motors, use two blocks, one for each motor. Add a long straight drive on button B, so it’s easier to try out. You don’t need the turn on a button any more.

Add and change:
ON START
set lijevaBrzina to 150
set desnaBrzina to 150
FUNCTION naprijed (vrijeme)
left motor forward, at speed lijevaBrzina
right motor forward, at speed desnaBrzina
(the rest stays the same)
ON BUTTON B PRESSED
naprijed (3000)
the function naprijed with its own speed for the left and right wheel

Try it

Press B and watch which way the robot drifts.

  • Does it drift left? The left wheel is slower, so lower desnaBrzina, for example to 140.
  • Does it drift right? Lower lijevaBrzina.

Change the number a little at a time, until the robot stays on the line for the whole metre. Write down your robot’s speeds:

lijevaBrzina = ______, desnaBrzina = ______

Then run the square again with button A. Does it stop closer to the start now?

Exercise 5: Slalom (optional)

Program: Plan, step 5   Technique: a driving plan

If you have time left, the robot can drive a slalom, like a skier between the poles. Put three cups in a row, half a metre apart. Put the robot 25 cm to the left of the row of cups and 25 cm before the first cup, so that it faces along the row.

the slalom plan: five drives of 50 cm and turns right, left, left, right, past the three cups on alternate sides

Each drive in the plan is 50 cm long. You worked out in exercise 1 how many milliseconds the robot needs for 50 cm. We carry on in the Plan project. Make the variable korak (Croatian for “step”) and set it to that number, so you don’t have to type it five times. Add an on button A+B pressed block with the slalom plan.

Add to the program:
ON START
set korak to (your number for 50 cm)
ON BUTTON A+B PRESSED
naprijed (korak)
okreni (DESNO)
naprijed (korak)
okreni (LIJEVO)
naprijed (korak)
okreni (LIJEVO)
naprijed (korak)
okreni (DESNO)
naprijed (korak)
buttons A and B together drive the slalom: five calls of naprijed and four turns

Try it

Press A and B together. Does the robot get past the cups without knocking any over? If it bumps into a cup, first check that the turn is exactly a quarter of a circle, then check korak.

Predict: what would the plan look like if the robot started to the right of the row of cups?
The same, only every turn is swapped: where there was LIJEVO, there is now DESNO, and where there was DESNO, there is now LIJEVO.

Extra tasks

  1. Triangle. Let the robot drive an equilateral triangle. Hint: at each corner of the triangle the robot turns by a third of a circle. How many times do you need to repeat, and how much longer does a third of a circle take than a quarter?
  2. Forward in centimetres. For the older ones: make a function naprijedCm, which you give a number of centimetres and which works out by itself how long to drive. Hint: from the table, work out how many centimetres the robot covers in a second.

What’s next?

Next time the robot no longer drives to a plan: you drive it, with the sender from the lesson The micro:bit as a controller.

What did we learn?

  • A function is a group of blocks with a name. A parameter is a number we give it when we call it.
  • The functions naprijed and okreni make the program shorter and easier to read.
  • A constant is a name for a number that never changes, for example LIJEVO and DESNO. We write the names of constants in capital letters.
  • Calibration: you measure, correct the number, then measure again.
  • The repeat block repeats the same blocks a given number of times.
  • A robot that only drives to a plan doesn’t know where it is, so small errors add up.
  • Two motors aren’t the same, so the robot only drives straight when each wheel gets its own speed.

Questions to finish

1. What is a function's parameter?
A number we give the function when we call it. The function naprijed gets vrijeme: how long it drives.
2. Why does the function naprijed wait 0.2 seconds at the end?
After it stops, the robot still rolls a little. If the next move started straight away, the moves would get mixed up.
3. What is calibration?
Setting a number by measuring: you measure, correct the number, then measure again, until it’s right. That’s how we set up the quarter turn.
4. How many times do we repeat forward and turn for a square, and why?
Four times, because a square has four sides and four corners.
5. Why doesn't the robot stop exactly at the start after the square?
Every turn and every side has a small error, and small errors add up. The robot doesn’t know where it is, because it has no sensors to tell it.
6. The robot drifts right. Which wheel's speed do you lower?
The left one. The robot drifts right because the left wheel turns faster than the right one.
7. Why do we write okreni (DESNO) in the program, and not okreni (4)?
Because okreni (DESNO) is clear straight away, while for okreni (4) you have to remember what we agreed. DESNO is a constant: a name for a number that never changes.