1 // altimeter.cxx - an altimeter tied to the static port.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain and comes with no warranty.
6 #include <simgear/math/interpolater.hxx>
8 #include "altimeter.hxx"
9 #include <Main/fg_props.hxx>
10 #include <Main/util.hxx>
13 // A higher number means more responsive
14 #define RESPONSIVENESS 10.0
17 // Altitude based on pressure difference from sea level.
18 // pressure difference inHG, altitude ft
19 static double altitude_data[][2] = {
43 { 29.62, 100000.00 }, // just to fill it in
48 Altimeter::Altimeter ( SGPropertyNode *node )
49 : _altitude_table(new SGInterpTable),
52 static_port("/systems/static")
55 for (i = 0; altitude_data[i][0] != -1; i++)
56 _altitude_table->addEntry(altitude_data[i][0], altitude_data[i][1]);
58 for ( i = 0; i < node->nChildren(); ++i ) {
59 SGPropertyNode *child = node->getChild(i);
60 string cname = child->getName();
61 string cval = child->getStringValue();
62 if ( cname == "name" ) {
64 } else if ( cname == "number" ) {
65 num = child->getIntValue();
66 } else if ( cname == "static-port" ) {
69 SG_LOG( SG_INSTR, SG_WARN, "Error in altimeter config logic" );
70 if ( name.length() ) {
71 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
77 Altimeter::Altimeter ()
78 : _altitude_table(new SGInterpTable)
81 for (int i = 0; altitude_data[i][0] != -1; i++)
82 _altitude_table->addEntry(altitude_data[i][0], altitude_data[i][1]);
85 Altimeter::~Altimeter ()
87 delete _altitude_table;
94 branch = "/instrumentation/" + name;
95 static_port += "/pressure-inhg";
97 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
99 _serviceable_node = node->getChild("serviceable", 0, true);
100 _setting_node = node->getChild("setting-inhg", 0, true);
101 _pressure_node = fgGetNode(static_port.c_str(), true);
102 _altitude_node = node->getChild("indicated-altitude-ft", 0, true);
106 Altimeter::update (double dt)
108 if (_serviceable_node->getBoolValue()) {
109 double pressure = _pressure_node->getDoubleValue();
110 double setting = _setting_node->getDoubleValue();
112 // Move towards the current setting
113 double last_altitude = _altitude_node->getDoubleValue();
114 double current_altitude =
115 _altitude_table->interpolate(setting - pressure);
116 _altitude_node->setDoubleValue(fgGetLowPass(last_altitude,
118 dt * RESPONSIVENESS));
122 // end of altimeter.cxx