]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/altimeter.cxx
Fix a dumb bug in NavDisplay text-enable.
[flightgear.git] / src / Instrumentation / altimeter.cxx
1 // altimeter.cxx - an altimeter tied to the static port.
2 // Written by David Megginson, started 2002.
3 // Modified by John Denker in 2007 to use a two layer atmosphere
4 // model in src/Environment/atmosphere.?xx
5 //
6 // This file is in the Public Domain and comes with no warranty.
7
8 // Example invocation, in the instrumentation.xml file:
9 //      <altimeter>
10 //        <name>encoder</name>
11 //        <number>0</number>
12 //        <static-pressure>/systems/static/pressure-inhg</static-pressure>
13 //        <quantum>10</quantum>
14 //        <tau>0</tau>
15 //      </altimeter>
16 // Note non-default name, quantum, and tau values.
17
18 #ifdef HAVE_CONFIG_H
19 #  include <config.h>
20 #endif
21
22 #include <simgear/math/interpolater.hxx>
23 #include <simgear/math/SGMath.hxx>
24
25 #include <Main/fg_props.hxx>
26 #include <Main/util.hxx>
27 #include <Environment/atmosphere.hxx>
28
29 #include "altimeter.hxx"
30
31 const double hPa2inHg = 29.92 / 1013.25;
32
33 Altimeter::Altimeter ( SGPropertyNode *node, double quantum )
34     : _rootNode( 
35        fgGetNode("/instrumentation",true)->
36            getChild( node->getStringValue("name", "altimeter"),
37                      node->getIntValue("number", 0),
38                      true)),
39       _static_pressure(node->getStringValue("static-pressure", "/systems/static/pressure-inhg")),
40       _tau(node->getDoubleValue("tau", 0.1)),
41       _quantum(node->getDoubleValue("quantum", quantum)),
42       _settingInHg(29.921260)
43 {
44     _tiedProperties.setRoot( _rootNode );
45 }
46
47 Altimeter::~Altimeter ()
48 {}
49
50 double
51 Altimeter::getSettingInHg() const
52 {
53     return _settingInHg;
54 }
55
56 void
57 Altimeter::setSettingInHg( double value )
58 {
59     _settingInHg = value;
60 }
61
62 double
63 Altimeter::getSettingHPa() const
64 {
65     return _settingInHg / hPa2inHg;
66 }
67
68 void
69 Altimeter::setSettingHPa( double value )
70 {
71     _settingInHg = value * hPa2inHg;
72 }
73
74
75 void
76 Altimeter::init ()
77 {
78     raw_PA = 0.0;
79     _kollsman = 0.0;
80     _pressure_node     = fgGetNode(_static_pressure.c_str(), true);
81     _serviceable_node  = _rootNode->getChild("serviceable", 0, true);
82     _press_alt_node    = _rootNode->getChild("pressure-alt-ft", 0, true);
83     _mode_c_node       = _rootNode->getChild("mode-c-alt-ft", 0, true);
84     _altitude_node     = _rootNode->getChild("indicated-altitude-ft", 0, true);
85 }
86
87 void
88 Altimeter::bind()
89 {
90     _tiedProperties.Tie("setting-inhg", this, &Altimeter::getSettingInHg, &Altimeter::setSettingInHg );
91     _tiedProperties.Tie("setting-hpa", this, &Altimeter::getSettingHPa, &Altimeter::setSettingHPa );
92 }
93
94 void
95 Altimeter::unbind()
96 {
97     _tiedProperties.Untie();
98 }
99
100 void
101 Altimeter::update (double dt)
102 {
103     if (_serviceable_node->getBoolValue()) {
104         double trat = _tau > 0 ? dt/_tau : 100;
105         double pressure = _pressure_node->getDoubleValue();
106         double press_alt = _press_alt_node->getDoubleValue();
107         // The mechanism settles slowly toward new pressure altitude:
108         raw_PA = fgGetLowPass(raw_PA, _altimeter.press_alt_ft(pressure), trat);
109         _mode_c_node->setDoubleValue(100 * SGMiscd::round(raw_PA/100));
110         _kollsman = fgGetLowPass(_kollsman, _altimeter.kollsman_ft(_settingInHg), trat);
111         if (_quantum)
112             press_alt = _quantum * SGMiscd::round(raw_PA/_quantum);
113         else
114             press_alt = raw_PA;
115         _press_alt_node->setDoubleValue(press_alt);
116         _altitude_node->setDoubleValue(press_alt - _kollsman);
117     }
118 }
119
120 // end of altimeter.cxx