Issues With P
Wanting to exit a loop once the goal is achieved can bring up a couple issues with P and the code provided above:
- The robot might exit out of the loop in the split-second that the goal was reached, and then overshoot without compensating. This can be solved by adding a wait at the end of the loop to allow the robot to overshoot.
- The robot will settle with no momentum 1% away from the goal. Then, with most Kp, the proportional will not output enough power to start moving again, and the robot will get stuck. This will be solved by the I.
- If you make a threshold, for example exiting out of the loop when the error is +-2%, and that is “good enough”, it still won’t work. This is because it brings back the issue with the robot exiting out of the loop while the robot still has momentum, because the loop, even with a wait, has a greater chance of falling within that threshold.
There are other issues with P:
- Imagine, with the Distance Maintainer, that my hand is moving away at a constant speed. The Proportional will then cause the robot to speed up to maintain the distance to my hand. However, the robot will never reduce the error past a certain point, once the robot has reached an equilibrium. The robot will be moving the same speed as my hand, but will never speed up to close the gap. The I solves this.
- A similar issue, if a P loop is used to make a arm lift to a certain position, but the arm is heavy, or there is a load, the P will never cause the arm to lift to its goal. That is because an arm needs a pretty constant power to lift it, but if the P decreases the closer the arm gets to the goal, the arm will start to stall.
- If the robot is a very small distance away from the goal, but not close enough to exit the loop, the robot will get stuck. This is because the P will not output enough power to move the robot, and increasing the Kp will cause the robot to oscillate if the error is any greater. The I will solve this.
- With the Distance Maintainer OR the Wheel Movement Replicator, we want the robot to be as aggressive as possible. That means that the robot needs not only to react quickly once there is a change, but also anticipate change and react in advance. This can be solved by D.
In the following sections, I will talk about what I and D is, and how to code them. I assure you, they are really quite a bit more complicated in theory than they are in practice, once you understand it will be a breeze to make.
PART FIVE: The I OF PID: Integral
I is the accumulated error. Every loop, we add the error to a counter. From now on, we will use a 20ms wait between loops, because we need to control the speed I increases.
This is how we would code a PI loop. I have made in bold changes since P:
Error = Current Distance – Wanted Distance
TotalError = TotalError + Error
Kp = 20
Ki = 5
Motor Power = (Error * Kp) + (TotalError * Ki)
What this does is if the Error stays above 0 for a couple loops, the TotalError will increase to try to bring the Error to 0 as quick as possible. As soon as the Error overshoots into the negative, the I will be reduced.
This is useful in a couple scenarios:
You are powering a heavy arm. It takes quite a bit more energy to lift the arm than to lower it, and a arm requires a constant force to stay up even at its goal. The P is proportional on both sides of 0 power, so it has no bias on which direction has more power. With a P loop, an arm can never reach its goal, because the power will decrease the closer the arm gets to the goal, and an arm needs a constant power to move.
If the Error continues to constantly be either positive or negative, TotalError will increase to compensate. In the above scenario, P will come to a equilibrium a bit below the goal, due to the weight of the arm. Then TotalError will increase until the arm starts lifting towards the goal. As soon as the arm overshoots, I will stop growing, and give a constant power to maintain the position of the arm.
Unfortunately, I have not completed this tutorial yet. To learn about D and tuning, go here:
The issue with I is that it is not very responsive, so gets in the way of going forwards. This is because it
goes to its maximum right at the start, and takes a while to wind back down to 0 after it overshoots. This
can be fixed by only using the I if the robot is 5% from its goal.
For extra responsiveness, the D is added. D is a way to predict the future, and therefore act on it. D is
the rate of change, so if the target moves or the error suddenly increases, for example, if we are using
PID for balancing an arm and force is applied to the arm, the D jumps up. This is good for aggressively
maintaining a position or sticking to a moving goal.
All of these added together, and weighed with their respective constants, resulting in a smooth and