5151#define CALIBRATION_DELAY_MS 4000
5252#define CALIBRATION_DURATION_MS 6000
5353#define RELOADSETTINGS_DELAY_MS 1000
54+ #define CONVERGENCE_MAGKP 20.0f
5455#define VARIANCE_WINDOW_SIZE 40
5556
57+ #define UNDONE 0
58+ #define DONE 1
59+ #define RUN 2
60+
5661// Private types
5762struct data {
5863 AttitudeSettingsData attitudeSettings ;
@@ -252,7 +257,7 @@ static filterResult complementaryFilter(struct data *this, float gyro[3], float
252257
253258 AttitudeStateData attitudeState ; // base on previous state
254259 AttitudeStateGet (& attitudeState );
255- this -> init = 0 ;
260+ this -> init = UNDONE ;
256261
257262 // Set initial attitude. Use accels to determine roll and pitch, rotate magnetic measurement accordingly,
258263 // so pseudo "north" vector can be estimated even if the board is not level
@@ -292,7 +297,7 @@ static filterResult complementaryFilter(struct data *this, float gyro[3], float
292297 return FILTERRESULT_OK ; // must return OK on initial initialization, so attitude will init with a valid quaternion
293298 }
294299 // check whether copter is steady
295- if (this -> init == 0 && this -> attitudeSettings .InitialZeroWhenBoardSteady == ATTITUDESETTINGS_INITIALZEROWHENBOARDSTEADY_TRUE ) {
300+ if (this -> init == UNDONE && this -> attitudeSettings .InitialZeroWhenBoardSteady == ATTITUDESETTINGS_INITIALZEROWHENBOARDSTEADY_TRUE ) {
296301 pseudo_windowed_variance_push_sample (& this -> gyro_var [0 ], gyro [0 ]);
297302 pseudo_windowed_variance_push_sample (& this -> gyro_var [1 ], gyro [1 ]);
298303 pseudo_windowed_variance_push_sample (& this -> gyro_var [2 ], gyro [2 ]);
@@ -308,40 +313,40 @@ static filterResult complementaryFilter(struct data *this, float gyro[3], float
308313 }
309314
310315
311- if (this -> init == 0 && xTaskGetTickCount () - this -> starttime < CALIBRATION_DELAY_MS / portTICK_RATE_MS ) {
316+ if (this -> init == UNDONE && xTaskGetTickCount () - this -> starttime < CALIBRATION_DELAY_MS / portTICK_RATE_MS ) {
312317 // wait 4 seconds for the user to get his hands off in case the board was just powered
313318 this -> timeval = PIOS_DELAY_GetRaw ();
314319 return FILTERRESULT_ERROR ;
315- } else if (this -> init == 0 && xTaskGetTickCount () - this -> starttime < (CALIBRATION_DELAY_MS + CALIBRATION_DURATION_MS ) / portTICK_RATE_MS ) {
320+ } else if (this -> init == UNDONE && xTaskGetTickCount () - this -> starttime < (CALIBRATION_DELAY_MS + CALIBRATION_DURATION_MS ) / portTICK_RATE_MS ) {
316321 // For first 6 seconds use accels to get gyro bias
317322 this -> attitudeSettings .AccelKp = 1.0f ;
318323 this -> attitudeSettings .AccelKi = 0.0f ;
319324 this -> attitudeSettings .YawBiasRate = 0.23f ;
320325 this -> accel_filter_enabled = false;
321326 this -> rollPitchBiasRate = 0.01f ;
322- this -> attitudeSettings .MagKp = this -> magCalibrated ? 20.0f : 0.0f ;
327+ this -> attitudeSettings .MagKp = this -> magCalibrated ? CONVERGENCE_MAGKP : 0.0f ;
323328 PIOS_NOTIFY_StartNotification (NOTIFY_DRAW_ATTENTION , NOTIFY_PRIORITY_REGULAR );
324329 } else if ((this -> attitudeSettings .ZeroDuringArming == ATTITUDESETTINGS_ZERODURINGARMING_TRUE ) && (flightStatus .Armed == FLIGHTSTATUS_ARMED_ARMING )) {
325330 this -> attitudeSettings .AccelKp = 1.0f ;
326331 this -> attitudeSettings .AccelKi = 0.0f ;
327332 this -> attitudeSettings .YawBiasRate = 0.23f ;
328333 this -> accel_filter_enabled = false;
329334 this -> rollPitchBiasRate = 0.01f ;
330- this -> attitudeSettings .MagKp = this -> magCalibrated ? 20.0f : 0.0f ;
331- this -> init = 0 ;
335+ this -> attitudeSettings .MagKp = this -> magCalibrated ? CONVERGENCE_MAGKP : 0.0f ;
336+ this -> init = UNDONE ;
332337 PIOS_NOTIFY_StartNotification (NOTIFY_DRAW_ATTENTION , NOTIFY_PRIORITY_REGULAR );
333- } else if (this -> init == 0 ) {
338+ } else if (this -> init == UNDONE ) {
334339 this -> rollPitchBiasRate = 0.0f ;
335340 if (this -> accel_alpha > 0.0f ) {
336341 this -> accel_filter_enabled = true;
337342 }
338343 this -> inittime = xTaskGetTickCount ();
339- this -> init = 1 ;
344+ this -> init = DONE ;
340345 // Allow running filter with custom MagKp for some time before reload settings
341- } else if (this -> init == 1 && (!this -> magCalibrated || (xTaskGetTickCount () - this -> inittime > RELOADSETTINGS_DELAY_MS / portTICK_RATE_MS ))) {
346+ } else if (this -> init == DONE && (!this -> magCalibrated || (xTaskGetTickCount () - this -> inittime > RELOADSETTINGS_DELAY_MS / portTICK_RATE_MS ))) {
342347 // Reload settings (all the rates)
343348 AttitudeSettingsGet (& this -> attitudeSettings );
344- this -> init = 2 ;
349+ this -> init = RUN ;
345350 }
346351
347352 // Compute the dT using the cpu clock
@@ -481,7 +486,7 @@ static filterResult complementaryFilter(struct data *this, float gyro[3], float
481486 return FILTERRESULT_WARNING ;
482487 }
483488
484- if (this -> init > 0 ) {
489+ if (this -> init != UNDONE ) {
485490 return FILTERRESULT_OK ;
486491 } else {
487492 return FILTERRESULT_CRITICAL ; // return "critical" for now, so users can see the zeroing period, switch to more graceful notification later
0 commit comments