New programmer here, I just started using pros and I have no idea how to code PID. Can anyone just like explain to me how it works and how to code it?
Might be some help:
PID Controller | Purdue SIGBots Wiki
Also how does PROS makes coding a PID different?
PID is a feedback loop system that takes in input from a sensor ( for example the position of a robot) like an inertial sensor, and then uses a set of calculations to return a specific output that you will pass to the motor as voltage or velocity to drive at the right speed to the exact location you want it to drive to.
P - Proportional
I - Integral
D - Derivatave
The Proportional Term of PID is the error between where you want to go and where you are. For example in a drive PID, you might be at position 0 and you want to travel 30 in so your desired location would be 30.
Then the Integral term of PID is the area under the error vs. time graph, or simply it is the accumulated error over time as you drive forward.
The derivative term is the slope of the error vs. time graph at a specific point in time. It essentially is the error minus the error you calculated a very short time ago.
PID uses these three calculations to calculate the motor power. But with these values to convert it to velocity or voltage you will have to tune three variables called kP, kD, and kI. These are constants that you multiply to each PID term. The more the kP is the faster your robot is going to go. The higher your kD is it is going to dampen ( slow down the robot if it thinks it’s going faster than it’s supposed to ). You don’t need to worry about the Integral term to much.
So you will set up a while loop and create a wait command for 20 milliseconds. Then you will calculate the position at that time of the robot in the loop by getting position of left and right motors, adding it up and dividing it by two. If you have four motors then do all four. Then create a variable called error and set it to desired position - current position.
Then for derivative you should do the error minus prev error, prev error will be zero initially, and and end of loop you will set prev error to error. Then for integral you will do integral += error to get total error.
Apart from this outside the loop you will have kP kI and kD. You will tune those later, but for now set kP to 0.5 and kI and kD to zero.
Then inside the loop you will create a variable called output and do (kP * error)
- (kI * integral) + ( kD * derivative) Then you will set velocity of all your motors to this value.
My GitHub with PID code : GitHub - Trishul-creator/Autonomous-Project-32630B