forked from iNavFlight/inav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pitotmeter.c
227 lines (186 loc) · 6.4 KB
/
pitotmeter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include "platform.h"
#include "build/debug.h"
#include "common/maths.h"
#include "config/parameter_group.h"
#include "config/parameter_group_ids.h"
#include "drivers/logging.h"
#include "drivers/pitotmeter.h"
#include "drivers/pitotmeter_ms4525.h"
#include "fc/config.h"
#include "fc/runtime_config.h"
#include "sensors/pitotmeter.h"
#include "sensors/sensors.h"
pitot_t pitot;
#ifdef PITOT
static float pitotPressureZero = 0;
static uint16_t calibratingP = 0; // pitot calibration = get new zero pressure value
static float pitotPressure = 0;
static float pitotTemperature = 0;
static float CalibratedAirspeed = 0;
PG_REGISTER_WITH_RESET_TEMPLATE(pitotmeterConfig_t, pitotmeterConfig, PG_PITOTMETER_CONFIG, 0);
#ifdef PITOT
#define PITOT_HARDWARE_DEFAULT PITOT_AUTODETECT
#else
#define PITOT_HARDWARE_DEFAULT PITOT_NONE
#endif
PG_RESET_TEMPLATE(pitotmeterConfig_t, pitotmeterConfig,
.pitot_hardware = PITOT_HARDWARE_DEFAULT,
.use_median_filtering = 1,
.pitot_noise_lpf = 0.6f,
.pitot_scale = 1.00f
);
bool pitotDetect(pitotDev_t *dev, uint8_t pitotHardwareToUse)
{
pitotSensor_e pitotHardware = PITOT_NONE;
requestedSensors[SENSOR_INDEX_PITOT] = pitotHardwareToUse;
switch (pitotHardwareToUse) {
case PITOT_AUTODETECT:
case PITOT_MS4525:
#ifdef USE_PITOT_MS4525
if (ms4525Detect(dev)) {
pitotHardware = PITOT_MS4525;
break;
}
#endif
/* If we are asked for a specific sensor - break out, otherwise - fall through and continue */
if (pitotHardwareToUse != PITOT_AUTODETECT) {
break;
}
case PITOT_FAKE:
#ifdef USE_PITOT_FAKE
if (fakePitotDetect(&pitot)) {
pitotHardware = PITOT_FAKE;
break;
}
#endif
/* If we are asked for a specific sensor - break out, otherwise - fall through and continue */
if (pitotHardwareToUse != PITOT_AUTODETECT) {
break;
}
case PITOT_NONE:
pitotHardware = PITOT_NONE;
break;
}
addBootlogEvent6(BOOT_EVENT_PITOT_DETECTION, BOOT_EVENT_FLAGS_NONE, pitotHardware, 0, 0, 0);
if (pitotHardware == PITOT_NONE) {
sensorsClear(SENSOR_PITOT);
return false;
}
detectedSensors[SENSOR_INDEX_PITOT] = pitotHardware;
sensorsSet(SENSOR_PITOT);
return true;
}
bool pitotInit(void)
{
if (!pitotDetect(&pitot.dev, pitotmeterConfig()->pitot_hardware)) {
return false;
}
return true;
}
bool pitotIsCalibrationComplete(void)
{
return calibratingP == 0;
}
void pitotSetCalibrationCycles(uint16_t calibrationCyclesRequired)
{
calibratingP = calibrationCyclesRequired;
}
static bool pitotReady = false;
#define PRESSURE_SAMPLES_MEDIAN 3
static int32_t applyPitotmeterMedianFilter(int32_t newPressureReading)
{
static int32_t barometerFilterSamples[PRESSURE_SAMPLES_MEDIAN];
static int currentFilterSampleIndex = 0;
static bool medianFilterReady = false;
int nextSampleIndex;
nextSampleIndex = (currentFilterSampleIndex + 1);
if (nextSampleIndex == PRESSURE_SAMPLES_MEDIAN) {
nextSampleIndex = 0;
medianFilterReady = true;
}
barometerFilterSamples[currentFilterSampleIndex] = newPressureReading;
currentFilterSampleIndex = nextSampleIndex;
if (medianFilterReady)
return quickMedianFilter3(barometerFilterSamples);
else
return newPressureReading;
}
typedef enum {
PITOTMETER_NEEDS_SAMPLES = 0,
PITOTMETER_NEEDS_CALCULATION,
} pitotmeterState_e;
bool pitotIsReady(void) {
return pitotReady;
}
uint32_t pitotUpdate(void)
{
static pitotmeterState_e state = PITOTMETER_NEEDS_SAMPLES;
switch (state) {
default:
case PITOTMETER_NEEDS_SAMPLES:
pitot.dev.start();
state = PITOTMETER_NEEDS_CALCULATION;
return pitot.dev.delay;
break;
case PITOTMETER_NEEDS_CALCULATION:
pitot.dev.get();
pitot.dev.calculate(&pitotPressure, &pitotTemperature);
if (pitotmeterConfig()->use_median_filtering) {
pitotPressure = applyPitotmeterMedianFilter(pitotPressure);
}
state = PITOTMETER_NEEDS_SAMPLES;
return pitot.dev.delay;
break;
}
}
#define P0 101325.0f // standard pressure
#define CCEXPONENT 0.2857142857f // exponent of compressibility correction 2/7
#define CASFACTOR 760.8802669f // sqrt(5) * speed of sound at standard
#define TASFACTOR 0.05891022589f // 1/sqrt(T0)
static void performPitotCalibrationCycle(void)
{
pitotPressureZero -= pitotPressureZero / 8;
pitotPressureZero += pitotPressure / 8;
calibratingP--;
}
int32_t pitotCalculateAirSpeed(void)
{
if (pitotIsCalibrationComplete()) {
const float CalibratedAirspeed_tmp = pitotmeterConfig()->pitot_scale * CASFACTOR * sqrtf(powf(fabsf(pitotPressure - pitotPressureZero) / P0 + 1.0f, CCEXPONENT) - 1.0f);
CalibratedAirspeed = CalibratedAirspeed * pitotmeterConfig()->pitot_noise_lpf + CalibratedAirspeed_tmp * (1.0f - pitotmeterConfig()->pitot_noise_lpf); // additional LPF to reduce baro noise
float TrueAirspeed = CalibratedAirspeed * TASFACTOR * sqrtf(pitotTemperature);
pitot.airSpeed = TrueAirspeed*100;
//debug[0] = (int16_t)(CalibratedAirspeed*100);
//debug[1] = (int16_t)(TrueAirspeed*100);
//debug[2] = (int16_t)((pitotTemperature-273.15f)*100);
//debug[3] = AirSpeed;
} else {
performPitotCalibrationCycle();
pitot.airSpeed = 0;
}
return pitot.airSpeed;
}
bool pitotIsHealthy(void)
{
return true;
}
#endif /* PITOT */