Steven715
Posts: 27
Joined: Mon Dec 24, 2012 9:01 pm
Location: Bedfordshire

PID PWM controld fan

Wed Dec 18, 2013 8:00 pm

Hello there,
firstly I am am sorry if this should be in the python section admins please move if you feel that this is more appropriate in the python section.

I am wanting to implement some code in the same as in this youtube video http://www.youtube.com/watch?v=gtdy4uctbvs I have downloaded the same python code that the creator seems to be using.
But I don't know how to get it to work.

Please can you point me in the right direction.

Link to the python script http://code.activestate.com/recipes/577 ... ontroller/

Many thanks Steve.

Code: Select all

#The recipe gives simple implementation of a Discrete Proportional-Integral-Derivative (PID) controller. PID controller gives output value for error between desired reference input and measurement feedback to minimize error value.
#More information: http://en.wikipedia.org/wiki/PID_controller
#
#[email protected]
#
#######	Example	#########
#
#p=PID(3.0,0.4,1.2)
#p.setPoint(5.0)
#while True:
#     pid = p.update(measurement_value)
#
#


class PID:
	"""
	Discrete PID control
	"""

	def __init__(self, P=2.0, I=0.0, D=1.0, Derivator=0, Integrator=0, Integrator_max=500, Integrator_min=-500):

		self.Kp=P
		self.Ki=I
		self.Kd=D
		self.Derivator=Derivator
		self.Integrator=Integrator
		self.Integrator_max=Integrator_max
		self.Integrator_min=Integrator_min

		self.set_point=0.0
		self.error=0.0

	def update(self,current_value):
		"""
		Calculate PID output value for given reference input and feedback
		"""

		self.error = self.set_point - current_value

		self.P_value = self.Kp * self.error
		self.D_value = self.Kd * ( self.error - self.Derivator)
		self.Derivator = self.error

		self.Integrator = self.Integrator + self.error

		if self.Integrator > self.Integrator_max:
			self.Integrator = self.Integrator_max
		elif self.Integrator < self.Integrator_min:
			self.Integrator = self.Integrator_min

		self.I_value = self.Integrator * self.Ki

		PID = self.P_value + self.I_value + self.D_value

		return PID

	def setPoint(self,set_point):
		"""
		Initilize the setpoint of PID
		"""
		self.set_point = set_point
		self.Integrator=0
		self.Derivator=0

	def setIntegrator(self, Integrator):
		self.Integrator = Integrator

	def setDerivator(self, Derivator):
		self.Derivator = Derivator

	def setKp(self,P):
		self.Kp=P

	def setKi(self,I):
		self.Ki=I

	def setKd(self,D):
		self.Kd=D

	def getPoint(self):
		return self.set_point

	def getError(self):
		return self.error

	def getIntegrator(self):
		return self.Integrator

	def getDerivator(self):
		return self.Derivator

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: PID PWM controld fan

Thu Dec 19, 2013 8:24 am

That code is only part of what you need. The full picture will include

desired temperature - actual temperature => PID => PWM drive => fan controller => fan

The code you have is the PID which takes the difference between the temperature you want and the temperature read by sensors, and produces a value as the output. That output then is used to set a pulse width signal which drives the fans at a certain speed.

So you also need electronics & code to read the temperature sensor, code for the PID (which you have), code to generate the PWM signal based on the PID output using the GPIO library, and electronics to convert the PWM signal to the power for the fans (a MOSFET).

I think you need to do more research on this project to find those missing pieces and understand how they fit together.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

Steven715
Posts: 27
Joined: Mon Dec 24, 2012 9:01 pm
Location: Bedfordshire

Re: PID PWM controld fan

Thu Dec 19, 2013 4:51 pm

Hello there thanks for the reply I should have said that i have the basics already in place I already have a temprature probe working that is currently logging temprature at the moment so that is not a problem. I also have a fan working on basic pwm using the following code in a live python environment.

Code: Select all

 import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
fan = GPIO.PWM(18, 50) 
fan.start(50) 
Then I can use

Code: Select all

fan.ChangeDutyCycle(10)
To change the speed

I just need to combine the three not to sure how to use it using the pid code

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: PID PWM controld fan

Thu Dec 19, 2013 5:12 pm

Probably best to google PID for the best description, but...

PID = Proportional, Integral, Differential.

There is a target (the temperature you want - set_point in your code)
There is an input (the temperature for the sensors - current value in your code)
There is an error (the target - the input - self.error in your code)
There is the PID math(s) code which applies some simple maths to the error resulting in the output
The output is used to set the PWM DutyCycle

You configure Kp, Ki and Kd (which are just numeric gains for Proportial, Integral and Differential respectively) to get the right fan speed out as a result of a given temperature in. If the temperature is nearly right, the fan spins slowly. If the error is large, the fan spins quickly.

It's not complex once you get your head round it, but it's worth doing some research online. PIDs aren't specific to temperature - they are a simple way to map input errors to a corrective outputs which then feedback. Your code simply loops reading the inputs, feeding them into the PID, applying the output value as the PWM duty cycle, and then loops round again.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

paulv
Posts: 559
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: PID PWM controld fan

Sat Dec 21, 2013 10:30 am

You may want to have a look at the post I created for a fan controlling the Pi CPU itself.
Maybe you can use the same principle for your application.

http://www.raspberrypi.org/forum/viewto ... 58#p463458

ghellquist
Posts: 69
Joined: Thu Aug 02, 2012 8:47 am
Location: Stockholm Sweden

Re: PID PWM controld fan

Sun Dec 22, 2013 8:52 am

Does it help with this explanation.

You need to run the control loop cyclically, say once every second. It depends on how fast the real world reacts to changes in fan speed. In order for the calculations to be reasonably simple you want the cycle time to be fairly constant, not varying widely from time to time.

So the algorithm runs for ever:
1) Read the actual temp
2) Get the desired temp
3) Calculate the difference between actual and desired
4) Do some calculations
5) Modify fan speed
-- wait, then start over

The PID algorithm goes into step 4 above.

The three letters stand for Proportional, Integration, Derivative. In many control situations it is enough to run only the P part. In this kind of control I would expect you to really need only the P and I parts, but hardly the D part.

P - proportional, simply multiply with some factor. Say, difference is ten degrees and factor is 10. Ten times ten gives 100, that is run the fan at 100%. As the real world slowly gets less warm, the difference is decrease. Say 5 degrees, giving 50% fan speed.

The problem with only P in this kind of application is that the temperature never reaches quite down to the desired temp. Using the example figures, say that the fan needs 55% speed to reach the desired temp. But as the difference decreases the the fan goes slower and slower, stopping fully when the desired and actual temp are the same.

This is where the I part kicks in. It integrates (remember math?) the difference over time. So, say in the example the P part has done its job, and there still is a difference, the I part slowly adds the difference so the fan increases in speed until you reach steady state. The I part has "memory" of the history of difference and updates this memory over time.

The D part finally, is used when there is a sudden change in difference. The I part makes changes sort of slow, adding the D part makes the response quicker. D stands for derivate (again a math term) or in this case the rate of change for the temperature.

// Gunnar

Steven715
Posts: 27
Joined: Mon Dec 24, 2012 9:01 pm
Location: Bedfordshire

Re: PID PWM controld fan

Wed Dec 25, 2013 3:33 pm

paulv wrote:You may want to have a look at the post I created for a fan controlling the Pi CPU itself.
Maybe you can use the same principle for your application.

http://www.raspberrypi.org/forum/viewto ... 58#p463458
Thank you paulv I will give your code a try and have a play around with it.

To everyone else I am sorry for not replying sooner I thought that I had this setup for sending me an email when ever someone replays. But I was wrong :cry:

Thank all for your replays. Merry Christmas Steve.

Steven715
Posts: 27
Joined: Mon Dec 24, 2012 9:01 pm
Location: Bedfordshire

Re: PID PWM controld fan

Sun Dec 29, 2013 7:56 pm

ghellquist wrote:Does it help with this explanation.

You need to run the control loop cyclically, say once every second. It depends on how fast the real world reacts to changes in fan speed. In order for the calculations to be reasonably simple you want the cycle time to be fairly constant, not varying widely from time to time.

So the algorithm runs for ever:
1) Read the actual temp
2) Get the desired temp
3) Calculate the difference between actual and desired
4) Do some calculations
5) Modify fan speed
-- wait, then start over

The PID algorithm goes into step 4 above.

The three letters stand for Proportional, Integration, Derivative. In many control situations it is enough to run only the P part. In this kind of control I would expect you to really need only the P and I parts, but hardly the D part.

P - proportional, simply multiply with some factor. Say, difference is ten degrees and factor is 10. Ten times ten gives 100, that is run the fan at 100%. As the real world slowly gets less warm, the difference is decrease. Say 5 degrees, giving 50% fan speed.

The problem with only P in this kind of application is that the temperature never reaches quite down to the desired temp. Using the example figures, say that the fan needs 55% speed to reach the desired temp. But as the difference decreases the the fan goes slower and slower, stopping fully when the desired and actual temp are the same.

This is where the I part kicks in. It integrates (remember math?) the difference over time. So, say in the example the P part has done its job, and there still is a difference, the I part slowly adds the difference so the fan increases in speed until you reach steady state. The I part has "memory" of the history of difference and updates this memory over time.

The D part finally, is used when there is a sudden change in difference. The I part makes changes sort of slow, adding the D part makes the response quicker. D stands for derivate (again a math term) or in this case the rate of change for the temperature.

// Gunnar
Thank you Gunnar,

The way you split that up in to 5 sections makes my little project seem much simpler. I know the general idea behind PID since we use this at work to control pump speeds against flow this is why I wanted to look into this just to play around with the PID loop in a safe environment. I did not know that the PID was named mainly after maths terms so I have learned more there I have read over the PID on the Wiki page but I need to study it to truly get my head around it.

Again I thank you.

I just need to get Christmas and new year over with then I can start playing around with my Pi again yay :D It's been a little hectic around hear just lately.

Thank you once again.

Steve

Return to “Automation, sensing and robotics”