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,(This is OK. 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. -
(Keeping it, as while not used now, it may be useful in the future.)PlcConfiguration
java class hasList<PlcGlobalVarList> globalVarLists
, but that is never used. Instead, we always use theglobalVarLists
that is part of thePlcResource
(which itself is part ofPlcConfiguration
). TheglobalVarLists
code inPlcConfiguration
can be removed. -
No need to increment cnt
forever, set it to the max value and decrement to0
. Can then use smaller integer type for less memory. Calculations may also be slightly simpler, for a small performance improvement. -
curTimer
can be a boolean, for less memory usage. Would not to be explicit in comments about whattrue
andfalse
then mean. -
Consider not generating timers at all if you don't use time
and have no continuous variables in the CIF specification, for less variables, better performance and less memory. Could then add a new option, to force generate them, if that is useful, but maybe only if that is requested by users.