Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in
  • E escet
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Graph
    • Compare revisions
    • Locked files
  • Issues 94
    • Issues 94
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 9
    • Merge requests 9
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test cases
  • Deployments
    • Deployments
    • Releases
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Eclipse ProjectsEclipse Projects
  • Eclipse ESCET (Supervisory Control Engineering Toolkit)
  • escet
  • Issues
  • #281
Closed
Open
Issue created Jan 01, 2022 by Ferdie Reijnen@freijnenDeveloper

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.

  1. The generated main POU has (always exactly two) output variables (timerValue0 and timerValue1). 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 say Add 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;
  1. curTimer is a global variable of resource Timers, but should be a variable of the main 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 as curTimerValue, part of main.

  2. 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, fvar_plus3_sum should be a temporary variable. (This is OK. They are temporary variables, TwinCAT doesn't make it explicitly by having VAR_TEMP, whereas Siemens does that.

  3. 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. (Keeping it, as while not used now, it may be useful in the future.)

  4. No need to increment cnt forever, set it to the max value and decrement to 0. Can then use smaller integer type for less memory. Calculations may also be slightly simpler, for a small performance improvement.

  5. curTimer can be a boolean, for less memory usage. Would not to be explicit in comments about what true and false then mean.

  6. 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.

Edited Aug 28, 2022 by Dennis Hendriks
Assignee
Assign to
Time tracking

Copyright © Eclipse Foundation, Inc. All Rights Reserved.     Privacy Policy | Terms of Use | Copyright Agent