Meridian Works
ENTR
Open the simulator
Process

Lesson 9

Analog Signals, Scaling and PID Control in a PLC

Analog inputs and valves, scaling and its trap, open and closed loop, what P, I and D each do, tuning as a method, and recipe data.

From Usta's field manual in Meridian Works, a story-driven PLC course: chapter 9, “Ranges and Loops”. Measuring things that are not on or off, holding them where you want them, and keeping the numbers of a recipe.

A. The number is not the thing

Every signal you have used so far had two states. The photo-eye is blocked or clear. The contactor is pulled in or dropped out. Writing the rung was easy, because there was nothing else to say.

A level is not like that. Neither is a temperature. They have a value. That value reaches the CPU through several steps. If you do not know this path, you will lose a lot of time to numbers that are nearly right.

From the level in the tank to the number in the programtransmitteranalog cardyour scaling rung0-100 %4-20 mA0-27648Only the last box in this chain knows what a percent is. That box is yours.

The transmitter turns what it measures into a current. It gives 4 mA at the bottom of its range and 20 mA at the top. Not zero, four. Because a broken wire gives zero mA. Zero must mean "wire broken", not "tank empty". That choice has saved a lot of tanks.

The analog card measures this current and turns it into a whole number. The range of that number depends on the platform:

  • In Siemens the full measuring range is 0 to 27648. A 16-bit number can go up to 32767. The margin is left so that you can see a signal that has gone out of range.
  • In Rockwell the module decides the range. Most analog modules have scaling in their configuration. The module can deliver the value directly in engineering units, as a REAL. If you use raw counts, read the range from the module's documentation.

The cards in this hall give 0–27648.

Your scaling rung turns raw counts into engineering units. Nobody does this for you. Until you do it, the number means nothing.

If somebody says "the level is 13824", they are not talking about litres. They are saying the transmitter is in the middle of its range. How many litres the middle is, is written on the transmitter's label. If it is not on the label, it is painted on the tank. If it is not there either, you go and look.

A. Scaling, and the trap in it

Turning a raw count into engineering units is one line of arithmetic:

value = raw × span ÷ 27648

For a 0–100 % level transmitter the span is 100. For a 0–200 °C temperature card it is 200. Two boxes are enough: a MUL and a DIV.

Multiply first. Always. If you divide first, you divide a small number by a large one. In an integer the result is zero. Then every level below full reads zero. You saw this in Chapter 5. You will see it again on a tired day.

Use REAL. A level of 61.4 % becomes 61 in an INT. Four tenths of a percent are lost. They are lost again when you subtract it from the setpoint to get the error. A loop that cannot see a tenth of a degree cannot hold a tenth of a degree. Process values are REAL. All of them.

Going out, to a valve or a heater, the sum turns round:

raw = percent × 27648 ÷ 100

That is one MUL by 276.48.

Both platforms have ready-made boxes. Siemens uses two boxes. NORM_X pulls the raw count into 0.0 to 1.0. SCALE_X turns that into engineering units. For an output the same two boxes are used in reverse order. In Rockwell the SCP (Scale with Parameters) box takes the two end points and does the sum in one box. You can also write the formula yourself with CPT. If you configured the module to scale, you do not need this rung at all. In this hall you will write the arithmetic by hand. That way you will also know what is inside those boxes.

Then clamp it. This is the trap. A controller can easily calculate 600 %. A gain of 10 on an error of 60 gives exactly that. 600 % of 27648 is 165,888. That number does not fit in a 16-bit INT. What happens depends on the platform and the conversion instruction. Some conversions wrap the number: 165,888 becomes −30,720 in an INT. Then the valve does not open a bit too far, it shuts completely. Some conversions raise an error and leave the output at its old value. You want neither. In this simulator the number wraps.

Clamp the percentage to the actuator's real travel before you scale it. In Siemens the LIMIT box does this in one box. In Rockwell you use two compares and a MOV. You will do the same in this hall: one rung with greater-than (>, GRT in Rockwell) and one with less-than (<, LES in Rockwell). It is the cheapest insurance in this book.

A. What a real signal looks like

A real transmitter does not sit still. Connect a meter and the last digit keeps moving. A little down, a little up. It goes on like that all day. This is called noise. Noise comes from the process itself: the liquid sloshes, bubbles form, a pump beats. It also comes from the wiring and from the card's own converter.

Know three things about noise.

One: it is usually harmless. A level moving ±0.2 around 60 % is a healthy tank with liquid in it. You do not need to do anything. A beginner sees the last digit moving and adds a filter. A filter you did not need is a delay added to the loop.

Two: it becomes harmful when you take its derivative. Rate of change is the difference between two readings divided by a short time. Noise makes that difference random. The short time makes it large. On a noisy signal a derivative term makes the output jump all the time and wears out the valve. If derivative has a bad name, this is usually why.

Three: a comparison needs a deadband. Say a rung reads "run the pump if Level > 60". The level sits at 60.0 and moves ±0.2. The pump starts and stops several times a minute. You saw the fix in Chapter 5: switch on at one value, off at another, and leave a gap between them. Noise will sooner or later find a comparison against a single value.

A filter, if you really need one, is one rung. You take a little of the new reading and a lot of the old value:

Filtered = Filtered + (Raw − Filtered) × 0.02

At a 10 ms scan this filter has a time constant of about half a second. That also means you have added half a second of delay to the loop. A filter is not free. Use the smallest filter that solves the problem. And know what you paid.

This rung runs once per scan. So its result depends on the scan time. If the scan gets longer, the filter gets slower. That is why on a real CPU you put a filter somewhere that runs at a fixed interval. You will see this on page ten.

A. The other end: valves

An analog output is the same chain in reverse. It has habits of its own.

A valve has a stroke, and the stroke is not the same thing as the signal. You send 50 %. After a while the valve goes somewhere near half open. On top of it sits a positioner. The positioner runs its own small loop with air and a feedback lever. This takes a second or two. Even then the valve does not go exactly where you asked. That is not a problem, because your loop measures the result, not the valve.

A valve is not linear. On a globe valve the first 10 % of stroke can pass much more than a tenth of the flow. Or much less. It depends on the shape of the plug inside the valve. So the process gain changes with the valve position. The process gain is how much the measured value changes for each percent of valve. A loop tuned well with the valve open can oscillate when the valve is nearly shut. This is normal. It is the reason tuning is done at the operating point. It is also the reason a loop that ran fine for five years goes wrong after the valve trim is changed.

Some actuators are not analog at all. A contactor is either open or closed. You cannot ask a contactor for 43 %. A loop that drives one has to be turned into on-off somewhere. You do this with hysteresis (page twelve) or with pulses. Pulses wear the contactor out. Find out which kind of actuator you have before you design the loop.

Check the fail position. If the air fails or the signal is lost, where does the valve go? Open or shut? Somebody chose that with a spring. It is the most important safety fact about the loop you are writing. A heating valve that fails open starts a fire. A cooling valve that fails shut starts the same fire.

A. What the plant does on its own

Before you control something, find out what it does when you leave it alone. Open the valve to a fixed position. Step back and watch. You can get two different answers. Each needs a different approach.

A self-regulating process. You open the valve half way. The level rises, slows down and stops somewhere. It stops because the outflow depends on the level. The fuller the tank, the more liquid it pushes out. In the end what comes in equals what goes out. A tank draining freely at the bottom is like this. So is a heated vessel. The hotter the vessel, the more heat it loses to the room. In the end the loss equals the heater.

On such a process one step test gives you two numbers:

  • gain: how much the value changes for each percent of valve,
  • time constant: how long the value takes to make 63 % of that change.

Both matter. The gain tells you how hard to push. The time constant tells you how long to wait before you say "it is not working".

An integrating process. You open the valve half way. The level rises, keeps rising, and the tank overflows. Nothing balances it. Because a pump downstream sets the outflow. The pump does not care how full the tank is. A tank between two pumps is like this. So is a position, a batch total, and anything that accumulates.

These processes have no natural resting point. They also behave differently under control. On page seven you will see that proportional control leaves an offset. An integrating process is left with an offset too. But that offset depends on the load, not on the setpoint.

Our header tank is self-regulating. The welded restriction on the outlet makes it so. It is the easier of the two. Everybody should start here.

B. Open loop and closed loop

Open loop is telling the plant what to do and hoping for a good result. You open the valve to 47 %, because 47 % was right last Tuesday. As long as nothing changes, this works. But the supply pressure changes. The temperature of the incoming liquid changes. The demand downstream changes. After four years the valve seat wears. Nothing stays the same for long.

Closed loop is measuring the result and using the difference.

error = setpoint − measurement

The setpoint is the value you want to reach. The whole idea is this subtraction. The rest of this chapter is about what to do with the difference. Look at what it buys you. The controller does not need to know the supply pressure, the liquid temperature or the state of the valve. It needs no model of the plant at all. It only has to see that the number is low, and push.

Two consequences follow. Remember both.

The controller only knows what the measurement tells it. If the transmitter reads four degrees low, the loop keeps the vessel four degrees hot, always. It does this perfectly and is sure it is right. Every argument about a loop should start with "is the measurement right?" A third of those arguments end there.

The controller cannot beat the plant. If the vessel needs 71 kW of heat and the heater can give at most 60 kW, you cannot reach the setpoint. Neither gain nor integral changes that. The controller stays at 100 % and the error stays. That is not the controller failing. The controller is correctly telling you the heater is too small.

B. P, and the gap it leaves

The simplest thing you can do with the error is multiply it by a number.

output = Kp × error

A large error opens the valve a lot. A small error opens it a little. It is a clear, stable and fast rule. It has one flaw. That flaw is hard to notice before you see it.

Proportional control: more gain comes closer, but never arrivessetpointlow gainhigh gaindroop

Proportional control cannot reach the setpoint. Look at the equation again. The output is non-zero only because the error is non-zero. If the level reached the setpoint exactly, the error would be zero. Then the output would be zero, the valve would shut and the tank would start to empty. So the level never gets there. It stops at the point where the valve opening it needs balances the error that produces that opening.

On a self-regulating process with a process gain K, the arithmetic is short. It is worth doing once:

settled value = setpoint × (K·Kp) / (1 + K·Kp)

For K = 1.6 and Kp = 2 the result is 0.762. You ask for 60 and the tank stops at 45.7 %. Raise Kp to 10 and the result is 0.941, which is 56.5 %. Better. Raise Kp to 100 and the result is 0.994. But the loop starts to oscillate. Because high gain plus even a small delay is enough for oscillation.

This gap is called droop (offset). It is not a fault. However careful you are, it does not go away. The governors on old steam engines had it too. The word "droop" comes from there.

There is only one honest way to close the gap: give the controller a memory. That way became common in the 1930s.

B. I, and how it goes wrong

Keep a running total. Every scan, add a small piece of the present error to it:

total = total + Ki × error × dt

Here dt is the length of one scan. As long as there is an error, the total grows and the valve opens a little more. When the error reaches zero, the total stops growing. But it stays where it is. That remaining value is the valve opening that holds the level exactly there. The controller found it without anybody telling it.

This is called integral action. It is the memory that proportional control does not have.

Look at the dt in that line. dt is the sample time. That is why every real PID block wants to know its sample time. If you move the same loop somewhere that runs more slowly, its tuning changes too. Even if you did not mean it to.

Now the failure nobody escapes. Everybody meets it one day.

Ask for something the plant cannot give. Say a level it cannot reach with the drain wide open. Or a temperature the heater cannot reach. The error never goes away. The total keeps adding. The valve has been wide open for three minutes. The total has climbed to four thousand. That means a valve 4000 % open. No such valve exists.

Then the load lifts. Now the plant can reach the setpoint. But the controller sails past it. Because before the valve can close by a millimetre, those four thousand must be paid back first. And that happens only at Ki per second.

The same load lifts: total bounded and unboundedsetpointtotal kept growing while the output was at its limittotal bounded by what the valve can doThe only difference between the two controllers is two rungs.

This is called windup. The fix is to stop the total from meaning something impossible.

The easy fix is to clamp the total to the output range. That is better than nothing, but not enough. A total clamped to 100 % still means a fully open valve. While the total unwinds, the tank still goes the wrong way.

The right fix is called back-calculation. If the output would leave its limits, set the total to the value that puts the output exactly on the limit:

if (P + I > max) then I = max − P
if (P + I < min) then I = min − P

That way the total never carries anything the actuator could not do. When the error changes direction, the loop responds on the very next scan.

Ready-made PID blocks have windup protection. Siemens PID_Compact has it, and so do the Rockwell PID and PIDE instructions. But the protection works against the output limits you give it. You give it those limits.

B. D, and why it is usually off

The third term looks at how fast the measurement is changing, and pushes against that change. Not against the error, against the rate.

contribution to output = −Kp × Td × (rate of change of measurement)

Think of it as anticipation. If the temperature is rising fast, the derivative term turns the heater down early. Because it can see that the temperature is about to reach the setpoint. On a process with a delay in it, this really helps.

Dead time: the command has left, and nothing has happened yetheater commandjacket temperaturedead timeMeanwhile the controller keeps correcting an error it has already corrected.

The reason is dead time. On our jacket there are six seconds of pipe between the heaters and the vessel. You turn the heat up and for six seconds nothing happens. During that time the controller still sees the error. Yet it has already corrected it. It turns the heat up again and again. That extra heat is on its way, inside the pipe. Then it all arrives at once. That is where overshoot comes from. That is why raising the gain on a delayed process does not speed it up. It makes it worse.

Derivative helps here. Because the rate of the measurement changes before the error does. So derivative reacts before proportional does.

Three warnings, in order of how often they will catch you.

It amplifies noise. Rate of change is a difference divided by a short time. A small wobble on the measurement becomes a large wobble on the output. On a noisy signal, derivative is not a tuning option. It is a valve-wearing machine.

Take the derivative of the measurement, not of the error. If you take the derivative of the error, the moment somebody changes the setpoint the rate is briefly huge. The output slams into a limit. This is called derivative kick. The derivative of the measurement gives the same benefit without this problem. Because when the setpoint changes, the measurement has not changed yet. Most ready-made blocks use the derivative of the measurement. So does the block in this editor.

It does not help a loop that already works well. Add derivative to a well-damped PI loop and you usually make the result worse. Try it on the jacket and watch. A tuning with no overshoot at all overshoots by a couple of degrees when you add derivative.

Most loops in most plants run as PI, with Td at zero. And they are right to.

B. The block

You have now built by hand everything a PID block does. Here is the block. The PID box in this editor has these inputs:

PID   PV    the measurement, scaled, REAL
      SP    the setpoint
      KP    the gain
      TI    integral time, seconds
      TD    derivative time, seconds
      LO    output lower limit
      HI    output upper limit
      OUT   where the answer is written
      I     where the running total is kept

Three of these must be a place: PV, OUT and I. You connect a tag to each. The rest can be numbers you type or tags you can change while it runs.

TI is Ki turned upside down. The block calculates:

output = Kp × ( error + (1/Ti)∫error·dt − Td × d(PV)/dt )

So Kp multiplies everything. When you change the gain, the integral and derivative grow or shrink with it. The gain is what you change most often. In this form the loop stays balanced when you change it.

Read TI like this: the time the integral part takes to contribute as much as the proportional part already does. On a process with a 40-second time constant, a good Ti is usually between 20 and 40 s. Page eleven shows where to start. In this editor Ti = 0 switches the integral off entirely. Then you have the P controller from page seven.

LO and HI are not decoration. The windup protection works against these limits. Set them to the actuator's real travel. For a valve that is 0 and 100. If the valve really cannot go below 20 %, write 20 as the lower limit. A loop that thinks it can shut a valve that cannot shut winds up against the difference.

In this editor the box's EN input is a gate, not a switch. The box runs while its rung is true. When the rung goes false, the box does not run. So it does not turn anything off. It leaves the last answer in OUT. If the heater must go to zero when the process stops, write that on its own rung. Put that rung above the loop, with no conditions. When the loop is allowed to run, it writes over that value. The safe value comes first. The clever part has its say afterwards. On real blocks, what happens when the block is not running depends on the block and its settings. If you do not know, write the safe value yourself.

The block on real platforms:

  • In Siemens you use PID_Compact. PID_Compact is a technology object. Its settings live in its own data block. You call it from a cyclic interrupt OB. That keeps the sample time constant. Ti is in seconds. Its commissioning window can run pretuning and fine tuning.
  • In Rockwell there are two choices. In ladder you use the PID instruction. Its settings live in its own tag of type PID. In a Function Block routine there is the more advanced PIDE. You run either one in a periodic task. Watch the units. On the PID instruction, the integral and derivative units depend on the form of the equation you choose. In some forms the time is in minutes, not seconds.

The rule is the same on both platforms: put the PID somewhere that runs at a fixed interval. In the main continuous scan (OB1 in Siemens, the continuous task in Rockwell) the scan time varies. If the scan time varies, so does dt. In this editor the scan is always 10 ms. So here it makes no difference.

B. Tuning is a method

Tuning is not a matter of feel. It is a short experiment followed by two or three corrections. It is over in twenty minutes.

First, look at the plant. Put the loop in manual. Raise the output by 10 % and watch. You want two numbers from that curve. How much did the measurement change? That is the process gain. How long did 63 % of the change take? That is the time constant. If nothing happens for a while before the measurement starts to move, that wait is the dead time. It is the most important number on this page.

Then start gently, on purpose.

  • Kp: start small. On our jacket Kp 2 is a good start. A cold jacket has an error of 65 °C. With Kp 2 the loop asks for 130 %. So the output hits its limit at first. That is normal.
  • Ti: start a little below the process time constant. If the process is 40 seconds, make Ti 30 to 40. The loop will be slow, but safe.
  • Td: zero. Leave it there unless the dead time is a large part of the time constant. Do not touch derivative before the rest is right.

Then two moves, in this order.

Speed comes from Kp. Raise Kp until the response starts to overshoot or oscillate. Then take back a third. That is your gain.

How fast the offset closes comes from Ti. Shorten Ti until the approach starts to overshoot. Then back off a little. A short Ti pushes harder.

Watch the curve, not the number. A settled loop is a flat line. An oscillating loop is a wave with the same average. The number on a display cannot tell them apart. That is why every control room has a trend screen. It is why this panel has one too.

A good curve rises, bends over as it nears the top and lies down on the line. Perhaps it passes by a degree. After that nothing happens.

A bad curve reaches the setpoint fast and goes past. It comes back and goes past on the other side. A little less each time. This loop is stable but under-damped. Reduce the gain. If the swings are not getting smaller, stop. Put the loop in manual. Halve the gain before you do anything else.

Ready-made blocks also have automatic tuning. PID_Compact's pretuning and fine tuning do this. In Rockwell there is an autotuning tool for PIDE as well. You may use them. But you judge the result yourself, by looking at the curve as this page describes.

B. When not to use it

A PID block is not a decoration. Half the loops that use one do not need it.

If the actuator is on-off, you do not have a loop, you have a thermostat. A contactor cannot do 43 %. Switch on at one value and off at a higher one. Leave a gap between them. That way noise does not keep switching the contactor. This is called hysteresis. You built it in Chapter 5. The tank sits inside a band instead of on a line. For a simple supply tank, a cooling fan or a room heater, that band is enough.

The mistake is made the other way round too. A hysteresis controller's average is on the setpoint. But the value is never on the setpoint at any moment. If somebody downstream cares about every moment rather than the average, you need a loop.

If the process is fast and the tolerance is wide, do not bother. Nobody controls a hydraulic clamp with a PID.

If the measurement is bad, fix the measurement first. A loop on a signal that reads four degrees low keeps the vessel four degrees hot, with great confidence. No tuning fixes that. Nor does a filter. Nor a bigger block. Nor a meeting.

If one loop is fighting another, stop and draw it on paper. Two controllers on the same process oscillate against each other. So does a loop whose output disturbs another loop. This looks exactly like bad tuning, but it is not. The answer is cascade control. That is not the subject of this chapter.


The rest of the page is blank. Only near the bottom is there a single line in small, square handwriting. A coffee ring sits in the corner.

"The loop is the easy half of the job. The other half is deciding what you want from it. Nobody writes that in a book, because it changes every week."

C. The recipe card: arrays, data types and retained data

Writing a number into the program is easy. 85 °C, 60 seconds. While you make one product, that is enough. When the second product arrives, it is not.

Only a few numbers change between two products. The fill, the interlock, the trip and the drain stay the same. If you copy the program for each product, you will have to fix the next fault in three places. Usually you fix two. The right answer is a table.

First define a data type. Gather the fields that describe one product. In this hall the Recipe_Step type has two fields: Temp (REAL, °C) and Soak_Ms (DINT, milliseconds). The time is in milliseconds because a timer preset wants milliseconds.

  • In Siemens this is called a PLC data type.
  • In Rockwell it is called a User-Defined Data Type.

The idea is the same. On site both are called a UDT.

Then declare an array of that type. An array is an ordered list of records of the same type. You reach each record by its number (the index): Recipe[2].Temp. The two platforms differ here:

  • In Siemens you choose the bounds of the array: Array[1..3] of Recipe_Step. The products on the panel are called 1, 2 and 3, so the table can start at 1 too.
  • In Rockwell an array always starts at 0. If you want to number three products 1 to 3, you declare four elements and leave 0 unused. Or you number the products from 0 in the program.

Do not forget this difference when you move a program from one platform to the other. If you forget it, every product shifts by one.

An index can be fixed or variable. Recipe[1] is fixed. The compiler checks it before the program runs. Recipe[Active] is variable. Which record it reaches is known only while it runs. What happens if Active points outside the table?

  • In Siemens this is a programming error. On an S7-1500, if there is no OB to handle it (Programming error), the CPU goes to STOP.
  • In Rockwell it is a major fault. If no fault routine handles it, the controller stops.

The rule is the same on both platforms: check the index before you use it. If the value is checked before it is put to the table, the table never meets a question it cannot answer. Be careful: an INT is 0 at power-up. In an array that starts at 1, 0 is an invalid index.

Getting the table into memory. The first answer is to fill the table with a rung on the first scan. The first-scan bit is FirstScan in Siemens and S:FS in Rockwell. This works, but the recipe stays inside the program. To change a number, somebody comes with a laptop and downloads the program.

The better answer is to keep the recipe in a retained data area:

  • In Siemens you create a global data block (global DB) for it. You can derive the DB from a PLC data type. You mark the data to keep as Retain. If you do not mark it, it goes back to its start value after a power cut.
  • In Rockwell there is no DB. A controller-scoped tag of the UDT type does the same job. Logix tags keep their values by default.

A rung that writes to a retained table on every first scan, with no condition, is a serious mistake. After every power cut it writes the old numbers in the program over the values the operator entered. And nobody notices. Run the loader only while the table is empty. Use a field to tell that the table is empty. In this hall that field is Edition. If it is 0, the table has never been written.

One last warning. Retained data survives a power cut. Downloading the program again, however, can overwrite those values. Back up the recipe before you download.