Electronic – PID control loop exit condition

microcontrollerpid controller

I'm helping programming a FLL LEGO robot (two motorized wheels and a ballpoint in the back are the contacts with the ground), experimenting with a PID control to make it turn. It may seem overkill, but everything else, including a pure proportional control can't account well for the "not so precise" motors and sensors.

That said, what is a good exiting condition for a PID algorithm?
Of course just waiting some time until the oscillation becomes imperceptible is good, but I wonder what the literature has to say, since in my application I need control back to the main program asap to do other things.

My guesses:

  • fine tune the number of iterations with the derivative and proportional therms arbitrarily close to the setpoint
  • a parallel control loop that "pulls the brake" after the setpoint has been reached a certain number of times (and fine tune it too)

If this is not the right place to ask this question feel free to move it wherever you want.

Best Answer

A PID algorithm corrects a control signal based on an error, and its goal is to reduce the error to zero.

By 'turning' I understand that you want your robot to move to a required position or to a certain distance. In that case, the error is the distance that the robot has yet to cover, and the control signal could be the PWM duty cycle that power the motorized wheels.

Consider this pseudo-code:

requiredPosition = obtainRequiredPosition();
error = requiredPosition - currentPosition;
while(error <> 0) {
    correction = PID(error);
    averagePower += correction;
    applyPower(averagePower);

    // Wait a little: either with a waiting loop
    // or, better, implement the whole thing in iterrupts.
    wait();

    currentPosition = captureCurrentPosition();
    error = requiredPosition - position;
};

I don't think you need the integral part of the PID. You don't need your robot to spend an equal amount of time with positive and negative error.

As an example, you would need the integral part to control the helm of a boat: When the boat is heading in a specific direction, you want to compensate the time going too much to the left with an equal time going too much to the right.

Related Topic