Improve generated PLC code
While working on #142 (closed) I noticed the following things that could be improved. This would also simplify the conversion to Siemens S7.
- The generated
main
POU has (always exactly two) output variables (timerValue0
andtimerValue1
). These are special variables that other POU's can access. It is not clear why the code has output variables. The comments in the code sayAdd output variables. These are required for the 'TON' timers.
. These are indeed used for the TON timers but that doesn't justify being output variables. The documentation also doesn't explain why we have these. Does anyone know why we have these? I think the code can be simplified as shown below.
// Handle 'time' and cycle time.
cnt := cnt + 1;
timer0(IN := curTimer = 0, PT := T#1D);
timer1(IN := curTimer = 1, PT := T#1D);
- timerValue0 := timer0.ET;
- timerValue1 := timer1.ET;
lastTimerValue := curTimerValue;
IF curTimer = 0 THEN
- curTimerValue := timerValue0;
+ curTimerValue := timer0.ET;
ELSE
- curTimerValue := timerValue1;
+ curTimerValue := timer1.ET;
END_IF;
curDeltaTime := curTimerValue - lastTimerValue;
curDeltaSecs := TIME_TO_LREAL(curDeltaTime) / 1000;
curTime := curTime + curDeltaSecs;
IF cnt MOD 10 = 0 THEN
curTimer := 1 - curTimer;
curTimerValue := T#0S;
timer0(IN := curTimer = 0, PT := T#1D);
timer1(IN := curTimer = 1, PT := T#1D);
- timerValue0 := timer0.ET;
- timerValue1 := timer1.ET;
END_IF;
-
curTimer
is a global variable of resourceTimers
, but should be a variable of themain
program. This variable is used to indicate which timer is active, part of 'Handle 'time' and cycle time'. Now it seems like some kind of special variable, I don't see why that is necessary. It fits with variables such ascurTimerValue
, part ofmain
. -
Functions use non-temporary variables. This means that memory is reserved to 'save' the value of the variables in the function between cycles. That is not necessary. Instead, all 'local' function variables should be temporary variables. See below how functions are now transformed. Instead,They are temporary variables, TwinCAT doesn't make it explicitly by havingfvar_plus3_sum
should be a temporary variable.VAR_TEMP
, whereas Siemens does that.
4 PlcConfiguration
java class has List<PlcGlobalVarList> globalVarLists
, but that is never used. Instead, we always use the globalVarLists
that is part of the PlcResource
(which itself is part of PlcConfiguration
). The globalVarLists
code in PlcConfiguration
can be removed.