]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/altimeter.cxx
ebd2f6d0de6fee2f28ac253f49db61322253c58e
[flightgear.git] / src / Instrumentation / altimeter.cxx
1 // altimeter.cxx - an altimeter tied to the static port.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 #include <simgear/math/interpolater.hxx>
7
8 #include "altimeter.hxx"
9 #include <Main/fg_props.hxx>
10 #include <Main/util.hxx>
11
12
13 // A higher number means more responsive
14 #define RESPONSIVENESS 10.0
15
16
17 // Altitude based on pressure difference from sea level.
18 // pressure difference inHG, altitude ft
19 static double altitude_data[][2] = {
20  { -8.41, -8858.27 },
21  { 0.00, 0.00 },
22  { 3.05, 2952.76 },
23  { 5.86, 5905.51 },
24  { 8.41, 8858.27 },
25  { 10.74, 11811.02 },
26  { 12.87, 14763.78 },
27  { 14.78, 17716.54 },
28  { 16.55, 20669.29 },
29  { 18.13, 23622.05 },
30  { 19.62, 26574.80 },
31  { 20.82, 29527.56 },
32  { 21.96, 32480.31 },
33  { 23.01, 35433.07 },
34  { 23.91, 38385.83 },
35  { 24.71, 41338.58 },
36  { 25.40, 44291.34 },
37  { 26.00, 47244.09 },
38  { 26.51, 50196.85 },
39  { 26.96, 53149.61 },
40  { 27.35, 56102.36 },
41  { 27.68, 59055.12 },
42  { 27.98, 62007.87 },
43  { 29.62, 100000.00 },            // just to fill it in
44  { -1, -1 }
45 };
46
47
48 Altimeter::Altimeter ( SGPropertyNode *node )
49     : _name(node->getStringValue("name", "altimeter")),
50       _num(node->getIntValue("number", 0)),
51       _static_pressure(node->getStringValue("static-pressure", "/systems/static/pressure-inhg")),
52       _altitude_table(new SGInterpTable)
53 {
54     int i;
55     for (i = 0; altitude_data[i][0] != -1; i++)
56         _altitude_table->addEntry(altitude_data[i][0], altitude_data[i][1]);
57 }
58
59 Altimeter::~Altimeter ()
60 {
61     delete _altitude_table;
62 }
63
64 void
65 Altimeter::init ()
66 {
67     string branch;
68     branch = "/instrumentation/" + _name;
69
70     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
71
72     _serviceable_node = node->getChild("serviceable", 0, true);
73     _setting_node = node->getChild("setting-inhg", 0, true);
74     _pressure_node = fgGetNode(_static_pressure.c_str(), true);
75     _altitude_node = node->getChild("indicated-altitude-ft", 0, true);
76 }
77
78 void
79 Altimeter::update (double dt)
80 {
81     if (_serviceable_node->getBoolValue()) {
82         double pressure = _pressure_node->getDoubleValue();
83         double setting = _setting_node->getDoubleValue();
84
85                                 // Move towards the current setting
86         double last_altitude = _altitude_node->getDoubleValue();
87         double current_altitude =
88             _altitude_table->interpolate(setting - pressure);
89         _altitude_node->setDoubleValue(fgGetLowPass(last_altitude,
90                                                     current_altitude,
91                                                     dt * RESPONSIVENESS));
92     }
93 }
94
95 // end of altimeter.cxx