Author Topic: fan speed control algorithm  (Read 14949 times)

0 Members and 1 Guest are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17867
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
fan speed control algorithm
« on: April 10, 2013, 12:33:01 pm »
I'm writing a program for a PWM controller for fans. Now my main concern is to not have the fan keep "hunting" I want to aim to stay around a set temperature but my sensor will not be too near the newly cooled fluid (radiator) so I'm thinking of checking temperature at regular intervals so that is has a chance to see the effect of what is just set to and can look at changes over time and react.

At the moment my basic algorithm is to to make the speed proportional to the difference in temperature between now and 5 seconds ago.

Anyone have further insight ?
 

Offline airiclenz

  • Contributor
  • Posts: 28
  • Country: se
    • Airic Lenz
Re: fan speed control algorithm
« Reply #1 on: April 10, 2013, 12:43:21 pm »
Why not store the min and the max values of your temperature sensor. Every time you reach a new max value, you update your max variable (and store it to the eeprom). The same for the min value. This way your system is kind of self calibrating over time. You may want to set the full fan speed to start somewhere below the max temperature to be on the safe side...

Then take an array of (e.g.) 10 values to store the last 10 measurements - measured every 3 seconds or so. The PWN value that then goes to the fan is just the average of these 10 last measurements. This "smoothens" the path of the fan following the temperature.

I once realized a battery display that way but I can't see why this should not work fort this case too.

Airic.
« Last Edit: April 10, 2013, 12:47:18 pm by airiclenz »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17867
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: fan speed control algorithm
« Reply #2 on: April 10, 2013, 12:51:14 pm »
This is bearing in mind that the heat to dissipate may go up or down so too much history may be useless and just slow down the response or make it run too fast for too long and over cool.

I was thinking of considering T-Tset and using a correcting figure based on the current T and the last T so that it reacts to the absolute temperature without over reacting.
 

Offline airiclenz

  • Contributor
  • Posts: 28
  • Country: se
    • Airic Lenz
Re: fan speed control algorithm
« Reply #3 on: April 10, 2013, 12:57:16 pm »
Yeah, on a second thought - the self calibration is probably not the best solution. Especially because the surrounding temperature plays a role here too.

I did not know that you have access to the current that is drawn. You could also mix this somehow with the boot-time-sensor temperature (which could be considered as the minimum for the current environment).

This minimum temp + a defined offset which you want to allow then defines the temperature range in which the fan is ramped up or down...

Airic
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17867
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: fan speed control algorithm
« Reply #4 on: April 10, 2013, 01:10:27 pm »
I'm told the fan has to run at a minimum speed to clear any crap out that may accumulate and jam it should it stop so i say on start at 20-50%. Then I have two ways I think:

1) measure current temperature and multiply it by a correction factor derived from the temperature over time difference. That way I account for how far out I am now and how fast it is happening.

2) I look at the temp difference and use that to generate a value to add or subtract from the existing speed setting, that way the faster it rises the faster it goes to compensate without just bolting to 100% or running full blast then just cutting to low.

The idea is to self regulate around the unknowns of: Ambient temp., coolant temp., cooling power of the radiator that will get worse as it "ages" due to clogging of the fins with rubbish, speed of coolant, speed of increase or decrease in heat load due to changes in operating requirements
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1054
  • Country: fi
Re: fan speed control algorithm
« Reply #5 on: April 10, 2013, 01:26:56 pm »
Why not just stick to simple PI(D) algorithm, I think that there is no need to try to re-invent the wheel (it looks like you are already thinking something like it already). In simplest form, i.e. proportional controller, PWM is just measured temperature minus setpoint temperature, and result of this is then multiplied by some suitable constant (P-coefficient of PID).
 
For eliminating the steady state error, add I-controller, which integrates (sums) the error calculated by the P-controller, multiplied by the I-coefficient.

Then enforce some limits to calculated PWM duty cycle to account real-life constraints.

Best way to counteract the fan jamming and start threshold would be to have two nested control loops, inner loop for controlling the fan speed, and outer loop setting the fan speed setpoint based on the measured and setpoint temperature. But that might be an overkill.

Regards,
Janne
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17867
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: fan speed control algorithm
« Reply #6 on: April 10, 2013, 01:28:37 pm »
yes it is like a pid, I didn't know you can just go find some pid algorithm, where do I find them ?
 

Offline fcb

  • Super Contributor
  • ***
  • Posts: 2125
  • Country: gb
  • Test instrument designer/G1YWC
    • Electron Plus
Re: fan speed control algorithm
« Reply #7 on: April 10, 2013, 01:28:55 pm »
PID algorithm, and enjoy wasting a day tuning it.
https://electron.plus Power Analysers, VI Signature Testers, Voltage References, Picoammeters, Curve Tracers.
 

Offline fcb

  • Super Contributor
  • ***
  • Posts: 2125
  • Country: gb
  • Test instrument designer/G1YWC
    • Electron Plus
Re: fan speed control algorithm
« Reply #8 on: April 10, 2013, 01:33:43 pm »
Apart from the obvious and vaguely rude "google it" answer, there is a really great old IC by NatSemi called an LM629, the datasheet for this certainly used to contain a really good explanation of PID and has some great explanations for things like slowing down the I term accumulation and anti-windup.
https://electron.plus Power Analysers, VI Signature Testers, Voltage References, Picoammeters, Curve Tracers.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17867
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: fan speed control algorithm
« Reply #9 on: April 10, 2013, 01:40:21 pm »
At just 70 quid a pot I'll go the "DIY" route, but I'll read the datasheet to try and grasp some of the maths (says the dyslexic)
 

Offline fcb

  • Super Contributor
  • ***
  • Posts: 2125
  • Country: gb
  • Test instrument designer/G1YWC
    • Electron Plus
Re: fan speed control algorithm
« Reply #10 on: April 10, 2013, 01:50:48 pm »
I wasn't suggesting you actually use an LM629  :scared:

When I started doing motor control many moons ago, this was a great IC that the competition used, and we couldn't afford to put three of them in a box - so I rolled my own version using PIC16C73 (at the time the biggest cored 16 in the series).

The datasheet was invaluable.
https://electron.plus Power Analysers, VI Signature Testers, Voltage References, Picoammeters, Curve Tracers.
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1054
  • Country: fi
Re: fan speed control algorithm
« Reply #11 on: April 10, 2013, 01:57:43 pm »
Here are some code:

http://www.codeproject.com/Articles/36459/PID-process-control-a-Cruise-Control-example

PID calculations are quite simple, IMO biggest difficulty is that if you must use integer arithmetic, to figure out the scalings while avoiding overflows etc.

Regards,
Janne
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1289
  • Country: fi
Re: fan speed control algorithm
« Reply #12 on: April 10, 2013, 02:03:27 pm »
You are discussing things that are very close to a classical controller. But lets start from basics.
You need to control temperature to agree with a target setpoint. For this you need a controller. The basic thing to keep in mind is that a controller applies a defined control law on the difference between setpoint and actual value, and produces the plant command variable as the result.  The command variable in this case is the duty cycle of the pwm signal controlling the fan speed, from 0 to 100%.
We will get to elaborations but that is all the actual controller does. I am sure you don't want to involve matrix calculations so lets keep it simple and use well defined and understood control laws only. That would then be the proportional-integral or PI. There are 2 easily understood terms here.

Proportional control or P-term is just a defined ratio between error signal and control signal, i.e.

C = Kpe, where

e is the error signal (Setpoint - Actual),
KP is the multiplier or constant of proportionality.

Proportional control has a shortcoming in that in order to produce a command signal there must exist an error signal. This is easy to see from the equation, since if e = 0, so does the command signal. You can make the necessary e smaller by increasing the gain, i.e. the P value but as that increases the gain of the feedback loop, there will be a limit after which the system is no longer stable.
Enter the integrative or I-term. This is the time integral of the error signal multiplied by a gain, i.e.

C = Ki/Ti * integral( e) dt, where

Ki is the integration gain,
Ti is the integrator time constant

The integral action will reset to zero the steady state error left by the P term. A PI controller is just the sum of the P and I-terms.
It is a known fact that tuning a PI(D) controller can be somewhat challenging as setting the gains too high is a quick route to oscillations or command runaway. This risk is significantly increased in the presence of time lags in the process, and this seems to be the case with your application. So too much should not be expected from the control dynamics but this scheme will work.

Now, there were some process disturbances. All of these will be handled by the PI controller but if you need better dynamics than what you can get by tuning the PI, there are 2 more things you can do: introduce the D term and/or use feedforward control.

The derivative or D-term is the time derivative of the error signal i.e. it reacts to the slope of e, or in other words changes. The equation is

C = Kd * de/dt, where

Kdis the derivation gain,
de/dt is the first derivative of the error signal.

This term will make the controller more responsive to changes in setpoint or actual value, increasing the control dynamics. Same cautions regarding stability as above.

The final trick is to use feedforward control. This method has saved the bacon several times for me in situations where classical PID dynamics just cannot cope. Using this method you bypass the contol loop and apply a direct command signal based on either the setpoint or  the actual value. Say you know that if the coolant temp is X degrees, the fan must run at Y% of full speed to cope. So why not feed the actual temp value back to the command signal with suitable gain to make it so.

So the feedforward correction signal would become

C = Y%/Xdeg (provided the speed is directly proportional to the temperature).

Any errors this calculation makes will be easily corrected by the controller - actually far easier than the case where you feed no signal, because then the controller must remove all error instead just the residual one.

An analog PID controller is easily constructed out of an op amp, 2 caps and 2 resistors and some auxiliary stuff. For a software controller it is a bit more complex as you will be going discrete time, but the wonderland of Inet is full of implementation examples, some of which actually work. I seem to recall that even the Arduino provides a PID controller, so that might be worth lookin into. as the source code is freely available.

Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17867
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: fan speed control algorithm
« Reply #13 on: April 10, 2013, 02:31:55 pm »
yes what you say makes sense. My main controlling factor should be the difference between current and desired temperature. I want to bring in the change speed to help it react fast enough to sudden increases but not over cool on slow decreases. I'll go looking for the PID library for arduino.
 

Offline gautamdamodar

  • Contributor
  • Posts: 11
  • Country: in
  • gautam
Re: fan speed control algorithm
« Reply #14 on: April 10, 2013, 02:53:10 pm »
Temperature being a slow process first option is ON/OFF control (no good tracking). Since the there may be considerable sensor lag(dead time), tracking may not be best even if you use PID through PWM. If you are using PID control, the variable being temperature, I (integral action) doesn't have much role and can be neglected. If you have access, please refer the book "Process Control Instrumentation Technology" by Curtis D. Johnson
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17867
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: fan speed control algorithm
« Reply #15 on: April 10, 2013, 03:02:52 pm »
they already have thermostatic control. I'm supposed to be bettering it.
 

Offline airiclenz

  • Contributor
  • Posts: 28
  • Country: se
    • Airic Lenz
Re: fan speed control algorithm
« Reply #16 on: May 29, 2013, 08:03:07 am »
Hello Simon,

I am wondering if there are any news regarding your fan-control. Did you find a nice PID library for Arduino?

A.
« Last Edit: May 29, 2013, 12:01:19 pm by airiclenz »
 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: fan speed control algorithm
« Reply #17 on: May 29, 2013, 08:40:00 am »
something like this?

%=% +1*((Average- Setpoint)*I + (Reading- Setpoint)*P + (Reading- Last)*D
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17867
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: fan speed control algorithm
« Reply #18 on: May 29, 2013, 11:58:49 am »
uh I didn't worry about a full blown PID library in the end but just something that bumps the speed up and down as the temperature changes. I used the difference between the minimum and the temperature change since last read to get a bit of reaction speed in for when the temperature rises suddenly.

I'm considering taking more samples and using the average every so often but not sure if it will help or complicate matters. I'm yet to set up a test rig although I got all the bits now.
 

Offline airiclenz

  • Contributor
  • Posts: 28
  • Country: se
    • Airic Lenz
Re: fan speed control algorithm
« Reply #19 on: May 29, 2013, 12:06:47 pm »
Ah, ok. Thanks for the update. Let us know how your test went.

A.
 

Offline miceuz

  • Frequent Contributor
  • **
  • Posts: 387
  • Country: lt
    • chirp - a soil moisture meter / plant watering alarm
Re: fan speed control algorithm
« Reply #20 on: May 29, 2013, 04:57:36 pm »
There is an Arduino PID library written by a guy who does PID as his dayjob. It has many usual PID pitfalls (like integral windup) solved already. Just check and reuse his code, it's mainly microcontroller agnostic https://github.com/br3ttb/Arduino-PID-Library

Offline IanB

  • Super Contributor
  • ***
  • Posts: 12009
  • Country: us
Re: fan speed control algorithm
« Reply #21 on: May 29, 2013, 05:41:04 pm »
uh I didn't worry about a full blown PID library in the end but just something that bumps the speed up and down as the temperature changes. I used the difference between the minimum and the temperature change since last read to get a bit of reaction speed in for when the temperature rises suddenly.

I'm considering taking more samples and using the average every so often but not sure if it will help or complicate matters. I'm yet to set up a test rig although I got all the bits now.

It often helps to have a bit of filtering on the measured input to a control loop. Otherwise the control algorithm will respond to the input noise as well as the desired signal. Since noise is effectively random, this will tend to result in random control moves on the output, which is typically undesirable.

Another useful approach to PID control implementation is the so called "velocity algorithm". If we consider PI control (which is sufficient for many purposes), we have:

Error = Measure - Setpoint
Output = Kp x [Error + 1/Ti x Integral(Error, dt) ]

Kp is the proportional gain and Ti is the integral time.

Such an algorithm is usually implemented on a microcontroller in discrete time steps. So we can consider the change in output from one step to the next:

Change-in-output = Kp x [Change-in-error + Dt / Ti x Error]

Dt is the cycle time of the control algorithm.

If the setpoint is not moving, then:

Change-in-output = Kp x [Change-in-measure + Dt / Ti x Error]

Even if the setpoint does get moved, it often helps to ignore it in the proportional action since it may produce smoother control responses.

The calculation above is very close to what you have been arriving at during your investigations. This puts it on a more rigorous footing. If you apply the calculation above, you have two constants to adjust in order to tune the controller, Kp and Ti. If Error and Output are both normalized so they are expressed in percent of scale, then typical values of Kp would be between 0.1 and 10, and typical values of Ti would be between 5 seconds and 500 seconds (where smaller numbers give stronger control action).
 

Offline 4to20Milliamps

  • Regular Contributor
  • *
  • Posts: 248
  • Country: us
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf