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