29
29
#include " embedded_math.h"
30
30
#include < air_density_observer.h>
31
31
#include " NAV_tuning_parameters.h"
32
- #include < pt2.h >
32
+ #include " system_configuration.h "
33
33
34
34
#define RECIP_STD_DENSITY_TIMES_2 1 .632f
35
35
@@ -56,8 +56,8 @@ class atmosphere_t
56
56
extrapolated_sea_level_pressure (101325 ),
57
57
GNSS_altitude_based_density_available (false ),
58
58
GNSS_altitude_based_density (1 .2255f ),
59
- old_density_correction (1 .0f ),
60
59
weight_sum (0 .0f ),
60
+ density_measurement_number (0 ),
61
61
density_factor_weighed_sum (0 .0f )
62
62
{
63
63
}
@@ -130,28 +130,48 @@ class atmosphere_t
130
130
return extrapolated_sea_level_pressure;
131
131
}
132
132
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 ;
140
169
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
+ }
143
171
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
+ }
155
175
156
176
private:
157
177
float calculateGasConstantHumAir (
@@ -168,8 +188,9 @@ class atmosphere_t
168
188
air_density_observer_t air_density_observer;
169
189
bool GNSS_altitude_based_density_available;
170
190
float GNSS_altitude_based_density;
171
- float old_density_correction ;
191
+ air_data_result first_result ;
172
192
float weight_sum;
193
+ uint8_t density_measurement_number;
173
194
float density_factor_weighed_sum;
174
195
};
175
196
0 commit comments