-
-
Notifications
You must be signed in to change notification settings - Fork 359
Adding PID controller Chapter. #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 7 commits
37b0068
874abf5
17b7226
aaa8c40
7a1ad44
4c0aa45
6bdd7ad
28c3f5c
182f4bf
6a8b5e1
a0608f3
45e66ef
570b7b1
2d2be13
aadf198
9a7849e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <stdio.h> | ||
|
||
struct pid_context { | ||
double kp; | ||
double ki; | ||
double kd; | ||
double setpoint; | ||
double last_error; | ||
double integral; | ||
double dt; // Normally you calculate the change in time. | ||
}; | ||
|
||
struct pid_context get_pid(double setpoint, double dt, double kp, double ki, | ||
double kd) { | ||
|
||
struct pid_context ctx = {0}; | ||
ctx.setpoint = setpoint; | ||
ctx.dt = dt; | ||
ctx.kp = kp; | ||
ctx.ki = ki; | ||
ctx.kd = kd; | ||
|
||
return ctx; | ||
} | ||
|
||
double pid_calculate(struct pid_context ctx, double input) { | ||
// Here you would calculate the time elapsed. | ||
double error = ctx.setpoint - input; | ||
ctx.integral += error * ctx.dt; | ||
double derivative = (error - ctx.last_error) / ctx.dt; | ||
ctx.last_error = error; | ||
|
||
return ctx.kp * error + ctx.ki * ctx.integral + ctx.kd * derivative; | ||
} | ||
|
||
int main() { | ||
struct pid_context ctx = get_pid(1.0, 0.01, 1.2, 1.0, 0.001); | ||
double input = 0.0; | ||
|
||
for (int i = 0; i < 100; ++i) { | ||
input += pid_calculate(ctx, input); | ||
printf("%g\n", input); | ||
} | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,86 @@ | ||||||||||
# Proportional-Integral-Derivative Controller | ||||||||||
Written by Gathros | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we might want to remove this now that we have the license at the bottom? I 'm actually not sure about this, though, so I'm happy leaving a note on authorship at the top too. |
||||||||||
|
||||||||||
Gathros marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
The Proportional-Integral-Derivative controller (PID controller) is a control loop feedback mechanism, used for continuously modulated control. | ||||||||||
The PID controller is comprised of three parts: proportional controller, integral controller, and derivative controller. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I might do something like this:
|
||||||||||
|
||||||||||
Before we get into how a PID controller works, we need a good example to explain things. | ||||||||||
Imagine you are making a self-driving RC car that drives on a line, how would make the car stay on track given that it moves with a constant speed. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence still doesn't make sense. Maybe:
|
||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a sentence here like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer introducing it over time, to make it easier to follow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My argument was that it was not followable without a transitional sentence. |
||||||||||
### Proportional Controller | ||||||||||
|
||||||||||
If the car is too far to the right then you should turn left and vice versa. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think sticking to "we" instead of "you" makes sense, but that's personal preference. Maybe something like:
|
||||||||||
Since there are a range of angles you can turn the wheel, you should turn proportional to the distance from the line. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
This is what the proportional controller (P controller) does, which is described by, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need a comma at the end |
||||||||||
|
||||||||||
$$ P = K_{p} e(t), $$ | ||||||||||
|
||||||||||
Where $K_{p}$ is a constant and $e(t)$ is the current distance from the line, which is called the error. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
The performance of the controller improves with larger $K_{p}$; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
if $K_{p}$ is too high then when the error is too high, the system becomes unstable. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
In this example, the car would turn in circles, since there is a maximum angle the wheel can turn, else it would zig zag around the line. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have an animation to show these. |
||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a note should be made here that the car's motion can be completely corrected with the Proportional controller, but it has the problem of overshooting a lot, thus additional controllers are necessary to maintain proper control of the car. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, this was not addressed. |
||||||||||
### Derivative Controller | ||||||||||
|
||||||||||
The P controller works well but it has the added problem of overshooting a lot, we need to dampen these oscillations. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the P controller really provide an oscillation? It seems like just an overshooting, right? If that's the case, it makes more sense to say "dampen this motion" then "dampen these oscillations" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P controllers may over shoot then it will start to oscillate around the track. So you're dampening this oscillation not the motion of the car. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear to me how we can model the error in the P controller as an oscillation. Again, an animation or some depiction could help here. |
||||||||||
One way to solve this is to make the rc car resistant to sudden changes of error. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
This is what the derivative controller (D controller) does, which is described by, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No comma at the end here either |
||||||||||
|
||||||||||
$$ D = K_{d} \frac{de(t)}{dt}$$ | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a derivative. |
||||||||||
|
||||||||||
Where $K_{d}$ is a constant. | ||||||||||
If $K_{d}$ is too high then the system is overdamped, i.e. the car takes too long to get back on track. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
If it's too low the system is underdamped, i.e. the car oscillates around the line. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
When the car returns to the track and there is little to no oscillations, the system is critically damped. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we show an animation with all three of these side-by-side? |
||||||||||
|
||||||||||
### Integral Controller | ||||||||||
|
||||||||||
The Proportional and Derivative controllers are robust enough to keep on course, but what if some wind starts pushing the car and introducing a constant error? | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
Well, we would need to know if we are spending too long on one side and account for it, we can figure it out by summing up all the errors and multiply it by a constant. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
This is what the integral controller (I controller) does, which is described by, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, no comma (I think) |
||||||||||
|
||||||||||
$$ I = K_{i} \int_{0}^{t} e(\uptau) d\uptau, $$ | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
|
||||||||||
Where $K_{i}$ is a constant. | ||||||||||
The peformance of the controller is better with higher $K_{i}$; but with higher $K_{i}$ it can introduce oscillations. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, a side-by-side animation would do well here |
||||||||||
|
||||||||||
### Proportional-Integral-Derivative Controller | ||||||||||
|
||||||||||
The PID controller is just a sum of all three controllers and is of the form, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again no comma (I think) |
||||||||||
|
||||||||||
$$ U = K_{p} e(t) + K_{i} \int_{0}^{t} e(x) dx + K_{d} \frac{de(t)}{dt} $$ | ||||||||||
|
||||||||||
To use a PID controller, you need to tune it by setting the constants, $K_{p}$, $K_{i}$, and $K_{d}$. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
If you choose the parameters for your PID controller incorrectly, the output will be unstable, i.e., the output diverges. | ||||||||||
There are multiple methods of tuning like, manual tuning, Ziegler–Nichols, Tyreus Luyben, Cohen–Coon, and Åström-Hägglund. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will these be covered? If so, leave a note. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They aren't algorithms it's just done by hand so I don't imagine there being a chapter on it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, then for completeness we might want to differentiate these. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If these are mentioned, they should be described. You can add a simple list with all of them and how they are differentiated from each other. It might be worth adding a separate heading for tuning and discussing these in-turn. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not link to the wiki page https://en.wikipedia.org/wiki/PID_controller#Overview_of_tuning_methods There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible, it would be nice to have citations for all of these with bibtex-cite There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you cite? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put the appropriate bibtex citation in the
at the bottom There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, Thanks. |
||||||||||
|
||||||||||
Theoretically, PID controllers can be used for any process with a measurable output and a known ideal output, | ||||||||||
leios marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
but controllers are used mainly for regulating temperature, pressure, force, flow rate, feed rate, speed and more. | ||||||||||
|
||||||||||
## The Algorithm | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
Luckily the algorithm is very simple, you just need to make the PID equation discrete. | ||||||||||
Thus, the equation looks like this: | ||||||||||
|
||||||||||
$$ U = K_{p} e(t_{j}) + \sum_{l=0}^{j} K_{i} e(t_{l}) \Delta t + K_{d} \frac{e(t_{j-1}) - e(t_{j})}{\Delta t}. $$ | ||||||||||
|
||||||||||
In the end the code looks like this: | ||||||||||
|
||||||||||
{% method %} | ||||||||||
{% sample lang="c" %} | ||||||||||
[import:26-34, lang:"c_cpp"](code/c/pid_controller.c) | ||||||||||
{% endmethod %} | ||||||||||
|
||||||||||
## Example Code | ||||||||||
|
||||||||||
The example code is of a 1-dimensional RC car that is trying to change from the first lane to the second lane, where the numbers represent the center of the lane. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need some more information here or in the code because it's unclear. What does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know what you want me to write there, I can't make it more clear. |
||||||||||
This example is we can't calculate the time elapsed, instead we are setting a value called dt for time elapsed. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sentence is unclear. Did you mean:
In this case, why are we using If the normal case is to calculate time, as indicated in line 10 of the code, why do we provide an example that doesn't do this? What is the point in the time parameter to begin with? Is it for dynamic timestepping? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I and D controllers use the time parameter for calculating integration and differentiation. |
||||||||||
|
||||||||||
{% method %} | ||||||||||
{% sample lang="c" %} | ||||||||||
[import, lang:"c_cpp"](code/c/pid_controller.c) | ||||||||||
{% endmethod %} | ||||||||||
|
||||||||||
<script> | ||||||||||
MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | ||||||||||
</script> |
Uh oh!
There was an error while loading. Please reload this page.