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