PLC Recomendations

ST no good for PLC? We use it almost exclusively (mainly because it is is the best implemented language/dev-environment in Beckhoff/Codesys. I could see using ladder and SFC a LOT more if each had a better dev environment in Codesys.)

What's wrong with this...

PROGRAM XXX_SEQ_TEMPLATE_XXX
VAR_INPUT
Enable :BOOL;
END_VAR

VAR_OUTPUT
Done :BOOL;
Busy :BOOL;
Error :BOOL;
ErrorID :STRING[80];
END_VAR

VAR CONSTANT
(*TODO: Replace this with the name of this POU*)
myName :STRING[32]:='SEQ_TEMPLATE';
END_VAR

VAR
myState :INT;
myLastState :INT;
isDone :BOOL;
ErrorState :INT;
StateTransition :R_TRIG;
Trigger :R_TRIG;
myTimer :TON;
(*Additional local variables here*)
END_VAR




CASE myState OF
0: (*initial state*)
Busy := FALSE;
Done := isDone;
Trigger(clk := Enable);
IF Trigger.Q THEN (* On rising enable line, init sequence... *)
Busy := TRUE;
Done := FALSE;
isDone := FALSE;
Error := FALSE;
ErrorID := ERR_NONE;
ErrorState := 0;
myTimer(in:= FALSE);
(*Add initialization for other variables and sequences here*)
myState := 10;
END_IF;

10: (* Step 1 of sequence *)
(* Wait for initial input state... *)
IF input1
AND input2
AND NOT input3
AND NOT input4
AND NOT (somethingElse OR thatEither)
THEN
myState := 20;
ELSIF WDErr THEN
(* some watchdog process timeout or something... *)
ErrorID := ERR_WDTIMEOUT;
END_IF;

20: (* Step 2 of sequence *)
(* Activate the thingamajig... *)
output1 := TRUE;
StartWDTimer2 := TRUE;
myState := 30;

30: (* Step 3 of sequence *)
(* Wait for feedback of activation of previous thingamajigger... *)
If input 6 AND NOT input7 THEN
myState := 99;
ELSIF WDErr2 THEN
(* another watchdog timer for the activation of the thingamajigger... *)
ErrorID := ERR_WDTIMEOUT2;
END_IF;

99: (* Done *)
isDone := TRUE;
myState := 0;


ELSE
Error := TRUE;
ErrorID := ERR_INVALID_STATE;
LOG_MSG_INT_INT('INVALID STATE TRANSITION IN %D, %D->%D ', myName, myLastState, myState);

END_CASE;



(*Watch for errors to occur*)
IF myState > 0 THEN
IF ErrorID <> '' THEN
Error:= TRUE;
END_IF;
IF Error THEN
LOG_ERROR_STATE(MYNAME, MYSTATE);
ERR_ADD_TRACEBACK(myName, myState);
ErrorState := myState;
myState := 0;
END_IF;
END_IF

(*Watch for state transitions*)
IF myState <> myLastState THEN
LOG_TRANSITION(myName, myLastState, myState);
END_IF;
myLastState := myState;


Each case statement should be coded like a rung of a ladder, but 'could' have some math or functions or whatever in them, as long as the function isn't blocking or processor-greedy. If another FB of indeterminate duration needs to be kicked off from one of the above case 'states', then the enable input of that FB is merely set to high from that case statement. Any given case then executes quickly, like a rung of a ladder, evaluating to some resolution (or none, meaning on the next scan of the PLC, that same case is evaluated again (and again) to see if a real-world sensor-state has come true, or a user watchdog process has come true instead).
When done without error, this program (or FB) returns to state 0 with it's Done flag set and Busy flag off. State 0 continues executing indefinitely until the enable input is set low and then high again, which resets the Done flag, reinits, and restarts the sequence.

The above is my boilerplate template for a ST state machine.

Jay
 
Beckhoff has a large range of controllers that use their Twincat IEC 6-1131 software, allow you to use their function libraries or write your own, and have communications libraries/ hardware to communicate with just about anything.

They use modular "slice IO" (their IO blocks used to be produced by WAGO, and are the same cage clamp style). They have hardware and communications libraries for everything from RS232, Modbus, Industrial TCP/IP, profibus, any of the CAN busses (device net too), fiber options, and their own Ethercat for high speed control/motion apps. Twincat will program everything from their small units similar in price to micro PLC's (but include rs232, rs485,CAN ,Ethernet) to full blown multi-core industrial computers integrated with touch panels.

Their ladder editor is a little clunky (the siemens S7-200 micro-win is my favorite ladder editor! I believe codesys writes Beckhoff's) but with the rest of the IEC Languages I use ladder much less, only where it makes sense. If ladder is limiting your applications, you need to learn structured text.

As for the comments on Structured text for only limited applications, once you get used to it you will probably find yourself writing mostly in Structured Text- especially if you have experience with C etc. I use lots of structured text, with ladder and Sequential function chart where they make monitoring/ troubleshooting overall operations easier. The Beckhoff crowd also swears by Continuous Function Chart for a good over all picture of a process, but I have not gotten the hang of that language YET.

I have used Beckhoff devices for connectivity with SQL through their ADS comm. library functions-we have received great support with any question about libraries etc- there are so many their documentation can be a little much, so asking their support guys to point you in the right direction is recommended!
 
C
The only problem I have with ST is that no one but a programmer will even attempt to troubleshoot it before calling the author. Usually at 3 AM. It's not a big language, but you do have to know it before that gibberish makes sense.

Regards,
cww
 
In reply to curt wuollet: I would guess that the majority of people who understand PLCs have never heard of ST, and that more than 99% of those who have heard of it have never seen it (and never will). Anything written in ST may as well be in hieroglyphics as far as most of them are concerned.

While we are talking about difficult to read programs, the one here is my favourite:

http://99-bottles-of-beer.net/language-perl-737.html

That's a real program, if anyone wants to try running it. I'm sure the programmer had a lot of fun writing it. I would hate to be the one who had to troubleshoot it though.
 
C
Reminds me of FORTH!

There is also an obfuscated C program contest that produces some impenetrable stuff.

Regards,
cww
 
I was previously a field applications engineer and PLC programmer for Schneider Electric (Square D/Modicon). One of the biggest surprises I got there when I started on was that they use a TON of structured text. In fact, the Unity Pro Structured Text editor is pretty good with "intellitype" style entry.

The biggest reason they were doing so was for complex array management.. mostly data collection and manipulation (which supports the above post which talked about ST being useful for math).

I guess it really depends on what angle (brand name and training pipeline) you come at the industry from.

I personally use ST as sparingly as possible, especially these days with the AB's and Opto 22's I work with.

Just thought it was interesting to see people who, even today, break out the ole ST editor for most of the program.
 
If you use the 'ole' ST sparingly and default to Ladder then good luck staying employed in five years.

Robert Trask, PE
 
If you are suggesting that people who default to ladder logic aren't "moving forward with the times" then I see a point there (albeit quite a generalization), but I hardly think ST has anything to do with that. ST does not have the power or functionality of a modern programming language.
 
I agree. A lot of examples I have seen of structured text have been text representations of ladder logic. Structured text is limited because it is implemented on scanning PLCs.

KEJR
 
In the majority of systems I have been involved with we 'default to ladder logic' at the request of the client. The maintainers of the systems are, by and large, plant maintenance personnel who are very capable with ladder logic. I believe that the maintainers can and do understand ST (or with a wee bit of training they will, being the simple language that it is), but ladder meets their needs. They have a lot of experience working with ladder, so why change it.
 
Take a look at the Schneider Twido Extreme PLC. It is a 12vdc unit and the twidosoft programming environment will allow you to write a program and switch between view in Ladder and Text. It's environmentally sealed (good stuff) but low voltage DC in and out (which may be okay, depending on what devices you control).
 
Top