Reputation: 57
I need to run some code every time the PLC starts. This code should only be run once and then never again until the PLC is restarted. I initialize some global variables and validate the persistent data before allowing the main PLC to run. This is because the actions of the machine can be damaging if some of these variables are not setup correctly.
Is there a way to start/stop the other PLC tasks? I noticed TwinCAT doesn't support initialization and shutdown interrupts for PLC tasks.
Upvotes: 0
Views: 3492
Reputation: 431
I don't know of a way to start/stop individual PLC tasks. You can start/stop a runtime though.
But perhaps it can be as simple as this code below, which will only run when your PLC starts.
VAR initialized: BOOL := FALSE;
IF NOT initialized THEN
(* Run your initialization code here *)
initialized := TRUE;
END_IF
(* Rest of your program here *)
Edit:
I used a state machine inside the initialization code to help with the task allowed time issue.
Example:
VAR
Initialized : BOOL := FALSE;
Init_State : UINT := 0;
END_VAR
IF NOT Initialized THEN
(* Initialization State Machine *)
CASE Init_State OF
0: (* First step in initialization *)
Init_State := Init_State + 1;
1: (* Second step in initialization *)
Init_State := Init_State + 1;
.
.
.
n: (* Last step in initialization *)
Initialized := TRUE;
END_CASE
END_IF
Upvotes: 0
Reputation: 533
TwinCAT has a 'PlcTaskSystemInfo' struct containing a boolean for FirstCycle. You can use that to run the initializing code only once.
VAR fbGetCurTaskIdx: GETCURTASKINDEX; (* Further example+explanation in Infosys *)
fbGetCurTaskIdx();
IF _TaskInfo[fbGetCurTaskIdx.index].FirstCycle THEN
(* Initialization code here *)
ELSE
(* Normal code here *)
END_IF;
Upvotes: 2