]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/dme.cxx
Prepare and implement reinit methods for instruments
[flightgear.git] / src / Instrumentation / dme.cxx
1 // dme.cxx - distance-measuring equipment.
2 // Written by David Megginson, started 2003.
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 <simgear/compiler.h>
11 #include <simgear/sg_inlines.h>
12 #include <simgear/math/sg_geodesy.hxx>
13 #include <simgear/math/sg_random.h>
14
15 #include <Main/fg_props.hxx>
16 #include <Navaids/navlist.hxx>
17 #include <Sound/audioident.hxx>
18
19 #include "dme.hxx"
20
21 /**
22  * Adjust the range.
23  *
24  * Start by calculating the radar horizon based on the elevation
25  * difference, then clamp to the maximum, then add a fudge for
26  * borderline reception.
27  */
28 static double
29 adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
30               double max_range_nm)
31 {
32     double delta_elevation_ft =
33         fabs(aircraft_altitude_ft - transmitter_elevation_ft);
34     double range_nm = 1.23 * sqrt(delta_elevation_ft);
35     if (range_nm > max_range_nm)
36         range_nm = max_range_nm;
37     else if (range_nm < 20.0)
38         range_nm = 20.0;
39     double rand = sg_random();
40     return range_nm + (range_nm * rand * rand);
41 }
42
43
44 DME::DME ( SGPropertyNode *node )
45     : _last_distance_nm(0),
46       _last_frequency_mhz(-1),
47       _time_before_search_sec(0),
48       _navrecord(NULL),
49       _name(node->getStringValue("name", "dme")),
50       _num(node->getIntValue("number", 0)),
51       _audioIdent(NULL)
52 {
53 }
54
55 DME::~DME ()
56 {
57     delete _audioIdent;
58 }
59
60 void
61 DME::init ()
62 {
63     string branch;
64     branch = "/instrumentation/" + _name;
65
66     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
67
68     _serviceable_node = node->getChild("serviceable", 0, true);
69     _electrical_node = fgGetNode("/systems/electrical/outputs/dme", true);
70     SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
71     _source_node = fnode->getChild("source", 0, true);
72     _frequency_node = fnode->getChild("selected-mhz", 0, true);
73     _in_range_node = node->getChild("in-range", 0, true);
74     _distance_node = node->getChild("indicated-distance-nm", 0, true);
75     _speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
76     _time_node = node->getChild("indicated-time-min", 0, true);
77
78     double d = node->getDoubleValue( "volume", 1.0 );
79     _volume_node = node->getChild("volume", 0, true);
80     _volume_node->setDoubleValue( d );
81
82     bool b = node->getBoolValue( "ident", false );
83     _ident_btn_node = node->getChild("ident", 0, true);
84     _ident_btn_node->setBoolValue( b );
85
86     std::ostringstream temp;
87     temp << _name << "-ident-" << _num;
88     if( NULL == _audioIdent ) 
89         _audioIdent = new DMEAudioIdent( temp.str() );
90     _audioIdent->init();
91
92     reinit();
93 }
94
95 void
96 DME::reinit ()
97 {
98     _time_before_search_sec = 0;
99 }
100
101 void
102 DME::update (double delta_time_sec)
103 {
104     if( delta_time_sec < SGLimitsd::min() )
105         return;  //paused
106
107                                 // Figure out the source
108     const char * source = _source_node->getStringValue();
109     if (source[0] == '\0') {
110         string branch;
111         branch = "/instrumentation/" + _name + "/frequencies/selected-mhz";
112         _source_node->setStringValue(branch.c_str());
113         source = _source_node->getStringValue();
114     }
115                                 // Get the frequency
116
117     double frequency_mhz = fgGetDouble(source, 108.0);
118     if (frequency_mhz != _last_frequency_mhz) {
119         _time_before_search_sec = 0;
120         _last_frequency_mhz = frequency_mhz;
121     }
122     _frequency_node->setDoubleValue(frequency_mhz);
123
124                                 // Get the aircraft position
125     // On timeout, scan again
126     _time_before_search_sec -= delta_time_sec;
127     if (_time_before_search_sec < 0) {
128         _time_before_search_sec = 1.0;
129
130         if( fgGetBool( "/sim/realism/dme-fallback-to-loc", true ) ) {
131             if( NULL == (_navrecord = globals->get_loclist()->findByFreq( frequency_mhz,
132                 globals->get_aircraft_position())) ) {
133
134                 _navrecord = globals->get_dmelist()->findByFreq( frequency_mhz,
135                     globals->get_aircraft_position());
136             }
137         } else {
138             _navrecord = globals->get_dmelist()->findByFreq( frequency_mhz,
139                 globals->get_aircraft_position());
140         }
141     }
142
143     // If it's off, don't bother.
144     if (!_serviceable_node->getBoolValue() ||
145         !_electrical_node->getBoolValue() ||
146         NULL == _navrecord ) {
147         _last_distance_nm = 0;
148         _in_range_node->setBoolValue(false);
149         _distance_node->setDoubleValue(0);
150         _speed_node->setDoubleValue(0);
151         _time_node->setDoubleValue(0);
152         _audioIdent->setIdent("", 0.0 );
153         return;
154     }
155
156     // Calculate the distance to the transmitter
157     SGVec3d location = SGVec3d::fromGeod(globals->get_aircraft_position());
158     
159     double distance_nm = dist(_navrecord->cart(), location) * SG_METER_TO_NM;
160
161     double range_nm = adjust_range(_navrecord->get_elev_ft(),
162                                    globals->get_aircraft_position().getElevationFt(),
163                                    _navrecord->get_range());
164
165     if (distance_nm <= range_nm) {
166         double volume = _volume_node->getDoubleValue();
167         if( false == _ident_btn_node->getBoolValue() )
168             volume = 0.0;
169
170         _audioIdent->setIdent(_navrecord->ident(), volume );
171
172         double speed_kt = (fabs(distance_nm - _last_distance_nm) *
173                            ((1 / delta_time_sec) * 3600.0));
174         _last_distance_nm = distance_nm;
175
176         _in_range_node->setBoolValue(true);
177         double tmp_dist = distance_nm - _navrecord->get_multiuse();
178         if ( tmp_dist < 0.0 ) {
179             tmp_dist = 0.0;
180         }
181         _distance_node->setDoubleValue( tmp_dist );
182         _speed_node->setDoubleValue(speed_kt);
183         if (SGLimitsd::min() < fabs(speed_kt))
184           _time_node->setDoubleValue(distance_nm/speed_kt*60.0);
185         
186     } else {
187         _last_distance_nm = 0;
188         _in_range_node->setBoolValue(false);
189         _distance_node->setDoubleValue(0);
190         _speed_node->setDoubleValue(0);
191         _time_node->setDoubleValue(0);
192         _audioIdent->setIdent("", 0.0 );
193     }
194     
195     _audioIdent->update( delta_time_sec );
196 }
197
198 // end of dme.cxx