What you learn today
- Waiting time per state
- Detect timeouts
- Reset the counter on transition
After a gas accident in the lab, a button is to suck the room empty. The vent sucks until the pressure is below 5 kPa and then keeps running for another 5 seconds so the rest ends up in the pipe as well.
Your machine does not answer two questions yet: How long has the run-on been going? And what if the pressure never drops below 5 kPa – because a door is open or the tank behind the vent is full? Then the vent sucks forever, and nobody notices.
Counting ticks
A tick lasts 0.5 s. A register ticks counts up by one every tick:
define RUNTIME 10 # ticks = 5 s run-on
define MAXTIME 40 # ticks = 20 s, then fault
alias ticks r2
add ticks ticks 1 # one more tick in this state
Write the times as ticks into the define and the seconds into the comment. Whoever changes the value later sees straight away what is meant.
Waiting time per state
The run-on simply waits until the counter is big enough:
s_runon:
sge tmp ticks RUNTIME
select next tmp READY next
j output
Detecting a timeout
Pump-down has two exits: success (pressure below 5 kPa) and timeout. Check the time first and then the success. The last select overwrites next, so success wins within the same tick:
s_pumpdown:
sgt tmp ticks MAXTIME
select next tmp FAULT next
slt tmp pressure EMPTY
select next tmp RUNON next
j output
Resetting the counter on a transition
Every new state has to start at zero. The safest place for that is one spot, namely where the new state is taken over:
output:
seq tmp next state # state unchanged?
select ticks tmp ticks 0 # otherwise counter to 0
move state next
That way no transition can forget the reset, not even one you add next month.
Without the reset the next state inherits the old counter value. If pump-down took 30 ticks, the run-on with its 10 ticks is over in its very first tick. And if the plant waited a minute in Ready, pump-down reports a timeout straight away although it has only just begun.
sleep 5 would be the wrong choice for the run-on. During sleep the chip reads nothing: if the sensor fails or someone presses the button in that time, it only notices afterwards. The counter waits without stopping the main loop.
Diagram
Exercise
Task
The lab purge has four states: Ready (0), Pump-down (1), Run-on (2), Fault (3). Devices: button “Spuelen”, gas sensor “Labor” (Pressure), active vent “Labor” (On, Mode), LED display “Status”.
Jump table, success during pump-down and output are done. Time is missing:
- The counter
tickscounts up by one every tick and is back at 0 after every state change. - Pump-down takes longer than 40 ticks (20 s) → Fault.
- The run-on ends after 10 ticks (5 s) → Ready.
Hints
Hint 1
Right after move next state: add ticks ticks 1.
Hint 2
In the pump-down block before the success check: sgt tmp ticks MAXTIME and select next tmp FAULT next.
Hint 3
Run-on: sge tmp ticks RUNTIME and select next tmp READY next.
Hint 4
In the output before move state next: seq tmp next state and select ticks tmp ticks 0.
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