
Lesson 13
Writing a PLC Programming Standard: Library Blocks, FB and AOI
A library instead of copies, a motor block with feedback, a state machine block, the rules of a standard, and FB and AOI blocks that carry their own data.
From Usta's field manual in Meridian Works, a story-driven PLC course: chapter 13, “Writing a Standard”. The first pages of the blank chapter. This time you are the one writing: a program written once and used everywhere.
A library instead of copies
For years in this plant the same motor circuit was copied thirty times. Every copy was a little different. One had an overload, one did not. In one stop dominated, in another it did not. When a bug was found it had to be fixed in thirty places, one by one. Usually one or two were forgotten.
A standard block solves this. A motor block is written once, tested, and put in the library. Every motor uses the same block. If a bug is found in the block, it is fixed in one place and every motor is fixed.
This has a cost. A standard block is a contract. The meaning of its inputs and outputs must not change, because every program that uses it relies on that meaning. So a good standard writes down:
- What each input and output means.
- When the block raises a fault and how the fault is cleared.
- The block's version and what changed.
Most large companies have their own library. Machine builders also come with their own standard software. Wrenfield's packaging machine came that way too. In this chapter you write the plant's own standard.
Write it once, test it once, use it everywhere. Every line you copy is a line you will one day fix separately.
FB and AOI: a block that carries its own data
A standard motor block needs memory: a fault bit, a timer, the last state. Each motor's memory must be separate. The pump's fault must not affect the capper. Both platforms do this with the same idea under different names.
In Siemens the block is an FB (Function Block). An FB's memory is its instance data. Every FB called from the main program gets its own instance DB: Pump_Data, Capper_Data. When an FB is called from inside another FB, a multi-instance can be used. Then the inner block's data lives in the outer block's static area. A station block carries its own motor block inside it, and the main program never sees the motor.
In Rockwell the same thing is called an AOI (Add-On Instruction). An AOI's memory is its backing tag. Each use gets its own tag of the AOI's type. An AOI can carry another AOI inside it as a local tag. That is the counterpart of the Siemens multi-instance.
The differences you need to know:
| Siemens FB | Rockwell AOI | |
|---|---|---|
| Memory | Instance DB or multi-instance | Backing tag |
| Parameter kinds | Input, Output, InOut, Static, Temp | Input, Output, InOut, Local |
| Online change | The body can be changed online | An AOI cannot be edited online |
| When the interface changes | Instance data is rebuilt | Every use is updated |
In this simulator the same block runs in both views. In the Logix view, think of it as an AOI.
The block is a mould. An instance is one machine cast from it.
A motor block with feedback
In chapter 1 we wrote: a coil is a promise, feedback is proof. The standard motor block is that sentence in program form.
The block compares two things: the command it gave (Out) and the feedback from the contactor's auxiliary contact (Fb). The two must agree. There are two kinds of disagreement:
- Command, but no feedback. The contactor coil may be burnt out, a fuse may have blown or the overload may have tripped.
- Feedback, but no command. The contactor's contacts may have welded. This is more dangerous, because the motor turns while the program thinks it has stopped it.
A contactor does not close instantly. It is a mechanical part and takes a few tens of milliseconds. So the block does not call a disagreement a fault at once. It waits a short time with a timer. In this course the time is 2 seconds. If the disagreement lasts longer, the fault latches and the output drops.
Clearing the fault is a rule too: the fault clears only while the command is off. If it could be cleared with the command on, the reset button could restart the motor by itself. The operator first stops the motor, then clears the fault, then starts again.
In Siemens this block is written as an FB, in Rockwell as an AOI. The logic inside is the same.
It is not enough for the program to say the motor is turning. The field has to say it too.
The state machine
Running a machine's modes with a few latched bits starts easily but ends badly. Three bits have eight combinations. Most of them are meaningless, but one day one of them happens.
A state machine uses a single integer instead: State. At every moment the machine is in exactly one state. Each transition is a single rung: "if I am in this state and this event happens, go to that state."
The codes in this course are a small part of PackML, a standard common in packaging:
| Code | State |
|---|---|
| 0 | STOPPED |
| 1 | IDLE |
| 2 | EXECUTE |
| 3 | HELD |
| 9 | ABORTED |
In ladder each transition is one rung: a State = state compare, the event contact, then a move of the new state. In Siemens the compare and move are CMP == and MOVE; in Rockwell they are EQU and MOV.
In a text language the same job is a CASE structure. Siemens uses SCL and Rockwell uses Structured Text, and they are written almost the same way:
CASE State OF
0: IF Reset_PB THEN State := 1; END_IF;
1: IF Start_PB THEN State := 2; END_IF;
END_CASE;
Two rules keep it safe. First: STOP and ABORT are written last. The last write in the scan wins. If the most severe state is written last, a START in the same scan cannot overwrite it. Second: outputs are derived from the state. The motor runs because the state is EXECUTE. No other bit runs it separately.
If you want to know what the machine is doing, look at one number. If that number lies, you look for the bug in one place.
The rules of a standard
A standard is not only blocks. It is also the rules that make everyone write the same way. These are the plant's rules:
- The same names. Every motor's tags follow the same pattern:
Wrap_Kcontactor,Wrap_Fbfeedback,Wrap_Faultfault,Wrap_Datainstance. A tag's name tells you what it is. - The same state codes. On the Siemens filling line and on the Rockwell packaging line alike, 2 means running and 9 means fault. An operator moving from one line to the other does not learn again.
- One writer. Only the state machine writes
State. Only the motor block writes the motor output. - The order is chosen on purpose. First the commands are calculated, then the blocks are called, then the state transitions are written, and STOP and ABORT come last.
- Every block is tested. Every block that enters the library has its own tests. If a block changes, the tests run again.
The standard stays the same on both platforms. Only the tool changes: FB and SCL on Siemens, AOI and Structured Text on Rockwell.
The rest of this chapter is blank. Whatever the plant teaches you from here on, you will write here.
Usta's manual began with wire and contacts. Yours begins with a standard.