Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / flight / position.c
blob2b783dadf7353fa369461d5fd98b6daf1f464708
1 /*
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)
8 * any later version.
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/>.
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #include <limits.h>
27 #include "platform.h"
29 #include "build/debug.h"
31 #include "common/maths.h"
33 #include "fc/runtime_config.h"
35 #include "flight/position.h"
36 #include "flight/imu.h"
37 #include "flight/pid.h"
39 #include "io/gps.h"
41 #include "scheduler/scheduler.h"
43 #include "sensors/sensors.h"
44 #include "sensors/barometer.h"
46 #include "pg/pg.h"
47 #include "pg/pg_ids.h"
49 typedef enum {
50 DEFAULT = 0,
51 BARO_ONLY,
52 GPS_ONLY
53 } altSource_e;
55 PG_REGISTER_WITH_RESET_TEMPLATE(positionConfig_t, positionConfig, PG_POSITION, 1);
57 PG_RESET_TEMPLATE(positionConfig_t, positionConfig,
58 .altSource = DEFAULT,
61 static int32_t estimatedAltitudeCm = 0; // in cm
63 #define BARO_UPDATE_FREQUENCY_40HZ (1000 * 25)
65 #ifdef USE_VARIO
66 static int16_t estimatedVario = 0; // in cm/s
68 int16_t calculateEstimatedVario(int32_t baroAlt, const uint32_t dTime) {
69 static float vel = 0;
70 static int32_t lastBaroAlt = 0;
72 int32_t baroVel = 0;
74 baroVel = (baroAlt - lastBaroAlt) * 1000000.0f / dTime;
75 lastBaroAlt = baroAlt;
77 baroVel = constrain(baroVel, -1500.0f, 1500.0f);
78 baroVel = applyDeadband(baroVel, 10.0f);
80 vel = vel * CONVERT_PARAMETER_TO_FLOAT(barometerConfig()->baro_cf_vel) + baroVel * (1.0f - CONVERT_PARAMETER_TO_FLOAT(barometerConfig()->baro_cf_vel));
81 int32_t vel_tmp = lrintf(vel);
82 vel_tmp = applyDeadband(vel_tmp, 5.0f);
84 return constrain(vel_tmp, SHRT_MIN, SHRT_MAX);
86 #endif
88 #if defined(USE_BARO) || defined(USE_GPS)
89 static bool altitudeOffsetSet = false;
91 void calculateEstimatedAltitude(timeUs_t currentTimeUs)
93 static timeUs_t previousTimeUs = 0;
94 static int32_t baroAltOffset = 0;
95 static int32_t gpsAltOffset = 0;
97 const uint32_t dTime = currentTimeUs - previousTimeUs;
98 if (dTime < BARO_UPDATE_FREQUENCY_40HZ) {
99 schedulerIgnoreTaskExecTime();
100 return;
102 previousTimeUs = currentTimeUs;
104 int32_t baroAlt = 0;
105 int32_t gpsAlt = 0;
107 #if defined(USE_GPS) && defined(USE_VARIO)
108 int16_t gpsVertSpeed = 0;
109 #endif
110 float gpsTrust = 0.3; //conservative default
111 bool haveBaroAlt = false;
112 bool haveGpsAlt = false;
113 #ifdef USE_BARO
114 if (sensors(SENSOR_BARO)) {
115 if (!baroIsCalibrationComplete()) {
116 performBaroCalibrationCycle();
117 } else {
118 baroAlt = baroCalculateAltitude();
119 haveBaroAlt = true;
122 #endif
124 #ifdef USE_GPS
125 if (sensors(SENSOR_GPS) && STATE(GPS_FIX)) {
126 gpsAlt = gpsSol.llh.altCm;
127 #ifdef USE_VARIO
128 gpsVertSpeed = GPS_verticalSpeedInCmS;
129 #endif
130 haveGpsAlt = true;
132 if (gpsSol.hdop != 0) {
133 gpsTrust = 100.0 / gpsSol.hdop;
135 // always use at least 10% of other sources besides gps if available
136 gpsTrust = MIN(gpsTrust, 0.9f);
138 #endif
140 if (ARMING_FLAG(ARMED) && !altitudeOffsetSet) {
141 baroAltOffset = baroAlt;
142 gpsAltOffset = gpsAlt;
143 altitudeOffsetSet = true;
144 } else if (!ARMING_FLAG(ARMED) && altitudeOffsetSet) {
145 altitudeOffsetSet = false;
147 baroAlt -= baroAltOffset;
148 gpsAlt -= gpsAltOffset;
151 if (haveGpsAlt && haveBaroAlt && positionConfig()->altSource == DEFAULT) {
152 if (ARMING_FLAG(ARMED)) {
153 estimatedAltitudeCm = gpsAlt * gpsTrust + baroAlt * (1 - gpsTrust);
154 } else {
155 estimatedAltitudeCm = gpsAlt; //absolute altitude is shown before arming, ignore baro
157 #ifdef USE_VARIO
158 // baro is a better source for vario, so ignore gpsVertSpeed
159 estimatedVario = calculateEstimatedVario(baroAlt, dTime);
160 #endif
161 } else if (haveGpsAlt && (positionConfig()->altSource == GPS_ONLY || positionConfig()->altSource == DEFAULT )) {
162 estimatedAltitudeCm = gpsAlt;
163 #if defined(USE_VARIO) && defined(USE_GPS)
164 estimatedVario = gpsVertSpeed;
165 #endif
166 } else if (haveBaroAlt && (positionConfig()->altSource == BARO_ONLY || positionConfig()->altSource == DEFAULT)) {
167 estimatedAltitudeCm = baroAlt;
168 #ifdef USE_VARIO
169 estimatedVario = calculateEstimatedVario(baroAlt, dTime);
170 #endif
175 DEBUG_SET(DEBUG_ALTITUDE, 0, (int32_t)(100 * gpsTrust));
176 DEBUG_SET(DEBUG_ALTITUDE, 1, baroAlt);
177 DEBUG_SET(DEBUG_ALTITUDE, 2, gpsAlt);
178 #ifdef USE_VARIO
179 DEBUG_SET(DEBUG_ALTITUDE, 3, estimatedVario);
180 #endif
183 bool isAltitudeOffset(void)
185 return altitudeOffsetSet;
187 #endif
189 int32_t getEstimatedAltitudeCm(void)
191 return estimatedAltitudeCm;
194 #ifdef USE_VARIO
195 int16_t getEstimatedVario(void)
197 return estimatedVario;
199 #endif