]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/airspeed_indicator.cxx
Melchior FRANZ:
[flightgear.git] / src / Instrumentation / airspeed_indicator.cxx
1 // airspeed_indicator.cxx - a regular pitot-static airspeed indicator.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 #include <math.h>
7
8 #include <simgear/math/interpolater.hxx>
9
10 #include "airspeed_indicator.hxx"
11 #include <Main/fg_props.hxx>
12 #include <Main/util.hxx>
13
14
15 // A higher number means more responsive.
16 #define RESPONSIVENESS 50.0
17
18 AirspeedIndicator::AirspeedIndicator ( SGPropertyNode *node )
19     :
20     name("airspeed-indicator"),
21     num(0),
22     pitot_port("/systems/pitot"),
23     static_port("/systems/static")
24 {
25     int i;
26     for ( i = 0; i < node->nChildren(); ++i ) {
27         SGPropertyNode *child = node->getChild(i);
28         string cname = child->getName();
29         string cval = child->getStringValue();
30         if ( cname == "name" ) {
31             name = cval;
32         } else if ( cname == "number" ) {
33             num = child->getIntValue();
34         } else if ( cname == "pitot-port" ) {
35             pitot_port = cval;
36         } else if ( cname == "static-port" ) {
37             static_port = cval;
38         } else {
39             SG_LOG( SG_INSTR, SG_WARN, "Error in aispeed-indicator config logic" );
40             if ( name.length() ) {
41                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
42             }
43         }
44     }
45 }
46
47
48 AirspeedIndicator::AirspeedIndicator ( int i )
49     :
50     name("airspeed-indicator"),
51     num(0),
52     pitot_port("/systems/pitot"),
53     static_port("/systems/static")
54 {
55     num = i;
56 }
57
58 AirspeedIndicator::~AirspeedIndicator ()
59 {
60 }
61
62 void
63 AirspeedIndicator::init ()
64 {
65     string branch;
66     branch = "/instrumentation/" + name;
67     pitot_port += "/total-pressure-inhg";
68     static_port += "/pressure-inhg";
69
70     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
71     _serviceable_node = node->getChild("serviceable", 0, true);
72     _total_pressure_node = fgGetNode(pitot_port.c_str(), true);
73     _static_pressure_node = fgGetNode(static_port.c_str(), true);
74     _density_node = fgGetNode("/environment/density-slugft3", true);
75     _speed_node = node->getChild("indicated-speed-kt", 0, true);
76 }
77
78 #ifndef FPSTOKTS
79 # define FPSTOKTS 0.592484
80 #endif
81
82 #ifndef INHGTOPSF
83 # define INHGTOPSF (2116.217/29.9212)
84 #endif
85
86 void
87 AirspeedIndicator::update (double dt)
88 {
89     if (_serviceable_node->getBoolValue()) {
90         double pt = _total_pressure_node->getDoubleValue() * INHGTOPSF;
91         double p = _static_pressure_node->getDoubleValue() * INHGTOPSF;
92         double r = _density_node->getDoubleValue();
93         double q = ( pt - p );  // dynamic pressure
94
95         // Now, reverse the equation (normalize dynamic pressure to
96         // avoid "nan" results from sqrt)
97         if ( q < 0 ) { q = 0.0; }
98         double v_fps = sqrt((2 * q) / r);
99
100                                 // Publish the indicated airspeed
101         double last_speed_kt = _speed_node->getDoubleValue();
102         double current_speed_kt = v_fps * FPSTOKTS;
103         _speed_node->setDoubleValue(fgGetLowPass(last_speed_kt,
104                                                  current_speed_kt,
105                                                  dt * RESPONSIVENESS));
106     }
107 }
108
109 // end of airspeed_indicator.cxx