]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/airspeed_indicator.cxx
0d59ffcf331cd010a9b5172c6b26b3e647519147
[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(node->getStringValue("name", "airspeed-indicator")),
21     _num(node->getIntValue("number", 0)),
22     _total_pressure(node->getStringValue("total-pressure", "/systems/pitot/total-pressure-inhg")),
23     _static_pressure(node->getStringValue("static-pressure", "/systems/static/pressure-inhg")),
24     _has_overspeed(node->getBoolValue("has-overspeed-indicator",false))
25 {
26 }
27
28 AirspeedIndicator::~AirspeedIndicator ()
29 {
30 }
31
32 void
33 AirspeedIndicator::init ()
34 {
35     string branch;
36     branch = "/instrumentation/" + _name;
37
38     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
39     _serviceable_node = node->getChild("serviceable", 0, true);
40     _total_pressure_node = fgGetNode(_total_pressure.c_str(), true);
41     _static_pressure_node = fgGetNode(_static_pressure.c_str(), true);
42     _density_node = fgGetNode("/environment/density-slugft3", true);
43     _speed_node = node->getChild("indicated-speed-kt", 0, true);
44
45   // overspeed-indicator properties
46     if (_has_overspeed) {
47         _ias_limit_node = node->getNode("ias-limit",0, true);
48         _mach_limit_node = node->getNode("mach-limit",0, true);
49         _alt_threshold_node = node->getNode("alt-threshold",0, true);
50         _airspeed_limit = node->getChild("airspeed-limit-kt", 0, true);
51
52         if (!_ias_limit_node->hasValue()) {
53           _ias_limit_node->setDoubleValue(248.0);
54         }
55
56         if (!_mach_limit_node->hasValue()) {
57           _mach_limit_node->setDoubleValue(0.48);
58         }
59
60         if (!_alt_threshold_node->hasValue()) {
61           _alt_threshold_node->setDoubleValue(13200);
62         }
63
64         string paSource = node->getStringValue("pressure-alt-source",
65           "/instrumentation/altimeter/pressure-alt-ft");
66         _pressure_alt = fgGetNode(paSource.c_str(), true);
67         _mach = fgGetNode("/velocities/mach", true);
68     }
69 }
70
71 #ifndef FPSTOKTS
72 # define FPSTOKTS 0.592484
73 #endif
74
75 #ifndef INHGTOPSF
76 # define INHGTOPSF (2116.217/29.9212)
77 #endif
78
79 void
80 AirspeedIndicator::update (double dt)
81 {
82     if (!_serviceable_node->getBoolValue()) {
83         return;
84     }
85     
86     double pt = _total_pressure_node->getDoubleValue() * INHGTOPSF;
87     double p = _static_pressure_node->getDoubleValue() * INHGTOPSF;
88     double r = _density_node->getDoubleValue();
89     double q = ( pt - p );  // dynamic pressure
90
91     // Now, reverse the equation (normalize dynamic pressure to
92     // avoid "nan" results from sqrt)
93     if ( q < 0 ) { q = 0.0; }
94     double v_fps = sqrt((2 * q) / r);
95
96                             // Publish the indicated airspeed
97     double last_speed_kt = _speed_node->getDoubleValue();
98     double current_speed_kt = v_fps * FPSTOKTS;
99     double filtered_speed = fgGetLowPass(last_speed_kt,
100                                              current_speed_kt,
101                                              dt * RESPONSIVENESS);
102     _speed_node->setDoubleValue(filtered_speed);
103
104     if (!_has_overspeed) {
105         return;
106     }
107     
108     double lmt = _ias_limit_node->getDoubleValue();
109     if (_pressure_alt->getDoubleValue() > _alt_threshold_node->getDoubleValue()) {
110         double mmo = _mach_limit_node->getDoubleValue();
111         lmt = (filtered_speed/_mach->getDoubleValue())* mmo;
112     }
113     
114     _airspeed_limit->setDoubleValue(lmt);
115 }
116
117 // end of airspeed_indicator.cxx