View Full Version : [Question(s)] PID Questions

11-09-2008, 02:36 AM
I'm struggling with writing my own software to control what are essentially servos. I have motors with an ESC for each one and a pot to register position. What I need is an algorithm to control the motor speed based on the difference between the current pot output and the goal. The standard solution to this problem -- a PID controller -- is a bit of a confused mess that I can't figure out.

I've implemented the "P" part -- Proportional. If there's a big difference in angle (pot minus goal) we move fast, otherwise slow, but always in the direction of the goal. The problem with this is that there is either overshoot or, if sensitivity is tuned low which is what I want, there's a big jerk when it comes to a stop. The other terms I can't figure out.

"Integral" -- how is that computed? Coming from a mathematical background I know that if the angle difference is zero from the start of time until now, the integral can be defined as zero. Fine. Now we set a new goal and the difference is non-zero. We respond and perhaps reach a new equilibrium where the angle difference is zero again. In any case the integral is non-zero forever from now until the end of time because of that short non-zero period. And yet that's a linear component to the control equation. How does that work?

And derivative is not much better, at least in practical terms. What is the method to measure derivative given discrete samples? Say I read out the pot at 237 and then 10 milliseconds later the pot is still 237 -- does that mean that the derivative term is zero for that round? Then 10ms later the value is 238. Is that treated as a rate of 1/10ms or 1/20ms?

For those who made it this far my question is: what is the practical method for damping the integral and smoothing the derivative in a PID controller?

If someone can explain that then we'll talk about tuning the parameters.

11-09-2008, 11:02 AM
I ran across a link where a guy was doing much the same thing. He lists his code and algorithms on the page:

Found via Make:

11-09-2008, 01:21 PM
That looks pretty simple. Seems too simple, but hey, I've been known to overthink these things a time or two. +Rep, Jes, Thanks for that link - bookmarked and stored away. :)

11-09-2008, 06:32 PM
Thanks for the links jes. The code there does answer the question of how people generally deal with sampling error and damping -- they don't. A controller with an integral term will always overshoot because of the need to "unwind" the integration. Actually Wescott did put an upper bound on the term in his code to reduce total possible saturation, but this reduces the effect without solving it. As for error in the differential, he mentions that it may be noisy due to uneven sampling, but doesn't really give any hints about how to mitigate that effect.

I've been running some simulations and I really like the smooth response of an integrative controller. I've found that I can eliminate overshoot by using a differential term and by damping the integration input while the differential is non-zero. Well see how that works out in practice.

11-09-2008, 07:01 PM
I'm looking forward to seeing what you come up with, Meta. I haven't had occassion to give it much thought - it'd be awesome if you come up with a working algorithm and share it!

11-17-2008, 02:12 AM
So I've spent a week working on this problem and I'm now discouraged and frustrated. The theory is sound -- at least on the whiteboard. In numerical simulation it also works OK. For some reason the physical system doesn't respond the same way; no matter how I tune the constants I get overshoot or lag. Small differences in particular result in ever increasing oscillations that would be really bad if I didn't have software limits that shut the system down in that case.

Any other references I need to be looking at? I'm thinking that perhaps I should calibrate my motors so that I can get an ideal linear response. The problem is that if the load changes -- perhaps just because the forearm is extended rather than withdrawn -- the response could change radically. It's hard to build a model that takes all the loads into account, and I was hoping not to.

11-17-2008, 06:22 AM
That is essentially the problem with PID, because of the inter-relatedness of the individual terms, tuning the system is nothing short of magic.

I had meant to send this link before. http://barello.net/Papers/Motor%20Control,%20SRS%2020080621.pdf

On page 4, he lays out a method for tuning:
1. set all gains to 0
2. increase Kp until oscillations occur, set final Kp to 2/3 of this value
3. increase Kd until there is no overshoot and response is snappy
4. increase Ki until to remove any residual error

In theory it work, but of course, the terms are so inter-related, that such a system isn't guaranteed to function.

11-17-2008, 11:26 AM
In the world of Instrumentation, loop tuning is in fact a "black art". After doing it for 30 or so years, I still learned something every time I had to tune a system. It never ceased to amaze me that 2 apparently identical systems could be very different to tune.

There is software now that gets a person in the ball park, but it still takes the tech to do the magic.

Here is the steps I would take for a basic loop.

1: Set Proportional (Gain), Integral, and Derivative to 0.
2: Set P first until you get oscillation, then back off by half. Then increase by half of what you backed off. If no Osc, increase by half of the remainder. If it oscillates, back off a tad. Eventually you arrive very close to the optimal setting.
3: Now start with the Integral until it is just too fast acting to work, and you're back in the oscillatiion game. Do the half step thing again until it settles in. This may have an effect on the Proportional.
4: Derivative... You may not need it. Don't mess with it unless really necessary as it adds another level of confusion.

Not everyone will agree with my methods, as I learned the basics back in the era of pneumatics. They worked for me for a lot of years. The starting settings are arbitrary, but I wouldn't start with a really large setting.

I like to look at a PID loop in the sense of "P" is how much correction you want to a given error. "I" How fast do you want it to react. "D" predicting the error.

Proportional vs Gain... a matter of how you think about things. I personally always liked Proportional as it gives a number larger than 1 where gain is a decimal. They are reciprocals of one another.

Hope this might help.