Skip to content

Commit b218909

Browse files
committed
LP-604 Add suitable lowpass filter to feed forward term
1 parent 73ba9ff commit b218909

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

flight/modules/Stabilization/inc/stabilization.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ typedef struct {
6363
struct pid innerPids[3], outerPids[3];
6464
// TPS [Roll,Pitch,Yaw][P,I,D]
6565
bool thrust_pid_scaling_enabled[3][3];
66+
float feedForward_alpha[3];
6667
} StabilizationData;
6768

6869

flight/modules/Stabilization/outerloop.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static DelayedCallbackInfo *callbackHandle;
6464
static AttitudeStateData attitude;
6565

6666
static uint8_t previous_mode[AXES] = { 255, 255, 255, 255 };
67+
static float gyro_filtered[3] = { 0, 0, 0 };
6768
static PiOSDeltatimeConfig timeval;
6869
static bool pitchMin = false;
6970
static bool pitchMax = false;
@@ -72,6 +73,7 @@ static bool rollMax = false;
7273

7374
// Private functions
7475
static void stabilizationOuterloopTask();
76+
static void GyroStateUpdatedCb(__attribute__((unused)) UAVObjEvent *ev);
7577
static void AttitudeStateUpdatedCb(__attribute__((unused)) UAVObjEvent *ev);
7678

7779
void stabilizationOuterloopInit()
@@ -87,6 +89,7 @@ void stabilizationOuterloopInit()
8789
PIOS_DELTATIME_Init(&timeval, UPDATE_EXPECTED, UPDATE_MIN, UPDATE_MAX, UPDATE_ALPHA);
8890

8991
callbackHandle = PIOS_CALLBACKSCHEDULER_Create(&stabilizationOuterloopTask, CALLBACK_PRIORITY, CBTASK_PRIORITY, CALLBACKINFO_RUNNING_STABILIZATION0, STACK_SIZE_BYTES);
92+
GyroStateConnectCallback(GyroStateUpdatedCb);
9093
AttitudeStateConnectCallback(AttitudeStateUpdatedCb);
9194
}
9295

@@ -99,13 +102,11 @@ void stabilizationOuterloopInit()
99102
static void stabilizationOuterloopTask()
100103
{
101104
AttitudeStateData attitudeState;
102-
GyroStateData gyroState;
103105
RateDesiredData rateDesired;
104106
StabilizationDesiredData stabilizationDesired;
105107
StabilizationStatusOuterLoopData enabled;
106108

107109
AttitudeStateGet(&attitudeState);
108-
GyroStateGet(&gyroState);
109110
StabilizationDesiredGet(&stabilizationDesired);
110111
RateDesiredGet(&rateDesired);
111112
StabilizationStatusOuterLoopGet(&enabled);
@@ -195,9 +196,9 @@ static void stabilizationOuterloopTask()
195196
}
196197

197198
// Feed forward: Assume things always get worse before they get better
198-
local_error[0] = local_error[0] - (gyroState.x * stabSettings.stabBank.AttitudeFeedForward.Roll);
199-
local_error[1] = local_error[1] - (gyroState.y * stabSettings.stabBank.AttitudeFeedForward.Pitch);
200-
local_error[2] = local_error[2] - (gyroState.z * stabSettings.stabBank.AttitudeFeedForward.Yaw);
199+
local_error[0] = local_error[0] - (gyro_filtered[0] * stabSettings.stabBank.AttitudeFeedForward.Roll);
200+
local_error[1] = local_error[1] - (gyro_filtered[1] * stabSettings.stabBank.AttitudeFeedForward.Pitch);
201+
local_error[2] = local_error[2] - (gyro_filtered[2] * stabSettings.stabBank.AttitudeFeedForward.Yaw);
201202

202203
for (t = STABILIZATIONSTATUS_OUTERLOOP_ROLL; t < STABILIZATIONSTATUS_OUTERLOOP_THRUST; t++) {
203204
reinit = (StabilizationStatusOuterLoopToArray(enabled)[t] != previous_mode[t]);
@@ -388,6 +389,18 @@ static void AttitudeStateUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
388389
#endif
389390
}
390391

392+
static void GyroStateUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
393+
{
394+
GyroStateData gyroState;
395+
396+
GyroStateGet(&gyroState);
397+
398+
gyro_filtered[0] = gyro_filtered[0] * stabSettings.feedForward_alpha[0] + gyroState.x * (1 - stabSettings.feedForward_alpha[0]);
399+
gyro_filtered[1] = gyro_filtered[1] * stabSettings.feedForward_alpha[1] + gyroState.y * (1 - stabSettings.feedForward_alpha[1]);
400+
gyro_filtered[2] = gyro_filtered[2] * stabSettings.feedForward_alpha[2] + gyroState.z * (1 - stabSettings.feedForward_alpha[2]);
401+
}
402+
403+
391404
/**
392405
* @}
393406
* @}

flight/modules/Stabilization/stabilization.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,21 @@ static void BankUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
364364
stabSettings.acroInsanityFactors[0] = (float)(stabSettings.stabBank.AcroInsanityFactor.Roll) * 0.01f;
365365
stabSettings.acroInsanityFactors[1] = (float)(stabSettings.stabBank.AcroInsanityFactor.Pitch) * 0.01f;
366366
stabSettings.acroInsanityFactors[2] = (float)(stabSettings.stabBank.AcroInsanityFactor.Yaw) * 0.01f;
367+
368+
// The dT has some jitter iteration to iteration that we don't want to
369+
// make thie result unpredictable. Still, it's nicer to specify the constant
370+
// based on a time (in ms) rather than a fixed multiplier. The error between
371+
// update rates on OP (~300 Hz) and CC (~475 Hz) is negligible for this
372+
// calculation
373+
const float fakeDt = 0.0025f;
374+
for (int t = 0; t < STABILIZATIONBANK_ATTITUDEFEEDFORWARD_NUMELEM; t++) {
375+
float tau = StabilizationBankAttitudeFeedForwardToArray(stabSettings.stabBank.AttitudeFeedForward)[t];
376+
if (tau < 0.0001f) {
377+
stabSettings.feedForward_alpha[t] = 0.0f; // not trusting this to resolve to 0
378+
} else {
379+
stabSettings.feedForward_alpha[t] = expf(-fakeDt / tau);
380+
}
381+
}
367382
}
368383

369384

0 commit comments

Comments
 (0)