forked from amperka/Troyka-IMU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GOST4401_81.cpp
114 lines (85 loc) · 2.84 KB
/
GOST4401_81.cpp
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
#include <GOST4401_81.h>
#include <math.h>
static float _GOST4401_geopotential2geometric(float altitude){
return altitude * GOST4401_E / (GOST4401_E - altitude);
}
static float _GOST4401_geometric2geopotential(float altitude){
return altitude * GOST4401_E / (GOST4401_E + altitude);
}
/**
* Returns geometric altitude value for the given pressure.
*
* @param float pressurePa - pressure in pascals
* @retval float geometric altitude in meters
*/
float GOST4401_getAltitude(float pressurePa){
if ((pressurePa <= GOST4401_MIN_PRESSURE) || (pressurePa > GOST4401_MAX_PRESSURE))
return NAN;
int idx = 0;
for (idx = 0; idx < GOST4401_LUT_RECORDS - 1; idx++){
if ((pressurePa <= ag_table[idx].press) && (pressurePa > ag_table[idx + 1].press))
break;
}
float Ps = ag_table[idx].press;
float Bm = ag_table[idx].t_grad;
float Tm = ag_table[idx].temp;
float Hb = ag_table[idx].alt;
float geopotH = 0;
if (Bm != 0.0) {
geopotH = ((Tm * pow(Ps / pressurePa, Bm * GOST4401_R / GOST4401_G) - Tm) / Bm);
} else {
geopotH = log10(Ps / pressurePa) * (GOST4401_R * Tm) / (GOST4401_G * 0.434294);
}
return _GOST4401_geopotential2geometric(Hb + geopotH);
};
/**
* Returns pressure in pascals for the given geometric altitude
*
* @param float altitude - geometric altitude in meters
* @retval float - pressure in pascals
*/
float GOST4401_getPressure(float altitude){
float geopotH = _GOST4401_geometric2geopotential(altitude);
if ((geopotH < GOST4401_MIN_GPALT) || (geopotH >= GOST4401_MAX_GPALT))
return NAN;
int idx = 0;
for (idx = 1; idx < GOST4401_LUT_RECORDS - 1; idx++) {
if ((geopotH >= ag_table[idx].alt) && (geopotH < ag_table[idx].alt))
break;
}
float Ps = ag_table[idx].press;
float Bm = ag_table[idx].t_grad;
float Tm = ag_table[idx].temp;
float Hb = ag_table[idx].alt;
float lP = 0;
if (Bm != 0.0) {
lP = log10(Ps) - (GOST4401_G / (Bm * GOST4401_R)) * log10((Tm + Bm * (geopotH - Hb)) / Tm);
} else {
lP = log10(Ps) - 0.434294 *(GOST4401_G * (geopotH - Hb)) / (GOST4401_R * Tm);
}
return pow(10, lP);
}
/**
* Returns temperature value in K for the given geometric altitude.
*
* @param float altitude - geometric altitude in meters
* @retval float - temperature in degrees K
*/
float GOST4401_getTemperature(float altitude){
float geopotH = _GOST4401_geometric2geopotential(altitude);
if ((geopotH < GOST4401_MIN_GPALT) || (geopotH >= GOST4401_MAX_GPALT))
return NAN;
int idx = 0;
for (idx = 1; idx < GOST4401_LUT_RECORDS - 1; idx++) {
if ((geopotH >= ag_table[idx].alt) && (geopotH < ag_table[idx].alt))
break;
}
float Bm = ag_table[idx].t_grad;
float Tm = ag_table[idx].temp;
float Hb = ag_table[idx].alt;
float temp = Tm;
if (Bm != 0.0){
temp += Bm * (geopotH - Hb);
}
return temp;
}