1 // radio.cxx -- implementation of FGRadio
2 // Class to manage radio propagation using the ITM model
3 // Written by Adrian Musceac, started August 2011.
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.
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.
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.
29 #include <Scenery/scenery.hxx>
31 #define WITH_POINT_TO_POINT 1
37 /** radio parameters (which should probably be set for each radio) */
39 _receiver_sensitivity = -110.0; // typical AM receiver sensitivity seems to be 0.8 microVolt at 12dB SINAD
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
47 _transmitter_power = 43.0;
49 /** pilot plane's antenna gain + AI aircraft antenna gain
50 * real-life gain for conventional monopole/dipole antenna
53 _propagation_model = 2; // choose between models via option: realistic radio on/off
62 double FGRadio::getFrequency(int radio) {
66 freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
69 freq = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
72 freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
78 /*** TODO: receive multiplayer chat message and voice
80 void FGRadio::receiveChat(SGGeod tx_pos, double freq, string text,
85 /*** TODO: receive navaid
87 double FGRadio::receiveNav(SGGeod tx_pos, double freq, int transmission_type) {
89 // need to implement transmitter power
90 if ( _propagation_model == 1) {
91 return LOS_calculate_attenuation(tx_pos, freq, 1);
93 else if ( _propagation_model == 2) {
94 return ITM_calculate_attenuation(tx_pos, freq, 1);
101 /*** Receive ATC radio communication as text
103 void FGRadio::receiveATC(SGGeod tx_pos, double freq, string text,
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;
115 if ( _propagation_model == 0) {
116 fgSetString("/sim/messages/atc", text.c_str());
118 else if ( _propagation_model == 1 ) {
119 // TODO: free space, round earth
120 double signal = LOS_calculate_attenuation(tx_pos, freq, ground_to_air);
122 SG_LOG(SG_GENERAL, SG_BULK, "Signal below receiver minimum sensitivity: " << signal);
123 //cerr << "Signal below receiver minimum sensitivity: " << signal << endl;
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);
133 else if ( _propagation_model == 2 ) {
134 // Use ITM propagation model
135 double signal = ITM_calculate_attenuation(tx_pos, freq, ground_to_air);
137 SG_LOG(SG_GENERAL, SG_BULK, "Signal below receiver minimum sensitivity: " << signal);
138 //cerr << "Signal below receiver minimum sensitivity: " << signal << endl;
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
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);
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);
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);
180 /*** Implement radio attenuation
181 based on the Longley-Rice propagation model
183 double FGRadio::ITM_calculate_attenuation(SGGeod pos, double freq,
184 int transmission_type) {
188 /** ITM default parameters
189 TODO: take them from tile materials (especially for sea)?
191 double eps_dielect=15.0;
192 double sgm_conductivity = 0.005;
195 if( (freq < 118.0) || (freq > 137.0) )
196 frq_mhz = 125.0; // sane value, middle of bandplan
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
207 double tx_pow = _transmitter_power;
208 double ant_gain = _antenna_gain;
211 if(transmission_type == 1)
212 tx_pow = _transmitter_power + 6.0;
214 if((transmission_type == 1) || (transmission_type == 3))
215 ant_gain = _antenna_gain + 3.0; //pilot plane's antenna gain + ground station antenna gain
217 double link_budget = tx_pow - _receiver_sensitivity + ant_gain;
219 FGScenery * scenery = globals->get_scenery();
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;
227 //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
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 );
234 /** position of sender radio antenna (HAAT)
235 sender can be aircraft or ground station
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;
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;
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)
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;
268 double max_points = distance_m / point_distance;
269 deque<double> _elevations;
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
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;
281 transmitter_height = sender_alt;
284 if(transmission_type == 1)
285 transmitter_height += ATC_HAAT;
287 transmitter_height += Aircraft_HAAT;
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;
293 unsigned int e_size = (deque<unsigned>::size_type)max_points;
295 while (_elevations.size() <= e_size) {
296 probe_distance += point_distance;
297 SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, probe_distance ));
299 double elevation_m = 0.0;
301 if (scenery->get_elevation_m( probe, elevation_m, NULL )) {
302 if((transmission_type == 3) || (transmission_type == 4)) {
303 _elevations.push_back(elevation_m);
306 _elevations.push_front(elevation_m);
310 if((transmission_type == 3) || (transmission_type == 4)) {
311 _elevations.push_back(elevation_m);
314 _elevations.push_front(0.0);
318 if((transmission_type == 3) || (transmission_type == 4)) {
319 _elevations.push_front(elevation_under_pilot);
320 _elevations.push_back(elevation_under_sender);
323 _elevations.push_back(elevation_under_pilot);
324 _elevations.push_front(elevation_under_sender);
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];
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;
347 /** first Fresnel zone radius
348 frequency in the middle of the bandplan, more accuracy is not necessary
350 double fz_clr= 8.657 * sqrt(distance_m / 0.125);
352 // TODO: If we clear the first Fresnel zone, we are into line of sight territory
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);
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);
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;
372 //if (errnum == 4) // if parameters are outside sane values for lrprop, the alternative method is used
374 signal = link_budget - dbloss;
379 /*** implement simple LOS propagation model (WIP)
381 double FGRadio::LOS_calculate_attenuation(SGGeod pos, double freq,
382 int transmission_type) {
384 if( (freq < 118.0) || (freq > 137.0) )
385 frq_mhz = 125.0; // sane value, middle of bandplan
389 double tx_pow = _transmitter_power;
390 double ant_gain = _antenna_gain;
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;
402 if(transmission_type == 1)
403 tx_pow = _transmitter_power + 6.0;
405 if((transmission_type == 1) || (transmission_type == 3))
406 ant_gain = _antenna_gain + 3.0; //pilot plane's antenna gain + ground station antenna gain
408 double link_budget = tx_pow - _receiver_sensitivity + ant_gain;
410 //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
412 SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
414 SGGeod sender_pos = pos;
416 sender_alt_ft = sender_pos.getElevationFt();
417 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
419 receiver_height = own_alt;
420 transmitter_height = sender_alt;
422 double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
424 if(transmission_type == 1)
425 transmitter_height += ATC_HAAT;
427 transmitter_height += Aircraft_HAAT;
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;
434 if (distance_m > total_horizon) {
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;