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

Day 295 of 365 · Expert · Month 10: Bits and data

Lookup tables

Precompute values instead of calculating every tick: a curve as a table in the stack, index from the reading with offset, step, floor and clamp.

Lesson 18 min Week 43

What you learn today

  • Precompute values
  • Table in the stack
  • Index from a reading

The workshop's exhaust should run harder as the temperature rises – but not linearly. Up to 22 °C hardly anything, from 30 °C strongly, from 34 °C fully. A formula for that would be long and hard to change. A table is short and everybody sees at once what happens.

Precomputing values

A lookup table holds finished results. Instead of calculating, the chip looks them up. That pays off in three cases:

  • The curve has no simple formula (kinks, steps, empirical values).
  • The formula is expensive, for example with pow, exp or a loop.
  • Somebody should adjust the values later without understanding the calculation.

Table in the stack

You set the table up once before start: with poke. Every slot is a step:

IndexTemperaturePump
020–21 °C0
122–23 °C5
224–25 °C10
326–27 °C20
428–29 °C30
530–31 °C50
632–33 °C70
7from 34 °C100

Index from the reading

Four steps turn a temperature into a table slot: subtract the offset, divide by the step width, round down, limit.

sub idx temp CELSIUSOFFSET
sub idx idx T_START          # offset
div idx idx STEP          # step width
floor idx idx
clamp idx idx 0 LAST      # never outside the table
get value db idx

27 °C gives (27 − 20) / 2 = 3.5, rounded down 3 – the pump runs at 20. Below 20 °C the index becomes negative, clamp turns it into 0. Above 34 °C it stays at 7.

Warning

An index outside the table reads foreign data or stops the chip. Without clamp, 50 °C gives index 15 – your ring buffer might be sitting there. clamp is mandatory for every table, and so is a valid index on NaN.

Tip

A table costs one line per entry. With 8 entries that is cheap, with 50 no longer. Tomorrow you will see how interpolation gets by with a few support points.

Diagram

The stack with its pointer slot 0 10 slot 1 20 slot 2 30 slot 3 slot 4 sp = 3 next free slot push 40 pop r0 r0 = 30 last in, first out
The temperature becomes the index, the index points at a table slot in the stack.

Exercise

Task

The table is already in the stack. Read the temperature of the sensor “Werkstatt”, compute the index (start 20 °C, 2 °C per slot, 0 to 7) and write the table value as Setting to the pump “Abluft”. Without the sensor the pump gets 0.

Hints

Hint 1

Index: Celsius, minus T_START, divided by STEP, floor, clamp idx idx 0 LAST.

Hint 2

get value db idx fetches the table value.

Hint 3

On NaN set a valid index first, at the end set the value to 0 with select.

Quiz

3 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