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