Skip to content

Commit

Permalink
Merge pull request #9224 from shota3527/sh_navpid_backcalc
Browse files Browse the repository at this point in the history
Navpid back calculation anti-windup can only shrink integrator
  • Loading branch information
DzikuVx authored Dec 8, 2023
2 parents 28d728c + 0b45c30 commit fc08d44
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/common/fp_pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ float navPidApply3(
/* Pre-calculate output and limit it if actuator is saturating */
const float outVal = newProportional + (pid->integrator * gainScaler) + newDerivative + newFeedForward;
const float outValConstrained = constrainf(outVal, outMin, outMax);
float backCalc = outValConstrained - outVal;//back-calculation anti-windup
if (SIGN(backCalc) == SIGN(pid->integrator)) {
//back calculation anti-windup can only shrink integrator, will not push it to the opposite direction
backCalc = 0.0f;
}

pid->proportional = newProportional;
pid->integral = pid->integrator;
Expand All @@ -104,7 +109,7 @@ float navPidApply3(
!(pidFlags & PID_ZERO_INTEGRATOR) &&
!(pidFlags & PID_FREEZE_INTEGRATOR)
) {
const float newIntegrator = pid->integrator + (error * pid->param.kI * gainScaler * dt) + ((outValConstrained - outVal) * pid->param.kT * dt);
const float newIntegrator = pid->integrator + (error * pid->param.kI * gainScaler * dt) + (backCalc * pid->param.kT * dt);

if (pidFlags & PID_SHRINK_INTEGRATOR) {
// Only allow integrator to shrink
Expand Down
1 change: 1 addition & 0 deletions src/main/common/maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
)
#define MIN(a, b) _CHOOSE(<, a, b)
#define MAX(a, b) _CHOOSE(>, a, b)
#define SIGN(a) ((a >= 0) ? 1 : -1)

#define _ABS_II(x, var) \
( __extension__ ({ \
Expand Down

0 comments on commit fc08d44

Please sign in to comment.