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 via constructor) */
38 _receiver_sensitivity = -110.0; // typical AM receiver sensitivity seems to be 0.8 microVolt at 12dB SINAD
40 /** AM transmitter power in dBm.
41 * Note this value is calculated from the typical final transistor stage output
42 * small aircraft have portable transmitters which operate at 36 dBm output (4 Watts)
43 * later store this value in aircraft description
44 * ATC comms usually operate high power equipment, thus making the link asymetrical; this is ignored for now
46 _transmitter_power = 43.0;
48 /** pilot plane's antenna gain + AI aircraft antenna gain
49 * real-life gain for conventional monopole/dipole antenna
52 _propagation_model = 2; // choose between models via option: realistic radio on/off
61 double FGRadio::getFrequency(int radio) {
65 freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
68 freq = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
71 freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
80 void FGRadio::receiveText(SGGeod tx_pos, double freq, string text,
84 double comm1 = getFrequency(1);
85 double comm2 = getFrequency(2);
86 if ( (freq != comm1) && (freq != comm2) ) {
87 cerr << "Frequency not tuned: " << freq << " Radio1: " << comm1 << " Radio2: " << comm2 << endl;
92 if ( _propagation_model == 0) {
93 fgSetString("/sim/messages/atc", text.c_str());
95 else if ( _propagation_model == 1 ) {
96 // free space, round earth
98 else if ( _propagation_model == 2 ) {
99 // Use ITM propagation model
100 double signal = ITM_calculate_attenuation(tx_pos, freq, ground_to_air);
102 SG_LOG(SG_GENERAL, SG_BULK, "Signal below receiver minimum sensitivity: " << signal);
103 //cerr << "Signal below receiver minimum sensitivity: " << signal << endl;
106 if ((signal > 0.0) && (signal < 12.0)) {
107 /** for low SNR values implement a way to make the conversation
108 * hard to understand but audible
109 * in the real world, the receiver AGC fails to capture the slope
110 * and the signal, due to being amplitude modulated, decreases volume after demodulation
111 * the workaround below is more akin to what would happen on a FM transmission
112 * therefore the correct way would be to work on the volume
115 string hash_noise = " ";
116 int reps = (int) (fabs(floor(signal - 11.0)) * 2);
117 int t_size = text.size();
118 for (int n = 1; n <= reps; ++n) {
119 int pos = rand() % (t_size -1);
120 text.replace(pos,1, hash_noise);
123 double volume = (fabs(signal - 12.0) / 12);
124 double old_volume = fgGetDouble("/sim/sound/voices/voice/volume");
125 SG_LOG(SG_GENERAL, SG_BULK, "Usable signal at limit: " << signal);
126 //cerr << "Usable signal at limit: " << signal << endl;
127 fgSetDouble("/sim/sound/voices/voice/volume", volume);
128 fgSetString("/sim/messages/atc", text.c_str());
129 fgSetDouble("/sim/sound/voices/voice/volume", old_volume);
132 SG_LOG(SG_GENERAL, SG_BULK, "Signal completely readable: " << signal);
133 //cerr << "Signal completely readable: " << signal << endl;
134 fgSetString("/sim/messages/atc", text.c_str());
143 /*** Implement radio attenuation
144 based on the Longley-Rice propagation model
146 double FGRadio::ITM_calculate_attenuation(SGGeod pos, double freq,
147 int transmission_type) {
151 /** ITM default parameters
152 TODO: take them from tile materials (especially for sea)?
154 double eps_dielect=15.0;
155 double sgm_conductivity = 0.005;
158 if( (freq < 118.0) || (freq > 137.0) )
159 frq_mhz = 125.0; // sane value, middle of bandplan
162 int radio_climate = 5; // continental temperate
163 int pol=1; // assuming vertical polarization although this is more complex in reality
164 double conf = 0.90; // 90% of situations and time, take into account speed
170 double tx_pow = _transmitter_power;
171 double ant_gain = _antenna_gain;
174 if(transmission_type == 1)
175 tx_pow = _transmitter_power + 6.0;
177 if((transmission_type == 1) || (transmission_type == 3))
178 ant_gain = _antenna_gain + 3.0; //pilot plane's antenna gain + ground station antenna gain
180 double link_budget = tx_pow - _receiver_sensitivity + ant_gain;
182 FGScenery * scenery = globals->get_scenery();
184 double own_lat = fgGetDouble("/position/latitude-deg");
185 double own_lon = fgGetDouble("/position/longitude-deg");
186 double own_alt_ft = fgGetDouble("/position/altitude-ft");
187 double own_alt= own_alt_ft * SG_FEET_TO_METER;
190 //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
192 SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
193 SGGeod max_own_pos = SGGeod::fromDegM( own_lon, own_lat, SG_MAX_ELEVATION_M );
194 SGGeoc center = SGGeoc::fromGeod( max_own_pos );
195 SGGeoc own_pos_c = SGGeoc::fromGeod( own_pos );
197 /** position of sender radio antenna (HAAT)
198 sender can be aircraft or ground station
200 double ATC_HAAT = 30.0;
201 double Aircraft_HAAT = 5.0;
202 double sender_alt_ft,sender_alt;
203 double transmitter_height=0.0;
204 double receiver_height=0.0;
205 SGGeod sender_pos = pos;
207 sender_alt_ft = sender_pos.getElevationFt();
208 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
209 SGGeod max_sender_pos = SGGeod::fromGeodM( pos, SG_MAX_ELEVATION_M );
210 SGGeoc sender_pos_c = SGGeoc::fromGeod( sender_pos );
211 //cerr << "ITM:: sender Lat: " << parent->getLatitude() << ", Lon: " << parent->getLongitude() << ", Alt: " << sender_alt << endl;
213 double point_distance= 90.0; // regular SRTM is 90 meters
214 double course = SGGeodesy::courseRad(own_pos_c, sender_pos_c);
215 double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
216 double probe_distance = 0.0;
217 /** If distance larger than this value (300 km), assume reception imposssible */
218 if (distance_m > 300000)
220 /** If above 8000 meters, consider LOS mode and calculate free-space att */
221 if (own_alt > 8000) {
222 dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55;
223 SG_LOG(SG_GENERAL, SG_BULK,
224 "LOS-mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation");
225 //cerr << "LOS-mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation" << endl;
226 signal = link_budget - dbloss;
231 double max_points = distance_m / point_distance;
232 deque<double> _elevations;
234 double elevation_under_pilot = 0.0;
235 if (scenery->get_elevation_m( max_own_pos, elevation_under_pilot, NULL )) {
236 receiver_height = own_alt - elevation_under_pilot + 3; //assume antenna located 3 meters above ground
239 double elevation_under_sender = 0.0;
240 if (scenery->get_elevation_m( max_sender_pos, elevation_under_sender, NULL )) {
241 transmitter_height = sender_alt - elevation_under_sender;
244 transmitter_height = sender_alt;
247 if(transmission_type == 1)
248 transmitter_height += ATC_HAAT;
250 transmitter_height += Aircraft_HAAT;
252 SG_LOG(SG_GENERAL, SG_BULK,
253 "ITM:: RX-height: " << receiver_height << " meters, TX-height: " << transmitter_height << " meters, Distance: " << distance_m << " meters");
254 cerr << "ITM:: RX-height: " << receiver_height << " meters, TX-height: " << transmitter_height << " meters, Distance: " << distance_m << " meters" << endl;
256 unsigned int e_size = (deque<unsigned>::size_type)max_points;
258 while (_elevations.size() <= e_size) {
259 probe_distance += point_distance;
260 SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, probe_distance ));
262 double elevation_m = 0.0;
264 if (scenery->get_elevation_m( probe, elevation_m, NULL )) {
265 if((transmission_type == 3) || (transmission_type == 4)) {
266 _elevations.push_back(elevation_m);
269 _elevations.push_front(elevation_m);
273 if((transmission_type == 3) || (transmission_type == 4)) {
274 _elevations.push_back(elevation_m);
277 _elevations.push_front(0.0);
281 if((transmission_type == 3) || (transmission_type == 4)) {
282 _elevations.push_front(elevation_under_pilot);
283 _elevations.push_back(elevation_under_sender);
286 _elevations.push_back(elevation_under_pilot);
287 _elevations.push_front(elevation_under_sender);
291 double max_alt_between=0.0;
292 for( deque<double>::size_type i = 0; i < _elevations.size(); i++ ) {
293 if (_elevations[i] > max_alt_between) {
294 max_alt_between = _elevations[i];
298 double num_points= (double)_elevations.size();
299 //cerr << "ITM:: Max alt between: " << max_alt_between << ", num points:" << num_points << endl;
300 _elevations.push_front(point_distance);
301 _elevations.push_front(num_points -1);
302 int size = _elevations.size();
303 double itm_elev[size];
304 for(int i=0;i<size;i++) {
305 itm_elev[i]=_elevations[i];
306 //cerr << "ITM:: itm_elev: " << _elevations[i] << endl;
310 /** first Fresnel zone radius
311 frequency in the middle of the bandplan, more accuracy is not necessary
313 double fz_clr= 8.657 * sqrt(distance_m / 0.125);
315 // TODO: If we clear the first Fresnel zone, we are into line of sight territory
317 // else we need to calculate point to point link loss
318 if((transmission_type == 3) || (transmission_type == 4)) {
319 // the sender and receiver roles are switched
320 point_to_point(itm_elev, receiver_height, transmitter_height,
321 eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
322 pol, conf, rel, dbloss, strmode, errnum);
327 point_to_point(itm_elev, transmitter_height, receiver_height,
328 eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
329 pol, conf, rel, dbloss, strmode, errnum);
331 SG_LOG(SG_GENERAL, SG_BULK,
332 "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum);
333 cerr << "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum << endl;
335 //if (errnum == 4) // if parameters are outside sane values for lrprop, the alternative method is used
337 signal = link_budget - dbloss;