C++ version of PLC ladder logic

I thought this would be easy but so far my little program isn't working.

I normally program in PLC languages. Ladder logic, Structured Text, and Function Block. Ladder isn't rocket science but porting it to C apparently is 8)

The following picture shows what I'm trying to accomplish

SD = Shutdown
PB = Pushbutton
Out = Output
Per = Permissive

Rung 1 is a permissive string. PB means Pushbutton. If there are no shutdowns (these also latch and require a reset pushbutton but that's not important to this question) and PB2 hasn't been pushed the output is true

Rung 2 is a latch. If the permissive is true and you push PB1 it latches the output. Push PB2 or have a shutdown go true and it de-latches the output. Normally there is a One Shot Bit after PB1 but I forgot to add it before I took the screenshot.

Somewhere underneath all of these | | symbols lies compiled C or some variant so surely this can be done?

Well... the solution was easy... Post the question here and magically all of the answers came to light.

I had an && where I needed an ||

Thanks anyway

Just out of interest would you care to tell us more on the subject? By the way screen prints are often impossible to read try and avoid using them.

Mark

holmes4:
Just out of interest would you care to tell us more on the subject? By the way screen prints are often impossible to read try and avoid using them.

Mark

I couldn't think of an easy way to show ladder logic here.

The first rung of the ladder is a permissive rung. The Stop button and any "shutdown" bit would typically be in this rung. The Stop button would be wired normally closed so that a wire break would open the circuit and drop the output. The "shutdown bits are inverted... Ie true = shutdown. Off = running. I latch them if the input trips in another routine that way a "blip" is latched and we know what caused it. The point being- If all is well then PERMISSIVE is true. If all is NOT WELL then PERMISSIVE is FALSE

The second rung is logically: IF (Start Button OR RUN) AND PERMISSIVE is TRUE THEN RUN = TRUE. RUN latches RUN on through the PERMISSIVE. If the PERMISSIVE drops out then RUN delatches and drops out.

What I did in my Sketch was NOT elegant. But it worked. I used buttons to simulate Emergency Stop, Turn on, Turn off. LED's to indicate button, permissive, and run status. Since it's the first Sketch that I've ever written I'd say it's not bad. Took me 1/2 the day though. Things like syntax and variable name consistency (LED is not the same as LEDStatus for instance) really kicked my butt.

look for openplc
not plcopen that is for proffesional
and 4 hours to get this result is very good.

Those two PLC rungs would probably port to two lines of C. They equate to simple logical expressions. Study the
Boolean Operators in the reference section to get a feel for them. As you already have good knowledge of
logic operators as acquired learning PLC ladder logic, you should have no problem converting them to
C/C++ statements.

retrolefty:
Those two PLC rungs would probably port to two lines of C. They equate to simple logical expressions. Study the
Boolean Operators in the reference section to get a feel for them. As you already have good knowledge of
logic operators as acquired learning PLC ladder logic, you should have no problem converting them to
C/C++ statements.
Arduino - Home

The first thing that I thought when I looked at my finished code is "this can be done in a much better way. This is brute force. It can be made more elegant". I agree with your first sentence.

It does begin to look like your ladder logic assumes (needs/ is dependant on) a lot of under lying "OS type" code which has very great overheads.

Mark

holmes4:
It does begin to look like your ladder logic assumes (needs/ is dependent on) a lot of under lying "OS type" code which has very great overheads.

Mark

I've never looked at the processor to see what it is. I may do that just for fun. The rungs of ladder logic that I posted are just a graphical display of boolean statements. I could've done the same thing with a structured text routine (If/Then statements).