2929#include " embedded_math.h"
3030#include < air_density_observer.h>
3131#include " NAV_tuning_parameters.h"
32- #include < pt2.h >
32+ #include " system_configuration.h "
3333
3434#define RECIP_STD_DENSITY_TIMES_2 1 .632f
3535
@@ -56,8 +56,8 @@ class atmosphere_t
5656 extrapolated_sea_level_pressure (101325 ),
5757 GNSS_altitude_based_density_available (false ),
5858 GNSS_altitude_based_density (1 .2255f ),
59- old_density_correction (1 .0f ),
6059 weight_sum (0 .0f ),
60+ density_measurement_number (0 ),
6161 density_factor_weighed_sum (0 .0f )
6262 {
6363 }
@@ -130,28 +130,48 @@ class atmosphere_t
130130 return extrapolated_sea_level_pressure;
131131 }
132132
133- void air_density_metering ( float pressure, float MSL_altitude)
134- {
135- air_data_result result = air_density_observer.feed_metering ( pressure, MSL_altitude);
136- if ( result.valid )
137- {
138- #if USE_AIR_DENSITY_LETHARGY
139- bool first_measurement = (weight_sum == 0 .0f );
133+ void air_density_metering (float pressure, float MSL_altitude)
134+ {
135+ air_data_result result = air_density_observer.feed_metering (pressure,
136+ MSL_altitude);
137+ if (result.valid )
138+ {
139+ if (density_measurement_number < 3 )
140+ ++density_measurement_number;
141+
142+ weight_sum = weight_sum * AIR_DENSITY_LETHARGY
143+ + (1 .0f - AIR_DENSITY_LETHARGY) / result.density_variance ;
144+ density_factor_weighed_sum = density_factor_weighed_sum
145+ * AIR_DENSITY_LETHARGY
146+ + (1 .0f - AIR_DENSITY_LETHARGY) * result.density_correction
147+ / result.density_variance ;
148+
149+ // postpone update unless we have two measurements
150+ switch (density_measurement_number)
151+ {
152+ case 1 :
153+ first_result = result; // remember and wait for better statistics
154+ // honorize trend for the moment
155+ density_correction = (1 .0f + result.density_correction ) * 0 .5f ;
156+ break ;
157+ case 2 :
158+ // use variance-weighed sum of both measurements
159+ density_correction = (first_result.density_correction
160+ / first_result.density_variance
161+ + result.density_correction / result.density_variance )
162+ / (1 .0f / first_result.density_variance
163+ + 1 .0f / result.density_variance );
164+ break ;
165+ default :
166+ // use IIR-filtered weighed sum of measurements
167+ density_correction = density_factor_weighed_sum / weight_sum;
168+ break ;
140169
141- weight_sum = weight_sum * AIR_DENSITY_LETHARGY + (AIR_DENSITY_LETHARGY -1 ) / result.density_variance ;
142- density_factor_weighed_sum = density_factor_weighed_sum * AIR_DENSITY_LETHARGY + (AIR_DENSITY_LETHARGY -1 ) * result.density_correction / result.density_variance ;
170+ }
143171
144- // postpone update unless we have two measurements
145- if ( ! first_measurement)
146- density_correction = density_factor_weighed_sum / weight_sum;
147- #else
148- density_correction = result.density_correction ;
149- #endif
150- // todo patch: emergency-brake for implausible values
151- if ( density_correction > 1 .15f || density_correction < 0 .85f )
152- density_correction = ONE;
153- }
154- }
172+ LIMIT_DENSITY_CORRECTION (density_correction);
173+ }
174+ }
155175
156176private:
157177 float calculateGasConstantHumAir (
@@ -168,8 +188,9 @@ class atmosphere_t
168188 air_density_observer_t air_density_observer;
169189 bool GNSS_altitude_based_density_available;
170190 float GNSS_altitude_based_density;
171- float old_density_correction ;
191+ air_data_result first_result ;
172192 float weight_sum;
193+ uint8_t density_measurement_number;
173194 float density_factor_weighed_sum;
174195};
175196
0 commit comments