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.
30 #include <simgear/scene/material/mat.hxx>
31 #include <Scenery/scenery.hxx>
33 #define WITH_POINT_TO_POINT 1
37 FGRadioTransmission::FGRadioTransmission() {
40 _receiver_sensitivity = -105.0; // typical AM receiver sensitivity seems to be 0.8 microVolt at 12dB SINAD or less
42 /** AM transmitter power in dBm.
43 * Typical output powers for ATC ground equipment, VHF-UHF:
44 * 40 dBm - 10 W (ground, clearance)
45 * 44 dBm - 20 W (tower)
46 * 47 dBm - 50 W (center, sectors)
47 * 50 dBm - 100 W (center, sectors)
48 * 53 dBm - 200 W (sectors, on directional arrays)
50 _transmitter_power = 43.0;
52 _tx_antenna_height = 2.0; // TX antenna height above ground level
54 _rx_antenna_height = 2.0; // RX antenna height above ground level
57 _rx_antenna_gain = 1.0; // maximum antenna gain expressed in dBi
58 _tx_antenna_gain = 1.0;
60 _rx_line_losses = 2.0; // to be configured for each station
61 _tx_line_losses = 2.0;
63 _polarization = 1; // default vertical
65 _propagation_model = 2;
67 _root_node = fgGetNode("sim/radio", true);
68 _terrain_sampling_distance = _root_node->getDoubleValue("sampling-distance", 90.0); // regular SRTM is 90 meters
73 FGRadioTransmission::~FGRadioTransmission()
78 double FGRadioTransmission::getFrequency(int radio) {
82 freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
85 freq = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
88 freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
95 void FGRadioTransmission::receiveChat(SGGeod tx_pos, double freq, string text, int ground_to_air) {
100 double FGRadioTransmission::receiveNav(SGGeod tx_pos, double freq, int transmission_type) {
102 // typical VOR/LOC transmitter power appears to be 100 - 200 Watt i.e 50 - 53 dBm
103 // vor/loc typical sensitivity between -107 and -101 dBm
104 // glideslope sensitivity between -85 and -81 dBm
105 if ( _propagation_model == 1) {
106 return LOS_calculate_attenuation(tx_pos, freq, 1);
108 else if ( _propagation_model == 2) {
109 return ITM_calculate_attenuation(tx_pos, freq, 1);
117 double FGRadioTransmission::receiveBeacon(SGGeod &tx_pos, double heading, double pitch) {
119 // these properties should be set by an instrument
120 _receiver_sensitivity = _root_node->getDoubleValue("station[0]/rx-sensitivity", _receiver_sensitivity);
121 _transmitter_power = watt_to_dbm(_root_node->getDoubleValue("station[0]/tx-power-watt", _transmitter_power));
122 _polarization = _root_node->getIntValue("station[0]/polarization", 1);
123 _tx_antenna_height += _root_node->getDoubleValue("station[0]/tx-antenna-height", 0);
124 _rx_antenna_height += _root_node->getDoubleValue("station[0]/rx-antenna-height", 0);
125 _tx_antenna_gain += _root_node->getDoubleValue("station[0]/tx-antenna-gain", 0);
126 _rx_antenna_gain += _root_node->getDoubleValue("station[0]/rx-antenna-gain", 0);
128 double freq = _root_node->getDoubleValue("station[0]/frequency", 144.8); // by default stay in the ham 2 meter band
130 double comm1 = getFrequency(1);
131 double comm2 = getFrequency(2);
132 if ( !(fabs(freq - comm1) <= 0.0001) && !(fabs(freq - comm2) <= 0.0001) ) {
136 double signal = ITM_calculate_attenuation(tx_pos, freq, 1);
143 void FGRadioTransmission::receiveATC(SGGeod tx_pos, double freq, string text, int ground_to_air) {
145 // adjust some default parameters in case the ATC code does not set them
146 if(ground_to_air == 1) {
147 _transmitter_power += 4.0;
148 _tx_antenna_height += 30.0;
149 _tx_antenna_gain += 2.0;
152 double comm1 = getFrequency(1);
153 double comm2 = getFrequency(2);
154 if ( !(fabs(freq - comm1) <= 0.0001) && !(fabs(freq - comm2) <= 0.0001) ) {
159 if ( _propagation_model == 0) { // skip propagation routines entirely
160 fgSetString("/sim/messages/atc", text.c_str());
162 else if ( _propagation_model == 1 ) { // Use free-space, round earth
164 double signal = LOS_calculate_attenuation(tx_pos, freq, ground_to_air);
169 fgSetString("/sim/messages/atc", text.c_str());
172 else if ( _propagation_model == 2 ) { // Use ITM propagation model
174 double signal = ITM_calculate_attenuation(tx_pos, freq, ground_to_air);
178 if ((signal > 0.0) && (signal < 12.0)) {
179 /** for low SNR values need a way to make the conversation
180 * hard to understand but audible
181 * in the real world, the receiver AGC fails to capture the slope
182 * and the signal, due to being amplitude modulated, decreases volume after demodulation
183 * the workaround below is more akin to what would happen on a FM transmission
184 * therefore the correct way would be to work on the volume
187 string hash_noise = " ";
188 int reps = (int) (fabs(floor(signal - 11.0)) * 2);
189 int t_size = text.size();
190 for (int n = 1; n <= reps; ++n) {
191 int pos = rand() % (t_size -1);
192 text.replace(pos,1, hash_noise);
195 //double volume = (fabs(signal - 12.0) / 12);
196 //double old_volume = fgGetDouble("/sim/sound/voices/voice/volume");
198 //fgSetDouble("/sim/sound/voices/voice/volume", volume);
199 fgSetString("/sim/messages/atc", text.c_str());
200 //fgSetDouble("/sim/sound/voices/voice/volume", old_volume);
203 fgSetString("/sim/messages/atc", text.c_str());
210 double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, int transmission_type) {
214 /** ITM default parameters
215 TODO: take them from tile materials (especially for sea)?
217 double eps_dielect=15.0;
218 double sgm_conductivity = 0.005;
220 double frq_mhz = freq;
222 int radio_climate = 5; // continental temperate
223 int pol= _polarization;
224 double conf = 0.90; // 90% of situations and time, take into account speed
228 int p_mode = 0; // propgation mode selector: 0 LOS, 1 diffraction dominant, 2 troposcatter
232 double clutter_loss = 0.0; // loss due to vegetation and urban
233 double tx_pow = _transmitter_power;
234 double ant_gain = _rx_antenna_gain + _tx_antenna_gain;
238 double link_budget = tx_pow - _receiver_sensitivity - _rx_line_losses - _tx_line_losses + ant_gain;
239 double signal_strength = tx_pow - _rx_line_losses - _tx_line_losses + ant_gain;
240 double tx_erp = dbm_to_watt(tx_pow + _tx_antenna_gain - _tx_line_losses);
243 FGScenery * scenery = globals->get_scenery();
245 double own_lat = fgGetDouble("/position/latitude-deg");
246 double own_lon = fgGetDouble("/position/longitude-deg");
247 double own_alt_ft = fgGetDouble("/position/altitude-ft");
248 double own_heading = fgGetDouble("/orientation/heading-deg");
249 double own_alt= own_alt_ft * SG_FEET_TO_METER;
254 SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
255 SGGeod max_own_pos = SGGeod::fromDegM( own_lon, own_lat, SG_MAX_ELEVATION_M );
256 SGGeoc center = SGGeoc::fromGeod( max_own_pos );
257 SGGeoc own_pos_c = SGGeoc::fromGeod( own_pos );
260 double sender_alt_ft,sender_alt;
261 double transmitter_height=0.0;
262 double receiver_height=0.0;
263 SGGeod sender_pos = pos;
265 sender_alt_ft = sender_pos.getElevationFt();
266 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
267 SGGeod max_sender_pos = SGGeod::fromGeodM( pos, SG_MAX_ELEVATION_M );
268 SGGeoc sender_pos_c = SGGeoc::fromGeod( sender_pos );
271 double point_distance= _terrain_sampling_distance;
272 double course = SGGeodesy::courseRad(own_pos_c, sender_pos_c);
273 double reverse_course = SGGeodesy::courseRad(sender_pos_c, own_pos_c);
274 double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
275 double probe_distance = 0.0;
276 /** If distance larger than this value (300 km), assume reception imposssible to spare CPU cycles */
277 if (distance_m > 300000)
279 /** If above 8000 meters, consider LOS mode and calculate free-space att to spare CPU cycles */
280 if (own_alt > 8000) {
281 dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55;
282 SG_LOG(SG_GENERAL, SG_BULK,
283 "ITM Free-space mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation");
284 //cerr << "ITM Free-space mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation" << endl;
285 signal = link_budget - dbloss;
290 int max_points = (int)floor(distance_m / point_distance);
291 double delta_last = fmod(distance_m, point_distance);
293 deque<double> elevations;
294 deque<string> materials;
297 double elevation_under_pilot = 0.0;
298 if (scenery->get_elevation_m( max_own_pos, elevation_under_pilot, NULL )) {
299 receiver_height = own_alt - elevation_under_pilot;
302 double elevation_under_sender = 0.0;
303 if (scenery->get_elevation_m( max_sender_pos, elevation_under_sender, NULL )) {
304 transmitter_height = sender_alt - elevation_under_sender;
307 transmitter_height = sender_alt;
311 transmitter_height += _tx_antenna_height;
312 receiver_height += _rx_antenna_height;
314 //cerr << "ITM:: RX-height: " << receiver_height << " meters, TX-height: " << transmitter_height << " meters, Distance: " << distance_m << " meters" << endl;
315 _root_node->setDoubleValue("station[0]/rx-height", receiver_height);
316 _root_node->setDoubleValue("station[0]/tx-height", transmitter_height);
317 _root_node->setDoubleValue("station[0]/distance", distance_m / 1000);
319 unsigned int e_size = (deque<unsigned>::size_type)max_points;
321 while (elevations.size() <= e_size) {
322 probe_distance += point_distance;
323 SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, probe_distance ));
324 const SGMaterial *mat = 0;
325 double elevation_m = 0.0;
327 if (scenery->get_elevation_m( probe, elevation_m, &mat )) {
328 if((transmission_type == 3) || (transmission_type == 4)) {
329 elevations.push_back(elevation_m);
331 const std::vector<string> mat_names = mat->get_names();
332 materials.push_back(mat_names[0]);
335 materials.push_back("None");
339 elevations.push_front(elevation_m);
341 const std::vector<string> mat_names = mat->get_names();
342 materials.push_front(mat_names[0]);
345 materials.push_front("None");
350 if((transmission_type == 3) || (transmission_type == 4)) {
351 elevations.push_back(0.0);
352 materials.push_back("None");
355 elevations.push_front(0.0);
356 materials.push_front("None");
360 if((transmission_type == 3) || (transmission_type == 4)) {
361 elevations.push_front(elevation_under_pilot);
362 if (delta_last > (point_distance / 2) ) // only add last point if it's farther than half point_distance
363 elevations.push_back(elevation_under_sender);
366 elevations.push_back(elevation_under_pilot);
367 if (delta_last > (point_distance / 2) )
368 elevations.push_front(elevation_under_sender);
372 double num_points= (double)elevations.size();
375 elevations.push_front(point_distance);
376 elevations.push_front(num_points -1);
378 int size = elevations.size();
380 itm_elev = new double[size];
382 for(int i=0;i<size;i++) {
383 itm_elev[i]=elevations[i];
386 if((transmission_type == 3) || (transmission_type == 4)) {
387 // the sender and receiver roles are switched
388 point_to_point(itm_elev, receiver_height, transmitter_height,
389 eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
390 pol, conf, rel, dbloss, strmode, p_mode, horizons, errnum);
391 if( _root_node->getBoolValue( "use-clutter-attenuation", false ) )
392 calculate_clutter_loss(frq_mhz, itm_elev, materials, receiver_height, transmitter_height, p_mode, horizons, clutter_loss);
395 point_to_point(itm_elev, transmitter_height, receiver_height,
396 eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
397 pol, conf, rel, dbloss, strmode, p_mode, horizons, errnum);
398 if( _root_node->getBoolValue( "use-clutter-attenuation", false ) )
399 calculate_clutter_loss(frq_mhz, itm_elev, materials, transmitter_height, receiver_height, p_mode, horizons, clutter_loss);
402 double pol_loss = 0.0;
403 // TODO: remove this check after we check a bit the axis calculations in this function
404 if (_polarization == 1) {
405 pol_loss = polarization_loss();
407 //SG_LOG(SG_GENERAL, SG_BULK,
408 // "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum);
409 //cerr << "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum << endl;
410 _root_node->setDoubleValue("station[0]/link-budget", link_budget);
411 _root_node->setDoubleValue("station[0]/terrain-attenuation", dbloss);
412 _root_node->setStringValue("station[0]/prop-mode", strmode);
413 _root_node->setDoubleValue("station[0]/clutter-attenuation", clutter_loss);
414 _root_node->setDoubleValue("station[0]/polarization-attenuation", pol_loss);
415 //if (errnum == 4) // if parameters are outside sane values for lrprop, bail out fast
418 // temporary, keep this antenna radiation pattern code here
419 double tx_pattern_gain = 0.0;
420 double rx_pattern_gain = 0.0;
421 double sender_heading = 270.0; // due West
422 double tx_antenna_bearing = sender_heading - reverse_course * SGD_RADIANS_TO_DEGREES;
423 double rx_antenna_bearing = own_heading - course * SGD_RADIANS_TO_DEGREES;
424 double rx_elev_angle = atan((itm_elev[2] + transmitter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m) * SGD_RADIANS_TO_DEGREES;
425 double tx_elev_angle = 0.0 - rx_elev_angle;
426 if (_root_node->getBoolValue("use-tx-antenna-pattern", false)) {
427 FGRadioAntenna* TX_antenna;
428 TX_antenna = new FGRadioAntenna("Plot2");
429 TX_antenna->set_heading(sender_heading);
430 TX_antenna->set_elevation_angle(0);
431 tx_pattern_gain = TX_antenna->calculate_gain(tx_antenna_bearing, tx_elev_angle);
434 if (_root_node->getBoolValue("use-rx-antenna-pattern", false)) {
435 FGRadioAntenna* RX_antenna;
436 RX_antenna = new FGRadioAntenna("Plot2");
437 RX_antenna->set_heading(own_heading);
438 RX_antenna->set_elevation_angle(fgGetDouble("/orientation/pitch-deg"));
439 rx_pattern_gain = RX_antenna->calculate_gain(rx_antenna_bearing, rx_elev_angle);
443 signal = link_budget - dbloss - clutter_loss + pol_loss + rx_pattern_gain + tx_pattern_gain;
444 double signal_strength_dbm = signal_strength - dbloss - clutter_loss + pol_loss + rx_pattern_gain + tx_pattern_gain;
445 double field_strength_uV = dbm_to_microvolt(signal_strength_dbm);
446 _root_node->setDoubleValue("station[0]/signal-dbm", signal_strength_dbm);
447 _root_node->setDoubleValue("station[0]/field-strength-uV", field_strength_uV);
448 _root_node->setDoubleValue("station[0]/signal", signal);
449 _root_node->setDoubleValue("station[0]/tx-erp", tx_erp);
451 //_root_node->setDoubleValue("station[0]/tx-pattern-gain", tx_pattern_gain);
452 //_root_node->setDoubleValue("station[0]/rx-pattern-gain", rx_pattern_gain);
461 void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[], deque<string> &materials,
462 double transmitter_height, double receiver_height, int p_mode,
463 double horizons[], double &clutter_loss) {
465 double distance_m = itm_elev[0] * itm_elev[1]; // only consider elevation points
467 if (p_mode == 0) { // LOS: take each point and see how clutter height affects first Fresnel zone
470 for (int k=3;k < (int)(itm_elev[0]) + 2;k++) {
472 double clutter_height = 0.0; // mean clutter height for a certain terrain type
473 double clutter_density = 0.0; // percent of reflected wave
474 get_material_properties(materials[mat], clutter_height, clutter_density);
476 double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
477 // First Fresnel radius
478 double frs_rad = 548 * sqrt( (j * itm_elev[1] * (itm_elev[0] - j) * itm_elev[1] / 1000000) / ( distance_m * freq / 1000) );
480 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
482 double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
483 double d1 = j * itm_elev[1];
484 if ((itm_elev[2] + transmitter_height) > ( itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) {
485 d1 = (itm_elev[0] - j) * itm_elev[1];
487 double ray_height = (grad * d1) + min_elev;
489 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
490 double intrusion = fabs(clearance);
492 if (clearance >= 0) {
495 else if (clearance < 0 && (intrusion < clutter_height)) {
497 clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
499 else if (clearance < 0 && (intrusion > clutter_height)) {
500 clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
510 else if (p_mode == 1) { // diffraction
512 if (horizons[1] == 0.0) { // single horizon: same as above, except pass twice using the highest point
513 int num_points_1st = (int)floor( horizons[0] * itm_elev[0]/ distance_m );
514 int num_points_2nd = (int)ceil( (distance_m - horizons[0]) * itm_elev[0] / distance_m );
515 //cerr << "Diffraction 1 horizon:: points1: " << num_points_1st << " points2: " << num_points_2nd << endl;
517 /** perform the first pass */
520 for (int k=3;k < num_points_1st + 2;k++) {
521 if (num_points_1st < 1)
523 double clutter_height = 0.0; // mean clutter height for a certain terrain type
524 double clutter_density = 0.0; // percent of reflected wave
525 get_material_properties(materials[mat], clutter_height, clutter_density);
527 double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[num_points_1st + 2] + clutter_height) / distance_m;
528 // First Fresnel radius
529 double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_1st - j) * itm_elev[1] / 1000000) / ( num_points_1st * itm_elev[1] * freq / 1000) );
531 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
533 double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[num_points_1st + 2] + clutter_height);
534 double d1 = j * itm_elev[1];
535 if ( (itm_elev[2] + transmitter_height) > (itm_elev[num_points_1st + 2] + clutter_height) ) {
536 d1 = (num_points_1st - j) * itm_elev[1];
538 double ray_height = (grad * d1) + min_elev;
540 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
541 double intrusion = fabs(clearance);
543 if (clearance >= 0) {
546 else if (clearance < 0 && (intrusion < clutter_height)) {
548 clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
550 else if (clearance < 0 && (intrusion > clutter_height)) {
551 clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
561 /** and the second pass */
563 j =1; // first point is diffraction edge, 2nd the RX elevation
564 for (int k=last+2;k < (int)(itm_elev[0]) + 2;k++) {
565 if (num_points_2nd < 1)
567 double clutter_height = 0.0; // mean clutter height for a certain terrain type
568 double clutter_density = 0.0; // percent of reflected wave
569 get_material_properties(materials[mat], clutter_height, clutter_density);
571 double grad = fabs(itm_elev[last+1] + clutter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
572 // First Fresnel radius
573 double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_2nd - j) * itm_elev[1] / 1000000) / ( num_points_2nd * itm_elev[1] * freq / 1000) );
575 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
577 double min_elev = SGMiscd::min(itm_elev[last+1] + clutter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
578 double d1 = j * itm_elev[1];
579 if ( (itm_elev[last+1] + clutter_height) > (itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) {
580 d1 = (num_points_2nd - j) * itm_elev[1];
582 double ray_height = (grad * d1) + min_elev;
584 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
585 double intrusion = fabs(clearance);
587 if (clearance >= 0) {
590 else if (clearance < 0 && (intrusion < clutter_height)) {
592 clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
594 else if (clearance < 0 && (intrusion > clutter_height)) {
595 clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
605 else { // double horizon: same as single horizon, except there are 3 segments
607 int num_points_1st = (int)floor( horizons[0] * itm_elev[0] / distance_m );
608 int num_points_2nd = (int)floor(horizons[1] * itm_elev[0] / distance_m );
609 int num_points_3rd = (int)itm_elev[0] - num_points_1st - num_points_2nd;
610 //cerr << "Double horizon:: horizon1: " << horizons[0] << " horizon2: " << horizons[1] << " distance: " << distance_m << endl;
611 //cerr << "Double horizon:: points1: " << num_points_1st << " points2: " << num_points_2nd << " points3: " << num_points_3rd << endl;
613 /** perform the first pass */
615 int j=1; // first point is TX elevation, 2nd is obstruction elevation
616 for (int k=3;k < num_points_1st +2;k++) {
617 if (num_points_1st < 1)
619 double clutter_height = 0.0; // mean clutter height for a certain terrain type
620 double clutter_density = 0.0; // percent of reflected wave
621 get_material_properties(materials[mat], clutter_height, clutter_density);
623 double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[num_points_1st + 2] + clutter_height) / distance_m;
624 // First Fresnel radius
625 double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_1st - j) * itm_elev[1] / 1000000) / ( num_points_1st * itm_elev[1] * freq / 1000) );
627 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
629 double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[num_points_1st + 2] + clutter_height);
630 double d1 = j * itm_elev[1];
631 if ( (itm_elev[2] + transmitter_height) > (itm_elev[num_points_1st + 2] + clutter_height) ) {
632 d1 = (num_points_1st - j) * itm_elev[1];
634 double ray_height = (grad * d1) + min_elev;
636 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
637 double intrusion = fabs(clearance);
639 if (clearance >= 0) {
642 else if (clearance < 0 && (intrusion < clutter_height)) {
644 clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
646 else if (clearance < 0 && (intrusion > clutter_height)) {
647 clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
656 /** and the second pass */
658 j =1; // first point is 1st obstruction elevation, 2nd is 2nd obstruction elevation
659 for (int k=last+2;k < num_points_1st + num_points_2nd +2;k++) {
660 if (num_points_2nd < 1)
662 double clutter_height = 0.0; // mean clutter height for a certain terrain type
663 double clutter_density = 0.0; // percent of reflected wave
664 get_material_properties(materials[mat], clutter_height, clutter_density);
666 double grad = fabs(itm_elev[last+1] + clutter_height - itm_elev[num_points_1st + num_points_2nd + 2] + clutter_height) / distance_m;
667 // First Fresnel radius
668 double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_2nd - j) * itm_elev[1] / 1000000) / ( num_points_2nd * itm_elev[1] * freq / 1000) );
670 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
672 double min_elev = SGMiscd::min(itm_elev[last+1] + clutter_height, itm_elev[num_points_1st + num_points_2nd +2] + clutter_height);
673 double d1 = j * itm_elev[1];
674 if ( (itm_elev[last+1] + clutter_height) > (itm_elev[num_points_1st + num_points_2nd + 2] + clutter_height) ) {
675 d1 = (num_points_2nd - j) * itm_elev[1];
677 double ray_height = (grad * d1) + min_elev;
679 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
680 double intrusion = fabs(clearance);
682 if (clearance >= 0) {
685 else if (clearance < 0 && (intrusion < clutter_height)) {
687 clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
689 else if (clearance < 0 && (intrusion > clutter_height)) {
690 clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
700 /** third and final pass */
702 j =1; // first point is 2nd obstruction elevation, 3rd is RX elevation
703 for (int k=last2+2;k < (int)itm_elev[0] + 2;k++) {
704 if (num_points_3rd < 1)
706 double clutter_height = 0.0; // mean clutter height for a certain terrain type
707 double clutter_density = 0.0; // percent of reflected wave
708 get_material_properties(materials[mat], clutter_height, clutter_density);
710 double grad = fabs(itm_elev[last2+1] + clutter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
711 // First Fresnel radius
712 double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_3rd - j) * itm_elev[1] / 1000000) / ( num_points_3rd * itm_elev[1] * freq / 1000) );
715 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
717 double min_elev = SGMiscd::min(itm_elev[last2+1] + clutter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
718 double d1 = j * itm_elev[1];
719 if ( (itm_elev[last2+1] + clutter_height) > (itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) {
720 d1 = (num_points_3rd - j) * itm_elev[1];
722 double ray_height = (grad * d1) + min_elev;
724 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
725 double intrusion = fabs(clearance);
727 if (clearance >= 0) {
730 else if (clearance < 0 && (intrusion < clutter_height)) {
732 clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
734 else if (clearance < 0 && (intrusion > clutter_height)) {
735 clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
747 else if (p_mode == 2) { // troposcatter: ignore ground clutter for now... maybe do something with weather
754 void FGRadioTransmission::get_material_properties(string mat_name, double &height, double &density) {
756 if(mat_name == "Landmass") {
761 else if(mat_name == "SomeSort") {
766 else if(mat_name == "Island") {
770 else if(mat_name == "Default") {
774 else if(mat_name == "EvergreenBroadCover") {
778 else if(mat_name == "EvergreenForest") {
782 else if(mat_name == "DeciduousBroadCover") {
786 else if(mat_name == "DeciduousForest") {
790 else if(mat_name == "MixedForestCover") {
794 else if(mat_name == "MixedForest") {
798 else if(mat_name == "RainForest") {
802 else if(mat_name == "EvergreenNeedleCover") {
806 else if(mat_name == "WoodedTundraCover") {
810 else if(mat_name == "DeciduousNeedleCover") {
814 else if(mat_name == "ScrubCover") {
818 else if(mat_name == "BuiltUpCover") {
822 else if(mat_name == "Urban") {
826 else if(mat_name == "Construction") {
830 else if(mat_name == "Industrial") {
834 else if(mat_name == "Port") {
838 else if(mat_name == "Town") {
842 else if(mat_name == "SubUrban") {
846 else if(mat_name == "CropWoodCover") {
850 else if(mat_name == "CropWood") {
854 else if(mat_name == "AgroForest") {
866 double FGRadioTransmission::LOS_calculate_attenuation(SGGeod pos, double freq, int transmission_type) {
868 double frq_mhz = freq;
870 double tx_pow = _transmitter_power;
871 double ant_gain = _rx_antenna_gain + _tx_antenna_gain;
874 double sender_alt_ft,sender_alt;
875 double transmitter_height=0.0;
876 double receiver_height=0.0;
877 double own_lat = fgGetDouble("/position/latitude-deg");
878 double own_lon = fgGetDouble("/position/longitude-deg");
879 double own_alt_ft = fgGetDouble("/position/altitude-ft");
880 double own_alt= own_alt_ft * SG_FEET_TO_METER;
883 double link_budget = tx_pow - _receiver_sensitivity - _rx_line_losses - _tx_line_losses + ant_gain;
885 //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
887 SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
889 SGGeod sender_pos = pos;
891 sender_alt_ft = sender_pos.getElevationFt();
892 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
894 receiver_height = own_alt;
895 transmitter_height = sender_alt;
897 double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
900 transmitter_height += _tx_antenna_height;
901 receiver_height += _rx_antenna_height;
904 /** radio horizon calculation with wave bending k=4/3 */
905 double receiver_horizon = 4.12 * sqrt(receiver_height);
906 double transmitter_horizon = 4.12 * sqrt(transmitter_height);
907 double total_horizon = receiver_horizon + transmitter_horizon;
909 if (distance_m > total_horizon) {
912 double pol_loss = 0.0;
913 if (_polarization == 1) {
914 pol_loss = polarization_loss();
916 // free-space loss (distance calculation should be changed)
917 dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55;
918 signal = link_budget - dbloss + pol_loss;
920 //cerr << "LOS:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm " << endl;
925 /*** calculate loss due to polarization mismatch
926 * this function is only reliable for vertical polarization
927 * due to the V-shape of horizontally polarized antennas
929 double FGRadioTransmission::polarization_loss() {
932 double roll = fgGetDouble("/orientation/roll-deg");
933 if (fabs(roll) > 85.0)
935 double pitch = fgGetDouble("/orientation/pitch-deg");
936 if (fabs(pitch) > 85.0)
938 double theta = fabs( atan( sqrt(
939 pow(tan(roll * SGD_DEGREES_TO_RADIANS), 2) +
940 pow(tan(pitch * SGD_DEGREES_TO_RADIANS), 2) )) * SGD_RADIANS_TO_DEGREES);
942 if (_polarization == 0)
943 theta_deg = 90.0 - theta;
946 if (theta_deg > 85.0) // we don't want to converge into infinity
949 double loss = 10 * log10( pow(cos(theta_deg * SGD_DEGREES_TO_RADIANS), 2) );
950 //cerr << "Polarization loss: " << loss << " dBm " << endl;
955 double FGRadioTransmission::watt_to_dbm(double power_watt) {
956 return 10 * log10(1000 * power_watt); // returns dbm
959 double FGRadioTransmission::dbm_to_watt(double dbm) {
960 return exp( (dbm-30) * log(10.0) / 10.0); // returns Watts
963 double FGRadioTransmission::dbm_to_microvolt(double dbm) {
964 return sqrt(dbm_to_watt(dbm) * 50) * 1000000; // returns microvolts