-
-
Notifications
You must be signed in to change notification settings - Fork 360
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 2 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,83 @@ | ||||||||||
| #Proportional-Integral-Derivative Controller | ||||||||||
|
||||||||||
|
|
||||||||||
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 in three parts proportional controller, integral controller, and derivative controller. | ||||||||||
|
||||||||||
|
|
||||||||||
| Before we get into how a PID controller works, we need a good example to use to explain how it work. | ||||||||||
|
||||||||||
| Imagine you are making a self driving rc car that drives on a line, how wuld make it work given that the car moves with a constent speed. | ||||||||||
|
||||||||||
|
|
||||||||||
|
Member
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:
Contributor
Author
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.
Member
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 would turn left and visa versa. | ||||||||||
|
||||||||||
| Since there is a range of angles you can turn the wheel, you can turn with proportion to how far you are from the line. | ||||||||||
|
||||||||||
| This is what the proportional controller (P controller) does, which is given by, | ||||||||||
|
||||||||||
|
|
||||||||||
| $$ P = K_{p} e(t), $$ | ||||||||||
|
|
||||||||||
| Where $K_{p}$ is a constant and $e(t)$ is the current error. | ||||||||||
|
||||||||||
| The performance of the controller improves with larger $K_{p}$; | ||||||||||
|
||||||||||
| The performance of the controller improves with larger $K_{p}$; | |
| The performance of the controller improves with larger $$K_{p}$$; |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it zig zag out of control?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is also not clear to me. I would be expecting an instability like what we see with euler methods. Also: what if K is a function of the error? Is this common practice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this was not addressed.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overshoting -> overshooting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted that I believe this statement should be in the previous section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer it here.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we -> We.
on -> one
I would split the sentences instead of using the comma
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make a more robust controller, we need to...
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The controller is not "given" by the formula. The behavior of the controller is described by the formula. The same problem is present in the P and I parts as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are de and dt? Maybe note that this is something like a derivative?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a derivative.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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. | |
| 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. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| If it's too low the system is underdamped, i.e. the car oscillates around the line. | |
| If it's too low, the system is underdamped, i.e. the car oscillates around the line. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the car returns to the track quickly...
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I -> It
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like:
The Proportional and Derivative controllers are enough to robustly control our system, but if some wind starts pushing the car while we are driving, then we will get a constant error.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To correct this error, we need...
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
Well, we would need to know if we are spending too long on one side and account for that, and we can figure that out by summing up all the errors and multiply it by a constant.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a different e? Its dependent on position instead of time. Maybe it would be nice to create three separate errors e_P, e_D and e_I so we can differentiate?
Also, is the integral going from 0 -> t but in terms of dx?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, a side-by-side animation would do well here
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PID controller is just a sum of all three controllers and is of the form,
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tune it, -> tune it (it doesn't need the comma)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to see one of those method expanded later, I've tried to make a PID controlled segway-type device using an arduino once, but I could never quite get the constants right manually.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mesurable -> measurable
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
speed, and more -> speed and more
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ## The Algorithm | |
| ## Putting it all together |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You -> you
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this, -> this:
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not clear from the code what this example is supposed to output or do, in general.
I just need a clear description of what this example code is trying to do. I am having a little trouble figuring out what the code is supposed to be doing, but I think it's a 1D analog to the car example, right? We are trying to keep the car on setpoint?
Uh oh!
There was an error while loading. Please reload this page.