[MSP SENSOR] Add support for MSP Airspeed sensor
[inav.git] / src / main / drivers / pitotmeter / pitotmeter_virtual.c
blob3f728285172a14cde4c4902d0b60b7d8941918f7
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdbool.h>
19 #include <stdint.h>
21 #include <platform.h>
23 #include "build/build_config.h"
24 #include "build/debug.h"
26 #include "common/axis.h"
28 #include "config/feature.h"
30 #include "common/maths.h"
31 #include "common/utils.h"
33 #include "fc/config.h"
35 #include "flight/pid.h"
36 #include "flight/wind_estimator.h"
38 #include "io/gps.h"
40 #include "navigation/navigation.h"
41 #include "navigation/navigation_private.h"
43 #include "sensors/pitotmeter.h"
45 #include "drivers/pitotmeter/pitotmeter.h"
46 #include "drivers/pitotmeter/pitotmeter_virtual.h"
48 #if defined(USE_WIND_ESTIMATOR) && defined(USE_PITOT_VIRTUAL)
50 static bool virtualPitotStart(pitotDev_t *pitot)
52 UNUSED(pitot);
53 return true;
56 static bool virtualPitotRead(pitotDev_t *pitot)
58 UNUSED(pitot);
59 return true;
62 static void virtualPitotCalculate(pitotDev_t *pitot, float *pressure, float *temperature)
64 UNUSED(pitot);
65 float airSpeed = 0.0f;
66 if (pitotIsCalibrationComplete()) {
67 if (isEstimatedWindSpeedValid()) {
68 uint16_t windHeading; //centidegrees
69 float windSpeed = getEstimatedHorizontalWindSpeed(&windHeading); //cm/s
70 float horizontalWindSpeed = windSpeed * cos_approx(CENTIDEGREES_TO_RADIANS(windHeading - posControl.actualState.yaw)); //yaw int32_t centidegrees
71 airSpeed = posControl.actualState.velXY - horizontalWindSpeed; //float cm/s or gpsSol.groundSpeed int16_t cm/s
72 } else {
73 airSpeed = pidProfile()->fixedWingReferenceAirspeed; //float cm/s
76 if (pressure)
77 //*pressure = sq(airSpeed / 100) * AIR_DENSITY_SEA_LEVEL_15C / 2 + P0;
78 *pressure = sq(airSpeed) * AIR_DENSITY_SEA_LEVEL_15C / 20000.0f + P0;
79 if (temperature)
80 *temperature = 288.15f; // Temperature at standard sea level (288.15 K)
83 bool virtualPitotDetect(pitotDev_t *pitot)
85 pitot->delay = 10000;
86 pitot->start = virtualPitotStart;
87 pitot->get = virtualPitotRead;
88 pitot->calculate = virtualPitotCalculate;
89 return feature(FEATURE_GPS);
91 #endif