@@ -64,6 +64,7 @@ static DelayedCallbackInfo *callbackHandle;
6464static AttitudeStateData attitude ;
6565
6666static uint8_t previous_mode [AXES ] = { 255 , 255 , 255 , 255 };
67+ static float gyro_filtered [3 ] = { 0 , 0 , 0 };
6768static PiOSDeltatimeConfig timeval ;
6869static bool pitchMin = false;
6970static bool pitchMax = false;
@@ -72,6 +73,7 @@ static bool rollMax = false;
7273
7374// Private functions
7475static void stabilizationOuterloopTask ();
76+ static void GyroStateUpdatedCb (__attribute__((unused )) UAVObjEvent * ev );
7577static void AttitudeStateUpdatedCb (__attribute__((unused )) UAVObjEvent * ev );
7678
7779void 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()
99102static 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 * @}
0 commit comments