PID Controllers
What are PIDS?
PID Controllers (short for Proportional–Integral–Derivative controllers) are feedback loops commonly used with machinery that require constant adjusting to their movement. For example: look at the Romi from earlier.
When you stop the Romi's movement through a trigger, the robot doesn't stop smoothly— it ends up jerking forward before stopping. This might not seem like a problem, but with much, much more powerful and expensive motors, these erratic movements which have no corrective action could cause a lot of hassle.
Another example is what you may have noticed driving your Romi, is that there are slight deviations in the motor speed and imperfections in the build of the wheels, which will cause the robot to depart its intended course. To combat this, we can use PID Controllers.
Error
The error is equal to the difference between the PID controller's target position, and the current position. Basically- it's measuring how far the controller itself is from its target. This value is used by the parameters below:
Parameters
Proportional Control (P)
The "P" value determines how strongly your PID controller reacts to error. This value is multiplied by the error mentioned above. The greater your KP value is, the stronger the controller reacts when the error is bigger. The P value is added onto the controller's position, bringing it closer to its target.
The kP value is determined by the user, often given as a parameter. It determines the magnitude of the controller's reaction.
The P value is equal to: P = error * kP
Theoretically, the P value is all you'll need. The problem is- you're using it to control a moving object. When your P value is equal to zero, your object still has its velocity
Integral Control (I)
If you have a weight at the end of an arm, for instance, then this will cause the motor powering it to offset from the target. The integral, thus finds the amount it's travelled and feeds said information to the motor. In other words, It's primarily used to counter other factors, such as gravity.
This one is not changed very often and is only changed when the exact values of what the aforementioned offset are, and even then only when it becomes important. Thereby, you will likely not need to worry about this for now.
Derivative Control (D)
The "D" value adds some de-acceleration to the P value, depending on how far the PID controller's position is from its target. It smoothens it out, so the PID controller won't overshoot as heavily once it reaches its target.
Here is a video that summarizes how the parameters work in the PID controller.
Using PID Controllers
Creating PIDController Objects
PIDController ObjectsThe parameters of a PID Controller are:
PIDController controller = new PIDController(double kP, double kI, double kD);All the parameters here are used to modify the values I mentioned above. The graph below is the ideal way a PID should move the robot, but the dark magic behind PID controllers often makes their actual movement not the case. Most of the time, as mentioned in the video, the P value will end up oscillating around the target, which could end up causing issues. This is why you need to tune the D value as well.
Tuning a PIDController Object
PIDController ObjectPIDs have different parameter values for everything. Because of this, there is a process called tuning a PID. To do this you have to continuously test the motor with different parameters until one of the parameter sets creates accurate results. For our purposes, we will change the moveRobotAtDistance() function to learn tuning.
PID Controller tuning is fundamentally trial and error. If something's messed up, we'll modify the values and retry.
When tuning a PID controller, we'll start to increase the kP value. This value tends to have the biggest impact on the controller. If it overshoots greatly, we'll decrease it. We want the kP value to be enough so the robot will gently oscillate around the target position.
After this, we'll start to increase the kD value. Try to increase it in small increments, such as 0.05. Tune the D value until the robot barely oscillates around the target position.
Conditions:
Here is the answer code (don't worry about the PIDController object parameters for now).
To use a PID, you have to use the pid.calculate() method. This method has 2 parameters, the current point, and the desired point. You can get the current point from Encoder objects. This method returns a double that will be used as a speed value that can be set to Spark motors. This is an example with our PIDController object being named pid and our Encoder object being named encoder:
// will precisely move the robot 12 units using PID
double speed = pid.calculate(encoder.getDistance(), 12)Last updated