Java-Ladder Translator / Interpreter

T

Thread Starter

Tony Anosike

Hi there, I am a student in the UK and I'm new to PLC programming languages. I'd like to write a translator in Java to translate IF THEN statements to Ladder Diagram. I haven't got a clue where to begin. I'll be grateful if anyone out there could help in any way.

Thanks

Tony
 
J

James Ingraham

Give up. It would be a lot easier to translate ladder logic into Java, and even that wouldn't be real pleasant. Obviously, you could write Java code that is translatable to ladder, but it is extremely easy to write code that has no ladder equivalent. PLC fans will jump on that comment, but a simple example is Thread.sleep(1000), or perhaps System.out.println("Hello, world!").

Ladder and Java both exist for a reason; some problems are suited to one language or the other. This is similar to math problems that are virtually intractable in Cartesian space but are ridiculously easy in polar coordinates. But Java and Ladder aren't really two ways of looking at the same data; the flow of the two languages is as different as the syntax.

Suck it up and learn ladder; in the end you'll be a better programmer for having learned an alternative way of thinking, even if you never use ladder again.

-James Ingraham
Sage Automation, Inc.
 
T

Tony Anosike

Hi James,
Thanks for your reply. Maybe I wasn't clear enough in my previous request. I'd like to write a translator to translate control sequences written as a series of IF THEN rules into Ladder Diagram. I have an idea that IF THEN rules can be translated into Ladder Diagram (manually). What I really want to know is how interpreters or translators are written (for automatic translation).

I will appreciate any further help.

Thanks

Tony
 
<p>I disagree with the other posting that implies you shold suck it up and just learn ladder. There is a lot to be learned from the effort of learning how to build a translator. I my be out of date here but I suggest you look at Lex and Yak for some help on constructing a compiler that takes Java code to ladder.

<p>This type of activity has been going on since the early days of the cross-compilers that helped us build Intel 8748 code from PDL-11 assembly language.

<p>Below is an example of IEC-1131 structured text and its equivalent pushdown stack oriented statement list. This statement list can be readily converted to ladder using the appropriate assembler.

<p>Yes code can be generated that has no ladder representation. However, the job of the compiler is to produce good code that is representable.

<p>The world of automation is getting more and more complex and languages other than ladder do and will continue to play a significant role. Those that are stuck in the ladder world need to suck it up and learn additional approaches to automation representation.
<pre>
Boolean : Bool1;
Boolean : Bool2;
Integer : Int1;

Begin

If(Bool1 and Not(Bool2)) Then
Int1 := Int1 + 1;
EndIf;


{Ladder logic equivalent in stack oriented format}

str IFELSE.NNL.BOOL1
andnot IFELSE.NNL.BOOL2
out $nl_IF1
;
strnot $nl_IF1
skp 1;

str $nl_CRon
add ( IFELSE.NNL.INT1, $k1, $nl_Itemp1 )
out $nl_CRbb
;
str $nl_CRon
movw((integer)$nl_Itemp1, (integer)IFELSE.NNL.INT1, 1)
out $nl_CRbb
;
lbl 1;
</pre>
 
The posting was not a manual translation. It was the output from a compiler. The compiler takes a high level lanquage and generates op-codes in a real controller.

My suggestion to you is that you need to use compiler design techniques such as lex and yak to produce the translation from Java to controller code.

Ten years ago a language specification was called an EBNF (something like that) and that was then used as the specification for the compiler designers to generate a compiler that produced the output in the desired format. Lex and Yak were tools (in C) that helped in the process of designing this translation.

I am not sure how a compiler designer would do the job in 2002 but I bet it is still very similar.

If you want to go from Java to a ladder representation, then specify the translation from Java to ladder, then specify the productions, write you productions (I showed you one in the example), then produce the code generator.

Most controllers display ladder but the instructions to the controller are somthing entirely different.

If the target is a pc then you need to generate pc code even if you represent it as ladder.
 
J

James Ingraham

Anonymous said:
"The world of automation is getting more and more complex and languages other than ladder do and will continue to play a significant role. Those that are stuck in the ladder world need to suck it up and learn additional approaches to automation representation."

This is actually sort of my point; you'll notice I said: "Ladder and Java both exist for a reason; some problems are suited to one language or the other."

Your point argues AGAINST the idea of trying to translate Java into ladder. Use the appropriate tool for the appropriate job, rather than trying to make one language do everything. If you are using Java, you should UNDER NO CIRCUMSTANCES write code that looks like ladder logic. You should use encapsulation, polymorphism, exceptions, etc., etc., etc.

Tony Anosike
"I'd like to write a translator to translate control sequences written as a series of IF THEN rules into Ladder Diagram."

Anonymous's post shows an example; I don't think I can come up with anything better. I can probably translate if I need to, but codifying a set of rules is very difficult.

-James Ingraham
Sage Automation, Inc
 
J
It seems to me the student is trying to learn how to do something. It appears that the desire is to write a translator.

This to me is a good learning execise. Many controller's represent their code as ladder and so being able to convert structured text whether it be Java, C or IEC-1131 is a worthwhile learning experience.

To be able to see how to represent boolean logic in different forms is a good goal. Developing a JAVA intrepreter seems to be a reasonable task to me.

Your answer appeared to say do not bother. My answer is give it a try and learn something.

John Arnold
 
I'm doing a similar task, except it's the opposite. I'm translating IL language into Java. First of all, you need to understand well Ladder, and master its syntax diagrams. Then make tables with all symbols, and your idea of how to translate them into Java. After that, all other ideas will come to you normally.
 
C
I suggest you look at Flex and Bison (lex and yacc) they are the tools specifically written for this type of task. The documentation is rather dense, but there are good books and tutorials available free. A google search will tell all. I'm fairly sure there are ports for legacy operating systems as well as Linux and UNIX. One interesting quirk is that you are translating into a graphical representation rather than a language per se, but I believe they would still work as long as the symbols are unambiguous. I mention Bison as I assume you will want to do something with the output. I have used these for a small 4GL project. For my own part, I find the logic statements to be just as easy to work with. Most ladder is if then with an implicit else. Forgetting the else can be bad :^)

Regards

cww
 
James Ingraham:
> Give up. It would be a lot easier to translate ladder logic into
> Java, and even that wouldn't be real pleasant. Obviously, you could
> write Java code that is translatable to ladder, but it is extremely
> easy to write code that has no ladder equivalent.

1) That wasn't the question asked. The question asked was to translate an if-then syntax into ladder, which is much easier.

2) Translating Java into ladder wouldn't be particularly difficult in principle. It would, however, require a full-blown compiler.

> PLC fans will jump on that comment, but a simple example is
> Thread.sleep(1000), or perhaps System.out.println("Hello, world!").

3) Those are system calls, not features of the language as such. They have nothing to do with the problem, and in any case the examples you give could be translated reasonably - Thread.sleep() into the appropriate timer logic, System.out to using the ASCII output module.

> Suck it up and learn ladder;

Writing a compiler into ladder would entail knowing it to a degree beyond that of most automation professionals.

> in the end you'll be a better programmer for having learned an
> alternative way of thinking, even if you never use ladder again.

That's sound advice in general (though ladder is not particularly high on the list of languages to learn for that reason).

Jiri -- Jiri Baum <[email protected]> http://www.csse.monash.edu.au/~jirib MAT LinuxPLC project --- http://mat.sf.net --- Machine Automation Tools
 
As always - first you have to understand both ladder and the language you're translating from, in mind-numbing detail (write it down, carefully). Then write a program that translates from one to the other.

There are programs around that help with some of this - for instance lexers and parsers to help read the input file - and techniques for the rest.

This is not really the right group to ask about interpreters and translators, you really want to look up things about compiler construction and the like.

Jiri -- Jiri Baum <[email protected]> http://www.csse.monash.edu.au/~jirib MAT LinuxPLC project --- http://mat.sf.net --- Machine Automation Tools
 
Tony Anosike:
> > I'd like to write a translator in Java to translate IF THEN
> > statements to Ladder Diagram.

Rami Saad:
> I'm doing a similar task, except it's the opposite. I'm translating IL
> language into Java.

One thing to keep in mind is that some variants of mnemonics are easily implemented using a stack. If the language has AND-BLOCK and OR-BLOCK instructions, check to see if that's what it is.

AND X => top-of-stack AND X -> top-of-stack LD X => push X onto stack AND-BLOCK => pop two things off the stack, AND them, push result

Rig the stack so that it doesn't overflow, just discards the bottom-most entries (a circular one is easiest).

Jiri -- Jiri Baum <[email protected]> http://www.csse.monash.edu.au/~jirib MAT LinuxPLC project --- http://mat.sf.net --- Machine Automation Tools
 
V

Vladimir E. Zyubin

Hello James.

The reason to write such a translator could be a porting reason. (thou I must say it is the only reason I see)

for example, there is a controller with LD interpreter only... and there is a task that ought to be programmed in java.... or a text (a set of texts) in java... etc.

And yes... perhaps we deal with the fallacy - somebody thinks the outgoing LD text will be more readable... In this case the project must be terminated immediately...

-- Best regards.
Vladimir E. Zyubin
mailto:zyubin(AT)iae.nsk.su
 
R

Richard Higginbotham

James:
> Your point argues AGAINST the idea of trying to translate Java into
> ladder. Use the appropriate tool for the appropriate job, rather than
> trying to make one language do everything. If you are using Java, you
> should UNDER NO CIRCUMSTANCES write code that looks like ladder logic.
> You should use encapsulation, polymorphism, exceptions, etc.,
> etc., etc.

Lots of things have been tranlated from one language to another for convience sake, its a little harder to debug but the expectation is that you won't have to dubug the lowest level code (in this case ladder) very much ;). Early c++ compilers compiled to C then went through a C compiler, and its been part of the bootstrapping process for many languages I'm sure. If its simply an educational exercise or trying to get java to run on a controler that will only accept ladder then it worth it for the sake of learning if nothing else.

As far as tools are concerned you might try lex (lexer) or bison (parser) or if you prefer to work with java you might try Antlr (lexer and parser). Theres also Java versions of lex and yacc (Jlex, ? I believe) as well. Each has its own stregnths and weaknesses all have examples and tutorials available which will include IF THEN constructs. I would think of Ladder very much like ASM (looking at John's example) and go from there.

regards,
Richard Higginbotham
 
M

Martin Schoeberl

This discussion is interesting and goes to different directions. One direction I'm interested is to translate Ladder to Java.

One possibility would be to write an interpreter in Java like the one Marc did in C:

>For people interested, I've implemented a ladder interpretor in C. Take
>a look at : http://www.sourceforge.net/projects/classicladder
But it would be more efficient (in terms of exectuion time) to compile it to simple Java statements. Of course you have to build some runtime structure in Java to support this compiled code, but this should not be that big problem.

My interest is because I've built a Java processor (http://www.jopdesign.com) and there is one application for remote monitoring (http://www.telealarm.net/) using this processor.

A translation to Java would extend this system to give it functionality like PLCs.

For those who want to program PLCs direct in Java I can recommand TAL :) Contact me for questions about it: [email protected].
 
Martin Schoeberl:
> This discussion is interesting and goes to different directions. One
> direction I'm interested is to translate Ladder to Java.
...
> But it would be more efficient (in terms of exectuion time) to compile
> it to simple Java statements.

The MatPLC has this for mnemonics to C - take a look at http://mat.sf.net/manual/logic/il.html

An instruction like "LD X001" gets translated into

# 5 "test.il" push; acc = master && plc_get(X001); /* LD X001 */

It would be no problem to change this so that it outputs Java instead. It's released under the GPL, so you're free to make use of it, as long as either the result is still under GPL, or you never give it to anyone.

Jiri -- Jiri Baum <[email protected]> http://www.csse.monash.edu.au/~jirib MAT LinuxPLC project --- http://mat.sf.net --- Machine Automation Tools
 
I would agree; writing a JAVA based ladder logic application would allow more people to learn
the basics of ladder and its functionality.

You could write a converter that could take
ladder logic and convert to JAVA, C, Pascal, etc.
but it is my understanding that the PLCs have
special firmware/uploading software that needs
ladder code. So hence, final output from JAVA
to ladder is a reasonable outcome for me. And,
for those already familiar, it would be easier
to troubleshoot. Other simulations in other
languages just offer different tools/viewpoints
to look at the same information.
 
C

Curt Wuollet

For that matter, there is really nothing binding
a PLC to running Ladder code. Several outputs
could be supported with one being that which is
most efficient on that particular processor. I
suspect a great many do more or less direct
interpretation of a structured text which isn't
very far from compilable assembler code. Since
PLC's are becoming very similar to PCs under
the covers, this would be an area of interest for
our project where there is a demonstrable IL
module with source to gaze upon. If people only
knew the fewer and fewer differences, they wouldn't
be so hard on PCs.

Regards

cww

 
Top