Implementing PID control

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all,

 

I have been going through various documents and sources regarding PID in particular I have also read the AVR APP NOTE AVR221 for implementing the a Discrete PID control on Microcontroller. So far I have understood that the Proportional term simple looks at the error and multiplies it with a known factor. The Integral term looks at the sum of error(Set Point - Process Value) and multiplies it with a known factor and finally the Derivative term looks at the difference between the Current Process Value and Last Process Value and multiplies this with a known Factor. Ultimately we have a result in Measured Value Unit/Time unit. The control loop has to be called at a regular interval to achieve the goal. My question is that often the result of the PID is scaled by a factor, how is this factor calculated? Does it depend on the system being controlled or is there a method to calculate this? Besides this Since I want to translate the result of the PID control to position of a valve which is actuated using a DC motor and the position of the Valve is determined with a reflective sensor which provides pulses on motor rotation. What should I do with the result of PID to translate it into pulses? I understand I will have to convert the result into percentage and then to pulses but how exactly do I get the percentage from PID result? Besides this all I am planning to use Fixed point arithmetic to avoid the use of float and for this I will have to scale the PI and D terms with a factor, what should I consider while selecting the scale factor?

 

I haven't yet decided the controller which I will need for this application but in general I would like to understand the control scheme and details involved to be able to implement it regardless of the controller used. I will be measuring temperature which I will scale by *1000 to avoid floats again so lets say 22.5 Degree will be represented by 2250. This temperature will then be the input to the PID control loop. Will the other parameters of the PID control loop be affected by the scale factor that I am using for Temperature? I kn ow these are a lot of questions to ask but I am trying to grasp not only the theory but also the way that I can implement it using a microcontroller.

 

Thank you for your time and effort and I hope to get a response soon. Please feel free to ask more details.

Best regards,

Owais.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, you will first need to know what your controlling signal output will be, as in an analog output, such as 0-5volts, or +/- 12 volts or a digital output, say 0-1000.

Then you will need to know what your feedback signal will be, will it be analog or digital.  Once you know what your "real world" signals will be, then determine how they will relate

to each other, is it linear, then a simple mapping of the each range to the other will be needed, if non-linear then you will be using an y=mx+b slope equation and you will need to

calibrate this somehow.   Once you have this down, then you can design your PID parameters to keep the output in their expected ranges.    Just some pencil and paper work along

with verification once the hardware is in place.   There are some good PID design papers on the interweb, search for "pid without PHD" for one, ask questions as they come up (one at a time).

 

 

Jim

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Jim for your response. Well my control signal be through a GPIO to an H-Bridge which will ultimately control the direction of a DC motor. Now in order to reach here I will first measure the Temperature through an ADC most probably 10Bit wide which will give me a value between 0-1023 depending on the output of the temperature sensor which will be in volts. Now I will have this ADC value converted to Temperature and scaled so that I will have an integer value instead of a float. This Temperature will then be the Process Value for the PID loop which will then calculate a result based on the PID components and Error between Set Point and Process Value. This output from the PID will be in this case Degree/TimeUnit. For this system the output to the DC motor will be in number of pulses from the reflective sensor mounted on motor gear, so lets say I have to increase the temperature I will turn the DC motor in one direction while keeping track how much the motor has moved through the reflective sensor. I also know the upper and lower limits of the motor through calibration so I will have the maximum number of pulses which will occur if the motor moves the valve from one end to the other. Based on the output from the PID controller I will have to convert the result in percentage and then from percentage to number of pulses which I can then count once the motor starts to move. Now I need to figure out how the PID output can be converted to a percentage.

Best regards,

Owais.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How many loops per second do you expect? 50? 100? 200? I think using fp would be easier.... if it turns out to be too slow, then use Plan B. If you can do 60 fp mults or adds in a ms, then a 50 ms loop would allow hundreds of fp mults and adds. How many fp mults and adds do you need? A mult and an add for P, I and D? 6 fp ops? Lets call it 60. Thats a ms. Got that much spare in a 50 ms loop?

 

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The PID gain (gains, really) are adjusted based on the physics of the system.  That's all part of the tuning process.  Too little gain and your response is slower than it could be.  Too much gain and you'll get oscillations.  It's very interesting to feed an impulse into the system (e.g. bump up the set point temperature 20 degrees) and watch the PID output and also process temperature as the system tries to correct.  I've seen systems that were fielded for years that had the gains way off and oscillated (damped) like crazy, and nobody noticed. Will you be using integration (of the error over time) as well?

 

I think you're right to stick with integer math in the PID.  Just keep your scaling in mind at all times, and check the limits of validity - that is, at what points the math breaks due to overflow or underflow.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Like Bob

 

Which update speed do you need, if temperature are involved you normally don't need to run very fast.

The main thing you need to know is what is the step response of the system (and that can be everything from us to hours!)

 

Perhaps read some appnotes for some DSP's (integer and FP).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks all for sharing.

 

@Bob: I am really not sure at the moment at what rate I will have to call the control loop but what I am sure about is that it will be once or at most twice a minute maybe even once in a few minutes since it takes a while until there is a change in temperature corresponding to a change in control output. In this case I will be monitoring and controlling temperature of a closed space such as a room.

 

@kk6gm: Thanks for the useful insight. I understand that the gains will depend on the system but for a starting point what should be the scaling factor for all the gains and should it be equal for P,I and D or does it differ for all. Since as I mentioned that the temperature will be a multiple of 1000 so how does the scaling factor for the PID gains relate to this? Regarding the limits, I understand that I will have to check for the upper and lower bounds of the system and based on that I will have to clamp the PID terms individually or clamp the whole PID result so to keep the output of the controller in bounds.

 

@theusch: Thanks for the suggestions I will go through these and see what I can learn.

 

Thanks everyone for the useful comments. I will be back once I have more insight.

Best regards,

Owais.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

then just use FP

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The P, I and D gains should all be separate (it's very common for one or more of them to be 0).  You'll have to figure out a ballpark for the gains based on some simple math.  Suppose your temperature is off 5 degrees.  Then your error number is 5000.  How do you want to translate this 5000 raw error number into output?  What if you run the PID once per second?  10 times per second?  Once every 10 seconds?  What is/are the natural periods of your process?

 

EDIT - OK, I see that you mentioned running the PID once a minute or so.  So over a period of 1 minute, you need to convert the 5000 raw error value into numbers that are reasonable (that make your process adjusting valve, etc make a reasonable change for that 5 degree error, for 1 minute.  Now you've got an estimated nominal gain multiplier (which may be less than 1!).  You'd probably want to be able to change that nominal gain up to 10x greater, and up to 1/10x smaller, until you get a better idea of the real gain multiplier range you need

Last Edited: Sun. Apr 10, 2016 - 01:10 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Its like digital audio.... you need to measure faster than you are controlling. The time constant of the temperature sensors is in their datasheet. Lets say its 5 secs, so 5 timeconstants is 25 secs, so you update the output every 50 secs. You could use fp and the internal 1mHz oscillator and still have spare execution time.

 

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is the controller for a brand new system, or replacing the controller on a system that is already up and running?

 

If it is a replacement controller it might be worthwhile to put a data logger on the system and plot its current profile.

This gives you real world info on the system's response times, and gives yo a baseline against which you can compare your new & improved system.

 

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks again for the comments everyone.

 

@kk6gm: Excuse my limited understanding. So lets say I have an error of 5000 or 500 depending on the scaling factor used 1000 or 100 respectively. Now if I use a P Factor of 1 I get 5000 or 500 as an output from the controller which has to be converted to a meaningful value before sending it to the actuator. This is where I am confused how to do this. Lets say I have a valve which is being controlled by a motor and to close the loop I have an optical transducer connected to the motor gear to read the number of rotations. Now lets say I know the maximum number of rotations needed to completely close the valve which is x rotations and -x to completely open it for example 300 rotations in one direction completely opens the valve and in other directions completely closes it. Now I need to convert the output from the controller to number of rotations which is what I can't figure how.

 

@JC: The controller is for a new system but the system is similar to many existing systems like Home Heating Thermostats.

 

@bobgardner: I understand the timing for the measurement is not very strict and I can use FP as well but what if I simply scale all the terms with a known value before making the calculations would this be a good idea? In this case since the room temperature takes a while to start changing, therefore I think I can make the measurement once a minute. I will have to use the time difference between every measurement for the controller as well.

 

on another note, I have been reading about the fixed point method for handling fractional values. Please let me know if there is a good source for reading about using fixed point arithmetic.

 

Thanks.

Best regards,

Owais.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Scaling with integers is good if you dont need much scaling. Why fret about whether a multiply will overflow? Thats why John W. Forrester invented floating point at IBM in the 50s.

 

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

osyed wrote:

Thanks again for the comments everyone.

 

@kk6gm: Excuse my limited understanding. So lets say I have an error of 5000 or 500 depending on the scaling factor used 1000 or 100 respectively. Now if I use a P Factor of 1 I get 5000 or 500 as an output from the controller which has to be converted to a meaningful value before sending it to the actuator. This is where I am confused how to do this. Lets say I have a valve which is being controlled by a motor and to close the loop I have an optical transducer connected to the motor gear to read the number of rotations. Now lets say I know the maximum number of rotations needed to completely close the valve which is x rotations and -x to completely open it for example 300 rotations in one direction completely opens the valve and in other directions completely closes it. Now I need to convert the output from the controller to number of rotations which is what I can't figure how.


I think you need to be thinking not in terms of valve _position_ but in terms of valve _velocity_, which will change every time the PID loop runs.  Your error output will drive the valve actuator motor in the correct direction (depending on whether the error is <0 or >0), and at a rate proportional to the error magnitude - that's the P term.  The actual location of the valve at any moment is not what matters.  What matters is how quickly the valve adjusts depending on the error value, and the system time constant.  This is where your gains come in, starting with just the P gain.  The gain multiplier value will convert your error value into a valve velocity value.  As you should have seen in studying PID, you will get results like in the classic PID tuning map depending on the gain, and you will adjust accordingly to get the best response (the one in the middle box): http://www.yokogawa.com/in/techn...

Finally, if you find that your valve is always controlling at nearly full open, or nearly full closed, then you've got the wrong sized valve for your process.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@kk6gm: Thanks for the response again. You are right about the valve velocity instead of valve position but from my understanding since the system time constant is relatively large and it will take few minutes for the temperature to rise or fall therefore it is not necessary to move the valve with a fast pace because the function of the valve is only to control the volume of hot water entering the heater which will ultimately heat up the room so I believe if I just adjust the valve position depending on the error lets say if the error is of certain magnitude I adjust the valve to some percent open and likewise. Please let me know if I am missing something here.

Best regards,

Owais.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PID loop tuning, in theory, involves quite a lot of elaborate math.  As as been pointed out before, it depends on a lot of things.  It is often very much easier to experiment and see what happens (presuming your equipment can tolerate that) and then adjust the tuning to suit.  Fundamentally, it's a tradeoff between frequency response and stability.

 

Your final post is, very simply, a "Proportional" system.  The actual room temperature is X, the desired room temperature is Y, the error signal is the difference.  If the room is too hot, the valve closes off the heat, if too cold, the valve opens and admits more hot water.

 

You could, for example, just open the valve fully when the room is too cold, and close it when the room is too hot.  Test and act once every five minutes or so, and given a large room you should be fine.  You will want some hysteresis to prevent opening and closing every five minutes when you're close to the correct temperature.  This would imply a Proportional Constant of 1.  But you probably don't want that, you want the valve to gently change a little instead of going from fully on to fully off all the time  (NB - PWM motor drivers work as on-off devices).

 

Given that we're talking about heating a room here, I don't think either Integral or Derivative (the 'ID' of 'PID') terms are really called for.  I believe the valve will actuate for a few seconds per five minutes or so, and oscillation at 0.003Hz is probably tolerable.  Let me know if I'm wrong about this.

 

To start the analysis, we first need to look at limits.  How fast does the room heat up with the valve fully open?  How fast does the room cool down with the valve fully closed?  These may be difficult to calculate and/or measure partly because there are dozens of other independent variables (how hot is it outside?  Has someone opened a door?).

 

Then we can look at drives.  How fast does the valve open?  How fast does it close? 

 

Then we need to look at the inputs.  What is the maximum range?  Where are the limits?  What should this device do when the inputs are out of range?

 

Then we look at the outputs.  What is their maximum range?  Where are their limits? 

 

Et cetera, and you need to put numbers to all of these before we can begin to help you calculate just the proportional term of the error signal equation. This is why measuring the system and adjusting the gains based on real-world experience is much easier.

 

For a much more simple design, allow me to suggest the following:  (all the rest o' you pundits feel free to criticize  ;-)

 

Pseudocode:

Prepare two timers, one that triggers every five minutes, and one that triggers every second.

Every five minutes:

 If the room is 0-5degC too cold, operate the 'open valve' for one second.

 If the room is 5-10degC too cold, operate the 'open valve' for two seconds.

 If the room is more than 10degC too cold operate the 'open valve' for five seconds

 

 If the room is 0-5degC too warm, operate the 'close valve' for one second.

And so on.

 

 

Obviously you may rearrange those numbers as you see fit - It doesn't have to be five minutes, it doesn't have to be 1,2,5 seconds, it doesn't have to be those degrees C (or F, or K, or R).  Does the valve fully open in five seconds?  If so, you might cut all the times in half.  Personally, I'd stick to integer math, but I like writing AVR code in assembler, and floating-point math in assembler isn't much fun.

 

Technically, that is a proportional control loop, albeit a highly crude one without Integral and Derivative terms.

 

TLDR version:  For a room heater control, you don't need a PID loop.  Simplify.

S.