DPRG List  

[DPRG] PID Loop Question

Subject: [DPRG] PID Loop Question
From: Rick Bickle rbickle at intconsys.com
Date: Tue Feb 23 12:23:27 CST 2010


Looks like you opened up a can of worms here... I'll put in my 2 cents

I'm sure the function of the P, I, and D terms is fairly apparent, but
I'll cover it briefly. There is also a paper on one approach to PID that
I wrote years ago in the DPRG tutorial section.
First of all, the PID algorithm must sample the speed or position of the
motor and calculate the error term. The error term is simply (set speed
- actual speed). The rest of the P, I, and D coefficients control how
the algorithm deals with the error.
PID algorithms can  also be used to set position, but I'll limit things
to speed here for simplicity.

ERROR = Set_Speed - Actual_Speed;

PID, or PDI, in my opinion of terms in order of importance...

P = (Error * P) - A simple proportion calculation which gives a linear
response to the error term.
D =  (Last Error - Current Error)*D - The differential between the last
error term and the present one. It indicates if the system is heading
toward or away from the set point.
I = ((Last Error + Current Error)/2) * I) - The average error over time
scaled by the I term.

PID interval:

Now, here's something that you don't necessarily see in papers on the
subject. The forth PID term is really the PID calculation interval.
Think about the system being controlled, in this case a motor, with a
given power and robot mass attached, and determine how fast the system
can reasonably respond to changes in motor power. Obviously, a robot is
not going to be able to respond to a PID algorithm adjusting its'
variables too quickly, so you have to allow enough time for the system
to react to the previous coefficient changes.

Also, take into consideration the resolution of the motor encoders and
the minimum motor speed.  In order to react in a meaningful way, the
algorithm needs to have several encoder ticks to be able to reliably
calculate the error term. If the PID loop is running too fast, it the
error term will only be 0 or 1, and the behavior of the system will be
erratic. Between 1 and 10 PID loops per second will usually suffice.

Limits and "Integral Windup":

In the PID calculation, you also need to place limits on each of the
terms so that any one term does not overload the system if the term is
set incorrectly. Choosing these limits is as important as choosing the
PID coefficients. This is especially true of the I term, which tends to
create "integral windup". Often the error term will never get to exactly
zero, so the integral term builds and builds until it causes erratic

PID Output:

In the motor  controllers that I built, the output of the PID algorithm
was simply a PWM signal which ranged from 0 to 255 - I added the PID
output to the existing PWM setting. Another option is to simply have the
PID output be the new PWM setting, although this would probably make it
harder to tune. Each of these options will affect the scale of the PID

Pseudocode example:

Putting this all together, we have:

Void PID_Loop (void)
                // Compute Error term
                Err = Set_Point - Encoder_Count ;

                // Compute P value
                Pval = Err * Pterm;
                If (Pval > Plimit) Pval = Plimit;
                If (Pval < -Plimit) Pval = -Plimit;

                // Compute D value
                Dval = (LastErr - Err)*Dterm;
                If (Dval > Dlimit) Dval = Dlimit;
                If (Dval < -Dlimit) Dval = -Dlimit;

                // Compute I value
                Ival = (LastErr + Err)/2;
                Ival *= Iterm;
                If (Ival > Ilimit) Ival = Ilimit;
                If (Ival < -Ilimit) Ival = -Ilimit;

                // Update vars for next loop
                LastErr = Err;

                // Calculate Output
                Output +=  Pval + Dval + Ival;

PID Tuning:

Now we come to the real heart of the subject. Implementing the PID is
not really difficult, but tuning the system properly can be. In my paper
on the website, I used a "brute force" approach in which I had the robot
loop through ranges of P and D terms, while it output speed data to a
serial port. This data was being captured by another PC. The robot made
hundreds of tries with different values, and in the end, I graphed the
responses of those terms that were in the ballpark, and chose the ones
with the best response.

There are of course many ways to mathematically calculate the response
of a system to given PID values, but in robotics, you rarely know the
exact parameters of your system. 
I went through the trial and error method setting the P term first, with
D and I set to 0. Then set the D term until a fast response to the error
is generated without too much overshoot. Finally, add the I term to
reduce oscillation and have the system get as close to the set point as

If anyone has a better suggestion for this that has worked, please let
me know.


Excellent Paper "PID Without a PHD"

From: dprglist-bounces at dprg.org [mailto:dprglist-bounces at dprg.org] On
Behalf Of Quinones, Jose
Sent: Monday, February 22, 2010 12:34 PM
To: dprglist at dprg.org
Subject: [DPRG] PID Loop Question

Hi Group,

I am trying to get a better understanding of the PID Loop. I have some,
but want to understand it as good as I understand tying up my shoes. I
want this to be as clear in my head as it is counting from 1 to 10 in

So far, I think I understand the Proportional aspect this good. If the
error is big, correct a lot. If the error is not so big, correct just a
little bit.

I think I have an idea of what Integral is. If the error has been big
for a long time, correct for a longer period of time. If the error has
not been too large for a long period of time, then apply a correction
only for a small period of time.

Not certain if I follow Derivative as well, but my impression is that it
tells us how quick the error is changing and if so, then apply a

And then it comes to putting it all together and knowing which component
caused which occurrence of what.

I know there is a bunch of tutorials out there, but they are all of the
style "if 1 + 1 = 2, what is the molecular constitution of binary star
system XT394-MY if the two suns collapse at the neocortex of the
meridian divesture?" In other words, WHAT???

What explanations can you offer or what tutorials can you point me to?
Thanks in advance for expanding my mind with your knowledge!

Best regards,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://list.dprg.org/pipermail/dprglist/attachments/20100223/778b4fc0/attachment.html

More information about the DPRG mailing list