2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
29 #include "build/debug.h"
31 #include "common/maths.h"
32 #include "common/filter.h"
34 #include "fc/runtime_config.h"
36 #include "flight/position.h"
37 #include "flight/imu.h"
38 #include "flight/pid.h"
42 #include "scheduler/scheduler.h"
44 #include "sensors/sensors.h"
45 #include "sensors/barometer.h"
48 #include "pg/pg_ids.h"
50 static float displayAltitudeCm
= 0.0f
;
51 static float zeroedAltitudeCm
= 0.0f
;
53 #if defined(USE_BARO) || defined(USE_GPS)
54 static float zeroedAltitudeDerivative
= 0.0f
;
57 static pt2Filter_t altitudeLpf
;
58 static pt2Filter_t altitudeDerivativeLpf
;
60 static int16_t estimatedVario
= 0; // in cm/s
63 void positionInit(void)
65 const float sampleTimeS
= HZ_TO_INTERVAL(TASK_ALTITUDE_RATE_HZ
);
67 const float altitudeCutoffHz
= positionConfig()->altitude_lpf
/ 100.0f
;
68 const float altitudeGain
= pt2FilterGain(altitudeCutoffHz
, sampleTimeS
);
69 pt2FilterInit(&altitudeLpf
, altitudeGain
);
71 const float altitudeDerivativeCutoffHz
= positionConfig()->altitude_d_lpf
/ 100.0f
;
72 const float altitudeDerivativeGain
= pt2FilterGain(altitudeDerivativeCutoffHz
, sampleTimeS
);
73 pt2FilterInit(&altitudeDerivativeLpf
, altitudeDerivativeGain
);
82 PG_REGISTER_WITH_RESET_TEMPLATE(positionConfig_t
, positionConfig
, PG_POSITION
, 4);
84 PG_RESET_TEMPLATE(positionConfig_t
, positionConfig
,
85 .altitude_source
= DEFAULT
,
86 .altitude_prefer_baro
= 100, // percentage 'trust' of baro data
88 .altitude_d_lpf
= 100,
91 #if defined(USE_BARO) || defined(USE_GPS)
92 void calculateEstimatedAltitude(void)
94 static bool wasArmed
= false;
95 static bool useZeroedGpsAltitude
= false; // whether a zero for the GPS altitude value exists
96 static float gpsAltCm
= 0.0f
; // will hold last value on transient loss of 3D fix
97 static float gpsAltOffsetCm
= 0.0f
;
98 static float baroAltOffsetCm
= 0.0f
;
99 static float newBaroAltOffsetCm
= 0.0f
;
101 float baroAltCm
= 0.0f
;
102 float gpsTrust
= 0.3f
; // if no hDOP value, use 0.3
103 bool haveBaroAlt
= false; // true if baro exists and has been calibrated on power up
104 bool haveGpsAlt
= false; // true if GPS is connected and while it has a 3D fix, set each run to false
106 // *** Get sensor data
108 if (sensors(SENSOR_BARO
)) {
109 baroAltCm
= getBaroAltitude();
110 haveBaroAlt
= true; // false only if there is no sensor on the board, or it has failed
114 if (sensors(SENSOR_GPS
) && STATE(GPS_FIX
)) {
115 // GPS_FIX means a 3D fix, which requires min 4 sats.
116 // On loss of 3D fix, gpsAltCm remains at the last value, haveGpsAlt becomes false, and gpsTrust goes to zero.
117 gpsAltCm
= gpsSol
.llh
.altCm
; // static, so hold last altitude value if 3D fix is lost to prevent fly to moon
118 haveGpsAlt
= true; // stays false if no 3D fix
119 if (gpsSol
.dop
.hdop
!= 0) {
120 gpsTrust
= 100.0f
/ gpsSol
.dop
.hdop
;
121 // *** TO DO - investigate if we should use vDOP or vACC with UBlox units;
123 // always use at least 10% of other sources besides gps if available
124 gpsTrust
= MIN(gpsTrust
, 0.9f
);
129 if (!ARMING_FLAG(ARMED
)) {
130 if (wasArmed
) { // things to run once, on disarming, after being armed
131 useZeroedGpsAltitude
= false; // reset, and wait for valid GPS data to zero the GPS signal
135 newBaroAltOffsetCm
= 0.2f
* baroAltCm
+ 0.8f
* newBaroAltOffsetCm
; // smooth some recent baro samples
136 displayAltitudeCm
= baroAltCm
- baroAltOffsetCm
; // if no GPS altitude, show un-smoothed Baro altitude in OSD and sensors tab, using most recent offset.
138 if (haveGpsAlt
) { // watch for valid GPS altitude data to get a zero value from
139 gpsAltOffsetCm
= gpsAltCm
; // update the zero offset value with the most recent valid gps altitude reading
140 useZeroedGpsAltitude
= true; // we can use this offset to zero the GPS altitude on arming
141 if (!(positionConfig()->altitude_source
== BARO_ONLY
)) {
142 displayAltitudeCm
= gpsAltCm
; // estimatedAltitude shows most recent ASL GPS altitude in OSD and sensors, while disarmed
145 zeroedAltitudeCm
= 0.0f
; // always hold relativeAltitude at zero while disarmed
149 if (!wasArmed
) { // things to run once, on arming, after being disarmed
150 baroAltOffsetCm
= newBaroAltOffsetCm
;
154 baroAltCm
-= baroAltOffsetCm
; // use smoothed baro with most recent zero from disarm period
156 if (haveGpsAlt
) { // update relativeAltitude with every new gpsAlt value, or hold the previous value until 3D lock recovers
157 if (!useZeroedGpsAltitude
&& haveBaroAlt
) { // armed without zero offset, can use baro values to zero later
158 gpsAltOffsetCm
= gpsAltCm
- baroAltCm
; // not very accurate
159 useZeroedGpsAltitude
= true;
161 if (useZeroedGpsAltitude
) { // normal situation
162 zeroedAltitudeCm
= gpsAltCm
- gpsAltOffsetCm
; // now that we have a GPS offset value, we can use it to zero relativeAltitude
166 // TO DO - smoothly reduce GPS trust, rather than immediately dropping to zero for what could be only a very brief loss of 3D fix
169 // Empirical mixing of GPS and Baro altitudes
170 if (useZeroedGpsAltitude
&& (positionConfig()->altitude_source
== DEFAULT
|| positionConfig()->altitude_source
== GPS_ONLY
)) {
171 if (haveBaroAlt
&& positionConfig()->altitude_source
== DEFAULT
) {
172 // mix zeroed GPS with Baro altitude data, if Baro data exists if are in default altitude control mode
173 const float absDifferenceM
= fabsf(zeroedAltitudeCm
- baroAltCm
) / 100.0f
* positionConfig()->altitude_prefer_baro
/ 100.0f
;
174 if (absDifferenceM
> 1.0f
) { // when there is a large difference, favour Baro
175 gpsTrust
/= absDifferenceM
;
177 zeroedAltitudeCm
= zeroedAltitudeCm
* gpsTrust
+ baroAltCm
* (1.0f
- gpsTrust
);
179 } else if (haveBaroAlt
&& (positionConfig()->altitude_source
== DEFAULT
|| positionConfig()->altitude_source
== BARO_ONLY
)) {
180 zeroedAltitudeCm
= baroAltCm
; // use Baro if no GPS data, or we want Baro only
184 zeroedAltitudeCm
= pt2FilterApply(&altitudeLpf
, zeroedAltitudeCm
);
185 // NOTE: this filter must receive 0 as its input, for the whole disarmed time, to ensure correct zeroed values on arming
188 displayAltitudeCm
= zeroedAltitudeCm
; // while armed, show filtered relative altitude in OSD / sensors tab
191 // *** calculate Vario signal
192 static float previousZeroedAltitudeCm
= 0.0f
;
193 zeroedAltitudeDerivative
= (zeroedAltitudeCm
- previousZeroedAltitudeCm
) * TASK_ALTITUDE_RATE_HZ
; // cm/s
194 previousZeroedAltitudeCm
= zeroedAltitudeCm
;
196 zeroedAltitudeDerivative
= pt2FilterApply(&altitudeDerivativeLpf
, zeroedAltitudeDerivative
);
199 estimatedVario
= lrintf(zeroedAltitudeDerivative
);
200 estimatedVario
= applyDeadband(estimatedVario
, 10); // ignore climb rates less than 0.1 m/s
204 DEBUG_SET(DEBUG_ALTITUDE
, 0, (int32_t)(100 * gpsTrust
));
205 DEBUG_SET(DEBUG_ALTITUDE
, 1, baroAltCm
);
206 DEBUG_SET(DEBUG_ALTITUDE
, 2, gpsAltCm
);
208 DEBUG_SET(DEBUG_ALTITUDE
, 3, estimatedVario
);
210 DEBUG_SET(DEBUG_RTH
, 1, displayAltitudeCm
);
211 DEBUG_SET(DEBUG_BARO
, 3, baroAltCm
);
213 #endif //defined(USE_BARO) || defined(USE_GPS)
215 int32_t getEstimatedAltitudeCm(void)
217 return lrintf(displayAltitudeCm
);
220 float getAltitude(void)
222 return zeroedAltitudeCm
;
226 int16_t getEstimatedVario(void)
228 return estimatedVario
;