← Robotics challenges

Patrol and alert

Build a patrol system in three milestones, a robot and a base that talk both ways, then add an alert log and status lights.

Hospitals use robots to carry medicines along set routes, and farms use them to check fences. A person starts and stops them from a base and hears from them only when something is wrong.

The assistance challenge puts the course together in a patrol system. A robot follows the line (lesson 5) and stops when blocked (lesson 3). A base station starts and stops it by radio (lesson 7), and the robot reports problems back once (lesson 9). The robot remembers its job in a mode variable (lesson 6). You will build it in three milestones, with less help each time.

As in lesson 9, both micro:bits run the same program: press A+B to make one of them the base.

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
patrol.ts
function followLine () {
    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()
    }
}
robot.elecfreaksCuteBot.start()
basic.forever(function () {
    if (robot.obstacleDistance() < 15) {
        robot.motorStop()
    } else {
        followLine()
    }
})
This is where the patrol starts: a robot on its own. You drag a box onto the line in front of it, then drag it away. What happens?

RunRun it and compare the result with your prediction.

Run

Drag a box onto the line in front of the robot, wait, then drag it away.

The program as blocks
Show as code
patrol.ts
function followLine () {
    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()
    }
}
robot.elecfreaksCuteBot.start()
basic.forever(function () {
    if (robot.obstacleDistance() < 15) {
        robot.motorStop()
    } else {
        followLine()
    }
})
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

The program as blocks
Show as code
patrol.ts
function followLine () {
    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()
    }
}
robot.elecfreaksCuteBot.start()
basic.forever(function () {
    if (robot.obstacleDistance() < 15) {
        robot.motorStop()
    } else {
        followLine()
    }
})

Sort each job to the robot or the base.

Jack's robot, when it hears a message:

The program as blocks
Show as code
radio.onReceivedString(function (receivedString) {
    if (receivedString == "go") {
        followLine()
    }
})

In the full patrol system, which jobs belong to the robot, and which to the base?

Follow the line
Send 'go' and 'stop'
Notice something in the way
Beep and show an X when told of a problem
Send 'blocked' once

Jack's robot only twitches when the base sends 'go': it moves for a moment, then stops. Why?

ModifyChange the code on purpose and check the result.

Milestone 1: start and stop from the base

The robot follows the line from the moment it starts. Put the base in charge:

  1. On the base, A sends "go" and B sends "stop".
  2. When a robot hears a message, it sets its mode: "patrol" for go, "stop" for stop. It starts in "stop".
  3. In forever, the robot follows the line only when its mode is "patrol"; otherwise it stops.
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?

On the base: A sends the word go, B sends stop (send string, in Radio).

Another hint

on radio received receivedString: if role = robot, then set mode to patrol for go, or stop for stop. Set mode to stop in on start.

Another hint

In forever, inside the robot's if: if mode = patrol, follow the line; else, robot motor stop.

The base's A sends 'go' and B sends 'stop'; the robot patrols only while its mode is patrol.

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

ModifyChange the code on purpose and check the result.

Milestone 2: report back

Make the robot report problems:

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?

While patrolling: if the distance is less than 15, stop, and send 'blocked' if not alerted (then set alerted to true). Else send 'clear' if alerted, set alerted to false, and follow the line.

Another hint

On the base, in on radio received: else if role = base, show an X and beep for 'blocked', and a tick for 'clear'.

The patrolling robot stops when blocked and tells the base once ('blocked', then 'clear'); the base shows an X and beeps, then a tick.

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.

Milestone 3: a log and status lights

Finish the system:

Then make it yours: a hospital delivery robot, a fence checker or a warehouse patrol. Change the messages and pictures to suit.

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?

A variable alerts on the base: change by 1 for each 'blocked'. The logo shows it.

Another hint

robot set color in three places: green while patrolling, red when blocked, off when stopped.

The base counts the alerts and shows the count on its logo; the robot's lights show green (patrolling), red (blocked) or off (stopped).

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

Info

On a real robot

Set up a tape loop and put a bare micro:bit with a person as the base (press A+B on it). Things to notice:

For teachers

The assistance challenge. Students build a patrol system in three milestones (the base starts and stops the robot by radio, the robot reports blockages once, then an alert log and status lights), combining line following, distance, modes, roles and two-way radio. About 60 minutes.

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

Students will be able to

Before class

This is the assistance strand's challenge, using lessons 3 and 5 (distance and line), 6 (mode), 7 (radio commands) and 9 (roles and alert once). The key design rule: radio handlers only change the mode; forever drives the motors. A move made in a handler would be undone straight away by forever (or, like Jack's patrol, would only run once).

Both micro:bits run the same program; A+B makes one the base. The base's A sends "go", B sends "stop". The robot sends "blocked" (once, using alerted) and "clear". The milestones fade the help: Milestone 1 gives three numbered steps, Milestone 2 an outline, Milestone 3 goals only. Each starter is the previous milestone's solution. Showcase and Patrol and alert together mark the course Challenge complete.

The robots start near the line in the simulator; if one does not pick it up, drag it on. The logo needs a micro:bit V2 (shake works on V1).

Running the lesson

  1. Predict and Run: a recap of lessons 3 and 5. Make sure everyone can block and unblock the robot.
  2. Investigate: the sort shows the system's two halves. Jack's patrol is the run-once slip in a new place: a handler runs once per message.
  3. Milestone 1: three steps on the board: send a word, set the mode, act on the mode.
  4. Milestone 2: the alerted pattern from lesson 9, now with 'clear' too. Test by blocking and unblocking the patrolling robot.
  5. Milestone 3: a log and status lights. Let groups choose a setting (hospital, farm, warehouse) and rename messages.
  6. Real robot (optional): one bare micro:bit as the base per team, each team its own radio group.

The hard part (on purpose)

Each side has its own job, and radio handlers only set the mode. Ask which part of the program drives the motors.

When students get stuck

Stretch and support

Stretch: Add a 'home' command: the base's logo sends 'home' and the robot turns round (a 'turn' mode that spins for 1.2 seconds, then goes back to patrol).

Support: Pair students for Milestone 2: one builds the robot side, one the base side, then they agree the words.

Where it leads

Real-world links: hospital delivery robots, farm fence checkers, warehouse robots, pipeline inspection. Discussion: what should the robot do when it cannot reach the base?

What each step is for

Now you can
Next lesson12. TripwireTwo robots work together. One stands still as a tripwire and, when the other passes, cues it to do a trick.