3838#include <ratedesired.h>
3939#include <stabilizationdesired.h>
4040#include <attitudestate.h>
41+ #include <gyrostate.h>
4142#include <stabilizationstatus.h>
4243#include <flightstatus.h>
4344#include <manualcontrolcommand.h>
@@ -63,6 +64,7 @@ static DelayedCallbackInfo *callbackHandle;
6364static AttitudeStateData attitude ;
6465
6566static uint8_t previous_mode [AXES ] = { 255 , 255 , 255 , 255 };
67+ static float gyro_filtered [3 ] = { 0 , 0 , 0 };
6668static PiOSDeltatimeConfig timeval ;
6769static bool pitchMin = false;
6870static bool pitchMax = false;
@@ -71,20 +73,23 @@ static bool rollMax = false;
7173
7274// Private functions
7375static void stabilizationOuterloopTask ();
76+ static void GyroStateUpdatedCb (__attribute__((unused )) UAVObjEvent * ev );
7477static void AttitudeStateUpdatedCb (__attribute__((unused )) UAVObjEvent * ev );
7578
7679void stabilizationOuterloopInit ()
7780{
7881 RateDesiredInitialize ();
7982 StabilizationDesiredInitialize ();
8083 AttitudeStateInitialize ();
84+ GyroStateInitialize ();
8185 StabilizationStatusInitialize ();
8286 FlightStatusInitialize ();
8387 ManualControlCommandInitialize ();
8488
8589 PIOS_DELTATIME_Init (& timeval , UPDATE_EXPECTED , UPDATE_MIN , UPDATE_MAX , UPDATE_ALPHA );
8690
8791 callbackHandle = PIOS_CALLBACKSCHEDULER_Create (& stabilizationOuterloopTask , CALLBACK_PRIORITY , CBTASK_PRIORITY , CALLBACKINFO_RUNNING_STABILIZATION0 , STACK_SIZE_BYTES );
92+ GyroStateConnectCallback (GyroStateUpdatedCb );
8893 AttitudeStateConnectCallback (AttitudeStateUpdatedCb );
8994}
9095
@@ -190,6 +195,10 @@ static void stabilizationOuterloopTask()
190195#endif /* if defined(PIOS_QUATERNION_STABILIZATION) */
191196 }
192197
198+ // Feed forward: Assume things always get worse before they get better
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 );
193202
194203 for (t = STABILIZATIONSTATUS_OUTERLOOP_ROLL ; t < STABILIZATIONSTATUS_OUTERLOOP_THRUST ; t ++ ) {
195204 reinit = (StabilizationStatusOuterLoopToArray (enabled )[t ] != previous_mode [t ]);
@@ -380,6 +389,18 @@ static void AttitudeStateUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
380389#endif
381390}
382391
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+
383404/**
384405 * @}
385406 * @}
0 commit comments