PLCgen: Implement timers with continuous variables
We restrict continuous variables to timers, as that is the primary use of CIF continuous variables in controllers. Setting a value for the delay should be possible, as well as detecting that the delay has reached its end. A secondary use is to check how much time is still remaining (which seems more relevant than how long ago the timer has been started).
In the PLC, TON
timers are used. One TON
block for each continuous CIF variable. A TON
timer can be started by calling it with a preset time value (in seconds). There is second way to call the TON
block to just update output for
passed time since the last call. After each call the TON
block outputs its current value. That value starts at 0
when setting a value, and increases with time until it reaches the preset value after 'preset value' seconds.
The output does not increase further after reaching the preset value even if more time passes.
To query remaining time of a running timer in CIF, it's simplest for the users if they can state such a query as val <= N
to query if remaining time is equal or less than N
seconds. Testing for timeout then becomes val <= 0
.
This implies CIF continuous variables must have derivative -1
and assigned values to continuous variables must be non-negative.
As the PLC output value goes up, in the implementation the remaining time in the PLC must be computed by subtracting the output value from the last preset value of the TON
block. Since the output value stops increasing when the timeout has been reached, a comparison in CIF again purely negative values will fail in the implementation. To ensure the CIF notion of continuous variables is maintained in the PLC implementation, only comparisons of continuous variables against ranges with at least one non-negative value is allowed.
Translation of continuous variables is thus as follows. For each continuous variable there is a preset variable P
, a variable for the remaining time R
, and a BLK
timer (a TON
block instance).
-
At the start of a transition, first
R
is updated by callingBLK(PT := P, IN := TRUE, Q => B, ET => V);
and computingR := SEL(B, P - V, 0.0);
with temporary variablesV
andB
. Note that this decouples valueR
from what happens withBLK
until the next transition. -
Assignment of a continuous variable changes
P
and callsBLK(PT := P, IN := FALSE);
. This resets the timer. Next, the timer must be restarted withBLK(PT := P, IN := TRUE);
.Although assigning a new value obviously changes the continuous variable in CIF in the new state,
R
lives in the state before the transition and doesn't need to change. It will change with the next transition through the first item. -
Comparing the continuous variable against a value non-negative
V
using comparison operatorc
generates the infix expressionR c V
.
Addresses #397 (closed)