Skip to content

Commit

Permalink
Fix DMax calculations (betaflight#13933)
Browse files Browse the repository at this point in the history
Refactor DMax calculations
  • Loading branch information
ctzsnooze authored Sep 28, 2024
1 parent 983b510 commit 4ed21bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
22 changes: 12 additions & 10 deletions src/main/flight/pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1199,26 +1199,28 @@ void FAST_CODE pidController(const pidProfile_t *pidProfile, timeUs_t currentTim
#endif

#ifdef USE_D_MAX
float dMaxFactor = 1.0f;
if (pidRuntime.dMaxPercent[axis] > 0) {
float dMaxMultiplier = 1.0f;
if (pidRuntime.dMaxPercent[axis] > 1.0f) {
float dMaxGyroFactor = pt2FilterApply(&pidRuntime.dMaxRange[axis], delta);
dMaxGyroFactor = fabsf(dMaxGyroFactor) * pidRuntime.dMaxGyroGain;
const float dMaxSetpointFactor = fabsf(pidSetpointDelta) * pidRuntime.dMaxSetpointGain;
dMaxFactor = MAX(dMaxGyroFactor, dMaxSetpointFactor);
dMaxFactor = 1.0f + (1.0f - pidRuntime.dMaxPercent[axis]) * dMaxFactor;
dMaxFactor = pt2FilterApply(&pidRuntime.dMaxLowpass[axis], dMaxFactor);
dMaxFactor = MIN(dMaxFactor, 1.0f / pidRuntime.dMaxPercent[axis]);
const float dMaxBoost = fmaxf(dMaxGyroFactor, dMaxSetpointFactor);
// dMaxBoost starts at zero, and by 1.0 we get Dmax, but it can exceed 1.
dMaxMultiplier += (pidRuntime.dMaxPercent[axis] - 1.0f) * dMaxBoost;
dMaxMultiplier = pt2FilterApply(&pidRuntime.dMaxLowpass[axis], dMaxMultiplier);
// limit the gain to the fraction that DMax is greater than Min
dMaxMultiplier = MIN(dMaxMultiplier, pidRuntime.dMaxPercent[axis]);
if (axis == FD_ROLL) {
DEBUG_SET(DEBUG_D_MAX, 0, lrintf(dMaxGyroFactor * 100));
DEBUG_SET(DEBUG_D_MAX, 1, lrintf(dMaxSetpointFactor * 100));
DEBUG_SET(DEBUG_D_MAX, 2, lrintf(pidRuntime.pidCoefficient[axis].Kd * dMaxFactor * 10 / DTERM_SCALE));
DEBUG_SET(DEBUG_D_MAX, 2, lrintf(pidRuntime.pidCoefficient[axis].Kd * dMaxMultiplier * 10 / DTERM_SCALE)); // actual D
} else if (axis == FD_PITCH) {
DEBUG_SET(DEBUG_D_MAX, 3, lrintf(pidRuntime.pidCoefficient[axis].Kd * dMaxFactor * 10 / DTERM_SCALE));
DEBUG_SET(DEBUG_D_MAX, 3, lrintf(pidRuntime.pidCoefficient[axis].Kd * dMaxMultiplier * 10 / DTERM_SCALE));
}
}

// Apply the dMaxFactor
preTpaD *= dMaxFactor;
// Apply the gain that increases D towards Dmax
preTpaD *= dMaxMultiplier;
#endif

pidData[axis].D = preTpaD * pidRuntime.tpaFactor;
Expand Down
7 changes: 4 additions & 3 deletions src/main/flight/pid_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,11 @@ void pidInitConfig(const pidProfile_t *pidProfile)
#ifdef USE_D_MAX
for (int axis = FD_ROLL; axis <= FD_YAW; ++axis) {
const uint8_t dMax = pidProfile->d_max[axis];
if ((dMax > 0) && (dMax > pidProfile->pid[axis].D)) {
pidRuntime.dMaxPercent[axis] = (float) pidProfile->pid[axis].D / dMax;
if ((pidProfile->pid[axis].D > 0) && dMax > pidProfile->pid[axis].D) {
pidRuntime.dMaxPercent[axis] = (float) dMax / pidProfile->pid[axis].D;
// fraction that Dmax is higher than D, eg if D is 8 and Dmax is 10, Dmax is 1.25 times bigger
} else {
pidRuntime.dMaxPercent[axis] = 0;
pidRuntime.dMaxPercent[axis] = 1.0f;
}
}
pidRuntime.dMaxGyroGain = D_MAX_GAIN_FACTOR * pidProfile->d_max_gain / D_MAX_LOWPASS_HZ;
Expand Down

0 comments on commit 4ed21bd

Please sign in to comment.