Kegconnection Complete Starter Kit and More Giveaway!


Home Brew Forums > Home Brewing Beer > Automated Brewing Forum > PID settings on arduino based hlt

Reply
 
LinkBack Thread Tools
Old 06-07-2012, 11:20 PM   #31
matho
Feedback Score: 0 reviews
Recipes 
 
Join Date: Jan 2012
Location: hazelbrook, New South Wales
Posts: 42
Liked 16 Times on 13 Posts
Likes Given: 6

Default

Hey fastTalker,
I never said that PID control isn't suited for heating water, I said I don't think the arduino PID library is very well suited for heating water.
here are my thoughts on the library, I could be wrong.

With the default sample time set to

Code:
    SampleTime = 100;
that would mean using a DS18B20 with a 12 bit resolution the conversion time is about 750mS the Iterm would have 7 increases before D could even get calculated, it gets worse when you put in the time it takes to ramp the temperature of the water. If the rate of rise is 2 deg c / min then that equates to 0.033 deg c / second, the finest resolution for a DS18B20 is 0.0625 degc so that means a measured temperature change every 2 seconds which would allow Iterm to increase 20 times before D would have a value

Code:
void PID::Compute()
{
   if(!inAuto) return;
   unsigned long now = millis();
   unsigned long timeChange = (now - lastTime);
   if(timeChange>=SampleTime)
   {
      /*Compute all the working error variables*/
	  double input = *myInput;
      double error = *mySetpoint - input;
      ITerm+= (ki * error);
      if(ITerm > outMax) ITerm= outMax;
      else if(ITerm < outMin) ITerm= outMin;
      double dInput = (input - lastInput);
 
      /*Compute PID Output*/
      double output = kp * error + ITerm- kd * dInput;
      
	  if(output > outMax) output = outMax;
      else if(output < outMin) output = outMin;
	  *myOutput = output;
	  
      /*Remember some variables for next time*/
      lastInput = input;
      lastTime = now;
   }
}
If I term is there to remove a constant error that persists with a P drive only, which it is, why is it allowed to ramp to 100% of the output? If you had a constant error that required 100% output then your drive system is too small. I believe that for D to really be effective it should be subtracted from I term like this

Code:
ITerm-=(Kd*dInput);
double output = kp * error + ITerm;
that would allow D to stop Iterm from winding up if the rate of change is large enough.

Code:
void PID::SetTunings(double Kp, double Ki, double Kd)
{
   if (Kp<0 || Ki<0 || Kd<0) return;
 
   dispKp = Kp; dispKi = Ki; dispKd = Kd;
   
   double SampleTimeInSec = ((double)SampleTime)/1000;  
   kp = Kp;
   ki = Ki * SampleTimeInSec;
   kd = Kd / SampleTimeInSec;
 
  if(controllerDirection ==REVERSE)
   {
      kp = (0 - kp);
      ki = (0 - ki);
      kd = (0 - kd);
   }
}
The last thing is if you increase the sample time the D gain is reduced and the I gain is increased.
I believe that the PID library would be well suited for fast responding symmetrical drive systems like motors but for heating water it isn't quite right but can be worked around.
Again, these are just my thoughts and I could be wrong as I'm not an expert on PID systems

cheers steve
__________________
matho is offline
 
Reply With Quote Quick reply to this message
Old 07-17-2012, 07:46 AM   #32
Blauhung
HBT_SUPPORTER.png
Feedback Score: 0 reviews
Recipes 
 
Join Date: Jan 2012
Location: Aloha, OR
Posts: 54
Liked 1 Times on 1 Posts
Likes Given: 1

Default

Just caught this thread and wanted to chime in. I haven't yet messed with it but I did find this in line 26 the PID_v1.cpp file of the PID library

Code:
SampleTime = 100;	//default Controller Sample Time is 0.1 seconds
Since for heating large volumes of water there is massively small changes in the I and D calculation over 0.1 seconds of time the Ki and Kd terms need to be high in order to show any effect. For our situation, it might be advisable to change that to 10,000 or at least mess with other orders of magnitudes to get Ki and Kd in line with the process.

edit: and looks like i read through everything except for the last page and Matho was already on the same path.... but again, since the SampleTime term only plays with how Ki and Kd interact with the setpoint, a longer sample time could make the PID library much more effective for our application
__________________
Blauhung is offline
 
Reply With Quote Quick reply to this message
Old 07-17-2012, 12:07 PM   #33
Adeering
Feedback Score: 0 reviews
Recipes 
 
Join Date: Jan 2012
Location: Sherwood, Arkansas
Posts: 663
Liked 45 Times on 37 Posts
Likes Given: 4

Default

Quote:
Originally Posted by matho View Post
Hey fastTalker,
I never said that PID control isn't suited for heating water, I said I don't think the arduino PID library is very well suited for heating water.
here are my thoughts on the library, I could be wrong.

With the default sample time set to
Code:
    SampleTime = 100;
Code:
void PID::Compute()
{
   if(!inAuto) return;
   unsigned long now = millis();
   unsigned long timeChange = (now - lastTime);
   if(timeChange>=SampleTime)
   {
      /*Compute all the working error variables*/
	  double input = *myInput;
      double error = *mySetpoint - input;
      ITerm+= (ki * error);
      if(ITerm > outMax) ITerm= outMax;
      else if(ITerm < outMin) ITerm= outMin;
      double dInput = (input - lastInput);
 
      /*Compute PID Output*/
      double output = kp * error + ITerm- kd * dInput;
      
	  if(output > outMax) output = outMax;
      else if(output < outMin) output = outMin;
	  *myOutput = output;
	  
      /*Remember some variables for next time*/
      lastInput = input;
      lastTime = now;
   }
}
If I term is there to remove a constant error that persists with a P drive only, which it is, why is it allowed to ramp to 100% of the output? If you had a constant error that required 100% output then your drive system is too small. I believe that for D to really be effective it should be subtracted from I term like this

Code:
ITerm-=(Kd*dInput);
double output = kp * error + ITerm;
cheers steve
Ok First im by no means an expert on PIDs as I recently just started learning, but there are a few things about your statements that are off.

First with the setpoint, that is the default setpoint, the library has a function to change it, which when libraries include functions like that, its because they can/should be changed to meet your needs. obviously this guy wasnt making this for brewing when he made this so his default isnt going to be what we need. To fix this call:
Code:
myPID.setSampleTime(5000)
and put in what you want for the delay, im going to initially test this with a 5 second delay as not much changes over 5 seconds with 5+gallons of water, you could probably do 10 and be fine.

Second your second point with the I input isnt correct, not the PID part but how he does it. If you compare your code

Code:
ITerm-=(Kd*dInput);
double output = kp * error + ITerm;
with the one provided

Code:
ITerm+= (ki * error);
      if(ITerm > outMax) ITerm= outMax;
      else if(ITerm < outMin) ITerm= outMin;
      double dInput = (input - lastInput);
 
      /*Compute PID Output*/
      double output = kp * error + ITerm- kd * dInput;
You see that the kd*dInput is included further down.

Again Im not an expert on PIDs and know little on them, however, I do know code very well.

A library is a base for the general use of the feature. It is not all powerful and cant be designed for every case. PIDs are used for tons of different situations so it would be impossible to make a library that requires no changes to work for everything. Tons of people have been using this PID for sous vide applications and have gotten great results, Im pretty sure it works for mashing as well.
__________________
Primary: None
Secondary: None
Bottle Conditioning/Carbonating: None
Kegged: Aphrodite's Potion
Bottled: None
Next Brews: Apple Pie, Pumpkin Ale, Unknown
Adeering is offline
 
Reply With Quote Quick reply to this message
Old 07-17-2012, 05:17 PM   #34
carlisle_bob
Feedback Score: 0 reviews
Recipes 
 
Join Date: Apr 2012
Location: Carlisle, PA
Posts: 1,205
Liked 27 Times on 26 Posts

Default

Hi

Be very carefull with the term PID. Around this froum it gets used to describe *any* digital process control. When you look into what is actually being done, well over 90% of the controlls are setpoint rather than PID.

To be very clear, A setpoint is a PID with no I =0 and D=0. To be properly called a PID both terms need to be non-zero. You can have PI controllers (pretty common), or PD controllers (pretty rare) as well as PID's and set points.

Bob

__________________
carlisle_bob is offline
 
Reply With Quote Quick reply to this message
Old 07-18-2012, 05:06 PM   #35
cwi
Registered User
Feedback Score: 0 reviews
Recipes 
 
Join Date: Jun 2010
Location: Austin
Posts: 845
Liked 30 Times on 27 Posts
Likes Given: 1

Default

Quote:
Originally Posted by carlisle_bob View Post
Hi

Be very carefull with the term PID. Around this froum it gets used to describe *any* digital process control. When you look into what is actually being done, well over 90% of the controlls are setpoint rather than PID.

To be very clear, A setpoint is a PID with no I =0 and D=0. To be properly called a PID both terms need to be non-zero. You can have PI controllers (pretty common), or PD controllers (pretty rare) as well as PID's and set points.

Bob
Be very careful reading threads. Most ones on the subject of a PID library, well over 90%, are actually talking about software. In fact, to be called a PID library, the code would actually have to be software.

These guys are just trying to determine how to hack a PID library to make it work for homebrewing.
__________________
cwi is offline
 
Reply With Quote Quick reply to this message
Old 07-19-2012, 12:20 AM   #36
carlisle_bob
Feedback Score: 0 reviews
Recipes 
 
Join Date: Apr 2012
Location: Carlisle, PA
Posts: 1,205
Liked 27 Times on 26 Posts

Default

Quote:
Originally Posted by cwi View Post
Be very careful reading threads. Most ones on the subject of a PID library, well over 90%, are actually talking about software. In fact, to be called a PID library, the code would actually have to be software.

These guys are just trying to determine how to hack a PID library to make it work for homebrewing.
Hi

If one is trying to make sense out of a control system, it might be a good idea to understand the nomenclature involved before digging into it. By far the majority of people around here are *not* actually using a PID. That's not going to make things any easier to understand.

Bob
__________________
carlisle_bob is offline
 
Reply With Quote Quick reply to this message
Old 07-19-2012, 01:18 AM   #37
cwi
Registered User
Feedback Score: 0 reviews
Recipes 
 
Join Date: Jun 2010
Location: Austin
Posts: 845
Liked 30 Times on 27 Posts
Likes Given: 1

Default

Quote:
Originally Posted by carlisle_bob View Post
Hi

If one is trying to make sense out of a control system, it might be a good idea to understand the nomenclature involved before digging into it. By far the majority of people around here are *not* actually using a PID. That's not going to make things any easier to understand.

Bob
Quote:
Originally Posted by carlisle_bob View Post
By far the majority of people around here are *not* actually using a PID.
Exactly- they are using a PID library. That is why offering a simple solution, like appropriate time constants or I and D gain values is preferable to confusing the issue with even more technical jargon. Your input would have been relevant if you had supplied P, PI, and PD libraries, since there probably aren't any given that the PID library is easily configured to revert to those modes using environment variables, parameters, constants, etc.

They just want it to work, and if they want to know more, it is better to give an appropriate link, as was done earlier in the thread, with more complete explanations.
__________________
cwi is offline
 
Reply With Quote Quick reply to this message
Old 07-20-2012, 02:13 AM   #38
FastTalker
Member
HBT_SUPPORTER.png
Feedback Score: 0 reviews
 
FastTalker's Avatar
Recipes 
 
Join Date: Sep 2011
Location: Nashville, TN
Posts: 103
Liked 1 Times on 1 Posts
Likes Given: 2

Default

Quote:
Originally Posted by matho View Post
Hey fastTalker,
I never said that PID control isn't suited for heating water, I said I don't think the arduino PID library is very well suited for heating water.
here are my thoughts on the library, I could be wrong.

With the default sample time set to
Code:
    SampleTime = 100;
that would mean using a DS18B20 with a 12 bit resolution the conversion time is about 750mS the Iterm would have 7 increases before D could even get calculated, it gets worse when you put in the time it takes to ramp the temperature of the water. If the rate of rise is 2 deg c / min then that equates to 0.033 deg c / second, the finest resolution for a DS18B20 is 0.0625 degc so that means a measured temperature change every 2 seconds which would allow Iterm to increase 20 times before D would have a value
If I understand what you are saying, then I have to disagree. The integration logic is taking the sample time into account. The smaller the sample time the, the smaller the increase of the I term of the equation every update of the PID. You will integrate more accurately the faster you update the PID output. If you are familiar with calculus then this should be very fundamental.

The I term and the D term calculations use the update time to correctly calculate the rate of change. Notice in the SetTunings() function these lines:
Code:
   ki = Ki * SampleTimeInSec;
   kd = Kd / SampleTimeInSec;
It is calculating instantaneous derivative. Technically, it would be more accurate to use the ACTUAL update time rather than the target update time. However, it will probably be close enough. You should make sure that your Arduino scan time is significantly faster than your PID update time though.

Quote:
Originally Posted by matho View Post
If I term is there to remove a constant error that persists with a P drive only, which it is, why is it allowed to ramp to 100% of the output? If you had a constant error that required 100% output then your drive system is too small. I believe that for D to really be effective it should be subtracted from I term like this

Code:
ITerm-=(Kd*dInput);
double output = kp * error + ITerm;
that would allow D to stop Iterm from winding up if the rate of change is large enough.
In the PIDs most basic form there is no limit to what each of the three terms can contribute to the output. So you limit the I term to keep the PID from winding up so high that the P and D have no effect on the output. This would allow you to have an I only controller if you wanted. The constant error you are referring to is the STEADY-STATE error you get with a P only controller.
And, unless I'm missing something here, isn't that what it is already doing.

There is no reason to update the controller slower. If you are capable of updating the controller faster, you should.

It seems you are over complicating this. A PID updates the output based on:
P - your error now
I - how long you have had error
D - how fast your error is changing

If it's not working change your gains, not the controller.

On another note, I don't agree with how the auto-tune functionality works. Closed-loop tuning is hard to do automatically. It would probably produce better results if it attempted to do an open-loop step change test to measure the first-order lag and dead-time.
__________________
FastTalker is offline
 
Reply With Quote Quick reply to this message
Old 07-20-2012, 02:28 AM   #39
FastTalker
Member
HBT_SUPPORTER.png
Feedback Score: 0 reviews
 
FastTalker's Avatar
Recipes 
 
Join Date: Sep 2011
Location: Nashville, TN
Posts: 103
Liked 1 Times on 1 Posts
Likes Given: 2

Default

BTW, here's a good visual for showing how accuracy of integration increases with decreased interval size:
http://en.wikipedia.org/wiki/Integral#Riemann_integral
http://en.wikipedia.org/wiki/File:Ri...onvergence.png

__________________
FastTalker is offline
 
Reply With Quote Quick reply to this message
Old 07-20-2012, 02:36 PM   #40
carlisle_bob
Feedback Score: 0 reviews
Recipes 
 
Join Date: Apr 2012
Location: Carlisle, PA
Posts: 1,205
Liked 27 Times on 26 Posts

Default

Quote:
Originally Posted by FastTalker View Post
..... It would probably produce better results if it attempted to do an open-loop step change test to measure the first-order lag and dead-time.
Hi

...and possibly consider how to improve (reduce) the lag and dead time before you spend a lot of effort on tuning things. In some systems it's easier to generate a known impulse (short duration power on) rather than a step. Either an impulse or a step can be used to get the basic lag and gain information you need.

Lag = how long before it got moving.
Gain = how much did you make it move with that input

Bob
__________________
carlisle_bob is offline
 
Reply With Quote Quick reply to this message
Reply


Quick Reply
Message:
Options
Thread Tools