# PID help/explaination

I’ve read a lot of different posts that try to explain the functionality of PID in programming, but none of them have quite helped me grasp the concept and how it works. From what I’ve learned so far, there are three constants, kP, kI, and kD, where kP makes most of the task while kL and kD compensate for overshoot or undershoot. However, I don’t understand how they are implemented into an algorithm in VEXCode Pro V5, specifically in C++. Could anyone show some sample code on how its implemented into code or explain how each constant is used?

Here’s an example to spin a motor to a certain number of degrees

``````void spinMotor(double target){
double kp=1;
double ki=0.2;
double kd=0.5;
double accuracy=1;
double error=target-motor.position(degrees);
double prevErrror;
double sum;
//These values will not work
while(fabs(target-motor.position(degrees))>accuracy){
error=target-motor.position(degrees)
double speed= (error*kp)+(sum*ki)+((error-prevError)*kd);
motor.spin(fwd, speed, volt);
wait(10,msec);
prevError=error;
sum+=error;
}
motor.stop();
}

``````

This code should be the guidelines for a basic PID

2 Likes