What you learn today
- Store values per minute
- Daily trend
- Detect peaks
Last night the battery ran empty. When did consumption pick up – and how high was the peak? A display only shows the moment. For the history the chip needs a memory, and that is the stack.
Once per minute
A tick lasts 0.5 seconds, so a minute is 120 ticks. A counter decides when to measure:
add tick tick 1
blt tick TICKS start
move tick 0
As long as the counter is below 120, the chip jumps straight back to start and waits for the next tick with yield. Only every 120th pass gets as far as the measurement.
A ring buffer in the stack
You know the ring buffer from the reading history: poke writes to a fixed address, the position moves one on and jumps back to 0 at the end with mod.
poke pos value
add pos pos 1
mod pos pos SLOTS
With 60 slots the buffer holds exactly the last hour. The stack has 512 slots – with SLOTS 480 that would be eight hours, a whole shift. For a whole game day you can also measure only every five minutes.
Detecting peaks
The peak is the maximum over all slots. You read your own stack with get and db:
search:
get tmp db i
max peak peak tmp
add i i 1
blt i SLOTS search
This loop needs 60 · 4 = 240 instructions. One tick allows 128 – the chip simply carries on in the next tick. That is no problem here, because the search only runs once a minute and the readings do not change in the meantime.
A NaN in the buffer poisons the peak: max with NaN gives NaN, for a whole hour. If the reading is missing, the chip therefore stores 0 and shows −1 on the history display.
What you make of it
With the history you see when consumption rises: at the start of a shift, when the furnace starts up, when night falls. The peak tells you what battery and cable have to be built for – not for the average, but for the worst moment.
Diagram
Exercise
Task
Once per minute (120 ticks) the chip reads PowerRequired of the analyzer “Hauptnetz”, shows the value on “Verlauf” and stores it in the ring buffer (60 slots). “Spitze” shows the maximum over the whole buffer. If the analyzer is missing: “Verlauf” shows −1, 0 is stored.
Hints
Hint 1
Display first: select tmp ok value -1, then for the buffer select value ok value 0.
Hint 2
Storing: poke pos value, add pos pos 1, mod pos pos SLOTS.
Hint 3
Search: peak and i to 0, then a loop with get tmp db i, max, add, blt.
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