← Robotics challenges

Follow the line

Use the two line sensors to tell which side the line is on, and steer to stay on it.

In many warehouses and hospitals, robots carry boxes and medicines along coloured lines on the floor. They do not know where they are; they only know whether each sensor still sees the line.

Under the front of the robot are two line sensors, a little to the left and a little to the right. Each one shines light at the floor and measures what bounces back: a dark line reflects less than the floor around it. In blocks, robot detect line left is true when the left sensor is over the line. You find it in Robot.

The simulator's arena has a dark loop to follow. If the robot does not start on the line, drag it onto the line, facing along it.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

The program as blocks
Show as code
line.ts
robot.elecfreaksCuteBot.start()
basic.forever(function () {
    if (robot.detectLine(RobotLineDetector.Left)) {
        robot.motorTank(10, 40)
    } else {
        robot.motorTank(40, 10)
    }
})

robot motor tank 10 40 steers left (the right wheel is faster). 40 10 steers right.

This robot uses only its left sensor. It starts on the line. What does it do?

RunRun it and compare the result with your prediction.

Run

Watch the robot go round the loop. Look closely at how it moves: smoothly, or in small swings?

The program as blocks
Show as code
line.ts
robot.elecfreaksCuteBot.start()
basic.forever(function () {
    if (robot.detectLine(RobotLineDetector.Left)) {
        robot.motorTank(10, 40)
    } else {
        robot.motorTank(40, 10)
    }
})
Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.

InvestigateTrace how each line of code connects to what you see.

Investigate

This version uses both sensors, so the robot knows which way it has drifted. It drives straight while both see the line, and steers only when one slips off. Both see the line is an and block (in Logic): it is true only when the question on each side is true.

The program as blocks
Show as code
line.ts
robot.elecfreaksCuteBot.start()
basic.forever(function () {
    if (robot.detectLine(RobotLineDetector.Left) && robot.detectLine(RobotLineDetector.Right)) {
        robot.motorTank(40, 40)
    } else if (robot.detectLine(RobotLineDetector.Left)) {
        robot.motorTank(10, 40)
    } else if (robot.detectLine(RobotLineDetector.Right)) {
        robot.motorTank(40, 10)
    } else {
        robot.motorStop()
    }
})

Match each case to the right move. Watch the robot in the simulator to check.

Kai's program:

The program as blocks
Show as code
robot.elecfreaksCuteBot.start()
basic.forever(function () {
    if (robot.detectLine(RobotLineDetector.Left) && robot.detectLine(RobotLineDetector.Right)) {
        robot.motorTank(40, 40)
    } else if (robot.detectLine(RobotLineDetector.Left)) {
        robot.motorTank(40, 10)
    } else if (robot.detectLine(RobotLineDetector.Right)) {
        robot.motorTank(10, 40)
    } else {
        robot.motorStop()
    }
})

Match what the sensors see with what the robot should do.

both see the line
only the left sees the line
only the right sees the line
neither sees the line

Kai's robot runs off the loop at the first bend. Which part is wrong?

ModifyChange the code on purpose and check the result.

Modify

Delivery robots go faster where the line is straight. Make the robot drive at 60 when both sensors see the line. Does it still get round the bends?

Need a hint?

Change the first robot motor tank (both sensors see the line) to 60 60.

When neither sensor sees the line, the robot stops. Make its lights turn red too, so a person knows it needs help.

Need a hint?

robot set color is in Robot. Put it in the last else, after robot motor stop, and choose red.

Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.

Go faster on straight sections: 60 when both sensors see the line.

This ticks by itself when your blocks do what is asked.

Turn the lights red when the line is lost.

This ticks by itself when your blocks do what is asked.

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Make a delivery robot: it follows the loop, but if something is on the line ahead, it stops and waits until it is moved.

Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.
Need a hint?

Add one more question at the very top of the if: robot obstacle distance < 10. Its move is robot motor stop.

Another hint

Click + on the if to add another else if, then drag the questions so the distance one comes first.

A delivery robot: follow the line, but stop if something blocks the line ahead.

This ticks by itself when your blocks do what is asked.

Info

On a real robot

For a real track, use black electrical tape (about 2 cm wide) on a light floor or a large sheet of paper. Things to notice:

For teachers

Students follow a line with one sensor (swinging along its edge), then two sensors (drive straight, steer towards the side that still sees the line), and combine line following with the distance sensor to make a delivery robot that stops when blocked. About 50 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

The robot has two line sensors under its front. Each is true when it is over the dark line. With one sensor the robot can only swing back and forth along the line's edge. With two, it knows which way it drifted: if only the left sensor still sees the line, the line is to the left, so it steers left (right wheel faster: robot motor tank 10 40). Steering the wrong way is the common slip (Kai's robot).

The simulator's sensors are on/off only. Real sensors measure how much light comes back, which allows smooth steering; that is a good teacher-led extension on real robots. The simulator also adds a small "line assist": after the robot loses the line it slows down for a moment, which helps it find the line again.

The Make combines lessons 3 and 5: the distance question must come first, or the robot would follow the line straight into the box. If the robot does not start on the line, students drag it onto the line, facing along it. MOVE Motor classes can use Kitronik's line-sensor blocks on the real robot; they give a number, so steering on the difference between left and right is a natural extension.

Running the lesson

  1. Predict: the one-sensor swing surprises students. Ask why it follows the edge, not the middle.
  2. Run: look for the swinging. If the robot is not on the line, drag it on.
  3. Investigate: act it out. One student is the robot, two others are the sensors calling 'line!'. Kai's robot is the wrong-way slip.
  4. Modify: faster straights may make it overshoot the bends: that is the trade-off real robots tune.
  5. Make: ask which question goes first and why. Then drag a box onto the line to test.
  6. Real robot (optional): black electrical tape on light paper works best.

The hard part (on purpose)

Steering towards the sensor that still sees the line. Ask which side the line is on when only the left sensor sees it.

When students get stuck

Stretch and support

Stretch: Count laps: when both sensors lose the line for a moment at a gap, or each time a button is pressed, add 1 and show it. Or: drive the loop the other way.

Support: Give the two-sensor program with the turns removed from the middle branches; students choose the steering for each.

Where it leads

Next unit: Modes. One robot, many jobs: a variable chooses between stop, drive and follow. Real-world links: warehouse robots, hospital delivery robots, robot lawn mowers that follow a boundary wire.

What each step is for

Now you can
Next lesson6. One robot, many jobsA mode variable remembers which job the robot is doing; buttons change it, and forever does that job.