What you learn today
- Detect NaN
- Guard with select
- Jump to a safe branch on NaN
Somebody removes the sensor in the lab to move it. The chip notices nothing – it just keeps reading. lbn with Average now returns NaN, and that travels silently through every calculation. From today on every controller in the course gets protection against it.
Where NaN comes from
lbn/lbwithAveragefinds no device: wrong name, device removed, cable cut.- Calculations without a result, such as 0 divided by 0.
- A
definewith another define as its value.
What happens then: every calculation with NaN gives NaN. Every comparison like slt or sgt gives 0. sgn returns 0. A display shows nonsense, a pump receives a value nobody anticipated.
The three tools
| Instruction | Result |
|---|---|
snan r? a | 1 if a is NaN, otherwise 0 |
snanz r? a | 0 if a is NaN, otherwise 1 – that is, “reading valid” |
bnan a target | jumps to the label if a is NaN |
Guarding with select
If the program should carry on, replace the broken value with a safe one:
snan missing press
select flow missing 0 flow # NaN: pump stands still
Jumping to a safe branch
If several devices have to react differently to a failure, jump away:
lbn temp SENSOR HASH("Raum") Temperature Average
bnan temp fault
# ... normal operation ...
j start
fault:
sbn HEATER HASH("Heizung") On 0
j start
Which direction is safe?
You decide that per device before writing the code:
| Device | safe state on sensor failure |
|---|---|
| heater | off – better cold than overheated |
| pump filling a tank | off – better empty than burst |
| warning light | on – a failure is a fault |
| display | colour red instead of a made-up number |
A comparison with NaN is never true. That looks like built-in protection – slt really does switch the heater off on NaN. But sgn, calculations and displays still get nonsense, and nobody learns about the failure. Protection that only works by accident is no protection.
Test every controller without its sensor too. In the simulator you simply remove the device – the exercises from today on do that in a test of their own.
Diagram
Exercise
Task
A safe heating controller. On the network: gas sensor “Raum”, wall heater “Heizung”, LED display “Raumtemp”, flashing light “Sensorfehler”.
- Normal operation: heater on below 19 °C (292.15 K), the display shows the temperature in °C, colour green.
- The flashing light shows with
snanwhether the sensor is missing. - If the sensor is missing: use
bnanto jump to a fault branch – heater off, display red.
Hints
Hint 1
Right after the lbn: snan missing temp, then switch the light.
Hint 2
bnan temp fault jumps away before anything is computed with NaN.
Hint 3
In the fault: branch heater off, display red, j start.
Quiz
5 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in