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

Day 134 of 365 · Advanced · Month 5: Memory and structure

State machines

A state machine keeps in a register what the plant is doing right now. Every tick checks the transitions, and a display shows the state.

Lesson 15 min Week 20

What you learn today

  • State in a register
  • Transitions with conditions
  • Display the state

A tank is to be filled at the push of a button. The pump runs until 3000 kPa are reached. After that it stays off – even if someone takes gas out and the pressure drops to 2500 kPa. Only when the tank is almost empty is the plant ready again, waiting for the next push.

“Pressure too low → pump on” does not get you far here. At 2500 kPa the pump should run once (right after the push) and once not (after filling). The reading alone is not enough – the chip has to remember what it is doing right now.

The state is a number

You give every stage of the process a number and keep it in a register:

StateNumberPump
Ready0off
Filling1on
Full2off
define READY 0       # waiting for start
define FILLING 1     # pump running
define FULL 2        # tank full
alias state r0

The numbers as define make the code readable: everyone understands seq tmp state FILLING, nobody understands seq tmp state 1. State 0 is the safe starting state on purpose, because every register starts at 0.

Transitions: state and condition

A transition always has two parts: which state you are in and what has to happen. Only when both are true does the state change.

fromconditionto
Readybutton “Start” pressedFilling
Fillingpressure ≥ 3000 kPaFull
Fullpressure < 1000 kPaReady

Every row of the table turns into a few lines of code:

seq tmp state READY           # am I in Ready?
and tmp tmp pressed           # ... and is Start pressed?
select next tmp FILLING next  # then go to Filling

That is the core idea: every tick reads the state and decides on the transitions. What the pump does then follows from the state alone:

seq pumpOn state FILLING      # on only in state Filling
sbn PUMP HASH("Fuellpumpe") On pumpOn
Warning

Work out all transitions from the old state and take over the new one only at the end: move next state at the start, move state next at the end. If you change state directly, the next line already checks the new value. If the line for Ready → Filling sits below the one for Full → Ready, a held button turns Full straight back into Filling within a single tick.

Showing the state

An LED display with the state number is the cheapest debugging there is. If it shows 1 and the pump is not running, the fault is the pump or its name – not your logic.

sbn DISPLAY HASH("Status") Setting state
Tip

If the analyzer fails, the pressure is NaN. Then the machine never leaves Filling and the pump runs forever. Send it back to Ready in that case – snanz ok pressure gives you a clean 0 or 1 for it.

Exercise

Task

Build the tank filler from the text. Devices on the data network:

  • Button “Start” (Activate)
  • Pipe analyzer “Tank” (Pressure)
  • Volume pump “Fuellpumpe” (On)
  • LED display “Status” (Setting = state number)

Transitions: Ready → Filling on a button press, Filling → Full from 3000 kPa, Full → Ready below 1000 kPa. Without the analyzer back to Ready. The pump only runs in state Filling.

Inputs and display are done. The transitions and the pump are missing.

Hints

Hint 1

Every transition: seq tmp state FROM, condition into cond, and tmp tmp cond, then select next tmp TO next.

Hint 2

For Ready → Filling pressed already is the condition: and tmp tmp pressed.

Hint 3

Without the analyzer: select next ok next READY as the last transition line.

Hint 4

Pump: seq pumpOn state FILLING and write it to “Fuellpumpe” with sbn.

Quiz

4 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