From 85855425a8c4195c265bbb3d8c0b64b912d34b62 Mon Sep 17 00:00:00 2001 From: borisbstyle Date: Sat, 19 Sep 2015 14:41:21 +0200 Subject: [PATCH] yawPTermState filter --- src/main/common/filter.h | 2 +- src/main/flight/pid.c | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/common/filter.h b/src/main/common/filter.h index 7b949e8046..be3bacd852 100644 --- a/src/main/common/filter.h +++ b/src/main/common/filter.h @@ -5,7 +5,7 @@ * Author: borisb */ -#define YAW_PTERM_CUT_RATIO 2 +#define YAW_PTERM_FILTER 30 typedef struct filterStatePt1_s { float state; diff --git a/src/main/flight/pid.c b/src/main/flight/pid.c index 589e7bb52e..6e87e0d47b 100644 --- a/src/main/flight/pid.c +++ b/src/main/flight/pid.c @@ -96,6 +96,7 @@ void pidResetErrorGyro(void) const angle_index_t rcAliasToAngleIndexMap[] = { AI_ROLL, AI_PITCH }; static filterStatePt1_t DTermState[3]; +static filterStatePt1_t yawPTermState; #ifdef AUTOTUNE bool shouldAutotune(void) @@ -185,6 +186,10 @@ static void pidLuxFloat(pidProfile_t *pidProfile, controlRateConfig_t *controlRa // -----calculate P component PTerm = RateError * pidProfile->P_f[axis] * PIDweight[axis] / 100; + if (axis == YAW) { + PTerm = filterApplyPt1(PTerm, &yawPTermState, YAW_PTERM_FILTER, dT); + } + // -----calculate I component. errorGyroIf[axis] = constrainf(errorGyroIf[axis] + RateError * dT * pidProfile->I_f[axis] * 10, -250.0f, 250.0f); @@ -284,9 +289,12 @@ static void pidMultiWii(pidProfile_t *pidProfile, controlRateConfig_t *controlRa PTerm -= ((int32_t)gyroADC[axis] / 4) * dynP8[axis] / 10 / 8; // 32 bits is needed for calculation + if (axis == YAW) { + PTerm = filterApplyPt1(PTerm, &yawPTermState, YAW_PTERM_FILTER, dT); + } + delta = (gyroADC[axis] - lastGyro[axis]) / 4; lastGyro[axis] = gyroADC[axis]; - delta1[axis] = delta; // Dterm low pass if (pidProfile->dterm_cut_hz) { @@ -366,6 +374,10 @@ static void pidMultiWii23(pidProfile_t *pidProfile, controlRateConfig_t *control PTerm -= ((int32_t)(gyroADC[axis] / 4) * dynP8[axis]) >> 6; // 32 bits is needed for calculation + if (axis == YAW) { + PTerm = filterApplyPt1(PTerm, &yawPTermState, YAW_PTERM_FILTER, dT); + } + delta = (gyroADC[axis] - lastGyro[axis]) / 4; // 16 bits is ok here, the dif between 2 consecutive gyro reads is limited to 800 lastGyro[axis] = gyroADC[axis]; @@ -480,6 +492,10 @@ static void pidMultiWiiHybrid(pidProfile_t *pidProfile, controlRateConfig_t *con PTerm -= ((int32_t)gyroADC[axis] / 4) * dynP8[axis] / 10 / 8; // 32 bits is needed for calculation + if (axis == YAW) { + PTerm = filterApplyPt1(PTerm, &yawPTermState, YAW_PTERM_FILTER, dT); + } + delta = (gyroADC[axis] - lastGyro[axis]) / 4; lastGyro[axis] = gyroADC[axis]; @@ -730,6 +746,10 @@ static void pidRewrite(pidProfile_t *pidProfile, controlRateConfig_t *controlRat // -----calculate P component PTerm = (RateError * pidProfile->P8[axis] * PIDweight[axis] / 100) >> 7; + if (axis == YAW) { + PTerm = filterApplyPt1(PTerm, &yawPTermState, YAW_PTERM_FILTER, dT); + } + // -----calculate I component // there should be no division before accumulating the error to integrator, because the precision would be reduced. // Precision is critical, as I prevents from long-time drift. Thus, 32 bits integrator is used.