← AI robots

Delivery robot

Build a sign-driven delivery robot in three milestones. Its own distance sensor overrules the model, it reports back through the bridge, and it counts its deliveries.

Delivery robots on footpaths and in hospitals follow instructions, but their own sensors always get the last word: if something is in the way, they stop, whatever they were told.

The challenge puts the course together. You start from your careful sign robot (Signs: some mistakes cost more): Go after two Gos, Left and Right turn, everything else stops, with a watchdog. Then, in three milestones with less help each time:

  1. The robot's distance sensor overrules the model: something in the way means stop, whatever the sign says.
  2. The robot reports back through the bridge: its distance, and when it is blocked.
  3. It counts deliveries and makes the system yours.

In the simulator, the buttons stand in for the model (A Left, B Right, A+B Go, logo Stop). Drag a box in front of a robot to block it.

WidthMore contexts, same idea.
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
robot.ts
radio.onReceivedString(function (receivedString) {
    if (receivedString == "Go") {
        goCount += 1
    } else {
        goCount = 0
    }
    mode = receivedString
    lastHeard = input.runningTime()
})
basic.forever(function () {
    if (input.runningTime() - lastHeard > 2000) {
        mode = "Stop"
    }
    if (mode == "Go" && goCount >= 2) {
        robot.motorTank(40, 40)
        robot.setColor(0x00ff00)
    } else if (mode == "Left") {
        robot.motorTank(-30, 30)
        robot.setColor(0xffff00)
    } else if (mode == "Right") {
        robot.motorTank(30, -30)
        robot.setColor(0xffff00)
    } else {
        robot.motorStop()
        robot.setColor(0xff0000)
    }
})
input.onButtonPressed(Button.A, function () {
    radio.sendString("Left")
})
input.onButtonPressed(Button.B, function () {
    radio.sendString("Right")
})
input.onButtonPressed(Button.AB, function () {
    radio.sendString("Go")
})
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
    radio.sendString("Stop")
})
let goCount = 0
let lastHeard = 0
let mode = ""
robot.elecfreaksCuteBot.start()
radio.setGroup(7)
The model reads Go (twice), and the robot sets off. A box is in its way. What happens?

RunRun it and compare the result with your prediction.

Run

Press A+B twice on one micro:bit, then drag a box in front of the other robot.

The program as blocks
Show as code
robot.ts
radio.onReceivedString(function (receivedString) {
    if (receivedString == "Go") {
        goCount += 1
    } else {
        goCount = 0
    }
    mode = receivedString
    lastHeard = input.runningTime()
})
basic.forever(function () {
    if (input.runningTime() - lastHeard > 2000) {
        mode = "Stop"
    }
    if (mode == "Go" && goCount >= 2) {
        robot.motorTank(40, 40)
        robot.setColor(0x00ff00)
    } else if (mode == "Left") {
        robot.motorTank(-30, 30)
        robot.setColor(0xffff00)
    } else if (mode == "Right") {
        robot.motorTank(30, -30)
        robot.setColor(0xffff00)
    } else {
        robot.motorStop()
        robot.setColor(0xff0000)
    }
})
input.onButtonPressed(Button.A, function () {
    radio.sendString("Left")
})
input.onButtonPressed(Button.B, function () {
    radio.sendString("Right")
})
input.onButtonPressed(Button.AB, function () {
    radio.sendString("Go")
})
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
    radio.sendString("Stop")
})
let goCount = 0
let lastHeard = 0
let mode = ""
robot.elecfreaksCuteBot.start()
radio.setGroup(7)
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

Which part should decide each of these?

Which way the sign says to go
Whether a box is right in front of the robot
Whether it's safe to keep driving forward this moment
Whether the delivery stop has been reached (the Stop sign)

Jo checks the distance in the last else if, after Go, Left and Right. Her robot still drives into boxes on Go. Why?

ModifyChange the code on purpose and check the result.

Milestone 1: the sensor wins

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?

Robot obstacle distance is in Robot › Sensors.

Another hint

In forever, before the Go branch: if obstacle distance < 15, then stop and set red; turn the old if into else if.

If something is closer than 15 cm, the robot stops and shows red, whatever the model says.

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

ModifyChange the code on purpose and check the result.

Milestone 2: report back

The bridge passes these on to the board, where they appear as values (in_dist, in_blocked).

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 second forever: radio send value dist, obstacle distance, then pause 500.

Another hint

A variable blocked (false at the start). When it stops for a box and blocked is false: radio send value blocked 1 and set blocked to true. When the way is clear again: send blocked 0 and set it to false.

The robot sends its distance twice a second, and sends blocked 1 once when it stops for something and blocked 0 once when the way is clear.

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

Run TypicalRun it and compare the result with your prediction.

Try the full system

Download milestone 2 to the robot, and the bridge program to the micro:bit on the computer. Drive with your signs and watch the robot's distance arrive. Block it with your hand.

Try it yourselfYour edits are saved in this browser.

Drive with your signs while the robot reports back: get Go sent, and at least 10 replies from the robot.

Use the board. This ticks when you get there.

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

Milestone 3: deliveries

Count deliveries: a Stop sign that arrives while the robot is going. Send the count back and show it when the robot is shaken. Then make it yours: a library book trolley, a lunch-order runner, a parcel robot. Change the signs, lights and sounds 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 delivery is a Stop sign that arrives while the robot was going (mode = Go).

Another hint

Send the count with send value. Pick a short name, such as done: names are 8 characters at most, so deliveries would be cut short.

The robot counts deliveries (a Stop that arrives while going), sends the count back, and shows it when shaken.

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

Info

On real robots

For teachers

Students build a sign-driven delivery robot in three milestones; its distance sensor overrules the model, it reports back through the bridge, and it counts deliveries. About 60 minutes.

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

Students will be able to

Before class

The challenge: everything from AI robots plus Robotics challenges. The robot starts as the careful sign robot (Go twice, Unsure and silence stop it). The milestones add no new blocks:

  1. The sensor wins: if obstacle distance < 15 → stop goes first in forever. The model only knows what the camera sees on the sign; the sensor knows what is in front of the robot. Jo's robot (the check last) is the mistake to watch for.
  2. Report back: a second forever sends dist twice a second; blocked 1 and blocked 0 go once each, using a variable (the same "send once" idea as Patrol and alert). The bridge turns them into lines such as dist:23, which the board shows as values.
  3. Deliveries: a Stop that arrives while going is a delivery; count it, send it back, show it.

Without robots: every milestone works in the simulator (buttons stand in for the model; drag a box to block). The full-system step is typical.

Running the lesson

  1. Predict: most expect it to stop. Ask: which line looks at the box?
  2. Investigate: Jo's robot is the order mistake; trace the if, else if aloud with mode = Go.
  3. Milestone 2: check blocked is sent once, not every loop; the board's message list shows it.
  4. Milestone 3: let teams choose their delivery story and signs.

The hard part (on purpose)

Putting the sensor check first so it overrules the model. When the robot pushes boxes, ask which question in the chain is asked first.

When students get stuck

Stretch and support

Stretch: Make the robot slow down (motor tank 20) between 15 and 30 cm, and stop under 15.

Support: Give milestone 1's if complete; students write the reports in milestone 2.

Where it leads

Where this leads: real robots fuse a camera model with many sensors (distance, bump, line), and safety checks always win. The Python pathway's Machine learning with Gradio trains models in code.

What each step is for

Now you can
You reached the endBack to AI robots