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,expor 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:
| Index | Temperature | Pump |
|---|---|---|
| 0 | 20–21 °C | 0 |
| 1 | 22–23 °C | 5 |
| 2 | 24–25 °C | 10 |
| 3 | 26–27 °C | 20 |
| 4 | 28–29 °C | 30 |
| 5 | 30–31 °C | 50 |
| 6 | 32–33 °C | 70 |
| 7 | from 34 °C | 100 |
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.
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.
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
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