You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/section-7/control.md
+17-6Lines changed: 17 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,24 @@ Or is there?
8
8
9
9
Control systems are the solutions to the three problems mentioned above. They help programmers manipulate motors to a great deal of control and flexibility. When designing a control algorithm for a robot mechanism, there are a number of different approaches to take. These range from very simple approaches, to advanced and complex ones. Each has tradeoffs. Some will work better than others in different situations, some require more mathematical analysis than others.
10
10
11
-
There are two fundamental types of mechanism controller that we will cover here:
11
+
## Feedback and Feedforward
12
12
13
-
**Feedforward control (or “open-loop control”)** refers to the class of algorithms which incorporate knowledge of how the mechanism under control is expected to operate. For example, using physics we can account for the force of gravity, friction, and other forces. Using this “model” of operation, the control input is chosen to make the mechanism get close to where it should be.
13
+
There are two fundamental types of mechanism controllers that we will cover here:
14
14
15
-
!!! note
16
-
The Feedforward control section is still being worked on, so only Feedback control is discussed.
15
+
**Feedforward control (or “open-loop control”)** refers to the class of algorithms which incorporate knowledge of how the mechanism under control is expected to operate. For example, using physics we can account for the force of gravity, friction, and other forces. Using this “model” of operation, the control input is chosen to make the mechanism get close to where it should be. In general, feedforward control is required whenever the system requires some constant control signal to remain at the desired setpoint (such as position control of a vertical arm where gravity will cause the arm to fall)
17
16
18
-
**Feedback control (or “closed-loop control”)** refers to the class of algorithms which use sensors to measure what a mechanism is doing, and issue corrective commands to move a mechanism from where it actually is, to where you want it to be.
17
+
**Feedback control (or “closed-loop control”)** refers to the class of algorithms which use sensors to measure what a mechanism is doing, and issue corrective commands to move a mechanism from where it actually is, to where you want it to be. Even with unlimited study, it is impossible to know every force that will be exerted on a robot’s mechanism in perfect detail. So feedback control is used to correct the error not covered by feedforward control.
19
18
20
-
It is usually common and best to use both. In the next few pages, we will discuss both control types extensively.
19
+
!!! Note
20
+
Basically "feed**forward**" is for predicting how you will act, "feed**back**" is for fixing your mistakes.
21
+
22
+
It is usually common and best to use both. In the next few pages, we will discuss both control types extensively.
23
+
24
+
## Trapezoid Profiles
25
+
26
+
It is often easy to get a motor to move at a certain velocity and acceleration using Feedforward + Feedback (which will be explained in the next few articles). However, what if we want a motor to turn to an exact position? In order to use feedforward effectively for position control, we need to come up with a sequence of velocities that will take the robot mechanism to the desire position. This is called a motion profile.
27
+
28
+
!!! warning
29
+
The Trapezoid Profiling section is still being worked on.
30
+
31
+
In some situations, using a motion profile is overkill and feedback control is enough. We will explain why that sometimes pure pid or motion profiling would be for certain situations.
Copy file name to clipboardExpand all lines: docs/section-7/feedback-control.md
+25-4Lines changed: 25 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,10 @@ As a summary, the controller has 3 parts:
20
20
The three quantities are added together to drive the error to zero. The three constants can be "tuned" (by changing around their values) for the error to be driven faster, slower, or to be less volatile.
21
21
22
22
In code, it looks something like this:
23
+
24
+
!!! warning
25
+
The code excerpt is only meant to show how PID works. This is not how we actually implement PID, but should give you a better idea of the inner workings of PID.
26
+
23
27
```Java
24
28
25
29
// kP, kI, kD can be any value, for example kP can = 0.5
where u(t) is the control effort (amount of feedback at time t), e(t) is the error at current time t, and \\(\tau\\) is the integration variable. Do not worry about the formula as long as you understand the video and how the code works.
63
68
64
69
### **PID in a nutshell:**
@@ -75,8 +80,14 @@ In general, the "optimal" PID controller gets the error to zero as fast as possi
75
80

76
81
77
82
To get those ideal constants... you kind of just guess and check. WPILIB provides a nice simulation of how it is like to tune these constants.
78
-
### [Click here to try tuning a PID Controller](https://docs.wpilib.org/en/stable/docs/software/advanced-controls/introduction/tuning-flywheel.html)
79
-
Scroll until you get to "Pure Feedback Control". Follow the instructions and see if you can get the optimal tuning solution. **DO NOT SKIP THIS PRACTICE**
83
+
84
+
[Click here to try tuning a PID Controller. Scroll to "Pure Feedback Control, skip everything else](https://docs.wpilib.org/en/stable/docs/software/advanced-controls/introduction/tuning-flywheel.html)
85
+
86
+
Follow the instructions and see if you can get the optimal tuning solution. The model simulates a flywheel shooter mechanism and halfway through the simulation it shoots a ball. **DO NOT SKIP THIS PRACTICE**
87
+
88
+
!!! note
89
+
Often times you don't actually have to use all the constants. In fact, it is generally advised not to use the I constant. When you omit the 'I' variable, the controller is called a "PD" rather than a "PID" controller. Similarly if you omit the 'I' and 'D' variable, the controller is called a P controller.
90
+
If P, PD, or PI controller works, leave it be. You should always implement the easiest solution to avoid bugs and confusion.
And for SparkMax, SparkFlex, or other Rev motor controllers, we use the [`SparkPIDController`](https://codedocs.revrobotics.com/java/com/revrobotics/sparkpidcontroller) (Take a look at the javadocs). Notice how there are other methods that let you control the velocity, acceleration, setting target, and more:
@@ -153,13 +168,19 @@ if (!pidController.atSetpoint()) {
153
168
pidController.reset();
154
169
```
155
170
156
-
You need to check out the documentation for each of these classes and familiarize youself with them.
171
+
!!! note
172
+
`SparkPIDController` which runs on a CANSparkMax can [obtain inputs and outputs at 1ms](https://docs.revrobotics.com/brushless/spark-max/control-interfaces), while the `PIDController` which runs on the RoboRIO runs every 20ms. For this reason, it is preferred to use the `SparkPIDController` for more precise control. Note that you need to write additional code to make the CANSparkMax run faster since by default it runs every 20ms.
173
+
174
+
You need to check out the documentation for each of these classes and familiarize youself with them. [Definitely read WPILIB's documentation on their PID Controllers](https://docs.wpilib.org/en/stable/docs/software/advanced-controls/controllers/pidcontroller.html).
157
175
158
176
# Conclusion
159
177
Thankfully, most of the math is handled by WPIlib or motor controller firmware. However, it is important to understand what is actually happening so that you can properly tune your control loops.
160
178
161
179
But PID is just the beginning. There are many more complicated and more powerful control methods built upon PID that will be discussed later in this section, including Following Trajectories using PathPlanner and Drivetrain Characterization. In addition, PID is often not enough to properly control a mechanism, feedforward algorithms are also needed...
162
180
181
+
## Additional materials
182
+
[This YouTube playlist explains in more detail of how PID works. Not required but highly recommended](https://www.youtube.com/watch?v=wkfEZmsQqiA&list=PLn8PRpmsu08pQBgjxYFXSsODEF3Jqmm-y)
0 commit comments