# How to find Kp, Kd for MCU PID control ?...

Hello,
I have just coded a microcontroller (PIC) based PI controller for a constant temperature water bath. The Kp and Kd values will be written
into the code. So there is no way of feeding it in real-time (as of now... later on I shall incorporate a calibration module into the controller).
The bath holds around 15 litres of water (50x30x10cm) and is adequately insulated. It has an 'immersion type' 1500W heater (220VAC). I need to tune the controller to optimum settings. The scheme for the PI is at the end of this post.
Lets say, I put in 15L water in it stable 30C. Without any controls, switch on the power to the heater at 20% power, so I feed in 300W of power. Start recording the water temperature rise at every minute till it stabilises at some value. Now from the plot how do I calculate the Kp, Kd values? I have loads of material and mathematics on PID, but how to actually do so... Please suggest a method for Kp, Kd considering this scheme... _________________________________________________________________
ProH:Pro = Error_Hi:Error_Lo * Kp DifH:Dif = (Error_P_Hi:Error_P_Lo - Error_Hi:Error_Lo) * Kd IntH:Int = IntH:Int + Error_Hi:Error_Lo * Ki TotH:Tot = IntH:Int + ProH:Pro + DifH:Dif
Duty Cyle = TotH:Tot * Constant
Duty Cyle Heater ON = ------------- Time Period _________________________________________________________________
(I've shown the DifH:Dif here although I force it to zero in my actual code.)
Also, suggest how to come up with a constant for the duty cycle and the time period.
Thanks, Mohit.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Sorry!!
Referring to earlier post, I meant Kp and Ki, not Kp, Kd.
Mohit.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Mohit,
How to calculate tuning constant is the most frequently asked question on this NG. The answer is always the same: They are called tuning constants because you tune them; you don't calculate them. The controller and its tuning constants effectively roll up all the uncertainties of the system into one point which you then adjust. If you had sufficiently accurate information to precalculate all the tuning constants you wouldn't need feedback control. You would simply program an on/off cycle into your controller and it would automatically provide the temperature you want.
So your idea of burying the tuning constants inside the program is fundamentally flawed. The only way you can get away with this is if you tuned an identical prototype and then packaged the entire system.
On the other hand, a heating system is one of the easiest systems to control. The controller for my entire house consists of a single on/off temperature switch with a tiny heating element in it to provide derivative control. (That helps to eliminate overshoot.) In your case, you have an integrating system with rate of rise of 42 seconds / degree C. An ideal integrator can be controlled by a controller with infinite gain, i.e. an on/off controller with a very tight deadband. In practice, the tighter the deadband (another form of controller gain) the narrower the temperature excursions. In your case, a deadband of 1 deg C will result in an ON cycle of 42 seconds. The OFF cycle time depends on your insulation. It will probably be a lot longer. What this all boils down to is this: you don't need a PI controller at all! Turn the heater ON when you are below setpoint, turn it OFF when you are below. That's how my house heater works, my water heater, and my toilet tank (not a heater, but another integrating system).
Your most important variable is the location of your sensor within the water bath. A location too far from the heat input will result in temperature cycling NO MATTER WHAT KIND OF CONTROL SYSTEM YOU USE. No system on earth (or in heaven) can prevent it. A sensor too close will be forever keeping a small point on setpoint and neglect the rest of the system. Having good mixing will make everything work better. In a simple case like yours, that would be to make sure your heat source is evenly distributed across the tank, not just in one corner. The best arrangement is to have the heater as an external blanket surrounding the entire tank, and a good mixer inside the tank. In that case it wouldn't matter where you put the sensor.
Note, this scenario would be entirely different if you where trying to inline heat a continuously flowing stream. Then PID control and its tuning would become relevant.
Walter

Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>

In the past I believed that. Then I started to calulate feed forwards (bias). Now I CALUCLATE gains. I tune the system only to compare calculated with the tuned results. I admit that the tuned ISE ( integrator squared error ) is smaller than the calculated ISE, but the the tuned values usually violate phase margin requirements. I also use the ISE to compare the results between actual and estimated positions.

The feedback control is for things that are not modeled. To expect the model to be perfect is unrealistic. However, a good model can get one within few percent of the SP without the feedback. Too many on this forum think that because the model is not perfect that it shouldn't be used. I disagree strongly. On a motion control system, the velocity and acceleration feed forwards ( bias ) is usually calculated empirically and does not change. Even if the feed forwards are not accurate the resulting motion with feed forwads will have a much lower ISE that using just a PID.

I agree here. Temperature control in this case is easy and a PI is over kill. Sliding mode control is much simpler. The sliding mode control will perform better if the sensor time constant, the lag between the actual and measured, is known.

It is easy to calculate the gains and time constants on a system like this. From these one can calculate the PID gains. There are companies that like Bestune and Expertune that make programs that do this. I don't know how well they work. I do know how well I can calculate the gains on our system systems.

the
cycle
works,
water
I agree, but you can keep the cycles small. Sliding mode control is results in small cycles even in perfect spreadsheet models. Hopefully the cycles are very small.

that
as
the
tuning
Mohit Venkat, take heed of the Walter's two paragraphs above on mixing and sensor placement. Look up least squares system identification. This will allow you to calculate the gains and time constants. Next look up pole placement. This is not an easy journey. It is easier to buy the programs that calculate the gains. Mohit Venkat's system does sound like an ideal system to learn how to calculate PID parameters.
Peter Nachtwey
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Peter,
Once again, our conclusions are relevant to a given industry. Peter's comments apply to mechanical systems, especially motion control. My comments apply to process control. There are several differences:
1 - In process control you do not have a very good model at all. For one thing, you are not permitted to test the process to determine its characteristics nor do you ever have a prototype. The closest thing is a very expensive process simulator. These are rarely applied and then only in the most complex systems.
2 - In process control the constants change all the time. The process configuration changes, i.e. valves open and close, equipment is put into service and bypassed. Next, operating conditions change with the weather. Finally characteristics change with time as fouling, catalyst degradation, etc. take their toll.
3 - Chemical and thermodynamic processes are highly non-linear so that the 'best' tuning constants change with operating point. Part of the tuning process is to make sure you are tuning at an actual operating point, not a presumed one. Another part is to make certain that you are stable over the entire operating range, not just the nominal operating point.
The 'process' given, i.e. a heater, is so simple that almost any method would work as long as you have good measurement and high gain.
Walter.

shouldn't
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>

in
Mohit only wants to be able to calculate the gains on a very simple system. To tell Mohit that he can only tune, and not calculate, the PID gains is not right.

We agree here.

Peter Nachtwey
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Your last statement rather sums up the problem. There is no single method which works for this. Your pile of mathematics will point to several methods. To make things slightly worse, there are a couple of different formulations for PID controls. Unfortunately, two incompatible half methods don't make for a full method. Your other mail mentioned that you want to use Ki instead of Kd. I would probably look at a full PID control in this case, as the d and i elements allow for a cycle.
The Kp, Ki, and Kd depend partly on how you would like your system to behave. If you would like the bath to rapidly attain temperature, even if it means overshooting, then you can use a large Kp, and compensate with Ki and Kd. Likewise if you want a slow acting system, then using a smaller Kd, might get you to the setpoint a bit slower, but would be more steady. Adding Kd would let the controller slow down its response if you were coming up too fast, and get started on correcting minor errors before the integral controller could see them. It helps the resonse if there is a system lag.
Your plot of temperature versus time is a great place to start. Basically this is a step response test. A first pass is to simply note that there is an equilibrium temperature which you will achieve at 300 W input
I basically swiped this wholesale from Process Control, by Peter Harriot. You will notice a lag time between the start of power and a large increase in the slope of the temperature. Denote this time as L. Take the maximum slop of the temperature curve, denote this as N. The full step resonse T is going to be the final temperature at 300 W less the 30C initial temperature.
For Proportional control Kc = T/(12*N*L)
For PI control
Kc = 0.9T/(12*N*L)
Tr = L/0.3
For PID control
KC = 1.2T/(12*N*L)
Tr = L/0.5
Td = 0.5L
This is from the Ziegler Nichols method. They use the integral and reset times, rather than the integral and reset gains. Knowing your poll time could translate those into gains and filter times. A warning is that this method is for quarter wave damping. There may be other methods which work much better, such as lambda tuning.
As for the duty cycle. I would suggest that after you equilibrate at 300W, that you turn the power off, and record the temperature decrease. Determine how many watt seconds of heat are lost per minute per degree above ambient. This will give you the watt seconds per minute you must deliver to the unit at any given temperature to maintain a constant temperature. Figure the duty cycle from that. Based upon the loss rate, and the lag time, you will have to decide if you want to have a lot of short pulses or a few long ones, or just a constant heat input.
After you do all this, run your system a few times and test it. You are likely to find that a little tweaking on the controls might make things work better for your purposes.
Michael

Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Watt seconds per minute? Shame on you!! Watt's wrong with watts? :-)
Bruce.
Herman Family wrote:

Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
Hello Michael, Walter and the group,
The system for which the controller I've designed is, admittedly, a very basic one - PID control of water temperature in a simple, insulated tank with an immersion heater.
I'm actually trying to teach myself process control and starting with temperature control. Therefore, such a basic setup. Further, I'm also teaching myself microcontroller coding (PIC). So again, the PIC setup I have is pretty basic. I've had a tough time coding PIC (taking time out from my regular work, etc. etc.), so I've not yet been able to incorporate a real-time calibration and control-values-input module into the PIC. I'll do so a little later.
The PIC I'll use will be programmed with a set of Kp,Ki. I'll run the setup and note its performance. Then, code a new set of Kp,Ki, program it into the PIC, run again, note performance... What I'd like to see is how the system performs, and finally how to get a nicely tuned PID controller. Sort of a hit-trial-error method.
This is the whole point. Definitely ON/OFF is enough for controlling the temperature here reasonably well, and that PID is overkill. But experience gained (big words) from this easy 'experiment' can then help me in other applications where using PID control makes sense.
Finally, regarding the reason why I asked the newsgroup how to find Kp, Ki for tuning the system...
Refering to the code I gave in my first post, Kp, Kd or Ki could be anything... 2 or 4 or 100 (only limited by 16 bit arithmetic I'm using) or could be 0.5 or 0.25. This is what was confusing me. I could have programmed the PIC using any of the above values and checked it out. But that would have taken a lot of time. Using suggestions from the group I can have a narrower range of values to try. Like, having calculated a loose set of values from the step-response test, I find that Kp>>Ki, vice-versa or Kp ~= Ki etc.
I have a lot of material on controls, PID tuning, that's how I came up with the step-reponse-test. But I still needed a bit more help.
I think that post from Michael was what I had hoped for. Although there still is some clarfications needed for it (how do I translate integral/reset times into respective gains).
Thanks everybody, Mohit.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>

I think the temperature control loop might be too simple be a good educational tool for PID. It's a special case. Something that definitely would add a bit more reality would be a continuously flowing stream. If you made the bath a lot smaller and used a small pump to circulate water through the bath and then through a car rad with a fan you would have a system with enough complexity to make it interesting.. Put your sensor at the outlet of the bath. A PID control would definitely be relevant in this application.
1 - Your total on/off pulse width would be based on the residence time in the tank. Obviously you would have to complete at least a few cycles within one residence time.
2 - Proportional gain would be inversely dependant on the process gain.
3 - Integral would be related to the amount of heat lost through the walls.
4 - Derivative would be related to the total heat capacity of the tank.
A good book dealing with the practical aspects of process control is F G Shinsky's "Process Control Systems". It provides just enough math that should be able to make reasonable estimates of the gain constants.
It should be possible to make a reasonably accurate model of the system I described above. The primary parameters should be reasonably constant so you wouldn't be chasing a moving target. And .. it doesn't have any valves! To see how your system responds to changes, move the fan or pinch down on the circulation hose.
Walter.
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>
And instead of reprogramming the PIC for every Kx or whatever, I'd just hang some pots on the box, let the PIC read the pots and adjust itself accordingly, and bitbang a continuous stream of status data out on one pin with an RS232 driver.
-Mike-
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>

You should be able to get by representing Gain, Time constants and temperatures using a fixed point format of 8,8 where the high byte is the integer and the low byte is the fraction of an integer. 8080H would represent 128.5. The time constants should be expressed in minutes since this numbering scheme will only handle numbers up to 255 and you could have a time constant over 255 seconds. You will need to have a 16X16 to 32 bit multiply and a 32 /16 bit = 16 bit result divide. Better yet, ignore what I just said and use a floating point package. You only need to update about every second so you have PLENTY of time to do it the easy way.
I like Walter's radiator idea. This will change the effective gain of the system. Adding or subtracting from the volume of water will change the response ( time constant ) of the system.
One more hint. Learn how to use Excels LINEST function. Then become an expert at using the LINEST function. The LINEST function is far more powerful than it appears on the surface. Any more hints will ruin your fun.
Peter Nachtwey
Add pictures here
<% if( /^image/.test(type) ){ %>
<% } %>
<%-name%>

## Site Timeline

• Share To

Polytechforum.com is a website by engineers for engineers. It is not affiliated with any of manufacturers or vendors discussed here. All logos and trade names are the property of their respective owners.