text
stringlengths 0
715
|
---|
float kI = 0; |
float kD = 0; |
Why are kI and kD both 0? |
If the robot barely moves, try increasing kP--this increases the motor power applied. If the robot jerks forward and goes too far, try decreasing kP. |
Your goal should be the green line in the graph below, where the robot reached the setpoint (target) quickly and stays there. The graphs represent's the robot's current distance, compared to the target; this applies to both turn PIDs and drive PIDs. |
Source: OscarLiang.com |
If your robot acts like the orange line on the graph above, where the robot goes past the target and reverses direction, consider adding in the kD term. As a starting point, set this term to 5 times the value of the kP term. Adding in the integral term (start at about 1/10th the value of kP) may also help. |
If your robot acts like the blue line on the graph above, where the robot takes a while to reach the target, increase kP or decrease kD. |
These rest of PID tuning comes down to trial and error; it's your job to find the set of values that works best for your specific robot. |
If you want a more mathematical approach to estimating kP, kI, and kD from the start, take a look at this page: |
Ziegler-Nichols Method |
Advanced coding shenanigins... |
The Ziegler-Nichols method is a simple way to estimate decent values for kP, kI, and kD. These estimates are a good starting point for manual tuning. |
We need two numbers before we can run this method: |
* Ku, the critical (or ultimate) gain |
* Tu, the period of the oscillations at the critical gain |
That is, Ku is the minimum value of kP for which the robot will oscillate steadily around the target. To find it, slowly increase kP until the robot wobbles consistently across the target. Then, find Tu by taking a video of the robot oscillating, and find the period, in seconds, of the oscillation. The period is how long it takes the robot to go from one side of the target, to the other side, and then back again. |
Once you've found Ku and kP, you can then calculate the estimated values of kP, kI, and kD using a few equations, as shown in this PID calculator: |
Alternate Link: https://html-7159868.codehs.me/pid.html |
|
A small technicality |
Graphing PIDs |
Graphing PIDs can be helpful when tuning them; it gives you a good idea of how quick and accurate the PID is. |
In the graph below, the green line shows how far the robot is from the target point. The robot starts off far from the target point, but reaches it by the end of the graph. The orange line is the motor power, which initially increases sharply, but then decreases when the robot gets close to the target point so it doesn't overshoot. |
Here's our simple graph |
First of all, make sure you include a few libraries at the top of the main file. |
#include <cmath> |
#include <vector> |
#include <cstdlib> |
Next, here's the main graphing function. It clears the brain screen and draws a graph displaying the robot's progress towards the target, and the motor power too. |
//graphing data, used for PID tuning |
void graphPID(std::vector<int> errorHistory, std::vector<float> powerHistory, int goal, float error, int time) { |
//goal is the PID goal (driveDistance) |
//error history is a list of all of the errors (range is 0 to driveDistance) |
//powerHistory is a list of the power applied (range is -1 to 1) |
//error is the current error |
//time is the current time, in milliseconds |
//Setup: clear screen and draw the target line |
Brain.Screen.clearScreen(); |
Brain.Screen.setPenWidth(2); |
Brain.Screen.setPenColor(white); |
Brain.Screen.drawLine(0, 60, 480, 60); |
Brain.Screen.setPenWidth(1); |
Brain.Screen.setPenColor(green); |
|
//display final error and time |
Brain.Screen.setCursor(1, 1); |
Brain.Screen.clearLine(1); |
Brain.Screen.print(" Final Error: "); |
Brain.Screen.print(error); |
Brain.Screen.print(" Time: "); |
Brain.Screen.print(time); |
//define the borders of the graph |
int minY = 60; //error = 0 (robot is at target) |
int maxY = 230; //error = driveDistance (Robot is at start) |
int minX = 10; //time = beginning |
int maxX = 470; //time = end |
//loop through each data point and graph it |
for (int i = 0; i < errorHistory.size() - 1; i++) { |
int x = minX + (maxX - minX) * i / errorHistory.size(); //find the x-value of this data point |
//graph velocity |
Brain.Screen.setPenColor(green); |
Brain.Screen.drawLine(x, minY + (float)errorHistory.at(i) / goal * (maxY - minY), x + (float)(maxX - minX) / errorHistory.size(), minY + (float)errorHistory.at(i + 1) / goal * (maxY - minY)); |
//graph power, changing color based on direction |
if (powerHistory.at(i) > 0) { |
Brain.Screen.setPenColor(orange); |
} else { |
Brain.Screen.setPenColor(yellow); |
} |
Brain.Screen.drawLine(x, maxY - std::abs(powerHistory.at(i)) * (maxY - minY), x + (float)(maxX - minX) / errorHistory.size(), maxY - std::abs(powerHistory.at(i + 1)) * (maxY - minY)); |
} |
} |
We've made the graphing function; now we just have to call it. But first, we need to log the error history and power history in the PID function. Put this code in the PID function, before the while loop. |
//lists |
std::vector<int> errorHistory; //keep track of error over time |
std::vector<float> powerHistory; //keep track of motor power over time |
int currentTime = 0; //keep track of time over time (wow!) |
Next, we'll update the history vectors with the current values, and then call the function. Put this code in the PID function while loop, before the wait(20, msec); line: |
//update histories and current time |
errorHistory.push_back(error); |
powerHistory.push_back(std::abs(motorPower)); |
currentTime += 20; |
|
//graph the PIDs |
graphPID(errorHistory, powerHistory, driveDistance, error, currrentTime); |
And with that, you should have a working graph while your PIDs are running! This graph can be used to make tuning the PIDs much easier, as it allows you to see the exact path of the robot to the target point. |
Tasks |
Multithreading made simple |
Tasks allow the code to execute multiple functions simultaneously. For example, one task could handle the driving inputs, and another task could spin a catapult or flywheel at the same time. |
Tasks themselves are functions that return an integer (it's C++, go along with it). You can put whatever code you want inside of it, but usually there's a while loop so actions within the task execute more than once. Here's a basic task (called myTask) that spins a flywheel forward: |
int myTask() { |
while (true) { |
//do something here |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.