A PLC Ladder Logic compiler and runtime in Forth
The purpose of this project is to allow a developer to inline an ASCII version
of a PLC Ladder Diagram in their source code and use it like they would any
other word in forth. Below is a simple example. A more advanced version of this
example can be seen in main.fs.
include l5k.fs
( INPUTS )
VARIABLE START
VARIABLE STOP
( OUTPUTS )
VARIABLE MOTOR
VARIABLE FAN
( LOCALS )
VARIABLE RUN
TIMER FAN_TIMER
LADDER: MOTOR_ROUTINE
|| ||
|| STOP START +------------------+ RUN ||
||---]/[---------+---] [---+-------| TOF |---------( )---||
|| | | | | ||
|| | RUN | | TIMER FAN_TIMER | ||
|| +---] [---+ | PRE T#3s | ||
|| | | ||
|| +------------------+ ||
|| ||
|| RUN MOTOR ||
||---] [--------------------------------------------------------( )---||
|| ||
|| ||
|| FAN_TIMER.DN FAN ||
||---] [--------------------------------------------------------( )---||
|| ||
SEE MOTOR_ROUTINE
\ : MOTOR_ROUTINE
\ R: STOP XIO R[ START XIC R, RUN XIC R] 3000 FAN_TIMER TOF RUN OTE R;
\ R: RUN XIC MOTOR OTE R;
\ R: FAN_TIMER.DN XIC FAN OTE R; ;
MOTOR_ROUTINE
MOTOR ? ( 0 )
START ON
MOTOR_ROUTINE
MOTOR ? ( -1 )
START OFF
MOTOR_ROUTINE
MOTOR ? ( -1 )
STOP ON
MOTOR_ROUTINE
MOTOR ? ( 0 )As you can tell from the SEE output, the ladder diagram is compiled into a
human-readable definition that looks very similar to the standard L5K format.
This keeps the execution fast, as the ladder diagram is compiled rather than
interpreted.
The ladder compiler is also vectorized, DEFERing the compiler for each
instruction. This allows the user to bring their own instruction set. If you
prefer the Siemens instruction set, simply include ladder.fs and implement the
desired instructions.
In order to run the example, you will need to install GForth. Once installed,
you can run the example program with gforth main.fs.
A brief description of each source file:
ladder.fsis the ladder logic compiler and runtime.l5k.fsis the extension to add support for L5K instructions.test.fscontains unit tests to verify functionality.main.fsis an example program to use as a reference.