âŠī¸Turn PID Tutorial
Last updated
Was this helpful?
Last updated
Was this helpful?
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.
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.
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.
Before we get into the actual PID loop, we need to define a few more variables:
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.
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.
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.
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.
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:
At this point, we can add the quintessential line of the PID algorithm! Here it is:
Then, we'll clamp the motorPower
variable between -1 and 1.
Next, we'll add the slew rate limiter to prevent jerky robot motion.
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.
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. We also make sure the error and prevError don't differ by more than 0.3 degrees, to prevent the code from exiting when the robot is going quickly past the target.
Next, we need to update the prevError
and prevMotorPower
variables, so they can be used in the next iteration of the loop.
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:
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