Programmable Logic Controllers (PLCs) have been used in industrial applications since the early 70s. Originally, PLCs were designed to replace hard-wired relay-logic in applications where use of ordinary computers could not be economically motivated. The PLCs proved to be very versatile, and their application areas have increased constantly, as have their ability to handle more complex control issues such as PID-control and hierarchical control of entire plants. However, their original heritage still influences both their behavior and programming.
To simulate the parallelism inherent in the wired relay-logic, a PLC executes its program cyclically, copying the input values into intermediary memory, executing the entire program and writing the result to an intermediary output-memory region. See Figure 32. This read-execute-write cycle is called a scan cycle. The PLC continuously executes the scan cycles until power is shut off. Given that the scan cycle period is “short enough” with respect to the time constants of the plant, this effectively simulates the parallel behavior inherent in hardwired relay-logic, that PLCs were once invented to replace. For an outside viewer, and specifically the process that is controlled, all outputs change their values simultaneously in response to the input-signals. Typical cycle times are 10 ms and continuously decreasing as processor and memory speeds increase.
The ladder rungs are evaluated in a fixed order beginning with the first rung, with the resulting values copied to the intermediary output-memory region. When the cycle has finished, and only then, the intermediary output-memory region is copied to the physical outputs by the PLC hardware in one operation. However, it is important to note that internally the PLC evaluates the rungs sequentially, usually from top to bottom and left to right. This means that previously evaluated rungs can affect the evaluation of the current rung, even though the result of those previous rungs are not yet shown to the outside world. For instance, it is (usually) a mistake to specify the same output from two different rungs. Some PLCs warn about such programming mistakes.
Figure 32. A PLC scan cycle. Read inputs copies the input values to intermediary memory. Execute program evaluates all rungs sequentially. Write outputs copy the resulting output values to the physical memory.
A PLC-program consists of a set of, in our case, boolean expressions[1]. These are all evaluated during one scan cycle, and their result determines the state of the output. The expressions are typically evaluated sequentially, one by one, and their results are stored in intermediate memory. When all expressions have been evaluated, the part of the intermediate memory that corresponds to the output signals is copied to the outputs and thus shown to the outside world. By using internal memory, sequential behavior can also be accomplished.
PLCs are traditionally programmed in Ladder Diagrams (LDs) for which there now exists an international standard, IEC 1131-3. An LD consists of graphic symbols, representing for instance contacts and coils, laid out in networks similar to a rung of a relay logic diagram. The contacts and coils are connected by links. A contact is an element that imparts a state to the link on its right side equal to the boolean AND of the state of the link to the left with an appropriate function of an associated boolean input, output or memory variable, see Table 3.
Normally open contact |
A
--| |-- |
The state of the left link is copied to the right link if the state of the boolean variable A is true. Else, the state of the right link is false. |
Normally closed contact |
A
--|/|-- |
The state of the left link is copied to the right link if the state of the boolean variable A is false. Else, the state of the right link is false. |
Positive transition sensing contact |
A
--|P|-- |
The state of the right link is true for one scan cycle when the state of the left link is true, and a rising edge of the boolean variable A is detected. |
Coil |
Q
--( )-- |
The state of the left link is copied to the boolean variable Q, and to the right link. |
Negative coil |
Q
--(/)-- |
The state of the left link is copied to the right link, and the negation of the left link is copied to the boolean variable Q. |
Latched coil, Set |
Q
--(S)-- |
The boolean variable Q is set true, when the left link is true, else Q is not affected. |
Latched coil, Reset |
Q
--(R)-- |
The boolean variable Q is set false when the left link is true, else Q is not affected. |
Positive transition sensing coil Q |
--(P)--
|
The boolean variable Q is set true for one scan cycle each time the left link goes from false to true. |
Table 3. Contacts and coils of the IEC 1131 Ladder Diagram (LD) language.
In addition to the coils and contacts
of Table 1, the standard allows the inclusion of function blocks in the ladder
diagram. Function blocks are program elements that appear as rectangular blocks,
and that are "wired" together in a manner resembling a circuit diagram.
Function blocks represent packages of software that can be reused in different
parts of an application. Provided that a function block has boolean inputs and
outputs, it can be connected into an LD. The most common function blocks are
the "set-reset" function blocks, counter blocks and timer blocks.
These all have boolean inputs and outputs and can, thus, be used in an LD. These
function blocks are described in Table 2 and Figure 2.
Latches |
Down counter |
|
|
Table 4. Some Function Blocks (FBs) useful in LDs.
Figure 33. The different timers of the IEC 1131 LD.
The standard for PLC programming languages IEC 1131-3, lists a number of alternative languages. Of these, Sequential Function Charts (SFCs) are the most important. SFC allows a PLC program to be organized into a set of steps and transitions interconnected by directed links. Associated with each step is a set of actions, and with each transition a transition condition. An SFC evolves as steps are activated and inactivated in accordance with the fulfillment of the transition conditions of the active steps. When a step is active its associated actions are executed. The SFC programming language can be viewed as a restricted variant of safe Petri nets, Peterson (1981), and would intuitively seem to be ideal for implementing supervisor state automata. However, a number of problems related to the asynchronous nature of a state-machine contra the synchronous nature of a PLC programs turn up.
An SFC consists of a set of steps connected by transitions, very similar to a Petri net, but the steps are drawn as rectangular boxes. To each step can be connected a number of actions
[1] Typically, modern PLCs include constructs for manipulating higher-level entities, such as integers and floating point numbers, text strings etc., see IEC 1131-3.