PID code for a lift

Hello, I’m not sure how to make a PID code for a double reverse four bar lift in C++ Pro in Vex Coding Studio. Does anyone know how to accomplish this?

Don’t know how it would be in VCS, but some general pseudo-code for a PID goes like

const double kp = 0.5;
const double kd = 0.2;  // your PID gains. Tune these to your liking

while(true){ // I would use some sort of settling utility for this, using a timer like millis()?

double error = target - lift_position; // the difference in position from your target v.s your current position

double proportional = kp*error; // proportional control with gain applied
double derivative = kd*(error - last_error); //derivative (rate of change) with gain applied

double power = proportional + derivative; // the final unit (typically voltage) that you get to plug into you lift

lift.move(power); // use whatever lift function you have in VCS and feed power into it

double last_error = error; // previous error from last cycle, used to calculate the difference in error between cycles

vex::sleep(20); //cycle time
}

This is a basic PD loop (which I mainly use) which should be a good starting point. You can of course expand on it, and add Integral (I removed it for simplicity) which will help to eliminate steady-state error, but you have to put additional code in to control it properly. It’s for the most part not necessary, but may be an issue with your lift.

I would advise reading up on this article to learn more about PID :slight_smile:

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.