PIDs for DR4B

I was wondering if anyone knew how to program PIDs for a DR4B in either VEXcode or Vex Coding Studio so it does not tip to one side. How would I compare both values together and adjust the voltage. Also, would I use a shaft encoder or the built-in encoders of V5 motors?

1 Like

Can you post some off your code. So we can see what you have so far in order to help you. Thanks.

double DR4BDesired;

double DR4BKP = 0.0;

double DR4BPrevError = 0.0;

double DR4BDerivative = 0.0;

double DR4BKD = 0.0;

double DR4BError = 0.0;

double DR4BKI = 0.0;

I know that is not a lot but that is all I have. I do not know how to actually calculate the error for the DR4V and calculate how to adjust the voltage. :confused:

If your goal is to just make sure both sides are equal, try something like this from my old drive straight PID:

while(LeftQuad.position(degrees) < target){
    double lpower = (target- LeftQuad.position(degrees))*.4;
    if (lpower > 55){ //set maximum power
      lpower = 55;
    error = LeftQuad.position(degrees) - RightQuad.position(degrees);
    integral += error;
    derivative = error - lastError;
    LeftFrontMotor.spin(forward, lpower, pct);
    LeftBackMotor.spin(forward, lpower, pct);

    RightFrontMotor.spin(forward, lpower + (error * kp) + (integral * ki) + (derivative * kd), pct);
    RightBackMotor.spin(forward, lpower + (error * kp) + (integral * ki) + (derivative * kd), pct);
    lastError = error;

Essentially you are just setting the power of the second motor to the power of the first plus the error (proportional) and doing the same for integral and derivative.

This should also be helpful:


Ok, thank you! Is this code using the VEXcode application or Vex Coding Studio?

Vexcode, but it’s exactly the same as how you’d do it in VCS if you are still using that.

Though you really should be using vexcode at this point…

1 Like

I know :sweat_smile: I just haven’t had time to learn about VEXcode. I heard that they are very similar so I just stuck with Vex Coding Studio. Thank you for your help!

In your example, are you using shaft encoders or built-in Motor encoders? And do I need to declare any variables beforehand, like the lpower or target?

I’m using shaft encoders there, but funny enough if you plug in your motor name in there it’ll work the same.

Yes, you have to declare all of those variables, but kp,ki,and kd are probably the only ones that need preset values (check out that link to learn more about it)

Also I noticed in your first thread you seemed to be looking to use to integrated motor PID. If you want to use that you just have to set your motor power type to RPM. Custom PID would be more accurate, but if you just want to get it going that’s definitely an option.