]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/airspeed_indicator.cxx
VS2015 compatability fixes.
[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 // Last modified by Eric van den Berg, 09 Dec 2012
4 //
5 // This file is in the Public Domain and comes with no warranty.
6
7 #ifdef HAVE_CONFIG_H
8 #  include "config.h"
9 #endif
10
11 #include <cmath>
12
13 #include <simgear/constants.h>
14 #include <simgear/math/interpolater.hxx>
15
16 #include "airspeed_indicator.hxx"
17 #include <Main/fg_props.hxx>
18 #include <Main/util.hxx>
19 #include <Environment/environment_mgr.hxx>
20 #include <Environment/environment.hxx>
21
22
23 // A higher number means more responsive.
24 #define RESPONSIVENESS 50.0
25
26 AirspeedIndicator::AirspeedIndicator ( SGPropertyNode *node )
27     :
28     _name(node->getStringValue("name", "airspeed-indicator")),
29     _num(node->getIntValue("number", 0)),
30     _total_pressure(node->getStringValue("total-pressure", "/systems/pitot/total-pressure-inhg")),
31     _static_pressure(node->getStringValue("static-pressure", "/systems/static/pressure-inhg")),
32     _has_overspeed(node->getBoolValue("has-overspeed-indicator",false)),
33     _pressure_alt_source(node->getStringValue("pressure-alt-source", "/instrumentation/altimeter/pressure-alt-ft")),
34     _ias_limit(node->getDoubleValue("ias-limit", 248.0)),
35     _mach_limit(node->getDoubleValue("mach-limit", 0.48)),
36     _alt_threshold(node->getDoubleValue("alt-threshold", 13200))
37 {
38   _environmentManager = NULL;
39 }
40
41 AirspeedIndicator::~AirspeedIndicator ()
42 {
43 }
44
45 void
46 AirspeedIndicator::init ()
47 {
48     std::string branch;
49     branch = "/instrumentation/" + _name;
50
51     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
52     _serviceable_node = node->getChild("serviceable", 0, true);
53     _total_pressure_node = fgGetNode(_total_pressure.c_str(), true);
54     _static_pressure_node = fgGetNode(_static_pressure.c_str(), true);
55     _density_node = fgGetNode("/environment/density-slugft3", true);
56     _speed_node = node->getChild("indicated-speed-kt", 0, true);
57     _tas_node = node->getChild("true-speed-kt", 0, true);
58     _mach_node = node->getChild("indicated-mach", 0, true);
59     
60   // overspeed-indicator properties
61     if (_has_overspeed) {
62         _ias_limit_node = node->getNode("ias-limit",0, true);
63         _mach_limit_node = node->getNode("mach-limit",0, true);
64         _alt_threshold_node = node->getNode("alt-threshold",0, true);
65         
66         if (!_ias_limit_node->hasValue()) {
67           _ias_limit_node->setDoubleValue(_ias_limit);
68         }
69
70         if (!_mach_limit_node->hasValue()) {
71           _mach_limit_node->setDoubleValue(_mach_limit);
72         }
73
74         if (!_alt_threshold_node->hasValue()) {
75           _alt_threshold_node->setDoubleValue(_alt_threshold);
76         }
77
78         _airspeed_limit = node->getChild("airspeed-limit-kt", 0, true);
79         _pressure_alt = fgGetNode(_pressure_alt_source.c_str(), true);
80     }
81     
82     _environmentManager = (FGEnvironmentMgr*) globals->get_subsystem("environment");
83 }
84
85 void
86 AirspeedIndicator::reinit ()
87 {
88     _speed_node->setDoubleValue(0.0);
89 }
90
91 void
92 AirspeedIndicator::update (double dt)
93 {
94     if (!_serviceable_node->getBoolValue()) {
95         return;
96     }
97     
98     double pt = _total_pressure_node->getDoubleValue() ;
99     double p = _static_pressure_node->getDoubleValue() ;
100     double qc = ( pt - p ) * SG_INHG_TO_PA ;  // Impact pressure in Pa, _not_ to be confused with dynamic pressure!!!
101
102     // Now, reverse the equation (normalize impact pressure to
103     // avoid "nan" results from sqrt)
104     qc = std::max( qc , 0.0 );
105     // Calibrated airspeed (using compressible aerodynamics) based on impact pressure qc in m/s
106     // Using calibrated airspeed as indicated airspeed, neglecting any airspeed indicator errors.
107     double v_cal = sqrt( 7 * SG_p0_Pa/SG_rho0_kg_p_m3 * ( pow( 1 + qc/SG_p0_Pa  , 1/3.5 )  -1 ) );
108
109                             // Publish the indicated airspeed
110     double last_speed_kt = _speed_node->getDoubleValue();
111     double current_speed_kt = v_cal * SG_MPS_TO_KT;
112     double filtered_speed = fgGetLowPass(last_speed_kt,
113                                              current_speed_kt,
114                                              dt * RESPONSIVENESS);
115     _speed_node->setDoubleValue(filtered_speed);
116     computeMach();
117
118     if (!_has_overspeed) {
119         return;
120     }
121     
122     double lmt = _ias_limit_node->getDoubleValue();
123     if (_pressure_alt->getDoubleValue() > _alt_threshold_node->getDoubleValue()) {
124         double mmo = _mach_limit_node->getDoubleValue();
125         lmt = (filtered_speed/_mach_node->getDoubleValue())* mmo;
126     }
127     
128     _airspeed_limit->setDoubleValue(lmt);
129 }
130
131 void
132 AirspeedIndicator::computeMach()
133 {
134   if (!_environmentManager) {
135     return;
136   }
137   
138   FGEnvironment env(_environmentManager->getEnvironment());
139   
140   double oatK = env.get_temperature_degc() + SG_T0_K - 15.0 ;         // OAT in Kelvin
141   oatK = std::max( oatK , 0.001 );                                // should never happen, but just in case someone flies into space...
142   double c = sqrt(SG_gamma * SG_R_m2_p_s2_p_K * oatK);                 // speed-of-sound in m/s at aircraft position
143   double pt = _total_pressure_node->getDoubleValue() * SG_INHG_TO_PA;  // total pressure in Pa
144   double p = _static_pressure_node->getDoubleValue() * SG_INHG_TO_PA;  // static pressure in Pa
145   p = std::max( p , 0.001 );                                           // should never happen, but just in case someone flies into space...
146   double rho = _density_node->getDoubleValue() * SG_SLUGFT3_TO_KGPM3;  // air density in kg/m3
147   rho = std::max( rho , 0.001 );                                      // should never happen, but just in case someone flies into space...
148   
149   // true airspeed in m/s
150   pt = std::max( pt , p );
151   double V_true = sqrt( 7 * p/rho * (pow( 1 + (pt-p)/p , 1/3.5 ) -1 ) );
152   // Mach number; _see notes in systems/pitot.cxx_
153   double mach = V_true / c;
154   
155   // publish Mach and TAS
156   _mach_node->setDoubleValue(mach);
157   _tas_node->setDoubleValue(V_true * SG_MPS_TO_KT );
158 }
159
160 // end of airspeed_indicator.cxx