PID Control

crazychicken

Platinum Member
Jan 20, 2001
2,081
0
0
I am writing c code to control a 8051c microcontroller attached to a car to make it follow a white line on a black floor.

the assignment is simply to make it turn all the way left if it is right of the line and right if it is left of the line. this seems silly. I've been researching PI and PID control, but I cant seem to find how I and D are calculated??

i know that
output=c_gain*error + something*c_integral + something* c_derivative

but i dont know exactly how to figure out the "somethings" and the constants

can anyone shed some light on this for me?

thanks
david
 

f95toli

Golden Member
Nov 21, 2002
1,547
0
0
AFAIK there is no simple way to determine the "best" constants for a PID-regulator. Besides, in order to calculate them you would need an almost perfect model oryour system whcih means that it is not very practical for real systems.
It is a bit simpler for PD and PI (I don't think P would be a good idea in your case) and you should be able to find information about how to do that in any book on control theory,

However, for what you are trying to do I think some simple method like Ziegler-Nicols would do if you really think you need PID.


 

RossGr

Diamond Member
Jan 11, 2000
3,383
1
0
It is not clear to me that following a line will give you the type of input a PID controller wants. Your input is on or off (line or no line). This is not enough for PID, it needs a continuous signal, temperature is a common application, you set a particular temperature as the set point then tune the controller to maintain the temperature within a range of the set point. Another common application of PID is a motor controller, the current is a nice continuously varying signal proportional to speed. In each of these cases the controller watches the distance from the set point and makes corrections based on the magnitude of that difference. It seems that are as many different way to implement a PID controller as there are programmers. If I were trying program a PID controller I would search for a control systems text book.

I would think that you will have to use an on/off controller because that is all the information you have. For a simple on/off you will need to track which side of the line you are on, for PID you would also need to know how far from the line you are.

My best guess is that to get good results with an on/off controller you will need to play with how far the wheels turn, I?ll bet that you will find that small turn angles will give the best results
 

crazychicken

Platinum Member
Jan 20, 2001
2,081
0
0
its not actually a "line or no line" type thing. We are using 2-3 photodiodes, so their voltatge will vary based on the amount of "white" (the masking tape line) they see.

You say dont use P control.... i think that is what they want us to do. I figured that PID was simply an extension of P, so i thought i'd try that. can someone explain generally what the difference between P, PI, PD, and PID is? I cant seem to find a good explanation online, simply alot of formulas. I am going to the library to get a control book sometime, but until then treat me like a second year electrical engineer! haha

Thanks

David
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Originally posted by: crazychicken
its not actually a "line or no line" type thing. We are using 2-3 photodiodes, so their voltatge will vary based on the amount of "white" (the masking tape line) they see.

You say dont use P control.... i think that is what they want us to do. I figured that PID was simply an extension of P, so i thought i'd try that. can someone explain generally what the difference between P, PI, PD, and PID is? I cant seem to find a good explanation online, simply alot of formulas. I am going to the library to get a control book sometime, but until then treat me like a second year electrical engineer! haha

Thanks

David
If all you have to do is turn all the way right or left based on your location relative to the line, then the P controller is ideal. Just assign a very huge gain (the constant that multiplies error in the feedback loop) so that any deviation from the course will cause a turn. Should take about one line to program. ;) For PID, PI, or PD controllers you need time constants, which will be way more complicated than you want for this.

P = proportional - signal from this part is proportional to error
I = integral - this portion integrates the error over time to allow for large corrections when large errors occur, though there are potential problems (integral windup) that make this undesirable for certain systems
D = derivative - this indicates the direction of error to allow for more rapid corrections

PID, PI, and PD controllers are typically only used for controlling more complex systems in which you want a decay ratio or some such. Let me know if you need more info.
 

hoppa

Senior member
Apr 10, 2004
253
0
0
I saw PID I thought Process ID and I got all amped for some C shell programming or something. oh well =)
 

f95toli

Golden Member
Nov 21, 2002
1,547
0
0
I still doubt a pure P-controller is a good idea, you are very likely to overshoot and start to oscillate; at least for values of P that gives you a reasonable fast response. P controllers work well in slow systems without lag. However, a P controller will work if you use a small P, it will just be extremely slow.

For an application like this I think a PI controller should work, if you don't need perfect controll you don't need to calculate anything, you can just start by choosing a good value of P (one that does not cause the system to overshoot too much) and then increase T slightly; after a few iterations you should be able to find something that work (it is MUCH harder to find good values for a PID controller this way).
 

rgwalt

Diamond Member
Apr 22, 2000
7,393
0
0
K = Proportional controller gain
E = Error between signal and setpoint
ti = Integral time
td = Derivative time

P = K*E
I = (1/ti)*Integral(Edt) from tstart to tcurrent
D = td*d(E)/dt

You can form combinations from each of these three types of control laws, though P, PI, and PID are most common. So, calculating a PID control signal would give:

K*E + (1/ti)*Integral(Edt) from tstart to tcurrent + td*d(E)/dt

Sometimes you'll see it calculated like this:

K*(E + (1/ti)*Integral(Edt) from tstart to tcurrent + td*d(E)/dt), so 1/ti and td would have to be scaled appropriately (by a factor of K) to be equivalent between the two systems.

An algorithm to compute the three types of control signal would be simple to develop...

Look up the book "Process Control" by Wayne Bequette in your school's science/engineering library (you are in college, right?). I have another controls book at home. If you want the name/author, drop me a PM.

Ryan
 

crazychicken

Platinum Member
Jan 20, 2001
2,081
0
0
the idea is for this not to be slow haha. There is a competition at the end of this course to see who's car can do the best on the track and go the fastest.

if i use f95toli's recommendation of a PI control, how exactly do i do the time integral of the error signal in c?

so lets say my "target" is for a particual photodiode to read 3v. If it reads 1v, then the error is 2v.

what is the integral error?
so i set the servos to
new value=error*p+integral error*integral constant

right?

sorry i'm so confusing

david
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
what is the integral error?

Integral error is a term that is the sum of the error measurements since some time (ie, the integral of the error value from t=0 if you graphed it as a function of time). The simplest way to represent it is to keep a 'running total' of the error measurements (multiplied by some constant) in a variable.

Derivative error is the first derivative of the error. You can calculate this, roughly, by tracking the last few calculated error values and doing a linear approximation of the derivative (change in error / change in time). You probably want to actually use the 'inverse' of the derivative; if the error is growing, you want to try harder to correct it, whereas if it is rapidly shrinking, you want to slow down your correction so that you don't overshoot.

What you do with the error value is dependent on the problem and what you are trying to achieve. More details would help, but keep in mind that this is not a homework forum!
 

FrankSchwab

Senior member
Nov 8, 2002
218
0
0
Not having done any PID work, my opinion is suspect...but I do have some half-baked intuitions...

"P" gives you an instantaneous measure of how far away you are from your desired (in this instance) location. For example, you're 2cm left of the line - you should probably turn right. The question, is how much to turn right - the amount is the "gain" for this term.
"I" gives you a way to deal with not quite catching up to the location. This helps deal with systemic errors that would otherwise be uncorrectable. For example, imagine following a line with a continually increasing curve. Your P term will tell you that you're left of the line; you adjust a little bit, and on your next measurement, you're still left of the line; you adjust a little bit, and on your next measurement, you're still left of the line. The I term allows you to sum these errors, and increase your gain (how much you turn) to get directly back over the line. It also helps deal with mechanical uncertainties - for example, if telling your steering to move 1 click to the right doesn't actually cause anything to happen (due to slop in the mechanisms), this term will help you get directly over the line.
"D" tells you how fast (and in what direction) the error is changing. Imagine a situation where you are way left of the line - you crank the steering hard right, and after a measurement or two shoot past the line, and are now going away from the line to the right. The D term allows you to say "hey, the error is getting smaller, maybe I should reduce some gain".

Control theory helps to define the optimal values for the gains for each of these terms; providing, of course, that you can elucidate the equations of motion of your system. Barring that, you'll need to do some tweaking of the gains that you assign the different terms to make things stable.

So,
P = How far off center you are - a Direct measurement.
I = Sum of P's over time. Some should be negative, some positive, you're trying to keep this 0.
D = Difference in P between now and the previous measurement. Use this to see where you're going to be in the future, and use it to help adjust your gain.

/frank
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Originally posted by: crazychicken
the idea is for this not to be slow haha. There is a competition at the end of this course to see who's car can do the best on the track and go the fastest.

if i use f95toli's recommendation of a PI control, how exactly do i do the time integral of the error signal in c?

so lets say my "target" is for a particual photodiode to read 3v. If it reads 1v, then the error is 2v.

what is the integral error?
so i set the servos to
new value=error*p+integral error*integral constant

right?

sorry i'm so confusing

david
The integral error is easily performed as a summation. Actually, I'm sure if you Google this you can find an explicit code that will do it for you, though it's probably worth your while to develop it yourself so you understand how it works. Otherwise, you won't really know how to tweak it to get it to work properly.
 

johnshaw

Junior Member
Feb 19, 2005
1
0
0
I have spent a few decades implementing the PID control algorithm, as well as more advanced techniques. Some information can be found at:
www.jashaw.com/pid

Some information is there for free, I have more in a downloadable book and a downloadable Excel based simulation, both for a small charge.

John Shaw
Process Control Solutions