]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/airspeed_indicator.cxx
223c4d774161414a8f598cde070aa52ef59f4283
[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 #ifdef HAVE_CONFIG_H
7 #  include "config.h"
8 #endif
9
10 #include <math.h>
11
12 #include <simgear/math/interpolater.hxx>
13
14 #include "airspeed_indicator.hxx"
15 #include <Main/fg_props.hxx>
16 #include <Main/util.hxx>
17
18
19 // A higher number means more responsive.
20 #define RESPONSIVENESS 50.0
21
22 AirspeedIndicator::AirspeedIndicator ( SGPropertyNode *node )
23     :
24     _name(node->getStringValue("name", "airspeed-indicator")),
25     _num(node->getIntValue("number", 0)),
26     _total_pressure(node->getStringValue("total-pressure", "/systems/pitot/total-pressure-inhg")),
27     _static_pressure(node->getStringValue("static-pressure", "/systems/static/pressure-inhg")),
28     _has_overspeed(node->getBoolValue("has-overspeed-indicator",false)),
29     _pressure_alt_source(node->getStringValue("pressure-alt-source", "/instrumentation/altimeter/pressure-alt-ft")),
30     _ias_limit(node->getDoubleValue("ias-limit", 248.0)),
31     _mach_limit(node->getDoubleValue("mach-limit", 0.48)),
32     _alt_threshold(node->getDoubleValue("alt-threshold", 13200))
33 {
34 }
35
36 AirspeedIndicator::~AirspeedIndicator ()
37 {
38 }
39
40 void
41 AirspeedIndicator::init ()
42 {
43     string branch;
44     branch = "/instrumentation/" + _name;
45
46     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
47     _serviceable_node = node->getChild("serviceable", 0, true);
48     _total_pressure_node = fgGetNode(_total_pressure.c_str(), true);
49     _static_pressure_node = fgGetNode(_static_pressure.c_str(), true);
50     _density_node = fgGetNode("/environment/density-slugft3", true);
51     
52     _sea_level_pressure_node = fgGetNode("/environment/pressure-sea-level-inhg", true);
53     _oat_celsius_node = fgGetNode("/environment/temperature-degc", true);
54   
55     _speed_node = node->getChild("indicated-speed-kt", 0, true);
56     _tas_node = node->getChild("true-speed-kt", 0, true);
57     _mach_node = node->getChild("indicated-mach", 0, true);
58     
59   // overspeed-indicator properties
60     if (_has_overspeed) {
61         _ias_limit_node = node->getNode("ias-limit",0, true);
62         _mach_limit_node = node->getNode("mach-limit",0, true);
63         _alt_threshold_node = node->getNode("alt-threshold",0, true);
64         
65         if (!_ias_limit_node->hasValue()) {
66           _ias_limit_node->setDoubleValue(_ias_limit);
67         }
68
69         if (!_mach_limit_node->hasValue()) {
70           _mach_limit_node->setDoubleValue(_mach_limit);
71         }
72
73         if (!_alt_threshold_node->hasValue()) {
74           _alt_threshold_node->setDoubleValue(_alt_threshold);
75         }
76
77         _airspeed_limit = node->getChild("airspeed-limit-kt", 0, true);
78         _pressure_alt = fgGetNode(_pressure_alt_source.c_str(), true);
79     }
80 }
81
82 void
83 AirspeedIndicator::reinit ()
84 {
85     _speed_node->setDoubleValue(0.0);
86 }
87
88 #ifndef FPSTOKTS
89 # define FPSTOKTS 0.592484
90 #endif
91
92 #ifndef INHGTOPSF
93 # define INHGTOPSF (2116.217/29.9212)
94 #endif
95
96 void
97 AirspeedIndicator::update (double dt)
98 {
99     if (!_serviceable_node->getBoolValue()) {
100         return;
101     }
102     
103     double pt = _total_pressure_node->getDoubleValue() * INHGTOPSF;
104     double p = _static_pressure_node->getDoubleValue() * INHGTOPSF;
105     double r = _density_node->getDoubleValue();
106     double q = ( pt - p );  // dynamic pressure
107
108     // Now, reverse the equation (normalize dynamic pressure to
109     // avoid "nan" results from sqrt)
110     if ( q < 0 ) { q = 0.0; }
111     double v_fps = sqrt((2 * q) / r);
112
113                             // Publish the indicated airspeed
114     double last_speed_kt = _speed_node->getDoubleValue();
115     double current_speed_kt = v_fps * FPSTOKTS;
116     double filtered_speed = fgGetLowPass(last_speed_kt,
117                                              current_speed_kt,
118                                              dt * RESPONSIVENESS);
119     _speed_node->setDoubleValue(filtered_speed);
120     computeMach(filtered_speed);
121
122     if (!_has_overspeed) {
123         return;
124     }
125     
126     double lmt = _ias_limit_node->getDoubleValue();
127     if (_pressure_alt->getDoubleValue() > _alt_threshold_node->getDoubleValue()) {
128         double mmo = _mach_limit_node->getDoubleValue();
129         lmt = (filtered_speed/_mach_node->getDoubleValue())* mmo;
130     }
131     
132     _airspeed_limit->setDoubleValue(lmt);
133 }
134
135 void
136 AirspeedIndicator::computeMach(double ias)
137 {
138    
139   // derived from http://williams.best.vwh.net/avform.htm#Mach
140   // names here are picked to be consistent with those formulae!
141
142   double oatK = _oat_celsius_node->getDoubleValue() + 273.15; // OAT in Kelvin
143   double CS = 38.967854 * sqrt(oatK); // speed-of-sound in knots at altitude
144   double CS_0 = 661.4786; // speed-of-sound in knots at sea-level
145   double P_0 = _sea_level_pressure_node->getDoubleValue();
146   double P = _static_pressure_node->getDoubleValue();
147   
148   double DP = P_0 * (pow(1 + 0.2*pow(ias/CS_0, 2), 3.5) - 1);
149   double mach = pow(5 * ( pow(DP/P + 1, 2.0/7.0) -1) , 0.5);
150   
151   // publish Mach and TAS
152   _mach_node->setDoubleValue(mach);
153   _tas_node->setDoubleValue(CS * mach);
154 }
155
156 // end of airspeed_indicator.cxx