-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
qnh.c
119 lines (105 loc) · 2.99 KB
/
qnh.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
/*
* Copyright (C) 2014 Christophe De Wagter
*
* This file is part of paparazzi.
*
* paparazzi 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 2, or (at your option)
* any later version.
*
* paparazzi 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 paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file qnh.c
* QNH module.
*
*/
#include "qnh.h"
#include "state.h"
#include "subsystems/abi.h"
#include "subsystems/sensors/baro.h"
#include "generated/airframe.h"
#ifndef QNH_BARO_ID
#define QNH_BARO_ID ABI_BROADCAST
#endif
struct qnh_struct qnh;
abi_event qnh_baro_event = {0, 0, 0};
void received_abs_baro_for_qnh(uint8_t sender_id, const float * pressure);
void received_abs_baro_for_qnh(__attribute__((__unused__)) uint8_t sender_id, const float * pressure)
{
qnh.baro_pressure = *pressure;
const float L = 0.0065; // [K/m]
const float T0 = 288.15; // [K]
const float g = 9.80665; // [m/s^2]
const float M = 0.0289644; // [kg/mol]
const float R = 8.31447; // [J/(mol*K)]
const float InvExpo = R * L / g / M;
const float MeterPerFeet = 0.3048;
float prel = qnh.baro_pressure / (qnh.qnh * 100.0f);
qnh.amsl_baro = (1 - pow(prel,InvExpo) ) * T0/L / MeterPerFeet;
qnh.baro_counter = 10;
}
#if PERIODIC_TELEMETRY
#include "subsystems/datalink/telemetry.h"
static void send_amsl(void)
{
DOWNLINK_SEND_AMSL(DefaultChannel, DefaultDevice, &qnh.amsl_baro, &qnh.amsl_gps);
}
#endif
void init_qnh(void) {
#if PERIODIC_TELEMETRY
register_periodic_telemetry(&telemetry_Ap, "AMSL", send_amsl);
#endif
qnh.qnh = 1013.25;
qnh.amsl_baro = 0;
qnh.baro_counter = 0;
qnh.amsl_gps = 0;
qnh.baro_pressure = 0;
AbiBindMsgBARO_ABS(QNH_BARO_ID, &qnh_baro_event, &received_abs_baro_for_qnh);
}
void compute_qnh(void)
{
const float L = 0.0065; // [K/m]
const float T0 = 288.15; // [K]
const float g = 9.80665; // [m/s^2]
const float M = 0.0289644; // [kg/mol]
const float R = 8.31447; // [J/(mol*K)]
const float Expo = g * M / R / L;
float h = stateGetPositionLla_f()->alt;
float Trel = 1 - L*h/T0;
qnh.qnh = round(qnh.baro_pressure / pow(Trel,Expo) / 100.0f);
}
float GetAmsl(void)
{
// If baro is OK
if (qnh.baro_counter > 0)
return qnh.amsl_baro;
// Otherwise use GPS
return qnh.amsl_gps;
}
void periodic_qnh(void)
{
const float MeterPerFeet = 0.3048;
/*
Check:
1200Pa per 100m
8.333cm per Pa
float Trel = 1 - L*h/T0;
float p = qnh * pow(Trel,Expo);
*/
float h = stateGetPositionLla_f()->alt;
qnh.amsl_gps = h / MeterPerFeet;
// Watchdog on baro
if (qnh.baro_counter > 0)
qnh.baro_counter--;
else
qnh.amsl_baro = 0.0;
}