Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi
I've been looking up PID and found a lot of useful info- but none could apply to my problem. So I borrow part of the code and I'll post it here hoping someone can tell me If I am doing it right or wrong for whatever I am doing.

So I have an articulated driving robot. That is, it turns right or left just be my sending to the Joint Actuator a number.

So I am working on a follow the wall problem, almost. Where I will tell the robot to turn left or turn right base on the distance from the wall.

Now, the robot seems to jiggles (Z patterns) quite a bit so I think I'll need some PID to stabilized it, hopefully.

Learning PID theory during undergrad was easy enough but now I find it a lot different then to program one. So, all I learned in school was finding TF of the plant and using rootlocus Nyquist plot or PZ placement .. and some Matlab command with a Step input response and there, I have a perfect working control. But hey, that was not anywhere close to what I am doing now. So all those school was useless :(


C#
// calculate the difference between the desired value and the actual value
           error = setpoint - PV;
           // track error over time, scaled to the timer interval
           integral = integral + (error * Dt);
           // determin the amount of change from the last time checked
           derivative = (error - preError) / Dt;

           // calculate how much drive the output in order to get to the
           // desired setpoint.
           output = (Kp * error) + (Ki * integral) + (Kd * derivative);

           // remember the error for the next time around.
           preError = error;



So in the above code, I am not too sure of the following, please explain if you can.

1.
Dt = sampling time? What is Dt for my case.

2.
output = the value i will need send to the actuator?

3.
PV = the value the Actuator currently holds?

4.
Setpoint = the new turn value to be send to the actuator?

BTW, my biggest problem of all is the Dt. I don't know when to check for PV and how to calculate Dt. Is it the change in time between now and the last time I check for PV? If so, then that will be quite small then.

Any help is appreciated.
Posted

1 solution

Setpoint = the desired position (or velocity or angle or whatever)
PV = the current position (or velocity or angle or whatever)

Dt = delta time (time since previous sample)

Output = what you are going to send to the actuator.

Kp, Ki, Kd = tunable constants -- you tune these to your system so that you smoothly control your actuator without too much overshoot (zig-zag around desired course) or undershoot (takes too long to get to desired course)

Dt should be calculated as the actual time between the now and the last time you checked. Ideally it is a small amount of time.

The number itself isn't necessarily small depending on the units you use to express it. Whatever units you use are bascially canceled out by your choice of Kp, Ki, Kd. If Dt is in milliseconds, then Kp is 1000x and Kd is 1/1000 of what it would be if Dt was in seconds. If you are doing all your calculations in floating point arithmetic, things should be fine no matter what units you use as long as you are consistent.

The wikipedia article gives you some guidance on how to tune Kp, Ki, and Kd to improve your system performance:

http://en.wikipedia.org/wiki/PID_controller[^]
 
Share this answer
 
Comments
aznftw 15-Jun-12 10:24am    
Hi I don't quite understand what you meant by "Whatever units you use are bascially canceled out by your choice of Kp, Ki, Kd. If Dt is in milliseconds, then Kp is 1000x and Kd is 1/1000 of what it would be if Dt was in seconds." can you explain that a bit more, an example if possible?
TRK3 15-Jun-12 13:16pm    
For purposes of illustration, lets assume that the input to your actuator is in units of volts (i.e. output is in volts) and that your setpoint and PV are measurement of an angle (in degrees).

Error also will be in units of degrees.

If your time is measured in milliSeconds, then:
* integral is in units of degree-milliSeconds
* derivative in in units of degrees per milliSecond

To get your output in volts, then:

Kp is in units of volts per degree.
(degrees * volts/degree = volts)

Ki is in units of volts per degree-millisecond
(degree-milliseconds * volts/degree-milliseconds = volts)

Kd in in units of volt-milliseconds per degree
(degrees/millisecond * volt-milliseconds = volts)

If you express time in terms of seconds, then

Ki is then in units of volts per degree-second (and is 1000x bigger to compensate for the fact that your multiplying by a time number is 1000x smaller)

Kd is then in units of volt-seconds per degree (and is 1000x smaller to compensate for the fact that you are dividing by a time number that is 1000x smaller)

All I'm saying is it doesn't matter what units you use, at the end of the day you've got 3 arbitrary constants whose values you are going to pick in a way that cancels out whatever units you choose.
aznftw 21-Jun-12 10:19am    
Hi

Right now I am using only Kp. I found a kp that works with litle oscillation. I am gonna use Ziegler–Nichols method to find KD and KI. But, its result was having even more oscillation than just the P-controller alone.

Do you have any idea?

BTW. Do you know anyplace that give tutorial on finding PID mathematically?
TRK3 21-Jun-12 13:21pm    
I am not sure that you can find the values on a purely mathematical basis. The optimal values are dependent on physical attributes of the system (such as how much friction, inertia and measurement error) -- if you knew those things accurately, you could model the system and determine the best choice of constants. However, if your model is wrong the constants will be wrong -- deriving the optimal constants empirically is a better approach.

You may not need KD and KI -- read the Wikipedia article. You only need the KI term if there is a cumulative error in your system (such as a measurement error) and you only need the KD term if you want to reduce the overshoot in the system. If there is already a lot of friction in the system, KD usually won't be needed.
aznftw 21-Jun-12 13:46pm    
I was thinking of finding the transfer function of my system. but, base on what you said about the friction and the inertia, it really freak me out. so, I am guessing now the transfer function won't even help much eh?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900