CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Implementing PID on Temperature Controller

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lloydi12345



Joined: 23 Aug 2011
Posts: 5

View user's profile Send private message

Implementing PID on Temperature Controller
PostPosted: Tue Aug 23, 2011 8:06 pm     Reply with quote

Hi CCS people. It is my first time to post here. I have a project which is a temperature controller and I would like to apply the principle of PID. It's a fryer and it is tasked to follow temperature settings for its appropriate time settings. I have read a lot of PID threads here and able to get along what PID really is. One of the threads I've read is this one: http://www.ccsinfo.com/forum/viewtopic.php?p=58275 I would like to apply PI only without D(I don't know if it's okay). I'm having problems about this code:

Code:
my_error = desired_temp - actual_temp
output = Kp * my_error  + Ki * sum;
sum += my_error;


Questions:
1. What will happen if my_error turns negative? Should I retain the sign or hide it in a variable and just use the positive value for the equation?

2. I am using an ON and OFF routine. Since a relay is used, I can't use PWM. I would like to check the "output" and parse it. For example:

Code:
if (ouput < 10)
seconds_on = 15
seconds_off = 5

if (ouput < 20)
seconds_on = 10
seconds_off = 10

if (ouput < 30)
seconds_on = 5
seconds_off = 15


So the total is 20 sec. Will this resolution be already adequate for my application or should I minimize it to 10 sec. Which is better?

Thanks. Smile
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Aug 23, 2011 8:36 pm     Reply with quote

1) Errors can be positive or negative. There is nothing odd about a negative error. Make very sure 'sum' can not roll over from very big positive to very big negative or the opposite. That could lead to a very dangerous error!

2) The time scale depends on your fryer and how close you want to regulate the temperature. Consider the fryer full on for 20 seconds starting slightly too cold. Will it be too hot by the time 20 seconds is over? If so 20 seconds is too long.
_________________
The search for better is endless. Instead simply find very good and get the job done.


Last edited by SherpaDoug on Wed Aug 24, 2011 5:52 am; edited 1 time in total
lloydi12345



Joined: 23 Aug 2011
Posts: 5

View user's profile Send private message

PostPosted: Tue Aug 23, 2011 10:23 pm     Reply with quote

Thanks SherpaDoug for the reply. I see. I get confuse with the output. Is the output not incremented to its previous value? like:

Code:
output = output + Kp * my_error  + Ki * sum;


Also, how will I know that the output value is the value that needs a lot of heating time? Is it the highest one or the lowest one? I don't really get it my bad.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Aug 24, 2011 5:57 am     Reply with quote

If Ki and Kp are scaled right then output will be the duty cycle or the power required. Higher output = more power needed. If the error is negative then the output will start to drop, as it should if things get too hot.
_________________
The search for better is endless. Instead simply find very good and get the job done.
lloydi12345



Joined: 23 Aug 2011
Posts: 5

View user's profile Send private message

PostPosted: Wed Aug 24, 2011 10:09 pm     Reply with quote

I see. How about the sum? What should be the value of the max positive and the max negative? What will I do if it reaches those values? Should I reset them to 0?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Aug 25, 2011 7:27 am     Reply with quote

Over time the sum should average to zero as positive and negative errors are added. In PID loops it is common to limit integrator "windup" for step changes in input by limiting the range of the integrator term.

http://en.wikipedia.org/wiki/Integral_windup
_________________
The search for better is endless. Instead simply find very good and get the job done.
lloydi12345



Joined: 23 Aug 2011
Posts: 5

View user's profile Send private message

PostPosted: Thu Aug 25, 2011 11:55 pm     Reply with quote

SherpaDoug wrote:
Over time the sum should average to zero as positive and negative errors are added. In PID loops it is common to limit integrator "windup" for step changes in input by limiting the range of the integrator term.

http://en.wikipedia.org/wiki/Integral_windup


Thank you, I learned a lot :D
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Fri Aug 26, 2011 5:43 pm     Reply with quote

Last year I built a PI controller for the heat plate on the bottom of the hummingbird feeder (quite a few stay around the Seattle area even when it is well below freezing). The heater kicks on when the ambient air temp gets below 34 deg F and off when the ambient air temp goes above 40 deg. F. The proportional controller has a couple of extra features (I use a 2.5 second cycle time on the heater and change the "on" time for the heat to maintain the heat plate at 70 degrees. One of the things I did since I was using remote sensing of the temperatures is use a measurement "window" where I turn the current to the heater off at the end of the cycle, measure the temps then turn the heat back on for the amount needed (even with separate grounds for the sensors, I was getting some error due to current). Below is a portion of the PI code I created that seems to work fine (the settings.variable are things I had stored in eeprom for getting the response the way I wanted it) - note this is NOT the complete program but does show the PI portion of the code:

(here is a link I found quite helpful in understanding it all:
http://igor.chudov.com/manuals/Servo-Tuning/PID-without-a-PhD.pdf )
also, the June 2006 Circuit Cellar had an article called "Home-brewed HERMS" that was about using PID for controlling home brewing stuff you would probably find helpful.

Here is a picture of it in "action" ...


Code:

// ********************************************************************* //
// new code block for implementing the PI (Proportional Integrating)     //
// controller.  Add code below here                                      //
// ********************************************************************* //
         
// First, calculate target temperature for heater based on ambient air temp
// and the specified setpoint for the heater.  Initially, just leave the
// correction factor as 0, but this allows us to add to it later after we
// have some experience on what is needed.  The correction factor "K_amb"
// is expressed as a percentage -- 50 would add 50% of the difference between
// the heater set point and the ambient air temperature.  Probably should put
// a limit on this equation so the Target can not go over 90 deg. F or some
// such number.

Amb_Adj = ((settings.Tset_HTR - AIR_avg) * settings.K_amb)/100; // adj for amb air

// now calculate the target temperature for heater - raise the target temp as
// the ambient air gets colder by way of the "Amb_Adj" calculation.
// Make sure the calculated target temp (in mv) does not exceed the maximum

Target = settings.Tset_HTR + Amb_Adj; // target= setpoint adj for amb air temp
if (Target > HTR_MAX) Target = HTR_MAX ; // limit max target temp (mv)

// calculate the error between current heater temp and current Target calc.
T_err = Target - HTR_raw;        // error is in mv
Tot_Err = Tot_Err + T_err;       // running total for the integrator
if( Tot_Err < 0) Tot_Err = 0;    // limits on Tot_Err
if( Tot_Err > Max_Err ) Tot_Err = Max_Err; // upper limit



// =============================================================
// Main calculations - calculate the PID portions of the control
// =============================================================

      // Proportional part - divide result by 10 because P_gain specified in
      // settings multiplied by 10 to allow fractional numbers e.g. 25 = 2.5
P_part = (T_err * settings.P_gain)/10;
      // Integral part -- just use 0 for now
I_part = Tot_Err / settings.I_gain;
      // Differential part -- just use 0 for now
D_part = 0;
      // now calculate percent power needed to controller:
Power_PCT = P_part + I_part + D_part;     
if(Power_PCT > 100) Power_PCT = 100;  // and limit to 100 % power possible
if(Power_PCT < 0)   Power_PCT = 0;    // make sure it does not go negative

// =============================================================
// Calculate pwidth as a percentage of TicksPerToggle from
// Power_PCT.  This allows us to change the TicksPerToggle and
// still have the percentage calculation work for the power setting
// Pwidth is used in the ISR to reload the width counter to determine
// the duty cycle on time for the heater.
// =============================================================

Pwidth = (TicksPerToggle * Power_PCT) / 100; // divide by 100 for pct.

//
// ************************************************************* //
// End of PI portion of controller code                          //
// ************************************************************* //

_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
lloydi12345



Joined: 23 Aug 2011
Posts: 5

View user's profile Send private message

PostPosted: Tue Aug 30, 2011 5:55 pm     Reply with quote

These codes are really helpful gpsmikey thanks. I already found the article you've recommended me last time but didn't really understood them all. So I'll give this one again another try and see if this time I'll be able to understand them. Thanks again.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Aug 30, 2011 6:30 pm     Reply with quote

I've done PID controls for motion apps several times in recent years, and
I did a project for a fast food commercial 2 gallon tank coffee brewer , demand heater last year.

Actually with a safety cutout on the high side of the demand heater control TRIAC drive, and a pair of LM35's doing failsafe sensing for each other , a well tuned PID algorithm tested out no better at all than a rudimentary "click bang", very ordinary set point method.
The simple set point method with a built in 2 degrees of hysteresis
was able to reach the desired temperature faster actually.

Is s small amount of overshoot really an issue with this app?

I think the key to good temp regulation is well coupled temp sensors actually.

In the case of something like a fat fryer - so long as there is a PHYSICAL over temp sensor ( say NC bimetal ) I feel you will be hard pressed to show any performance improvement no matter how hard you push a PID approach.

If simply learning to do a PID control?
Thats another kettle of fried fish;-))


The ONLY situation where think you see PID being an advantage is to paper over and compensate for unusually poor coupling of your sensors to the bucket of fat ;-))
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group