-
-
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 13 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,118 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| # 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 has three components: proportional controller, integral controller, and derivative controller. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Before we get into how a PID controller works, we need a good example to explain things. | ||||||||||||||||||||||||||||||||||||||||||||||
| For the following sections, imagine you are designing a self driving RC car that tries to remain on a line as it is moving with a constant speed. How would you keep it on course. | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| For the following sections, imagine you are designing a self driving RC car that tries to remain on a line as it is moving with a constant speed. How would you keep it on course. | |
| For the following sections, imagine you are designing a self-driving RC car that tries to remain on a line as it is moving with a constant speed. How would you keep it on course? |
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 add a sentence here like:
This could be done with a PID controller, which is a combination of Proportional, Integral, and Derivative (PID) controllers.
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 introducing it over time, to make it easier to follow.
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.
My argument was that it was not followable without a transitional sentence.
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.
| Since there are a range of angles you can turn the wheel, you should turn proportional to the distance from the line. | |
| Since there are a range of angles you could turn the wheel by, it is unclear what strategy would work best to return the RC car to the line; however, it is clear that if the angle chosen is proportional to the distance from the line, the car will always be moving towards it. |
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_{p}$ is a constant and $e(t)$ is the current distance from the line, which is called the error. | |
| Where $$K_{p}$$ is an arbitrary constant and $$e(t)$$ is the current distance from the line, which is called the 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.
| 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.
| if $K_{p}$ is too high then when the error is too high, the system becomes unstable. | |
| however, if $$K_{p}$$ and $$e(t)$$ are too high then the system becomes unstable. |
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 would be nice to have an animation to show these.
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.
| 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. | |
| <div style="text-align:center"> | |
| <video width="500" height="500" autoplay controls loop> | |
| <source src="res/circling.mp4" type="video/mp4"> | |
| Your browser does not support the video tag. | |
| </video> | |
| </div> | |
| If the proportional controller is used alone, the car will not move appropriately. | |
| For example, if the car is too far out, the angle will be above $$2\pi$$, which means that the car cannot quite return to the line and will instead move in circles, as shown below: | |
| <div style="text-align:center"> | |
| <video style="width:100%" controls> | |
| <source src="res/circling.mp4" type="video/mp4"> | |
| <img class="center" src="res/IFS_triangle_4.png" alt="First Children" style=" | |
| width:100%"> | |
| </video> | |
| </div> | |
| On the other hand, if the car is sufficiently close to the line, it will oscillate back and forth, but never rest on the line, itself, as shown below: | |
| ADD VIDEO |
At least, I think this is why it turns in circles. I need to look at it a bit more.
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.
| One way to solve this is to make the rc car resistant to sudden changes of error. | |
| One way to do 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 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. |
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.
Could we show an animation with all three of these side-by-side?
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 Proportional and Derivative controllers are robust enough to keep on course, but what if some wind starts pushing the car constantly of track? | |
| The Proportional and Derivative controllers are robust enough to keep the RC car on course, but what if some wind or other external force starts pushing the car constantly off 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.
| Well, we would need to know if we are spending too long on one side to account for it, and we can figure it out by summing up all the displacements, usually refered to as errors, and multiply it by a constant. | |
| Well, we would need to know if we are spending too long on one side to account for this, and we can figure that out by taking a sum of all the displacements, usually referred to as errors, and multiplying by a constant. |
Sorry, I just didn't know what "it" was exactly.
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.
\uptau doesn't seem to exist in mathjax, can we use tau?
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.
| To use a PID controller, you need to tune it by setting the constants, $K_{p}$, $K_{i}$, and $K_{d}$. | |
| To use a PID controller, you need to tune it by setting the constants, $$K_{p}$$, $$K_{i}$$, and $$K_{d}$$. |
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.
| There are multiple methods of tuning like, manual tuning, Ziegler–Nichols, Tyreus Luyben, Cohen–Coon, and Åström-Hägglund.{{ "wikipid" | cite }} | |
| There are multiple methods of tuning like manual tuning, Ziegler–Nichols, Tyreus Luyben, Cohen–Coon, and Åström-Hägglund.{{ "wikipid" | cite }} | |
| `` |
leios marked this conversation as resolved.
Show resolved
Hide resolved
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.
Move this to after the example code
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 |
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 need some more information here or in the code because it's unclear. What does setpoint mean? "where the numbers represent the center of the lane" is really vague. What does that mean?
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 don't know what you want me to write there, I can't make it more clear.
Uh oh!
There was an error while loading. Please reload this page.