← AI robots

When the model is unsure

A robot driven by a model needs a safe action for when the model is unsure and for when it goes quiet. The board sends Unsure and repeats its label; the robot stops on Unsure, and a watchdog stops it when nothing arrives.

A delivery robot loses its connection in a lift. Should it keep rolling the way it was going, or stop and wait? Safety engineers decide this before anything is built.

In Drive with gestures, the robot kept going when your hand left the camera (and a wrist remote only sends when its action changes). Two things fix that:

In the simulator, A+B sends Forward once, like a model that then goes quiet, and B sends Unsure.

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) {
    mode = receivedString
    lastHeard = input.runningTime()
})
basic.forever(function () {
    if (input.runningTime() - lastHeard > 2000) {
        mode = "Stop"
    }
    if (mode == "Forward") {
        robot.motorTank(50, 50)
    } else if (mode == "Left") {
        robot.motorTank(-30, 30)
    } else if (mode == "Right") {
        robot.motorTank(30, -30)
    } else {
        robot.motorStop()
    }
})
// Test remote: A+B sends Forward once (like a model that then goes quiet); B sends Unsure.
input.onButtonPressed(Button.AB, function () {
    radio.sendString("Forward")
})
input.onButtonPressed(Button.B, function () {
    radio.sendString("Unsure")
})
let lastHeard = 0
let mode = ""
robot.elecfreaksCuteBot.start()
radio.setGroup(7)
You press A+B once, then nothing else. What does the other robot do?

RunRun it and compare the result with your prediction.

Run

Press A+B once and count. Then press A+B and, before it stops, B.

The program as blocks
Show as code
robot.ts
radio.onReceivedString(function (receivedString) {
    mode = receivedString
    lastHeard = input.runningTime()
})
basic.forever(function () {
    if (input.runningTime() - lastHeard > 2000) {
        mode = "Stop"
    }
    if (mode == "Forward") {
        robot.motorTank(50, 50)
    } else if (mode == "Left") {
        robot.motorTank(-30, 30)
    } else if (mode == "Right") {
        robot.motorTank(30, -30)
    } else {
        robot.motorStop()
    }
})
// Test remote: A+B sends Forward once (like a model that then goes quiet); B sends Unsure.
input.onButtonPressed(Button.AB, function () {
    radio.sendString("Forward")
})
input.onButtonPressed(Button.B, function () {
    radio.sendString("Unsure")
})
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.

Run TypicalRun it and compare the result with your prediction.

With your model

Use your model from last lesson and the same bridge. Show Forward, then slide your hand half out of the frame. The board shows Unsure and sends it. Then unplug the bridge for three seconds: the robot stops by itself.

Try it yourselfYour edits are saved in this browser.

Show a gesture half in the frame, or none, until the board sends Unsure.

Use the board. This ticks when you get there.

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

Investigate

The program as blocks
Show as code
robot.ts
radio.onReceivedString(function (receivedString) {
    mode = receivedString
    lastHeard = input.runningTime()
})
basic.forever(function () {
    if (input.runningTime() - lastHeard > 2000) {
        mode = "Stop"
    }
    if (mode == "Forward") {
        robot.motorTank(50, 50)
    } else if (mode == "Left") {
        robot.motorTank(-30, 30)
    } else if (mode == "Right") {
        robot.motorTank(30, -30)
    } else {
        robot.motorStop()
    }
})
// Test remote: A+B sends Forward once (like a model that then goes quiet); B sends Unsure.
input.onButtonPressed(Button.AB, function () {
    radio.sendString("Forward")
})
input.onButtonPressed(Button.B, function () {
    radio.sendString("Unsure")
})
let lastHeard = 0
let mode = ""
robot.elecfreaksCuteBot.start()
radio.setGroup(7)

Match each problem with what keeps the robot safe.

The model is 55% Forward and 45% Left
The laptop goes to sleep
The model sends a word the robot doesn't know

The board stopped repeating the label every second, and only sent it when it changed. What would happen when you hold Forward steadily?

Why stop, rather than keep doing the last thing, when unsure?

ModifyChange the code on purpose and check the result.

Modify

Need a hint?

Change 2000 to 1000 in the watchdog's if.

Need a hint?

Add an else if: mode = Unsure, then robot motor stop and robot set color yellow.

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.

Make the watchdog stop the robot after one second of silence.

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

When Unsure, stop and show yellow lights, so you can see why it stopped.

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

This starter has no watchdog. Add one that, after 1.5 seconds of silence, stops the robot, turns its lights red and beeps once. Any new label should set it going again.

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?

Remember when the last message arrived (lastHeard = running time) in on radio received string.

Another hint

In forever: if running time − lastHeard > 1500, set the mode to Lost. Beep once when it first becomes Lost (only if the mode wasn't Lost already).

A lost-signal alarm: after 1.5 seconds of silence the robot stops, shows red lights and beeps once; any new label sets it going again.

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

For teachers

Students make a model-driven robot safe when the model is unsure (the board sends Unsure) and when it goes quiet (a watchdog stops the robot after a few seconds of silence). About 45 minutes.

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

Students will be able to

Before class

The idea: a robot driven by a model needs a safe action for two different problems:

For the watchdog to work, a robot that is working fine must keep hearing from the board, so the board repeats the current label every second (a heartbeat). Without the repeat, holding Forward steadily would trip the watchdog.

Without robots: in the simulator, A+B sends Forward once and B sends Unsure; the model step is typical.

Running the lesson

  1. Predict: most say it never stops (that was last lesson). Ask: what does lastHeard remember?
  2. Run: count the seconds aloud together.
  3. With your model: unplugging the bridge is the most convincing moment; do it at the front.
  4. Investigate: the why repeat question is the hard one; draw a timeline of messages.

The hard part (on purpose)

Deciding what the robot does when the model isn't sure, and the watchdog timing. Ask what should happen when nothing has been heard for a while, before they build it.

When students get stuck

Stretch and support

Stretch: Slow down instead of stopping when Unsure arrives once, and stop only after three Unsures in a row.

Support: Give the watchdog's if complete; students add only the actions inside it.

Where it leads

Next: some mistakes cost more than others. Real world: dead man's switches on trains, heartbeat checks in drones and delivery robots.

What each step is for

Now you can
Next lesson4. Signs: some mistakes cost moreMistaking Stop for Go is worse than mistaking Left for Right, so the robot should need more confidence before a risky action than a safe one. The board asks 95% for Go and 70% for the rest; the robot also waits for Go twice in a row.