]> git.mxchange.org Git - flightgear.git/blob - src/Radio/radio.cxx
add function for navaid reception
[flightgear.git] / src / Radio / radio.cxx
1 // radio.cxx -- implementation of FGRadio
2 // Class to manage radio propagation using the ITM model
3 // Written by Adrian Musceac, started August 2011.
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <math.h>
26 #include <stdlib.h>
27 #include <deque>
28 #include "radio.hxx"
29 #include <Scenery/scenery.hxx>
30
31 #define WITH_POINT_TO_POINT 1
32 #include "itm.cpp"
33
34
35 FGRadio::FGRadio() {
36         
37         /** radio parameters (which should probably be set for each radio) */
38         
39         _receiver_sensitivity = -110.0; // typical AM receiver sensitivity seems to be 0.8 microVolt at 12dB SINAD
40         
41         /** AM transmitter power in dBm.
42         *       Note this value is calculated from the typical final transistor stage output
43         *       small aircraft have portable transmitters which operate at 36 dBm output (4 Watts)
44         *       later possibly store this value in aircraft description
45         *       ATC comms usually operate high power equipment, thus making the link asymetrical; this is taken care of in propagation routines
46         **/
47         _transmitter_power = 43.0;
48         
49         /** pilot plane's antenna gain + AI aircraft antenna gain
50         *       real-life gain for conventional monopole/dipole antenna
51         **/
52         _antenna_gain = 2.0;
53         _propagation_model = 2; //  choose between models via option: realistic radio on/off
54         
55 }
56
57 FGRadio::~FGRadio() 
58 {
59 }
60
61
62 double FGRadio::getFrequency(int radio) {
63         double freq = 118.0;
64         switch (radio) {
65                 case 1:
66                         freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
67                         break;
68                 case 2:
69                         freq = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
70                         break;
71                 default:
72                         freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
73                         
74         }
75         return freq;
76 }
77
78 /*** TODO: receive multiplayer chat message and voice
79 ***/
80 void FGRadio::receiveChat(SGGeod tx_pos, double freq, string text,
81         int ground_to_air) {
82
83 }
84
85 /*** TODO: receive navaid 
86 ***/
87 double FGRadio::receiveNav(SGGeod tx_pos, double freq, int transmission_type) {
88         
89         // need to implement transmitter power
90         if ( _propagation_model == 1) {
91                 return LOS_calculate_attenuation(tx_pos, freq, 1);
92         }
93         else if ( _propagation_model == 2) {
94                 return ITM_calculate_attenuation(tx_pos, freq, 1);
95         }
96         
97         return -1;
98
99 }
100
101 /*** Receive ATC radio communication as text
102 ***/
103 void FGRadio::receiveATC(SGGeod tx_pos, double freq, string text,
104         int ground_to_air) {
105
106         /*
107         double comm1 = getFrequency(1);
108         double comm2 = getFrequency(2);
109         if ( (freq != comm1) &&  (freq != comm2) ) {
110                 cerr << "Frequency not tuned: " << freq << " Radio1: " << comm1 << " Radio2: " << comm2 << endl;
111                 return;
112         }
113         else {
114         */
115                 if ( _propagation_model == 0) {
116                         fgSetString("/sim/messages/atc", text.c_str());
117                 }
118                 else if ( _propagation_model == 1 ) {
119                         // TODO: free space, round earth
120                         double signal = LOS_calculate_attenuation(tx_pos, freq, ground_to_air);
121                         if (signal <= 0.0) {
122                                 SG_LOG(SG_GENERAL, SG_BULK, "Signal below receiver minimum sensitivity: " << signal);
123                                 //cerr << "Signal below receiver minimum sensitivity: " << signal << endl;
124                                 return;
125                         }
126                         else {
127                                 SG_LOG(SG_GENERAL, SG_BULK, "Signal completely readable: " << signal);
128                                 //cerr << "Signal completely readable: " << signal << endl;
129                                 fgSetString("/sim/messages/atc", text.c_str());
130                                 fgSetDouble("/sim/radio/comm1-signal", signal);
131                         }
132                 }
133                 else if ( _propagation_model == 2 ) {
134                         // Use ITM propagation model
135                         double signal = ITM_calculate_attenuation(tx_pos, freq, ground_to_air);
136                         if (signal <= 0.0) {
137                                 SG_LOG(SG_GENERAL, SG_BULK, "Signal below receiver minimum sensitivity: " << signal);
138                                 //cerr << "Signal below receiver minimum sensitivity: " << signal << endl;
139                                 return;
140                         }
141                         if ((signal > 0.0) && (signal < 12.0)) {
142                                 /** for low SNR values implement a way to make the conversation
143                                 *       hard to understand but audible
144                                 *       in the real world, the receiver AGC fails to capture the slope
145                                 *       and the signal, due to being amplitude modulated, decreases volume after demodulation
146                                 *       the workaround below is more akin to what would happen on a FM transmission
147                                 *       therefore the correct way would be to work on the volume
148                                 **/
149                                 /*
150                                 string hash_noise = " ";
151                                 int reps = (int) (fabs(floor(signal - 11.0)) * 2);
152                                 int t_size = text.size();
153                                 for (int n = 1; n <= reps; ++n) {
154                                         int pos = rand() % (t_size -1);
155                                         text.replace(pos,1, hash_noise);
156                                 }
157                                 */
158                                 double volume = (fabs(signal - 12.0) / 12);
159                                 double old_volume = fgGetDouble("/sim/sound/voices/voice/volume");
160                                 SG_LOG(SG_GENERAL, SG_BULK, "Usable signal at limit: " << signal);
161                                 //cerr << "Usable signal at limit: " << signal << endl;
162                                 fgSetDouble("/sim/sound/voices/voice/volume", volume);
163                                 fgSetString("/sim/messages/atc", text.c_str());
164                                 fgSetDouble("/sim/radio/comm1-signal", signal);
165                                 fgSetDouble("/sim/sound/voices/voice/volume", old_volume);
166                         }
167                         else {
168                                 SG_LOG(SG_GENERAL, SG_BULK, "Signal completely readable: " << signal);
169                                 //cerr << "Signal completely readable: " << signal << endl;
170                                 fgSetString("/sim/messages/atc", text.c_str());
171                                 fgSetDouble("/sim/radio/comm1-signal", signal);
172                         }
173                         
174                 }
175                 
176         //}
177         
178 }
179
180 /***  Implement radio attenuation               
181           based on the Longley-Rice propagation model
182 ***/
183 double FGRadio::ITM_calculate_attenuation(SGGeod pos, double freq,
184                                int transmission_type) {
185
186         
187         
188         /** ITM default parameters 
189                 TODO: take them from tile materials (especially for sea)?
190         **/
191         double eps_dielect=15.0;
192         double sgm_conductivity = 0.005;
193         double eno = 301.0;
194         double frq_mhz;
195         if( (freq < 118.0) || (freq > 137.0) )
196                 frq_mhz = 125.0;        // sane value, middle of bandplan
197         else
198                 frq_mhz = freq;
199         int radio_climate = 5;          // continental temperate
200         int pol=1;      // assuming vertical polarization although this is more complex in reality
201         double conf = 0.90;     // 90% of situations and time, take into account speed
202         double rel = 0.90;      
203         double dbloss;
204         char strmode[150];
205         int errnum;
206         
207         double tx_pow = _transmitter_power;
208         double ant_gain = _antenna_gain;
209         double signal = 0.0;
210         
211         if(transmission_type == 1)
212                 tx_pow = _transmitter_power + 6.0;
213
214         if((transmission_type == 1) || (transmission_type == 3))
215                 ant_gain = _antenna_gain + 3.0; //pilot plane's antenna gain + ground station antenna gain
216         
217         double link_budget = tx_pow - _receiver_sensitivity + ant_gain; 
218
219         FGScenery * scenery = globals->get_scenery();
220         
221         double own_lat = fgGetDouble("/position/latitude-deg");
222         double own_lon = fgGetDouble("/position/longitude-deg");
223         double own_alt_ft = fgGetDouble("/position/altitude-ft");
224         double own_alt= own_alt_ft * SG_FEET_TO_METER;
225         
226         
227         //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
228         
229         SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
230         SGGeod max_own_pos = SGGeod::fromDegM( own_lon, own_lat, SG_MAX_ELEVATION_M );
231         SGGeoc center = SGGeoc::fromGeod( max_own_pos );
232         SGGeoc own_pos_c = SGGeoc::fromGeod( own_pos );
233         
234         /**     position of sender radio antenna (HAAT)
235                         sender can be aircraft or ground station
236         **/
237         double ATC_HAAT = 30.0;
238         double Aircraft_HAAT = 5.0;
239         double sender_alt_ft,sender_alt;
240         double transmitter_height=0.0;
241         double receiver_height=0.0;
242         SGGeod sender_pos = pos;
243         
244         sender_alt_ft = sender_pos.getElevationFt();
245         sender_alt = sender_alt_ft * SG_FEET_TO_METER;
246         SGGeod max_sender_pos = SGGeod::fromGeodM( pos, SG_MAX_ELEVATION_M );
247         SGGeoc sender_pos_c = SGGeoc::fromGeod( sender_pos );
248         //cerr << "ITM:: sender Lat: " << parent->getLatitude() << ", Lon: " << parent->getLongitude() << ", Alt: " << sender_alt << endl;
249         
250         double point_distance= 90.0; // regular SRTM is 90 meters
251         double course = SGGeodesy::courseRad(own_pos_c, sender_pos_c);
252         double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
253         double probe_distance = 0.0;
254         /** If distance larger than this value (300 km), assume reception imposssible */
255         if (distance_m > 300000)
256                 return -1.0;
257         /** If above 8000 meters, consider LOS mode and calculate free-space att */
258         if (own_alt > 8000) {
259                 dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55;
260                 SG_LOG(SG_GENERAL, SG_BULK,
261                         "ITM Free-space mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation");
262                 //cerr << "ITM Free-space mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation" << endl;
263                 signal = link_budget - dbloss;
264                 return signal;
265         }
266         
267                 
268         double max_points = distance_m / point_distance;
269         deque<double> _elevations;
270
271         double elevation_under_pilot = 0.0;
272         if (scenery->get_elevation_m( max_own_pos, elevation_under_pilot, NULL )) {
273                 receiver_height = own_alt - elevation_under_pilot + 3; //assume antenna located 3 meters above ground
274         }
275
276         double elevation_under_sender = 0.0;
277         if (scenery->get_elevation_m( max_sender_pos, elevation_under_sender, NULL )) {
278                 transmitter_height = sender_alt - elevation_under_sender;
279         }
280         else {
281                 transmitter_height = sender_alt;
282         }
283         
284         if(transmission_type == 1) 
285                 transmitter_height += ATC_HAAT;
286         else
287                 transmitter_height += Aircraft_HAAT;
288         
289         SG_LOG(SG_GENERAL, SG_BULK,
290                         "ITM:: RX-height: " << receiver_height << " meters, TX-height: " << transmitter_height << " meters, Distance: " << distance_m << " meters");
291         cerr << "ITM:: RX-height: " << receiver_height << " meters, TX-height: " << transmitter_height << " meters, Distance: " << distance_m << " meters" << endl;
292         
293         unsigned int e_size = (deque<unsigned>::size_type)max_points;
294         
295         while (_elevations.size() <= e_size) {
296                 probe_distance += point_distance;
297                 SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, probe_distance ));
298                 
299                 double elevation_m = 0.0;
300         
301                 if (scenery->get_elevation_m( probe, elevation_m, NULL )) {
302                         if((transmission_type == 3) || (transmission_type == 4)) {
303                                 _elevations.push_back(elevation_m);
304                         }
305                         else {
306                                  _elevations.push_front(elevation_m);
307                         }
308                 }
309                 else {
310                         if((transmission_type == 3) || (transmission_type == 4)) {
311                                 _elevations.push_back(elevation_m);
312                         }
313                         else {
314                         _elevations.push_front(0.0);
315                         }
316                 }
317         }
318         if((transmission_type == 3) || (transmission_type == 4)) {
319                 _elevations.push_front(elevation_under_pilot);
320                 _elevations.push_back(elevation_under_sender);
321         }
322         else {
323                 _elevations.push_back(elevation_under_pilot);
324                 _elevations.push_front(elevation_under_sender);
325         }
326         
327         
328         double max_alt_between=0.0;
329         for( deque<double>::size_type i = 0; i < _elevations.size(); i++ ) {
330                 if (_elevations[i] > max_alt_between) {
331                         max_alt_between = _elevations[i];
332                 }
333         }
334         
335         double num_points= (double)_elevations.size();
336         //cerr << "ITM:: Max alt between: " << max_alt_between << ", num points:" << num_points << endl;
337         _elevations.push_front(point_distance);
338         _elevations.push_front(num_points -1);
339         int size = _elevations.size();
340         double itm_elev[size];
341         for(int i=0;i<size;i++) {
342                 itm_elev[i]=_elevations[i];
343                 //cerr << "ITM:: itm_elev: " << _elevations[i] << endl;
344         }
345
346         
347         /** first Fresnel zone radius
348                 frequency in the middle of the bandplan, more accuracy is not necessary
349         */
350         double fz_clr= 8.657 * sqrt(distance_m / 0.125);
351         
352         // TODO: If we clear the first Fresnel zone, we are into line of sight territory
353
354         // else we need to calculate point to point link loss
355         if((transmission_type == 3) || (transmission_type == 4)) {
356                 // the sender and receiver roles are switched
357                 point_to_point(itm_elev, receiver_height, transmitter_height,
358                         eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
359                         pol, conf, rel, dbloss, strmode, errnum);
360                 
361         }
362         else {
363
364                 point_to_point(itm_elev, transmitter_height, receiver_height,
365                         eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
366                         pol, conf, rel, dbloss, strmode, errnum);
367         }
368         SG_LOG(SG_GENERAL, SG_BULK,
369                         "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum);
370         cerr << "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum << endl;
371         
372         //if (errnum == 4)      // if parameters are outside sane values for lrprop, the alternative method is used
373         //      return -1;
374         signal = link_budget - dbloss;
375         return signal;
376
377 }
378
379 /*** implement simple LOS propagation model (WIP)
380 ***/
381 double FGRadio::LOS_calculate_attenuation(SGGeod pos, double freq,
382                                int transmission_type) {
383         double frq_mhz;
384         if( (freq < 118.0) || (freq > 137.0) )
385                 frq_mhz = 125.0;        // sane value, middle of bandplan
386         else
387                 frq_mhz = freq;
388         double dbloss;
389         double tx_pow = _transmitter_power;
390         double ant_gain = _antenna_gain;
391         double signal = 0.0;
392         double ATC_HAAT = 30.0;
393         double Aircraft_HAAT = 5.0;
394         double sender_alt_ft,sender_alt;
395         double transmitter_height=0.0;
396         double receiver_height=0.0;
397         double own_lat = fgGetDouble("/position/latitude-deg");
398         double own_lon = fgGetDouble("/position/longitude-deg");
399         double own_alt_ft = fgGetDouble("/position/altitude-ft");
400         double own_alt= own_alt_ft * SG_FEET_TO_METER;
401         
402         if(transmission_type == 1)
403                 tx_pow = _transmitter_power + 6.0;
404
405         if((transmission_type == 1) || (transmission_type == 3))
406                 ant_gain = _antenna_gain + 3.0; //pilot plane's antenna gain + ground station antenna gain
407         
408         double link_budget = tx_pow - _receiver_sensitivity + ant_gain; 
409
410         //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
411         
412         SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
413         
414         SGGeod sender_pos = pos;
415         
416         sender_alt_ft = sender_pos.getElevationFt();
417         sender_alt = sender_alt_ft * SG_FEET_TO_METER;
418         
419         receiver_height = own_alt;
420         transmitter_height = sender_alt;
421         
422         double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
423         
424         if(transmission_type == 1) 
425                 transmitter_height += ATC_HAAT;
426         else
427                 transmitter_height += Aircraft_HAAT;
428         
429         /** radio horizon calculation with wave bending k=4/3 */
430         double receiver_horizon = 4.12 * sqrt(receiver_height);
431         double transmitter_horizon = 4.12 * sqrt(transmitter_height);
432         double total_horizon = receiver_horizon + transmitter_horizon;
433         
434         if (distance_m > total_horizon) {
435                 return -1;
436         }
437         
438         // free-space loss (distance calculation should be changed)
439         dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55;
440         signal = link_budget - dbloss;
441         SG_LOG(SG_GENERAL, SG_BULK,
442                         "LOS:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm ");
443         cerr << "LOS:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm " << endl;
444         return signal;
445         
446 }