Wiping the slate clean on startup so old errors don't prevent a start.
VAR fbGetTaskIndex : FB_GetCurTaskIndex; nCycleCount : UDINT; END_VAR fbGetTaskIndex(); nCycleCount := _TaskInfo[fbGetTaskIndex.index].CycleCount; IF nCycleCount = 1 THEN // This is the first scan END_IF Use code with caution.
Without a initialization bit, your PLC logic simply resumes from its last state or starts with default values that might not be appropriate for a running machine. Common use cases include: beckhoff first scan bit
Ensuring your sequences (SFC) start at "Step 0."
Note: This method is more robust because it relies on the system's own cycle counter rather than a variable you might accidentally overwrite elsewhere. Best Practices Wiping the slate clean on startup so old
TwinCAT provides internal system information via the Tc2_System library. You can check if the current cycle is the very first one by looking at the system task info.
IF bFirstScan THEN // Perform Initialization Tasks here iTargetVelocity := 1500; bMachineReady := FALSE; END_IF // All other machine logic goes here... // The very last line of the program: bFirstScan := FALSE; Use code with caution. 2. Using FB_GetCurTaskIndex (The Pro Method) Common use cases include: Ensuring your sequences (SFC)
If you use the manual variable method, ensure the line bFirstScan := FALSE; is at the very bottom of your MAIN task. If you put it in a sub-function, other parts of your program might miss the "True" state.
Use the first scan to ensure all physical outputs are in a "Safe/Off" state before the logic takes over.