PID Controller

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

Guys I want to study implementation of PID controller using AVR microcontroller. So if anybody has implemented it in some project which I can easily understand then please give me the algorithms and the code.

Preferably if you have implemented it for line following or micromouse then that would be better because I will implement it for these two things.

Even a link to some nice website will do which is not too complex to understand for a newbie.

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

Quote:

Even a link to some nice website will do

avrfreaks.net :wink: Do a search here for "PID" and You will find several threads on the subject (some of them with good links to other sites).

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

There are at least three related projects just by searching the academy for "PID"

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

I have implemented PID for my robots for motion control. The concept is really simple -
Output = Kp * Proportional error – Kd * Derivative error + Ki * Integral error
But there are a lot of issues which will occur(some which i remember):
1. The PID algorithm works with fixed time samples - this may lead to integrator overflow.
2. The derivative term may contribute to making the robot unpredictable.
Hence i would recommend that u implement the algo first and then spend a lot of time tuning it (i.e adjusting Kp,Kd,Ki) values.
Unfortunately i dont have any code to give you. However refer to the site
www.barello.net. This contains a beautiful RTOS called avrx and also some papers on closed loop motion control.

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

I successfully implimented a PID controller for a pump application.
I got my info from here:
http://www.embedded.com/2000/0010/0010feat3.htm

Here is my C code:

struct SPid {

    double	dState;	         // Last position input
	double	iState;         // Integrator state
    // Maximum and minimum allowable integrator state
    double	iMax;
    double iMin;
    double iBand;         // integral operating band
    double	iGain;         // integral gain
    double	pGain; 		   // proportional gain
    double	dGain;      // derivative gain
} ;

double update_pid(volatile 0 struct SPid *pid, double error, double position)
{
    
    // calculate the proportional term
    pTerm = pid->pGain * error;
    // calculate the integral state with appropriate limiting
    if (fabs(error) < (pid->iBand)) 			// only accumulate error while in band
        pid->iState = pid->iState + error;
    else
        pid->iState = 0;
    
    if (pid->iState > pid->iMax)				// limit accumulator to bounds iMax,iMin
        pid->iState = pid->iMax;
    else if (pid->iState < (pid->iMin))
        pid->iState = pid->iMin;
		
    iTerm = pid->iGain * pid->iState;  // calculate the integral term
    dTerm = pid->dGain * (position - pid->dState);
    pid->dState = position;
	drive=pTerm + iTerm - dTerm;
	return( drive);

}

I used a VB app to plot response times and adjust PID values. The system works really well, except for a noise problem I had with the internal ADC. I finally had to switch to an external ADC to fix the problem. It appeared the PWM output was crossing-talking into the ADC somehow. Grounds, perhaps?

Tim Ressel
Portland, OR
timr@earthling.net

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

Dear all,
I am doing project "temperature control using PID controller" .when we go for relay auto tuning of PID controller,we have to Substitute a relay with amplitude d and from the desribing function we get the eqn Ku =4d/pa .
my doubt here how will i obtain d (ie.,relay amplitude)?
In order to analyse this , I first implented simple ON OFF(relay) temperature controller .From that I observed the oscillations getting are asymmetric around the setpoint .In order to get symmetric oscillations, i know that we have to select a suitable value for d ? Is there any theoretical method to find d? I really got stuck in this part . Considering the fact that i want to design completely automatic system , i want my miccrocontroller to find d rather than manually . So it would be very helpful if you give me a solution for this and I will be always indebted to you.
hopefully,
--varun

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

It's probably asymmetric because heating takes much less time then the subsequent cooling time. This of course depends entirely on the what is being heated and the environment.

I guess an autotuning PID controller is really a PID controller being tuning by another P/I/PI/PD/PID controller. I think you will not find much information on autotuning, this is a typical trade-secret ;)

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

There is an Arduino PID Library that OP could see. Arduino works on ATmega8, ATmega168, ATmega328 and ATmega1280. Some additional ports to other devices have been done.

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

I think wikipedia describes the basics for tuning very well http://en.wikipedia.org/wiki/PID_controller

Most of the auto-tuning articles I found in the internet were based on Ziegler-Nichols tests. Here's one of them http://www.ceasiamag.com/article-2255-autotuningcontrolusingzieglernichols-Asia.html

There are some "better" methods documented as well (google for auto-tuning!) Most of them go deep into control theory and/or rely on testing parameters for some simplified model (like first order plus delay).

Depending on your application any one of them could be good enough. Manual tuning is probably the most optimal method assuming process is measurable and one has enough experience on that.

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

what is actually pid's o/p ? i mean p+i+d a count value(which is directly fed to a dac) or a "%" value .if count , how can we standardise that system , ie., how to make that system compatible for different resolution dac?
--varun

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

Quote:

what is actually pid's o/p ? i mean p+i+d a count value(which is directly fed to a dac) or a "%" value

It is whatever you want it to be. And then you scale your coefficients accordingly.

Perhaps it makes it easier for you to convert input values to percent, do calculations, result is a percent, and then convert to output units? Then that is a fine way to go.

Temperature is a slllowww signal with well-spaced loop closures--usually measured in seconds or minutes or even hours.

On the other hand, motion control and pressure control and similar will close the loop every few milliseconds. then the time to do the conversions may be a factor. So you then may scale the coefficients to go directly from, say, ADC counts to PWM counts.

Lee

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

how can i reduce overshoot for my pid controller ..?using gain scheduling or by using setpoint weighting?which is the most prefered method?

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

Quote:

how can i reduce overshoot for my pid controller ..?using gain scheduling or by using setpoint weighting?which is the most prefered method?

How about "standard" methods such as reducing P gain or adding more D?

http://www.embedded.com/2000/001...

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

But if I reduce my P gain it will increase rise time ,doesn't it ? So I am in search of method which doesnt take much time and gives less overshoot aswell .I am not quite sure whether there is any theorertical method . If anybody has any solution for this please do reply.
--varun

Last Edited: Tue. Dec 8, 2009 - 08:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The real world is a compromise at best. Therefore you usually have to trade one advantage for another - getting the tradeoff correct is the 'fun' part. That is engineering.

As for PID - there has been volumes written on it. You have the power of the interweb at your fingertips but yet can't extract common information?

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

If you are open minded and you find your self not satisfied with the compromise you have to often make when tuning PID, then you might take a look at fuzzy logic. It is much closer to human thinking, easier for tuning, and unlike PID you can have many input variables, and single rule base that handles multiple outputs.
http://en.wikipedia.org/wiki/Fuz...
http://forum.e-lab.de/topic.php?...

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

Did you even bother to study the link I gave?

Did you then learn anything about the D term?

I'm out. As Kartman says there is a LOT of info out there. For real work, I suggest the Karl Astrom works:
http://www.bestwebbuys.com/books...

1.  	   	  Adaptive Control
by Karl Johan Astrom, Bjorn Wittenmark

Hardcover, Addison-Wesley (December 1994)
Other Formats: | Book | Paperback |
List Price: $124.00

2. 		Automatic Tuning of Pid Controllers
by Karl Johan Astrom, Tore Hagglund

Hardcover, Instrumentation Systems & (May 1988)
List Price: $45.00

3. 		Computer-Controlled Systems: Theory and Design 3/E
by Karl Johan Astrom , Bjorn Wittenmark

(January 1997)
List Price: $117.00

4. 		Computer-Controlled Systems: Theory and Design
by Karl J. Astrom, Bjorn Wittenmark

Book, Prentice-Hall (January 1984)
Other Formats: | Hardcover |

6. 		Control of Complex Systems
by Karl J. Astrom (Editor)

Hardcover, Springer Verlag (September 2000)
List Price: $175.00

7. 		Pid Controllers
by Karl J. Astrom, Tore Hagglund

Paperback, Instrumentation Systems & (January 1995)
List Price: $95.00

8. 		The Reaction Wheel Pendulum
by Karl Astrom, Mark Spong (Editor), Karl J. Astrom (Editor), Mark W. Spong (Editor)

Paperback, Morgan & Claypool (May 2007)
List Price: $40.00

9. 		Advanced Pid Control
by Karl J. Astrom, Karl J. Strom, Tore Hagglund

Paperback, Instrumentation Systems & (August 2005)
List Price: $99.00

10. 		Predictive Functional Control: Principles and Industrial Applications
by Jacques Richalet, Donal O'Donovan, Karl Astrom (Foreword by)

Hardcover, Springer Verlag (June 2009)
List Price: $149.00

11. 		Feedback Systems: An Introduction for Scientists & Engineers
by Karl J. Astrom, Richard M. Murray

Hardcover, Princeton Univ Pr (May 2008)

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.