extendedSolutions
Member
Could experienced PLC programmer take a look at these lines of code? It is for controlling a stepper servo motor, as you will see some familiar motor commands. This code is intended for a Panasonic FP-x plc, so there are some FP specific elements, such as R904A=1 flag being COM2 ready, F7_MV2 for moving two words into a dword, and the mess needed to call MODBUS send (F145_SEND) and receive (F146_RECV).
Mainly, I am wondering if the structure I am using to control sequential MODBUS communications are as efficient (in writing style, as well as execution) as they can be. For example, the DF(X1) actions would ideally be:
if(DF(X1)) then
setSpeedAcceleration(10,10,200);
setFeedDistance(1000);
motorFeedToDistance();
while (readMotorCompleteStatus = false);
end_if
and the DF(X2) would be:
if(DF(X2)) then
setDirection(-1);
setHome();
motorSeekHome();
while (readMotorCompleteStatus = false);
zeroEncoderCount();
zeroPosition();
end_if
But it seems I need to separate each MODBUS communicating step into individual ladder rungs. Please let me know if there are smarter ways to do the same thing shown here.
Mainly, I am wondering if the structure I am using to control sequential MODBUS communications are as efficient (in writing style, as well as execution) as they can be. For example, the DF(X1) actions would ideally be:
if(DF(X1)) then
setSpeedAcceleration(10,10,200);
setFeedDistance(1000);
motorFeedToDistance();
while (readMotorCompleteStatus = false);
end_if
and the DF(X2) would be:
if(DF(X2)) then
setDirection(-1);
setHome();
motorSeekHome();
while (readMotorCompleteStatus = false);
zeroEncoderCount();
zeroPosition();
end_if
But it seems I need to separate each MODBUS communicating step into individual ladder rungs. Please let me know if there are smarter ways to do the same thing shown here.
if (DF(X1)) then
(* set speed and accel, do a feed cmd, and read status for complete *)
seq_feed := TRUE;
tms_readStatusDone :=FALSE;
tms_feedDone :=FALSE;
tms_setSpeed:=TRUE;
tms_setSpeedDone :=FALSE;
end_if;
if (DF(tms_setSpeedDone)) then
tms_feed:=TRUE;
tms_feedDone :=FALSE;
end_if;
if (DF(tms_feedDone)) then
tms_readStatus:=TRUE;
tms_readStatusDone :=FALSE;
seq_feed := FALSE;
end_if;
IF (tms_setSpeed) THEN
DT110:=20; //acceleration of 20
DT111:=20; //deacceleration of 20
DT112:=100; //speed of 100
DT113:=0; //higher byte of move distance
DT114:=distance; //lower byte of move distance
F7_MV2(s1 := 16#5, s2 := 16#2001, d => controlword);
IF (R904A) THEN
F145_SEND(s1_Control := controlword,
s2_Start := DT110,
d_AdrType := DT0,
d_AdrOffs := 27); //address to put all 5 words
tms_setSpeed := FALSE;
tms_setSpeedDone :=TRUE;
end_IF;
end_IF;
IF (tms_feed) THEN
DT110:=16#66; //cmd 66 is feed to distance
F7_MV2(s1 := 16#1, s2 := 16#2001, d => controlword);
IF (R904A) THEN
F145_SEND(s1_Control := controlword,
s2_Start := DT110,
d_AdrType := DT0,
d_AdrOffs := 124);
tms_feed := FALSE;
tms_feedDone := TRUE;
end_IF;
end_IF;
IF (tms_readStatus) THEN
F7_MV2(s1 := 16#1, s2 := 16#2001, d => controlword);
IF (R904A) THEN
F146_RECV(s1_Control := controlword,
s2_AdrType := DT0,
s2_AdrOffs := 1, (* 40002 for SC status code *)
d_Start := datafromtsm);
(* tms_readStatus := FALSE; *)
if (datafromtsm = 16#9) then //9 means motor move done
(* motor is in position and enabled *)
tms_readStatus := FALSE;
tms_readStatusDone := TRUE;
end_if;
end_IF;
end_IF;
if (DF(X2)) then
(* call Seek Home *)
seq_seekHome:=TRUE;
tms_setDir := TRUE;
tms_setDirDone:=FALSE;
end_if;
if (tms_setDirDone) then
tms_setDirDone:=FALSE;
tms_setHome:=TRUE;
tms_setHomeDone :=FALSE;
end_if;
if (tms_setHomeDone) then
tms_setHomeDone:=FALSE;
tms_seekHome:=TRUE;
tms_seekHomeDone :=FALSE;
end_if;
if (tms_setHomeDone) then
tms_setHomeDone:=FALSE;
tms_seekHome:=TRUE;
tms_seekHomeDone :=FALSE;
end_if;
if (tms_seekHomeDone) then
tms_seekHomeDone :=FALSE;
tms_readStatus:=TRUE;
tms_readStatusDone :=FALSE;
end_if;
if (tms_readStatusDone AND seq_seekHome) then
tms_readStatusDone :=FALSE;
tms_zero126:=TRUE;
tms_zero126Done:=FALSE;
end_if;
if (tms_zero126Done) then
tms_zero126Done:=FALSE;
tms_resetEncoder:=TRUE;
tms_resetEncoderDone :=FALSE;
end_if;
if (tms_resetEncoderDone) then
tms_resetEncoderDone:=FALSE;
tms_resetPosition:=TRUE;
tms_resetPositionDone :=FALSE;
seq_seekHome :=FALSE;
end_if;
IF (tms_setDir) THEN
DT110:=16#FF;
DT111:=16#FF;
F7_MV2(s1 := 16#2, s2 := 16#2001, d => controlword);
IF (R904A) THEN
F145_SEND(s1_Control := controlword,
s2_Start := DT110,
d_AdrType := DT0,
d_AdrOffs := 30);
tms_setDir := FALSE;
tms_setDirDone :=TRUE;
end_IF;
end_IF;
IF (tms_setHome) THEN
DT110:=16#35;
DT111:=16#4C;
F7_MV2(s1 := 16#2, s2 := 16#2001, d => controlword);
IF (R904A) THEN
F145_SEND(s1_Control := controlword,
s2_Start := DT110,
d_AdrType := DT0,
d_AdrOffs := 125);
tms_setHome := FALSE;
tms_setHomeDone :=TRUE;
end_IF;
end_IF;
IF (tms_seekHome) THEN
DT110:=16#6E;
F7_MV2(s1 := 16#1, s2 := 16#2001, d => controlword);
IF (R904A) THEN
F145_SEND(s1_Control := controlword,
s2_Start := DT110,
d_AdrType := DT0,
d_AdrOffs := 124);
tms_seekHome := FALSE;
tms_seekHomeDone := TRUE;
end_IF;
end_IF;
IF (tms_zero126) THEN
DT110:=16#0;
F7_MV2(s1 := 16#1, s2 := 16#2001, d => controlword);
IF (R904A) THEN
F145_SEND(s1_Control := controlword,
s2_Start := DT110,
d_AdrType := DT0,
d_AdrOffs := 125);
tms_zero126 := FALSE;
tms_zero126Done := TRUE;
end_IF;
end_IF;
IF (tms_resetEncoder) THEN
DT110:=16#98;
F7_MV2(s1 := 16#1, s2 := 16#2001, d => controlword);
IF (R904A) THEN
F145_SEND(s1_Control := controlword,
s2_Start := DT110,
d_AdrType := DT0,
d_AdrOffs := 124);
tms_resetEncoder := FALSE;
tms_resetEncoderDone := TRUE;
end_IF;
end_IF;
IF (tms_resetPosition) THEN
DT110:=16#A5;
F7_MV2(s1 := 16#1, s2 := 16#2001, d => controlword);
IF (R904A) THEN
F145_SEND(s1_Control := controlword,
s2_Start := DT110,
d_AdrType := DT0,
d_AdrOffs := 124);
tms_resetPosition := FALSE;
tms_resetPositionDone := TRUE;
end_IF;
end_IF;