PID growing out of control

Is this the right way to do PID? currently have ID disabled for simplicity.

    error = hypot(global_y - ytarget, global_x - xtarget);
    derivative = error - prevError;
    integral = integral + error;
    if (error == 0){
      integral = 0;
    }
    if (error > 40){
      integral = 0;
    }

    double scaling = fmin(kP * error, 10);

There is an issue where scaling grows instead of shrinking even if the robot is getting closer to it’s target.

I believe I’ve gotten it fixed. It was a problem with the movement algorithm.

1 Like

The error in a system is the target state - current state. You have that backwards in your code, so your error variable would start negative and get closer to 0 (assuming you’re trying to drive forward) which means it is growing.

2 Likes