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:
The board sends Unsure when no label is confident enough (it needs 80% here), so the robot can stop.
The board sends the current label again every second. If the robot hears nothing for two seconds (the laptop sleeps, the cable comes out, the robot drives out of radio range), a watchdog stops it.
In the simulator, A+B sends Forward once, like a model that then goes quiet, and B sends Unsure.
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
WidthMore contexts, same idea.
Make your prediction first. Help opens once you've locked it in.
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
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)
} elseif (mode == "Left") {
robot.motorTank(-30, 30)
} elseif (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 = 0let mode = ""robot.elecfreaksCuteBot.start()
radio.setGroup(7)
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.
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)
} elseif (mode == "Left") {
robot.motorTank(-30, 30)
} elseif (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 = 0let 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
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)
} elseif (mode == "Left") {
robot.motorTank(-30, 30)
} elseif (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 = 0let 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
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.
I can make a robot stop when it hears nothing for a while (a watchdog).
I can explain why the board repeats its label every second.
Before class
The idea: a robot driven by a model needs a safe action for two different problems:
The model can't decide (no label above 80%): the board sends Unsure, and the robot's else (or an Unsure branch) stops it.
Nothing arrives (laptop asleep, cable out, out of radio range): a watchdog remembers when the last message came (lastHeard = running time) and stops the robot when too long has passed.
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
Predict: most say it never stops (that was last lesson). Ask: what does lastHeard remember?
Run: count the seconds aloud together.
With your model: unplugging the bridge is the most convincing moment; do it at the front.
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
The robot stops every two seconds even while the gesture is held. Ask: Is the board repeating the label (repeat: 1)? Is the robot's radio group right? Fix: Use this lesson's board; check the group.
The robot never stops after silence. Ask: Is lastHeard set in on radio received string? Is the check in forever? Fix: Set lastHeard on every message and compare in forever.
The robot beeps over and over. Ask: Does it beep only when the mode first becomes Lost? Fix: Beep only if the mode wasn't Lost already.
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
With your model: With a real model, half-shown gestures and an empty frame produce Unsure, and the robot stops instead of carrying on.
Investigate: Unsure covers a model that can't decide; the watchdog covers silence. Both lead to the safe action, stop, and the repeated label keeps a working robot from tripping the watchdog.
Modify: The watchdog's time and the Unsure action are design choices: shorter is safer but less forgiving of a slow connection.
Now you can
Make a robot stop when the model is unsure.
Make a robot stop when it hears nothing for a while (a watchdog).
Explain why the board repeats its label every second.