FUNCTION_BLOCK state_manager
VAR_INPUT
thread : INT := 1;
power : BOOL := FALSE;
stop : BOOL := FALSE;
poweredOn : BOOL;
END_VAR
VAR_OUTPUT
motorStartup : BOOL;
motorShutoff : BOOL;
idle : BOOL;
freeMove : BOOL;
continiousTarget : BOOL;
home : BOOL;
END_VAR
VAR
state : INT := 1;
END_VARstate := thread;
IF power THEN (* Power up *)
motorStartup := TRUE;
motorShutoff := FALSE;
idle := FALSE;
freeMove := FALSE;
continiousTarget := FALSE;
home := FALSE;
END_IF;
IF NOT stop THEN
IF poweredOn AND state = 1 THEN (* idle *)
motorShutoff := FALSE;
idle := TRUE;
freeMove := FALSE;
continiousTarget := FALSE;
home := FALSE;
ELSIF state = 2 AND poweredOn THEN (* Free Move *)
motorShutoff := FALSE;
idle := FALSE;
freeMove := TRUE;
continiousTarget := FALSE;
home := FALSE;
ELSIF state = 3 AND poweredOn THEN (* Move to target *)
motorShutoff := FALSE;
idle := FALSE;
freeMove := FALSE;
continiousTarget := TRUE;
home := FALSE;
ELSIF state = 4 AND poweredOn THEN (* Homing *)
motorShutoff := FALSE;
idle := FALSE;
freeMove := FALSE;
continiousTarget := FALSE;
home := TRUE;
END_IF
END_IF
IF stop THEN
motorShutoff := TRUE;
idle := TRUE;
freeMove := FALSE;
continiousTarget := FALSE;
home := FALSE;
state := 1;
END_IF