
Lesson 16
PLC Recipes, Alarm Classes and Cyclic Tasks
A product table and a working copy, alarm classes and the lamp language, acknowledge on an edge, and a loop that runs on its own clock.
From Usta's field manual in Meridian Works, a story-driven PLC course: chapter 16, “Recipes, Alarms and Tasks”. The rest of the standard: a product table and a working copy, alarm classes, acknowledge on an edge, and a loop on its own clock.
The table and the working copy
When a machine starts making more than one product, the products' numbers spread through the program. The volume sits on one rung, the temperature on another, the torque somewhere else. When the product changes, somebody changes these numbers one by one. One day they forget one.
A recipe is the answer to this. The products' numbers sit in a table. The machine does not read the table directly. What the machine reads is a working copy. Loading moves one row of the table into the working copy.
This separation gains three things:
- The batch is not spoiled. The working copy changes only while the machine is stopped. Even if somebody turns the selector during a batch, the running batch is not affected.
- A wrong selection does no harm. The number is checked first. A number that is not in the table is refused, and the old recipe stays in place.
- Which recipe is running is known. The number of the loaded product sits beside the working copy.
The shape of the table. A recipe record is a user data type (UDT): volume, temperature and torque. In real projects the table is usually an array of this type and sits inside a data block (DB). In this simulator a user type cannot contain another user type. So the table inside the DB is made of columns. Each parameter is an array, each product is an index. The working copy is a single record. Loading moves three values from the same index into that record.
Check the index first. With the selector on 7, Recipes.Volume[Product_Sel] reads outside the table. The CPU stops when that happens. Put the check before the copies, on the same rung. A box that gets no power never reads its index at all.
Start values. In a real DB every field has a start value. In this simulator data blocks have no start values. A startup block writes the table. This block runs once as the CPU starts, before the first cycle.
The operator selects the recipe, the program loads it. No number changes in the middle of a batch.
Alarm classes and the lamp language
An alarm is not just a lamp. An alarm answers three questions. What happened? What is the machine doing? What is expected of me?
That is why every alarm has a class. The class says what the alarm does to the machine and whether it asks for an acknowledge. This plant's standard has three classes:
| Class | Machine | Acknowledge |
|---|---|---|
| FAULT | stops | required |
| WARNING | runs | required |
| MESSAGE | runs | not required |
Every alarm has two states. The first is whether the alarm is there right now. The input says that. The second is whether the alarm has been seen. The program holds that. At the moment the alarm arrives, a "not seen" bit is set. Only an acknowledge clears this bit. The alarm going away by itself does not clear it.
The lamp language comes from these two states:
| Is the alarm there? | Has it been seen? | Lamp |
|---|---|---|
| yes | no | flashes |
| yes | yes | steady |
| no | no | flashes |
| no | yes | out |
The third row matters most. An overload relay resets itself when it cools down. If the lamp only follows the input, it goes out, and nobody knows why the machine stopped. The "not seen" bit keeps that information on the panel.
A fault holds the machine. An alarm in the FAULT class stops the machine. Until the alarm has gone and been acknowledged, START does not start the machine. After the acknowledge the machine does not start by itself either. Somebody presses START again.
Information is not an alarm. A MESSAGE only shows a condition. It waits for no acknowledge and does not flash.
The operator terminals of both platforms know alarm classes too. The class, the colour and the acknowledge rule are chosen in the alarm's definition. The program must still know the alarm itself and what the machine will do. The lamps in this chapter are the program's half of the job.
A flashing lamp says "look at me". A steady lamp says "you know". Nobody looks at a panel that mixes the two up.
An acknowledge is a moment
The acknowledge button is not a state but a moment. At the moment the operator presses it, they say they have seen the alarms waiting at that moment. They have not yet seen the alarms that come later.
If the acknowledge is tied to the button's level, two bad things happen:
- If the button is held, every new alarm is acknowledged the moment it arrives. The lamp never flashes.
- The operators learn this quickly. They tie the button down with a cable tie. After that the panel says nothing at all.
The right way is to acknowledge on an edge. On the Siemens side a P contact (positive edge) does this. On the Rockwell side an ONS instruction goes after the contact. An ONS has a storage bit. Give every ONS its own storage bit.
The order of the rungs matters. An acknowledge and a new alarm can happen in the same scan. If the acknowledge rung sits before the alarm rungs, the new alarm has the last word and stays unseen. If the order is the other way round, the new alarm is acknowledged before anybody sees it.
The alarm log. For the shift report the codes of the last few alarms are kept in an array. When a new alarm arrives, these steps follow:
- The old codes move down one place. The shift starts from the oldest: first the code in [4] goes to [5], then the code in [3] goes to [4], and so on.
- The new code is written into [1].
- The counter goes up by one.
If you start the shift from the newest, the code in [1] spreads through the whole array. The shift and the count both happen once, at the moment the alarm arrives. An alarm that lasts a long time is still one alarm.
An acknowledge does not change the log. An acknowledge says the alarm was seen. It does not say the alarm never happened.
Before you blame the operator who tied the button down, look at the program that made him do it.
The cyclic task
The main program is a loop. One scan ends and the next one begins. How long a scan takes depends on how long the program is. As rungs are added to the program, the scan grows longer. On a real CPU this time also varies a little from scan to scan.
Some jobs do not like this uncertainty. A control loop is the first of them. Every time a PID runs, it looks at how much the measurement has changed. The derivative part divides that change by the time between two runs. If that time is uncertain, the loop's behaviour is uncertain too.
That is what a cyclic task is for. A cyclic task is a block that runs on a fixed period. The program does not call it. The CPU watches its own clock and runs the block when its time comes. On the Siemens side this is called a cyclic interrupt block. On the Rockwell side it is called a periodic task. The names differ, but the idea is the same.
Why is a loop better inside a task?
- The sample time is fixed. If the loop is in a 100 ms task, it always runs every 100 ms. Adding rungs to the main program does not affect the loop.
- The tuning travels. If a loop was tuned for 100 ms, the same tuning works the same way in a 100 ms task on another CPU.
- The derivative part stays calm. The derivative divides the noise on the measurement by the time between two runs. If that time is 10 ms, the noise counts ten times as much as at 100 ms. The heater command swings between its two limits.
Choosing the period. The period is chosen for the speed of the process. For a temperature loop, somewhere between 100 ms and 1 s is usually enough. Pressure and flow loops want a shorter period. The period cannot be shorter than a scan.
In this simulator:
- The main program's scan time is fixed, and the scenario sets it. Within one test run the scan time does not change. The same program can be tried at different scan times, though.
- When its time comes, the task runs at the start of the scan, before the main program. It runs at most once per scan. It does not interrupt the main program.
- The PID measures the time between two of its runs from the clock. So a loop in the main program does not get the time wrong. But it divides the noise by a very small time.
A loop's clock must not depend on how long the program is. Give it a clock of its own.
Between the task and the main program
A task uses the same memory as the main program. That is a convenience and a danger.
The task's tags are its own. The loop's working variables are written in the task: the measured temperature, the output and the integral. The main program may read them but must not write them. The value of a tag written from two places depends on which one ran last. Finding that by reading rungs is very hard.
A task should be short. Only time-bound work belongs in a task: the scaling, the loop and the output. The machine's sequence, the alarms and the recipe stay in the main program. A long task makes the main program's scan longer too.
The counter sits inside the task. To see that the task really runs on its period, put a counter in it. The counter goes up by one every time the task runs. The trend recorder or a watch table reads this number. In a 100 ms task the number must go up by a hundred in ten seconds.
A timer is not a task. A self-resetting 100 ms timer in the main program looks like a task. But the timer only starts again on the next scan. Its period is 100 ms plus one scan. As the scan changes, so does the period.
In this simulator every task has its own timer and edge memory. A timer in a task runs on the task's clock. On real CPUs a task can interrupt another task or the main program. This simulator does not interrupt. On a real plant, though, a tag written from two places can change even in the middle of a scan.
Keep the task short, keep its tags to itself, and show that it counts.