Reputation: 1764
I am not 100% sure if this fits StackOverflow or I should have posted on a more automation-specific StackExchange site, so my apologies to the moderators anyway.
I am trying to program an ABB PM564 PLC with CodeSys, which controls a motor connected to its ouputs (FORWARD
is output0, REVERSE
is output1). The scenario inputs are a POWER
button (input0) which turns on/off the motor and a SENSOR
(input1) which would cause the motor to invert its rotation when activated.
Cause the instant reverse would cause a jam to the motor I have to use a delay
function (Timer On-Delay, aka TON) to pause the motor for an internal of 1sec, before changing the rotation.
Here is the block of code in Structured Text language:
FUNCTION_BLOCK SWITCH
VAR_INPUT
POWER : BOOL;
SENSOR : BOOL;
END_VAR
VAR_OUTPUT
FORWARD : BOOL;
REVERSE : BOOL;
END_VAR
VAR
switch: BOOL;
delay : TON;
END_VAR
-
delay(IN:=switch, PT:=T#1000ms);
IF POWER THEN
IF NOT(FORWARD OR REVERSE) THEN
FORWARD:=TRUE;
REVERSE:=FALSE;
END_IF;
ELSE
FORWARD:=FALSE;
REVERSE:=FALSE;
END_IF;
IF SENSOR THEN
switch:=TRUE;
END_IF;
IF switch THEN
IF FORWARD THEN
FORWARD:=FALSE;
IF delay.Q THEN
REVERSE:=TRUE;
switch:=FALSE;
END_IF;
END_IF;
IF REVERSE THEN
REVERSE:=FALSE;
IF delay.Q THEN
FORWARD:=TRUE;
switch:=FALSE;
END_IF;
END_IF;
END_IF;
The problem is that IF
block statement at line 25 of the implementation code does not seem to execute, causing the motor to pause for a sec and continue to rotate FORWARD
every time..
Upvotes: 2
Views: 2247
Reputation: 12705
It's my guess but the problem maybe here:
IF POWER THEN
IF NOT(FORWARD OR REVERSE) THEN
FORWARD:=TRUE;
REVERSE:=FALSE;
END_IF;
This code shall be executed on every scan of the PLC. So when the timer is running, then neither of FORWARD
or REVERSE
is TRUE
. Hence this if statement executes and makes FORWARD = TRUE
.
Try this:
delay(IN:=switch, PT:=T#1000ms);
IF POWER THEN
IF (NOT(FORWARD OR REVERSE) AND NOT(switch)) THEN
FORWARD:=TRUE;
REVERSE:=FALSE;
REV_DELAY:=FALSE;
FWD_DELAY:=FALSE;
END_IF;
ELSE
FORWARD:=FALSE;
REVERSE:=FALSE;
END_IF;
IF SENSOR THEN
switch:=TRUE;
END_IF;
IF switch THEN
IF FORWARD THEN
FORWARD:=FALSE;
REV_DELAY:=TRUE;
FWD_DELAY:=FALSE;
END_IF;
IF REVERSE THEN
REVERSE:=FALSE;
FWD_DELAY:=TRUE;
REV_DELAY:=FALSE;
END_IF;
END_IF;
IF (switch AND delay.Q) THEN
REVERSE:=REV_DELAY;
FORWARD:=FWD_DELAY;
REV_DELAY:=FALSE;
FWD_DELAY:=FALSE;
switch:=FALSE;
END_IF;
Upvotes: 2