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