One problem I face quite often is that I need to have some code executed only once in the first cycle of the PLC, and then never again. Up until now I’ve always used a boolean of some sort; “bFirstCycleExecuted”, instantiated to false and then set to true after the first cycle resulting in something like:
PROGRAM MAIN
VAR
bFirstCycleExecuted : BOOL := FALSE;
END_VAR
IF NOT bFirstCycleExecuted THEN
// The code that you want to be executed at the first cycle
bFirstCycleExecuted := TRUE;
END_IF
There actually is a built-in way in TwinCAT to know whether the current cycle is the first one or not, using the global data type PlcTaskSystemInfo. Among other parameters in this data type is the boolean FirstCycle. In your program you have access to an array of the data type PlcTaskSystemInfo, accessing it by _TaskInfo[index_of_current_task]. The index of current task can be retrieved by using the function block GETCURTASKINDEX. Replacing the above piece of code with our new knowledge results in:
PROGRAM MAIN
VAR
fbGetCurTaskIndex : GETCURTASKINDEX;
END_VAR
fbGetCurTaskIndex();
IF _TaskInfo[fbGetCurTaskIndex.index].FirstCycle THEN
// The code that you want to be executed at the first cycle
END_IF
lopment (TDD) doesn’t seem to be all too common among TwinCAT-developers, which is a shame. From my experience, TDD has a strong foothold everywhere among developers, but TwinCAT? Not so much. And I don’t blame them. There are TDD frameworks for C++, C#, Ada, Python and basically any other language and/or development environment. Do a Google search on the web on any programming language/IDE and TDD and you get thousands of results. Do the same for TwinCAT and you’re on your own.
Together with TwinCAT release 4022.0 Beckhoff released their product “
