]> git.mxchange.org Git - flightgear.git/blob - src/Radio/radio.cxx
Merge branch 'attenuation' into navaids-radio
[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
27 #include <stdlib.h>
28 #include <deque>
29 #include "radio.hxx"
30 #include <simgear/scene/material/mat.hxx>
31 #include <Scenery/scenery.hxx>
32
33 #define WITH_POINT_TO_POINT 1
34 #include "itm.cpp"
35
36
37 FGRadioTransmission::FGRadioTransmission() {
38         
39         
40         _receiver_sensitivity = -110.0; // typical AM receiver sensitivity seems to be 0.8 microVolt at 12dB SINAD
41         
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)
49         **/
50         _transmitter_power = 43.0;
51         
52         _tx_antenna_height = 2.0; // TX antenna height above ground level
53         
54         _rx_antenna_height = 2.0; // RX antenna height above ground level
55         
56         
57         _rx_antenna_gain = 1.0; // gain expressed in dBi
58         _tx_antenna_gain = 1.0;
59         
60         _rx_line_losses = 2.0;  // to be configured for each station
61         _tx_line_losses = 2.0;
62         
63         _polarization = 1; // default vertical
64         
65         _propagation_model = 2; 
66         _terrain_sampling_distance = fgGetDouble("/sim/radio/sampling-distance", 90.0); // regular SRTM is 90 meters
67 }
68
69 FGRadioTransmission::~FGRadioTransmission() 
70 {
71 }
72
73
74 double FGRadioTransmission::getFrequency(int radio) {
75         double freq = 118.0;
76         switch (radio) {
77                 case 1:
78                         freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
79                         break;
80                 case 2:
81                         freq = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
82                         break;
83                 default:
84                         freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
85                         
86         }
87         return freq;
88 }
89
90 /*** TODO: receive multiplayer chat message and voice
91 ***/
92 void FGRadioTransmission::receiveChat(SGGeod tx_pos, double freq, string text, int ground_to_air) {
93
94 }
95
96 /*** TODO: receive navaid 
97 ***/
98 double FGRadioTransmission::receiveNav(SGGeod tx_pos, double freq, int transmission_type) {
99         
100         // typical VOR/LOC transmitter power appears to be 200 Watt ~ 53 dBm
101         // vor/loc typical sensitivity between -107 and -101 dBm
102         // glideslope sensitivity between -85 and -81 dBm
103         if ( _propagation_model == 1) {
104                 return LOS_calculate_attenuation(tx_pos, freq, 1);
105         }
106         else if ( _propagation_model == 2) {
107                 return ITM_calculate_attenuation(tx_pos, freq, 1);
108         }
109         
110         return -1;
111
112 }
113
114 /*** Receive ATC radio communication as text
115 ***/
116 void FGRadioTransmission::receiveATC(SGGeod tx_pos, double freq, string text, int ground_to_air) {
117
118         
119         if(ground_to_air == 1) {
120                 _transmitter_power += 6.0;
121                 _tx_antenna_height += 30.0;
122                 _tx_antenna_gain += 3.0; 
123         }
124         
125         
126         double comm1 = getFrequency(1);
127         double comm2 = getFrequency(2);
128         if ( !(fabs(freq - comm1) <= 0.0001) &&  !(fabs(freq - comm2) <= 0.0001) ) {
129                 return;
130         }
131         else {
132         
133                 if ( _propagation_model == 0) {
134                         fgSetString("/sim/messages/atc", text.c_str());
135                 }
136                 else if ( _propagation_model == 1 ) {
137                         // TODO: free space, round earth
138                         double signal = LOS_calculate_attenuation(tx_pos, freq, ground_to_air);
139                         if (signal <= 0.0) {
140                                 return;
141                         }
142                         else {
143                                 
144                                 fgSetString("/sim/messages/atc", text.c_str());
145                                 /** write signal strength above threshold to the property tree
146                                 *       to implement a simple S-meter just divide by 3 dB per grade (VHF norm)
147                                 **/
148                                 fgSetDouble("/sim/radio/comm1-signal", signal);
149                         }
150                 }
151                 else if ( _propagation_model == 2 ) {
152                         // Use ITM propagation model
153                         double signal = ITM_calculate_attenuation(tx_pos, freq, ground_to_air);
154                         if (signal <= 0.0) {
155                                 return;
156                         }
157                         if ((signal > 0.0) && (signal < 12.0)) {
158                                 /** for low SNR values implement a way to make the conversation
159                                 *       hard to understand but audible
160                                 *       in the real world, the receiver AGC fails to capture the slope
161                                 *       and the signal, due to being amplitude modulated, decreases volume after demodulation
162                                 *       the workaround below is more akin to what would happen on a FM transmission
163                                 *       therefore the correct way would be to work on the volume
164                                 **/
165                                 /*
166                                 string hash_noise = " ";
167                                 int reps = (int) (fabs(floor(signal - 11.0)) * 2);
168                                 int t_size = text.size();
169                                 for (int n = 1; n <= reps; ++n) {
170                                         int pos = rand() % (t_size -1);
171                                         text.replace(pos,1, hash_noise);
172                                 }
173                                 */
174                                 double volume = (fabs(signal - 12.0) / 12);
175                                 double old_volume = fgGetDouble("/sim/sound/voices/voice/volume");
176                                 SG_LOG(SG_GENERAL, SG_BULK, "Usable signal at limit: " << signal);
177                                 //cerr << "Usable signal at limit: " << signal << endl;
178                                 fgSetDouble("/sim/sound/voices/voice/volume", volume);
179                                 fgSetString("/sim/messages/atc", text.c_str());
180                                 fgSetDouble("/sim/radio/comm1-signal", signal);
181                                 fgSetDouble("/sim/sound/voices/voice/volume", old_volume);
182                         }
183                         else {
184                                 fgSetString("/sim/messages/atc", text.c_str());
185                                 /** write signal strength above threshold to the property tree
186                                 *       to implement a simple S-meter just divide by 3 dB per grade (VHF norm)
187                                 **/
188                                 fgSetDouble("/sim/radio/comm1-signal", signal);
189                         }
190                         
191                 }
192                 
193         }
194         
195 }
196
197 /***  Implement radio attenuation               
198           based on the Longley-Rice propagation model
199 ***/
200 double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, int transmission_type) {
201
202         
203         
204         /** ITM default parameters 
205                 TODO: take them from tile materials (especially for sea)?
206         **/
207         double eps_dielect=15.0;
208         double sgm_conductivity = 0.005;
209         double eno = 301.0;
210         double frq_mhz;
211         if( (freq < 118.0) || (freq > 137.0) )
212                 frq_mhz = 125.0;        // sane value, middle of bandplan
213         else
214                 frq_mhz = freq;
215         int radio_climate = 5;          // continental temperate
216         int pol= _polarization; 
217         double conf = 0.90;     // 90% of situations and time, take into account speed
218         double rel = 0.90;      
219         double dbloss;
220         char strmode[150];
221         int p_mode = 0; // propgation mode selector: 0 LOS, 1 diffraction dominant, 2 troposcatter
222         double horizons[2];
223         int errnum;
224         
225         double clutter_loss = 0.0;      // loss due to vegetation and urban
226         double tx_pow = _transmitter_power;
227         double ant_gain = _rx_antenna_gain + _tx_antenna_gain;
228         double signal = 0.0;
229         
230         
231         double link_budget = tx_pow - _receiver_sensitivity - _rx_line_losses - _tx_line_losses + ant_gain;     
232
233         FGScenery * scenery = globals->get_scenery();
234         
235         double own_lat = fgGetDouble("/position/latitude-deg");
236         double own_lon = fgGetDouble("/position/longitude-deg");
237         double own_alt_ft = fgGetDouble("/position/altitude-ft");
238         double own_alt= own_alt_ft * SG_FEET_TO_METER;
239         
240         
241         //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
242         
243         SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
244         SGGeod max_own_pos = SGGeod::fromDegM( own_lon, own_lat, SG_MAX_ELEVATION_M );
245         SGGeoc center = SGGeoc::fromGeod( max_own_pos );
246         SGGeoc own_pos_c = SGGeoc::fromGeod( own_pos );
247         
248         
249         double sender_alt_ft,sender_alt;
250         double transmitter_height=0.0;
251         double receiver_height=0.0;
252         SGGeod sender_pos = pos;
253         
254         sender_alt_ft = sender_pos.getElevationFt();
255         sender_alt = sender_alt_ft * SG_FEET_TO_METER;
256         SGGeod max_sender_pos = SGGeod::fromGeodM( pos, SG_MAX_ELEVATION_M );
257         SGGeoc sender_pos_c = SGGeoc::fromGeod( sender_pos );
258         //cerr << "ITM:: sender Lat: " << parent->getLatitude() << ", Lon: " << parent->getLongitude() << ", Alt: " << sender_alt << endl;
259         
260         double point_distance= _terrain_sampling_distance; 
261         double course = SGGeodesy::courseRad(own_pos_c, sender_pos_c);
262         double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
263         double probe_distance = 0.0;
264         /** If distance larger than this value (300 km), assume reception imposssible */
265         if (distance_m > 300000)
266                 return -1.0;
267         /** If above 8000 meters, consider LOS mode and calculate free-space att */
268         if (own_alt > 8000) {
269                 dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55;
270                 SG_LOG(SG_GENERAL, SG_BULK,
271                         "ITM Free-space mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation");
272                 //cerr << "ITM Free-space mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation" << endl;
273                 signal = link_budget - dbloss;
274                 return signal;
275         }
276         
277                 
278         int max_points = (int)floor(distance_m / point_distance);
279         double delta_last = fmod(distance_m, point_distance);
280         
281         deque<double> _elevations;
282         deque<string> materials;
283         
284
285         double elevation_under_pilot = 0.0;
286         if (scenery->get_elevation_m( max_own_pos, elevation_under_pilot, NULL )) {
287                 receiver_height = own_alt - elevation_under_pilot; 
288         }
289
290         double elevation_under_sender = 0.0;
291         if (scenery->get_elevation_m( max_sender_pos, elevation_under_sender, NULL )) {
292                 transmitter_height = sender_alt - elevation_under_sender;
293         }
294         else {
295                 transmitter_height = sender_alt;
296         }
297         
298         
299         transmitter_height += _tx_antenna_height;
300         receiver_height += _rx_antenna_height;
301         
302         
303         SG_LOG(SG_GENERAL, SG_BULK,
304                         "ITM:: RX-height: " << receiver_height << " meters, TX-height: " << transmitter_height << " meters, Distance: " << distance_m << " meters");
305         cerr << "ITM:: RX-height: " << receiver_height << " meters, TX-height: " << transmitter_height << " meters, Distance: " << distance_m << " meters" << endl;
306         
307         unsigned int e_size = (deque<unsigned>::size_type)max_points;
308         
309         while (_elevations.size() <= e_size) {
310                 probe_distance += point_distance;
311                 SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, probe_distance ));
312                 const SGMaterial *mat = 0;
313                 double elevation_m = 0.0;
314         
315                 if (scenery->get_elevation_m( probe, elevation_m, &mat )) {
316                         if((transmission_type == 3) || (transmission_type == 4)) {
317                                 _elevations.push_back(elevation_m);
318                                 if(mat) {
319                                         const std::vector<string> mat_names = mat->get_names();
320                                         materials.push_back(mat_names[0]);
321                                 }
322                                 else {
323                                         materials.push_back("None");
324                                 }
325                         }
326                         else {
327                                  _elevations.push_front(elevation_m);
328                                  if(mat) {
329                                          const std::vector<string> mat_names = mat->get_names();
330                                          materials.push_front(mat_names[0]);
331                                 }
332                                 else {
333                                         materials.push_front("None");
334                                 }
335                         }
336                 }
337                 else {
338                         if((transmission_type == 3) || (transmission_type == 4)) {
339                                 _elevations.push_back(0.0);
340                                 materials.push_back("None");
341                         }
342                         else {
343                                 _elevations.push_front(0.0);
344                                 materials.push_front("None");
345                         }
346                 }
347         }
348         if((transmission_type == 3) || (transmission_type == 4)) {
349                 _elevations.push_front(elevation_under_pilot);
350                 if (delta_last > (point_distance / 2) )                 // only add last point if it's farther than half point_distance
351                         _elevations.push_back(elevation_under_sender);
352         }
353         else {
354                 _elevations.push_back(elevation_under_pilot);
355                 if (delta_last > (point_distance / 2) )
356                         _elevations.push_front(elevation_under_sender);
357         }
358         
359         
360         double max_alt_between=0.0;
361         for( deque<double>::size_type i = 0; i < _elevations.size(); i++ ) {
362                 if (_elevations[i] > max_alt_between) {
363                         max_alt_between = _elevations[i];
364                 }
365         }
366         
367         double num_points= (double)_elevations.size();
368
369         _elevations.push_front(point_distance);
370         _elevations.push_front(num_points -1);
371         int size = _elevations.size();
372         double itm_elev[size];
373         for(int i=0;i<size;i++) {
374                 itm_elev[i]=_elevations[i];
375                 //cerr << "ITM:: itm_elev: " << _elevations[i] << endl;
376         }
377
378         if((transmission_type == 3) || (transmission_type == 4)) {
379                 // the sender and receiver roles are switched
380                 point_to_point(itm_elev, receiver_height, transmitter_height,
381                         eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
382                         pol, conf, rel, dbloss, strmode, p_mode, horizons, errnum);
383                 if( fgGetBool( "/sim/radio/use-clutter-attenuation", false ) )
384                         clutterLoss(frq_mhz, distance_m, itm_elev, materials, receiver_height, transmitter_height, p_mode, horizons, clutter_loss);
385         }
386         else {
387                 point_to_point(itm_elev, transmitter_height, receiver_height,
388                         eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
389                         pol, conf, rel, dbloss, strmode, p_mode, horizons, errnum);
390                 if( fgGetBool( "/sim/radio/use-clutter-attenuation", false ) )
391                         clutterLoss(frq_mhz, distance_m, itm_elev, materials, transmitter_height, receiver_height, p_mode, horizons, clutter_loss);
392         }
393         SG_LOG(SG_GENERAL, SG_BULK,
394                         "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum);
395         cerr << "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum << endl;
396         
397         cerr << "Clutter loss: " << clutter_loss << endl;
398         //if (errnum == 4)      // if parameters are outside sane values for lrprop, the alternative method is used
399         //      return -1;
400         signal = link_budget - dbloss - clutter_loss;
401         return signal;
402
403 }
404
405 /*** Calculate losses due to vegetation and urban clutter (WIP)
406 *        We are only worried about clutter loss, terrain influence 
407 *        on the first Fresnel zone is calculated in the ITM functions
408 ***/
409 void FGRadioTransmission::clutterLoss(double freq, double distance_m, double itm_elev[], deque<string> materials,
410         double transmitter_height, double receiver_height, int p_mode,
411         double horizons[], double &clutter_loss) {
412         
413         distance_m = itm_elev[0] * itm_elev[1]; // only consider elevation points
414         
415         if (p_mode == 0) {      // LOS: take each point and see how clutter height affects first Fresnel zone
416                 int mat = 0;
417                 int j=1; 
418                 for (int k=3;k < (int)(itm_elev[0]) + 2;k++) {
419                         
420                         double clutter_height = 0.0;    // mean clutter height for a certain terrain type
421                         double clutter_density = 0.0;   // percent of reflected wave
422                         get_material_properties(materials[mat], clutter_height, clutter_density);
423                         
424                         double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
425                         // First Fresnel radius
426                         double frs_rad = 548 * sqrt( (j * itm_elev[1] * (itm_elev[0] - j) * itm_elev[1] / 1000000) / (  distance_m * freq / 1000) );
427                         
428                         //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
429                         
430                         double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
431                         double d1 = j * itm_elev[1];
432                         if ((itm_elev[2] + transmitter_height) > ( itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) {
433                                 d1 = (itm_elev[0] - j) * itm_elev[1];
434                         }
435                         double ray_height = (grad * d1) + min_elev;
436                         
437                         double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
438                         double intrusion = fabs(clearance);
439                         
440                         if (clearance >= 0) {
441                                 // no losses
442                         }
443                         else if (clearance < 0 && (intrusion < clutter_height)) {
444                                 
445                                 clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
446                         }
447                         else if (clearance < 0 && (intrusion > clutter_height)) {
448                                 clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
449                         }
450                         else {
451                                 // no losses
452                         }
453                         j++;
454                         mat++;
455                 }
456                 
457         }
458         else if (p_mode == 1) {         // diffraction
459                 
460                 if (horizons[1] == 0.0) {       //      single horizon: same as above, except pass twice using the highest point
461                         int num_points_1st = (int)floor( horizons[0] * itm_elev[0]/ distance_m ); 
462                         int num_points_2nd = (int)ceil( (distance_m - horizons[0]) * itm_elev[0] / distance_m ); 
463                         //cerr << "Diffraction 1 horizon:: points1: " << num_points_1st << " points2: " << num_points_2nd << endl;
464                         int last = 1;
465                         /** perform the first pass */
466                         int mat = 0;
467                         int j=1; 
468                         for (int k=3;k < num_points_1st + 2;k++) {
469                                 if (num_points_1st < 1)
470                                         break;
471                                 double clutter_height = 0.0;    // mean clutter height for a certain terrain type
472                                 double clutter_density = 0.0;   // percent of reflected wave
473                                 get_material_properties(materials[mat], clutter_height, clutter_density);
474                                 
475                                 double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[num_points_1st + 2] + clutter_height) / distance_m;
476                                 // First Fresnel radius
477                                 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) );
478                                 
479                                 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
480                                 
481                                 double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[num_points_1st + 2] + clutter_height);
482                                 double d1 = j * itm_elev[1];
483                                 if ( (itm_elev[2] + transmitter_height) > (itm_elev[num_points_1st + 2] + clutter_height) ) {
484                                         d1 = (num_points_1st - j) * itm_elev[1];
485                                 }
486                                 double ray_height = (grad * d1) + min_elev;
487                                 
488                                 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
489                                 double intrusion = fabs(clearance);
490                                 
491                                 if (clearance >= 0) {
492                                         // no losses
493                                 }
494                                 else if (clearance < 0 && (intrusion < clutter_height)) {
495                                         
496                                         clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
497                                 }
498                                 else if (clearance < 0 && (intrusion > clutter_height)) {
499                                         clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
500                                 }
501                                 else {
502                                         // no losses
503                                 }
504                                 j++;
505                                 mat++;
506                                 last = k;
507                         }
508                         
509                         /** and the second pass */
510                         mat +=1;
511                         j =1; // first point is diffraction edge, 2nd the RX elevation
512                         for (int k=last+2;k < (int)(itm_elev[0]) + 2;k++) {
513                                 if (num_points_2nd < 1)
514                                         break;
515                                 double clutter_height = 0.0;    // mean clutter height for a certain terrain type
516                                 double clutter_density = 0.0;   // percent of reflected wave
517                                 get_material_properties(materials[mat], clutter_height, clutter_density);
518                                 
519                                 double grad = fabs(itm_elev[last+1] + clutter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
520                                 // First Fresnel radius
521                                 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) );
522                                 
523                                 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
524                                 
525                                 double min_elev = SGMiscd::min(itm_elev[last+1] + clutter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
526                                 double d1 = j * itm_elev[1];
527                                 if ( (itm_elev[last+1] + clutter_height) > (itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) { 
528                                         d1 = (num_points_2nd - j) * itm_elev[1];
529                                 }
530                                 double ray_height = (grad * d1) + min_elev;
531                                 
532                                 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
533                                 double intrusion = fabs(clearance);
534                                 
535                                 if (clearance >= 0) {
536                                         // no losses
537                                 }
538                                 else if (clearance < 0 && (intrusion < clutter_height)) {
539                                         
540                                         clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
541                                 }
542                                 else if (clearance < 0 && (intrusion > clutter_height)) {
543                                         clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
544                                 }
545                                 else {
546                                         // no losses
547                                 }
548                                 j++;
549                                 mat++;
550                         }
551                         
552                 }
553                 else {  // double horizon: same as single horizon, except there are 3 segments
554                         
555                         int num_points_1st = (int)floor( horizons[0] * itm_elev[0] / distance_m ); 
556                         int num_points_2nd = (int)floor(horizons[1] * itm_elev[0] / distance_m ); 
557                         int num_points_3rd = (int)itm_elev[0] - num_points_1st - num_points_2nd; 
558                         //cerr << "Double horizon:: horizon1: " << horizons[0] << " horizon2: " << horizons[1] << " distance: " << distance_m << endl;
559                         //cerr << "Double horizon:: points1: " << num_points_1st << " points2: " << num_points_2nd << " points3: " << num_points_3rd << endl;
560                         int last = 1;
561                         /** perform the first pass */
562                         int mat = 0;
563                         int j=1; // first point is TX elevation, 2nd is obstruction elevation
564                         for (int k=3;k < num_points_1st +2;k++) {
565                                 if (num_points_1st < 1)
566                                         break;
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);
570                                 
571                                 double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[num_points_1st + 2] + clutter_height) / distance_m;
572                                 // First Fresnel radius
573                                 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) );
574                                 
575                                 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
576                                 
577                                 double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[num_points_1st + 2] + clutter_height);
578                                 double d1 = j * itm_elev[1];
579                                 if ( (itm_elev[2] + transmitter_height) > (itm_elev[num_points_1st + 2] + clutter_height) ) {
580                                         d1 = (num_points_1st - j) * itm_elev[1];
581                                 }
582                                 double ray_height = (grad * d1) + min_elev;
583                                 
584                                 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
585                                 double intrusion = fabs(clearance);
586                                 
587                                 if (clearance >= 0) {
588                                         // no losses
589                                 }
590                                 else if (clearance < 0 && (intrusion < clutter_height)) {
591                                         
592                                         clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
593                                 }
594                                 else if (clearance < 0 && (intrusion > clutter_height)) {
595                                         clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
596                                 }
597                                 else {
598                                         // no losses
599                                 }
600                                 j++;
601                                 last = k;
602                         }
603                         mat +=1;
604                         /** and the second pass */
605                         int last2=1;
606                         j =1; // first point is 1st obstruction elevation, 2nd is 2nd obstruction elevation
607                         for (int k=last+2;k < num_points_1st + num_points_2nd +2;k++) {
608                                 if (num_points_2nd < 1)
609                                         break;
610                                 double clutter_height = 0.0;    // mean clutter height for a certain terrain type
611                                 double clutter_density = 0.0;   // percent of reflected wave
612                                 get_material_properties(materials[mat], clutter_height, clutter_density);
613                                 
614                                 double grad = fabs(itm_elev[last+1] + clutter_height - itm_elev[num_points_1st + num_points_2nd + 2] + clutter_height) / distance_m;
615                                 // First Fresnel radius
616                                 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) );
617                                 
618                                 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
619                                 
620                                 double min_elev = SGMiscd::min(itm_elev[last+1] + clutter_height, itm_elev[num_points_1st + num_points_2nd +2] + clutter_height);
621                                 double d1 = j * itm_elev[1];
622                                 if ( (itm_elev[last+1] + clutter_height) > (itm_elev[num_points_1st + num_points_2nd + 2] + clutter_height) ) { 
623                                         d1 = (num_points_2nd - j) * itm_elev[1];
624                                 }
625                                 double ray_height = (grad * d1) + min_elev;
626                                 
627                                 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
628                                 double intrusion = fabs(clearance);
629                                 
630                                 if (clearance >= 0) {
631                                         // no losses
632                                 }
633                                 else if (clearance < 0 && (intrusion < clutter_height)) {
634                                         
635                                         clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
636                                 }
637                                 else if (clearance < 0 && (intrusion > clutter_height)) {
638                                         clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
639                                 }
640                                 else {
641                                         // no losses
642                                 }
643                                 j++;
644                                 mat++;
645                                 last2 = k;
646                         }
647                         
648                         /** third and final pass */
649                         mat +=1;
650                         j =1; // first point is 2nd obstruction elevation, 3rd is RX elevation
651                         for (int k=last2+2;k < (int)itm_elev[0] + 2;k++) {
652                                 if (num_points_3rd < 1)
653                                         break;
654                                 double clutter_height = 0.0;    // mean clutter height for a certain terrain type
655                                 double clutter_density = 0.0;   // percent of reflected wave
656                                 get_material_properties(materials[mat], clutter_height, clutter_density);
657                                 
658                                 double grad = fabs(itm_elev[last2+1] + clutter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
659                                 // First Fresnel radius
660                                 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) );
661                                 
662                                 
663                                 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
664                                 
665                                 double min_elev = SGMiscd::min(itm_elev[last2+1] + clutter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
666                                 double d1 = j * itm_elev[1];
667                                 if ( (itm_elev[last2+1] + clutter_height) > (itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) { 
668                                         d1 = (num_points_3rd - j) * itm_elev[1];
669                                 }
670                                 double ray_height = (grad * d1) + min_elev;
671                                 
672                                 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
673                                 double intrusion = fabs(clearance);
674                                 
675                                 if (clearance >= 0) {
676                                         // no losses
677                                 }
678                                 else if (clearance < 0 && (intrusion < clutter_height)) {
679                                         
680                                         clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
681                                 }
682                                 else if (clearance < 0 && (intrusion > clutter_height)) {
683                                         clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
684                                 }
685                                 else {
686                                         // no losses
687                                 }
688                                 j++;
689                                 mat++;
690                                 
691                         }
692                         
693                 }
694         }
695         else if (p_mode == 2) {         //      troposcatter: ignore ground clutter for now...
696                 clutter_loss = 0.0;
697         }
698         
699 }
700
701 /***    Temporary material properties database
702 *               height: median clutter height
703 *               density: radiowave attenuation factor
704 ***/
705 void FGRadioTransmission::get_material_properties(string mat_name, double &height, double &density) {
706         
707         if(mat_name == "Landmass") {
708                 height = 15.0;
709                 density = 0.2;
710         }
711
712         else if(mat_name == "SomeSort") {
713                 height = 15.0;
714                 density = 0.2;
715         }
716
717         else if(mat_name == "Island") {
718                 height = 15.0;
719                 density = 0.2;
720         }
721         else if(mat_name == "Default") {
722                 height = 15.0;
723                 density = 0.2;
724         }
725         else if(mat_name == "EvergreenBroadCover") {
726                 height = 20.0;
727                 density = 0.2;
728         }
729         else if(mat_name == "EvergreenForest") {
730                 height = 20.0;
731                 density = 0.2;
732         }
733         else if(mat_name == "DeciduousBroadCover") {
734                 height = 15.0;
735                 density = 0.3;
736         }
737         else if(mat_name == "DeciduousForest") {
738                 height = 15.0;
739                 density = 0.3;
740         }
741         else if(mat_name == "MixedForestCover") {
742                 height = 20.0;
743                 density = 0.25;
744         }
745         else if(mat_name == "MixedForest") {
746                 height = 15.0;
747                 density = 0.25;
748         }
749         else if(mat_name == "RainForest") {
750                 height = 25.0;
751                 density = 0.55;
752         }
753         else if(mat_name == "EvergreenNeedleCover") {
754                 height = 15.0;
755                 density = 0.2;
756         }
757         else if(mat_name == "WoodedTundraCover") {
758                 height = 5.0;
759                 density = 0.15;
760         }
761         else if(mat_name == "DeciduousNeedleCover") {
762                 height = 5.0;
763                 density = 0.2;
764         }
765         else if(mat_name == "ScrubCover") {
766                 height = 3.0;
767                 density = 0.15;
768         }
769         else if(mat_name == "BuiltUpCover") {
770                 height = 30.0;
771                 density = 0.7;
772         }
773         else if(mat_name == "Urban") {
774                 height = 30.0;
775                 density = 0.7;
776         }
777         else if(mat_name == "Construction") {
778                 height = 30.0;
779                 density = 0.7;
780         }
781         else if(mat_name == "Industrial") {
782                 height = 30.0;
783                 density = 0.7;
784         }
785         else if(mat_name == "Port") {
786                 height = 30.0;
787                 density = 0.7;
788         }
789         else if(mat_name == "Town") {
790                 height = 10.0;
791                 density = 0.5;
792         }
793         else if(mat_name == "SubUrban") {
794                 height = 10.0;
795                 density = 0.5;
796         }
797         else if(mat_name == "CropWoodCover") {
798                 height = 10.0;
799                 density = 0.1;
800         }
801         else if(mat_name == "CropWood") {
802                 height = 10.0;
803                 density = 0.1;
804         }
805         else if(mat_name == "AgroForest") {
806                 height = 10.0;
807                 density = 0.1;
808         }
809         else {
810                 height = 0.0;
811                 density = 0.0;
812         }
813         
814 }
815
816 /*** implement simple LOS propagation model (WIP)
817 ***/
818 double FGRadioTransmission::LOS_calculate_attenuation(SGGeod pos, double freq, int transmission_type) {
819         double frq_mhz;
820         if( (freq < 118.0) || (freq > 137.0) )
821                 frq_mhz = 125.0;        // sane value, middle of bandplan
822         else
823                 frq_mhz = freq;
824         double dbloss;
825         double tx_pow = _transmitter_power;
826         double ant_gain = _rx_antenna_gain + _tx_antenna_gain;
827         double signal = 0.0;
828         
829         double sender_alt_ft,sender_alt;
830         double transmitter_height=0.0;
831         double receiver_height=0.0;
832         double own_lat = fgGetDouble("/position/latitude-deg");
833         double own_lon = fgGetDouble("/position/longitude-deg");
834         double own_alt_ft = fgGetDouble("/position/altitude-ft");
835         double own_alt= own_alt_ft * SG_FEET_TO_METER;
836         
837         
838         double link_budget = tx_pow - _receiver_sensitivity - _rx_line_losses - _tx_line_losses + ant_gain;     
839
840         //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
841         
842         SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
843         
844         SGGeod sender_pos = pos;
845         
846         sender_alt_ft = sender_pos.getElevationFt();
847         sender_alt = sender_alt_ft * SG_FEET_TO_METER;
848         
849         receiver_height = own_alt;
850         transmitter_height = sender_alt;
851         
852         double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
853         
854         
855         transmitter_height += _tx_antenna_height;
856         receiver_height += _rx_antenna_height;
857         
858         
859         /** radio horizon calculation with wave bending k=4/3 */
860         double receiver_horizon = 4.12 * sqrt(receiver_height);
861         double transmitter_horizon = 4.12 * sqrt(transmitter_height);
862         double total_horizon = receiver_horizon + transmitter_horizon;
863         
864         if (distance_m > total_horizon) {
865                 return -1;
866         }
867         
868         // free-space loss (distance calculation should be changed)
869         dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55;
870         signal = link_budget - dbloss;
871         SG_LOG(SG_GENERAL, SG_BULK,
872                         "LOS:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm ");
873         //cerr << "LOS:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm " << endl;
874         return signal;
875         
876 }
877
878