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