Beckhoff First Scan Bit [better] -
Most TwinCAT developers create a global boolean variable and set it to TRUE by default. At the very end of their main program, they set it to FALSE . VAR_GLOBAL bFirstScan : BOOL := TRUE; END_VAR Use code with caution. Main Logic (MAIN PRG):
Here is the standard way to achieve this in TwinCAT 3 (IEC 61131-3).
⚠️ If you use Method 1 in multiple tasks (e.g., a fast cyclic task and a slower periodic task), it will evaluate properly for each task's respective first execution loop.
: Clearing OTL (Latches) or variables that must start in a known state.
// This runs once when the FB is created (first scan of the FB) METHOD FB_Init : BOOL VAR_INPUT bInitRetains : BOOL; // TRUE if retain variables are restored bInCopyCode : BOOL; // TRUE if FB is copied END_VAR fSpeed := 0.0; // Initialize internal variable bReady := FALSE; END_METHOD
Many developers prefer a manual method for simplicity or for use in older versions of TwinCAT where system structures might differ. This relies on the initialization value of a boolean variable.
: If you prefer not to use the system global, you can create a local "Init" flag: