stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 38

Day 38 of 365 · Beginner · Month 2: Order and data network

Catching NaN: snan, snanz, bnan

snan, snanz and bnan detect NaN. How to notice a failed sensor, bring devices into a safe state and report the fault.

Lesson 12 min Week 6

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/lb with Average finds no device: wrong name, device removed, cable cut.
  • Calculations without a result, such as 0 divided by 0.
  • A define with 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

InstructionResult
snan r? a1 if a is NaN, otherwise 0
snanz r? a0 if a is NaN, otherwise 1 – that is, “reading valid”
bnan a targetjumps 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:

Devicesafe state on sensor failure
heateroff – better cold than overheated
pump filling a tankoff – better empty than burst
warning lighton – a failure is a fault
displaycolour red instead of a made-up number
Warning

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.

Tip

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

Three readings, five batch modes Sensor “Raum” 300 K Sensor “Raum” 310 K Sensor “Raum” 250 K lbn Average 286.67 Sum 860 Minimum 250 Maximum 310 Count 3 Sum on temperatures: 860 K – the heater thinks there is a fire.
If Average finds no matching device, the result is NaN. Every calculation with it gives NaN again.

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 snan whether the sensor is missing.
  • If the sensor is missing: use bnan to 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

Esc
↑ ↓ selectEnter open/ opens search anywhere