text
stringlengths
0
715
drivePID(800); //drive the robot forward 800 degrees
drivePID(-500); //drive the robot backwards 500 degrees
You can put the above code in your autonomous function.
We're not done yet, though--you'll have to tune the PID constants in order to make the PID work well for your specific robot.
Turn PID Tutorial
In this tutorial, we'll show you how to code a simple turn PID (i.e. the robot turns in place). We'll use an inertial sensor to keep track of the robot's rotation.
We'll assume you have already completed the Drive PID tutorial (you should). Thus, some steps will be simplified.
Before we get started, let's set up the inertial sensor in your devices tab. We'll name ours "Inertial",
Next, make sure you calibrate your inertial sensor in the main function of the robot using this line of code. This line should run when the program starts--make sure the robot is stationary for the first few seconds, so the inertial sensor calibrates correctly.
Inertial.calibrate();
Now, the PID! First of all, let's create a function called turnPID that accepts an integer variable, called turnDistance. This variable will tell the robot how far to turn, in degrees.
//Turn PID, turn the robot in place using a PID controller
int turnPID(int turnDistance) {
//driveDistance is how far the robot should turn, in degrees (positive is counterclockwise)
return 0; //Keep this line at the end of the function!
}
This function doesn't do anything yet, but it's a start. From now on, place all of the turn PID code in this function.
Next, let's add in our PID constants.
//PID constants
float kP = 0.05;
float kI = 0;
float kD = 0;
Before we get into the actual PID loop, we need to define a few more variables:
//Variables for turn PID
float error = 0; //how far the robot is from the target, in degrees
float integral = 0; //area under the error vs time graph
float derivative = 0; //slope of the error vs time graph
float prevError = 0; //the error for the previous iteration of the PID loop
//Motor power variables
float motorPower = 0; //how much power to apply to the motors, ranging from -1 (clockwise at full power) to 1 (counterclockwise at full power)
float prevMotorPower = 0; //the motor power for the previous iteration of the PID loop
One last thing--we need to keep track of the inertial sensor's initial position, so we know how far the robot has turned compared to it's rotation before the PID runs. We'll define a variable called startDistance to keep track of this.
float startDistance = Inertial.rotation(degrees);
Now that the setup is done, we can start coding the main PID loop. We'll start by calculating the current distance of the robot. We do this by taking the difference between the robot's current position and its starting position.
while(true) {
//Calculate the current distance of the robot and store it as a number (float)
float currentDistance = startDistance - Inertial.rotation(degrees);
wait(20, msec);//don't hog CPU, this stays at the end of the loop
}
What is currentDistance?
From now on, until told otherwise, assume all of the code goes in this while loop, after currentDistance is calculated, but before the wait function is called.
The next order of business is to calculate the robot's error--how far it is from the target. This is used to calculate the proportional term of the PID.
error = turnDistance - currentDistance; //calculate error
After that, we have to calculate the integral term. We'll only activate it when the robot is within 10 degrees of the target position, to avoid integral windup.
if (error < 10 && error > -10) {
integral += error; //updated the integral term if |error| < 10
}
Next, we have to find the derivative term. "Derivative" means "slope", so we can simply take the difference between the current error and the error in the last iteration of the loop (stored as prevError) to get the derivative term:
derivative = error - prevError; //calculate the derivative term
At this point, we can add the quintessential line of the PID algorithm! Here it is:
motorPower = (kP * error) + (kI * integral) + (kD * derivative); //calculate motor power
Then, we'll clamp the motorPower variable between -1 and 1.
//keep motor power between -1 and 1
if (motorPower > 1) motorPower = 1;
if (motorPower < -1) motorPower = -1;
Next, we'll add the slew rate limiter to prevent jerky robot motion.
//slew rate limiter
float slewRate = 0.1f;
if (motorPower > prevMotorPower + slewRate) motorPower = prevMotorPower + slewRate;
if (motorPower < prevMotorPower - slewRate) motorPower = prevMotorPower - slewRate;
Now, we can apply the refined motorPower variable to the motors. We multiply it by 11 because the motor voltage varies from -11 to 11. Note that the motorPower is multiplied by -11 for the left drive motors, so the robot turns in place instead of driving forward.
Left1.spin(forward, -11 * motorPower, volt);
Left2.spin(forward, -11 * motorPower, volt);
Left3.spin(forward, -11 * motorPower, volt);
Right1.spin(forward, 11 * motorPower, volt);
Right2.spin(forward, 11 * motorPower, volt);
Right3.spin(forward, 11 * motorPower, volt);
Next, we need to update the prevError and prevMotorPower variables, so they can be used in the next iteration of the loop.
//update "previous" variables
prevMotorPower = motorPower;
prevError = error;
All of the above code, combined, will work. But as of now, the code will keep running forever, even when the robot is at the target. Thus, we need to include a line of code that exits the PID loop once the robot is within 1 degree of the target. Feel free to tune the exit condition, but within 1 degree is a pretty good baseline.
//Exit the PID if the robot is within 1 degree of the target
if (error > -1 && error < 1) {
break;
}
That finishes up the code in the while loop. However, the motors may still be turning, even after the PID is done. To account for this, put this code after the while loop but before the end of the turnPID function:
//stop the motors when the PID is done
Left1.stop();
Left2.stop();
Left3.stop();
Right1.stop();
Right2.stop();
Right3.stop();
And with that, your turn PID code is done! We're not finished yet, though--you'll have to tune the PID constants in order to make the PID work well for your specific robot.
Tuning PIDs
Perseverance makes perfect
At this point, you should have coded the PIDs fully. Now, you have to tune those three constants to make the PID work well on your specific robot. Here's a quick recap of each constant:
* Proportional (kP): if the robot is far from the target, set the motor power high so the robot gets there faster. If the robot is close, set the motor power low so the robot doesn't overshoot.
* Integral (kI): if the robot is close to the target, but not quite there, increase the motor power over time so the robot doesn't stall.
* Derivative (kD): if the robot is rapidly approaching the target, apply the brakes so the robot doesn't go too far.
Remember, everything on the robot--drivetrain friction, gear ratio, weight distribution, center of gravity--influence the optimal PID values for the robot. That is, you might have to re-adjust the PID values if something major changes on the robot.
Let's say we're tuning the drive PID (the process is the same for the turn PID). We started with these values. Try out the drive PID. How well does it work?
//PID constants
float kP = 0.5;