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