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