MiniPID-Java icon indicating copy to clipboard operation
MiniPID-Java copied to clipboard

Resetting the error-sum on min/max-output can cause oscillations

Open DanielWeigl opened this issue 5 years ago • 0 comments

At line https://github.com/tekdemo/MiniPID-Java/blob/master/src/com/stormbots/MiniPID.java#L304 you reset the error sum, if the output reaches a limit - but if the output reached the limit because of the I-term going (correctly) towards the limit, then this causes oscillations.

It would (imo) be better to just keep the error sum as is (not resetting and not adding new error)

Example: green is the I-term and yellow-dotted the output - which should be driven to its upper bound - but the I-term keeps resetting and thus oscilliating.

image

After the red line i made this code change:

        // Figure out what we're doing with the error.
        if (minOutput != maxOutput && !bounded(output, minOutput, maxOutput)) {
            // NOP
            // keep the error sum as is to ensures a smooth transition when the P term
            // decreases enough for the I term to start acting upon the controller
            // From that point the I term will build up as would be expected
        } else if (outputRampRate != 0 && !bounded(output, lastOutput - outputRampRate, lastOutput + outputRampRate)) {
            errorSum = error;
        } else if (maxIOutput != 0) {
            errorSum = constrain(errorSum + error, -maxError, maxError);
            // In addition to output limiting directly, we also want to prevent I term
            // buildup, so restrict the error directly
        } else {
            errorSum += error;
        }

DanielWeigl avatar Oct 07 '20 19:10 DanielWeigl