]> git.mxchange.org Git - flightgear.git/blob - src/Radio/radio.cxx
Add separate fields for receiver and transmitter:
[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 = 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         if (p_mode == 0) {      // LOS: take each point and see how clutter height affects first Fresnel zone
432                 int mat = 0;
433                 int j=1; // first point is TX elevation, last is RX elevation
434                 for (int k=3;k < (int)(itm_elev[0]) + 2;k++) {
435                         
436                         double clutter_height = 0.0;    // mean clutter height for a certain terrain type
437                         double clutter_density = 0.0;   // percent of reflected wave
438                         get_material_properties(materials[mat], clutter_height, clutter_density);
439                         //cerr << "Clutter:: material: " << materials[mat] << " height: " << clutter_height << ", density: " << clutter_density << endl;
440                         double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
441                         // First Fresnel radius
442                         double frs_rad = 548 * sqrt( (j * itm_elev[1] * (itm_elev[0] - j) * itm_elev[1] / 1000000) / (  distance_m * freq / 1000) );
443                         assert(frs_rad > 0);
444                         //cerr << "Clutter:: fresnel radius: " << frs_rad << endl;
445                         //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
446                         
447                         double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
448                         double d1 = j * itm_elev[1];
449                         if ((itm_elev[2] + transmitter_height) > ( itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) {
450                                 d1 = (itm_elev[0] - j) * itm_elev[1];
451                         }
452                         double ray_height = (grad * d1) + min_elev;
453                         //cerr << "Clutter:: ray height: " << ray_height << " ground height:" << itm_elev[k] << endl;
454                         double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
455                         double intrusion = fabs(clearance);
456                         //cerr << "Clutter:: clearance: " << clearance << endl;
457                         if (clearance >= 0) {
458                                 // no losses
459                         }
460                         else if (clearance < 0 && (intrusion < clutter_height)) {
461                                 
462                                 clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * freq/100;
463                         }
464                         else if (clearance < 0 && (intrusion > clutter_height)) {
465                                 clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * freq/100;
466                         }
467                         else {
468                                 // no losses
469                         }
470                         j++;
471                         mat++;
472                 }
473                 
474         }
475         else if (p_mode == 1) {         // diffraction
476                 
477                 if (horizons[1] == 0.0) {       //      single horizon: same as above, except pass twice using the highest point
478                         int num_points_1st = (int)floor( horizons[0] * itm_elev[0]/ distance_m ); 
479                         int num_points_2nd = (int)ceil( (distance_m - horizons[0]) * itm_elev[0] / distance_m ); 
480                         cerr << "Diffraction 1 horizon:: points1: " << num_points_1st << " points2: " << num_points_2nd << endl;
481                         int last = 1;
482                         /** perform the first pass */
483                         int mat = 0;
484                         int j=1; // first point is TX elevation, 2nd is obstruction elevation
485                         for (int k=3;k < num_points_1st + 2;k++) {
486                                 if (num_points_1st < 1)
487                                         break;
488                                 double clutter_height = 0.0;    // mean clutter height for a certain terrain type
489                                 double clutter_density = 0.0;   // percent of reflected wave
490                                 get_material_properties(materials[mat], clutter_height, clutter_density);
491                                 //cerr << "Clutter:: material: " << materials[mat] << " height: " << clutter_height << ", density: " << clutter_density << endl;
492                                 double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[num_points_1st + 2] + clutter_height) / distance_m;
493                                 // First Fresnel radius
494                                 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) );
495                                 assert(frs_rad > 0);
496                                 //cerr << "Clutter:: fresnel radius: " << frs_rad << endl;
497                                 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
498                                 
499                                 double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[num_points_1st + 2] + clutter_height);
500                                 double d1 = j * itm_elev[1];
501                                 if ( (itm_elev[2] + transmitter_height) > (itm_elev[num_points_1st + 2] + clutter_height) ) {
502                                         d1 = (num_points_1st - j) * itm_elev[1];
503                                 }
504                                 double ray_height = (grad * d1) + min_elev;
505                                 //cerr << "Clutter:: ray height: " << ray_height << " ground height:" << itm_elev[k] << endl;
506                                 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
507                                 double intrusion = fabs(clearance);
508                                 //cerr << "Clutter:: clearance: " << clearance << endl;
509                                 if (clearance >= 0) {
510                                         // no losses
511                                 }
512                                 else if (clearance < 0 && (intrusion < clutter_height)) {
513                                         
514                                         clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * freq/100;
515                                 }
516                                 else if (clearance < 0 && (intrusion > clutter_height)) {
517                                         clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * freq/100;
518                                 }
519                                 else {
520                                         // no losses
521                                 }
522                                 j++;
523                                 mat++;
524                                 last = k;
525                         }
526                         
527                         /** and the second pass */
528                         mat +=1;
529                         j =1; // first point is diffraction edge, 2nd the RX elevation
530                         for (int k=last+2;k < (int)(itm_elev[0]) + 2;k++) {
531                                 if (num_points_2nd < 1)
532                                         break;
533                                 double clutter_height = 0.0;    // mean clutter height for a certain terrain type
534                                 double clutter_density = 0.0;   // percent of reflected wave
535                                 get_material_properties(materials[mat], clutter_height, clutter_density);
536                                 //cerr << "Clutter:: material: " << materials[mat] << " height: " << clutter_height << ", density: " << clutter_density << endl;
537                                 double grad = fabs(itm_elev[last+1] + clutter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
538                                 // First Fresnel radius
539                                 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) );
540                                 assert(frs_rad > 0);
541                                 //cerr << "Clutter:: fresnel radius: " << frs_rad << endl;
542                                 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
543                                 
544                                 double min_elev = SGMiscd::min(itm_elev[last+1] + clutter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
545                                 double d1 = j * itm_elev[1];
546                                 if ( (itm_elev[last+1] + clutter_height) > (itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) { 
547                                         d1 = (num_points_2nd - j) * itm_elev[1];
548                                 }
549                                 double ray_height = (grad * d1) + min_elev;
550                                 //cerr << "Clutter:: ray height: " << ray_height << " ground height:" << itm_elev[k] << endl;
551                                 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
552                                 double intrusion = fabs(clearance);
553                                 //cerr << "Clutter:: clearance: " << clearance << endl;
554                                 if (clearance >= 0) {
555                                         // no losses
556                                 }
557                                 else if (clearance < 0 && (intrusion < clutter_height)) {
558                                         
559                                         clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * freq/100;
560                                 }
561                                 else if (clearance < 0 && (intrusion > clutter_height)) {
562                                         clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * freq/100;
563                                 }
564                                 else {
565                                         // no losses
566                                 }
567                                 j++;
568                                 mat++;
569                         }
570                         
571                 }
572                 else {  // double horizon: same as single horizon, except there are 3 segments
573                         
574                         int num_points_1st = (int)floor( horizons[0] * itm_elev[0] / distance_m ); 
575                         int num_points_2nd = (int)ceil( (horizons[1] - horizons[0]) * itm_elev[0] / distance_m ); 
576                         int num_points_3rd = (int)itm_elev[0] - num_points_1st - num_points_2nd; 
577                         
578                         cerr << "Clutter:: points1: " << num_points_1st << " points2: " << num_points_2nd << " points3: " << num_points_3rd << endl;
579                         int last = 1;
580                         /** perform the first pass */
581                         int mat = 0;
582                         int j=1; // first point is TX elevation, 2nd is obstruction elevation
583                         for (int k=3;k < num_points_1st +2;k++) {
584                                 if (num_points_1st < 1)
585                                         break;
586                                 double clutter_height = 0.0;    // mean clutter height for a certain terrain type
587                                 double clutter_density = 0.0;   // percent of reflected wave
588                                 get_material_properties(materials[mat], clutter_height, clutter_density);
589                                 //cerr << "Clutter:: material: " << materials[mat] << " height: " << clutter_height << ", density: " << clutter_density << endl;
590                                 double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[num_points_1st + 2] + clutter_height) / distance_m;
591                                 // First Fresnel radius
592                                 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) );
593                                 assert(frs_rad > 0);
594                                 //cerr << "Clutter:: fresnel radius: " << frs_rad << endl;
595                                 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
596                                 
597                                 double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[num_points_1st + 2] + clutter_height);
598                                 double d1 = j * itm_elev[1];
599                                 if ( (itm_elev[2] + transmitter_height) > (itm_elev[num_points_1st + 2] + clutter_height) ) {
600                                         d1 = (num_points_1st - j) * itm_elev[1];
601                                 }
602                                 double ray_height = (grad * d1) + min_elev;
603                                 //cerr << "Clutter:: ray height: " << ray_height << " ground height:" << itm_elev[k] << endl;
604                                 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
605                                 double intrusion = fabs(clearance);
606                                 //cerr << "Clutter:: clearance: " << clearance << endl;
607                                 if (clearance >= 0) {
608                                         // no losses
609                                 }
610                                 else if (clearance < 0 && (intrusion < clutter_height)) {
611                                         
612                                         clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * freq/100;
613                                 }
614                                 else if (clearance < 0 && (intrusion > clutter_height)) {
615                                         clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * freq/100;
616                                 }
617                                 else {
618                                         // no losses
619                                 }
620                                 j++;
621                                 last = k;
622                         }
623                         mat +=1;
624                         /** and the second pass */
625                         int last2=1;
626                         j =1; // first point is 1st obstruction elevation, 2nd is 2nd obstruction elevation
627                         for (int k=last+2;k < num_points_1st + num_points_2nd +2;k++) {
628                                 if (num_points_2nd < 1)
629                                         break;
630                                 double clutter_height = 0.0;    // mean clutter height for a certain terrain type
631                                 double clutter_density = 0.0;   // percent of reflected wave
632                                 get_material_properties(materials[mat], clutter_height, clutter_density);
633                                 //cerr << "Clutter:: material: " << materials[mat] << " height: " << clutter_height << ", density: " << clutter_density << endl;
634                                 double grad = fabs(itm_elev[last+1] + clutter_height - itm_elev[num_points_1st + num_points_2nd + 2] + clutter_height) / distance_m;
635                                 // First Fresnel radius
636                                 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) );
637                                 //cerr << "Clutter:: fresnel radius: " << frs_rad << " points2: " << num_points_2nd << " j: " << j << endl;
638                                 assert(frs_rad > 0);
639                                 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
640                                 
641                                 double min_elev = SGMiscd::min(itm_elev[last+1] + clutter_height, itm_elev[num_points_1st + num_points_2nd +2] + clutter_height);
642                                 double d1 = j * itm_elev[1];
643                                 if ( (itm_elev[last+1] + clutter_height) > (itm_elev[num_points_1st + num_points_2nd + 2] + clutter_height) ) { 
644                                         d1 = (num_points_2nd - j) * itm_elev[1];
645                                 }
646                                 double ray_height = (grad * d1) + min_elev;
647                                 //cerr << "Clutter:: ray height: " << ray_height << " ground height:" << itm_elev[k] << endl;
648                                 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
649                                 double intrusion = fabs(clearance);
650                                 //cerr << "Clutter:: clearance: " << clearance << endl;
651                                 if (clearance >= 0) {
652                                         // no losses
653                                 }
654                                 else if (clearance < 0 && (intrusion < clutter_height)) {
655                                         
656                                         clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * freq/100;
657                                 }
658                                 else if (clearance < 0 && (intrusion > clutter_height)) {
659                                         clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * freq/100;
660                                 }
661                                 else {
662                                         // no losses
663                                 }
664                                 j++;
665                                 mat++;
666                                 last2 = k;
667                         }
668                         
669                         /** third and final pass */
670                         mat +=1;
671                         j =1; // first point is 2nd obstruction elevation, 3rd is RX elevation
672                         for (int k=last2+2;k < (int)itm_elev[0] + 2;k++) {
673                                 if (num_points_3rd < 1)
674                                         break;
675                                 double clutter_height = 0.0;    // mean clutter height for a certain terrain type
676                                 double clutter_density = 0.0;   // percent of reflected wave
677                                 get_material_properties(materials[mat], clutter_height, clutter_density);
678                                 //cerr << "Clutter:: material: " << materials[mat] << " height: " << clutter_height << ", density: " << clutter_density << endl;
679                                 double grad = fabs(itm_elev[last2+1] + clutter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
680                                 // First Fresnel radius
681                                 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) );
682                                 cerr << "Clutter:: fresnel radius: " << frs_rad << " points2: " << num_points_3rd << " j: " << j << endl;
683                                 assert(frs_rad > 0);
684                                 //cerr << "Clutter:: fresnel radius: " << frs_rad << endl;
685                                 //double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 );    // K=4/3
686                                 
687                                 double min_elev = SGMiscd::min(itm_elev[last2+1] + clutter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
688                                 double d1 = j * itm_elev[1];
689                                 if ( (itm_elev[last2+1] + clutter_height) > (itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) { 
690                                         d1 = (num_points_3rd - j) * itm_elev[1];
691                                 }
692                                 double ray_height = (grad * d1) + min_elev;
693                                 //cerr << "Clutter:: ray height: " << ray_height << " ground height:" << itm_elev[k] << endl;
694                                 double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;                
695                                 double intrusion = fabs(clearance);
696                                 //cerr << "Clutter:: clearance: " << clearance << endl;
697                                 if (clearance >= 0) {
698                                         // no losses
699                                 }
700                                 else if (clearance < 0 && (intrusion < clutter_height)) {
701                                         
702                                         clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * freq/100;
703                                 }
704                                 else if (clearance < 0 && (intrusion > clutter_height)) {
705                                         clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * freq/100;
706                                 }
707                                 else {
708                                         // no losses
709                                 }
710                                 j++;
711                                 mat++;
712                                 
713                         }
714                         
715                 }
716         }
717         else if (p_mode == 2) {         //      troposcatter: ignore ground clutter for now...
718                 clutter_loss = 0.0;
719         }
720         
721 }
722
723 /***    Temporary material properties database
724 *               height: median clutter height
725 *               density: radiowave attenuation factor
726 ***/
727 void FGRadioTransmission::get_material_properties(string mat_name, double &height, double &density) {
728         
729         if(mat_name == "Landmass") {
730                 height = 15.0;
731                 density = 0.2;
732         }
733
734         else if(mat_name == "SomeSort") {
735                 height = 15.0;
736                 density = 0.2;
737         }
738
739         else if(mat_name == "Island") {
740                 height = 15.0;
741                 density = 0.2;
742         }
743         else if(mat_name == "Default") {
744                 height = 15.0;
745                 density = 0.2;
746         }
747         else if(mat_name == "EvergreenBroadCover") {
748                 height = 20.0;
749                 density = 0.2;
750         }
751         else if(mat_name == "EvergreenForest") {
752                 height = 20.0;
753                 density = 0.2;
754         }
755         else if(mat_name == "DeciduousBroadCover") {
756                 height = 15.0;
757                 density = 0.3;
758         }
759         else if(mat_name == "DeciduousForest") {
760                 height = 15.0;
761                 density = 0.3;
762         }
763         else if(mat_name == "MixedForestCover") {
764                 height = 20.0;
765                 density = 0.25;
766         }
767         else if(mat_name == "MixedForest") {
768                 height = 15.0;
769                 density = 0.25;
770         }
771         else if(mat_name == "RainForest") {
772                 height = 25.0;
773                 density = 0.55;
774         }
775         else if(mat_name == "EvergreenNeedleCover") {
776                 height = 15.0;
777                 density = 0.2;
778         }
779         else if(mat_name == "WoodedTundraCover") {
780                 height = 5.0;
781                 density = 0.15;
782         }
783         else if(mat_name == "DeciduousNeedleCover") {
784                 height = 5.0;
785                 density = 0.2;
786         }
787         else if(mat_name == "ScrubCover") {
788                 height = 3.0;
789                 density = 0.15;
790         }
791         else if(mat_name == "BuiltUpCover") {
792                 height = 30.0;
793                 density = 0.7;
794         }
795         else if(mat_name == "Urban") {
796                 height = 30.0;
797                 density = 0.7;
798         }
799         else if(mat_name == "Construction") {
800                 height = 30.0;
801                 density = 0.7;
802         }
803         else if(mat_name == "Industrial") {
804                 height = 30.0;
805                 density = 0.7;
806         }
807         else if(mat_name == "Port") {
808                 height = 30.0;
809                 density = 0.7;
810         }
811         else if(mat_name == "Town") {
812                 height = 10.0;
813                 density = 0.5;
814         }
815         else if(mat_name == "SubUrban") {
816                 height = 10.0;
817                 density = 0.5;
818         }
819         else if(mat_name == "CropWoodCover") {
820                 height = 10.0;
821                 density = 0.1;
822         }
823         else if(mat_name == "CropWood") {
824                 height = 10.0;
825                 density = 0.1;
826         }
827         else if(mat_name == "AgroForest") {
828                 height = 10.0;
829                 density = 0.1;
830         }
831         else {
832                 height = 0.0;
833                 density = 0.0;
834         }
835         
836 }
837
838 /*** implement simple LOS propagation model (WIP)
839 ***/
840 double FGRadioTransmission::LOS_calculate_attenuation(SGGeod pos, double freq, int transmission_type) {
841         double frq_mhz;
842         if( (freq < 118.0) || (freq > 137.0) )
843                 frq_mhz = 125.0;        // sane value, middle of bandplan
844         else
845                 frq_mhz = freq;
846         double dbloss;
847         double tx_pow = _transmitter_power;
848         double ant_gain = _antenna_gain;
849         double signal = 0.0;
850         double ATC_HAAT = 30.0;
851         double Aircraft_HAAT = 5.0;
852         double sender_alt_ft,sender_alt;
853         double transmitter_height=0.0;
854         double receiver_height=0.0;
855         double own_lat = fgGetDouble("/position/latitude-deg");
856         double own_lon = fgGetDouble("/position/longitude-deg");
857         double own_alt_ft = fgGetDouble("/position/altitude-ft");
858         double own_alt= own_alt_ft * SG_FEET_TO_METER;
859         
860         if(transmission_type == 1)
861                 tx_pow = _transmitter_power + 6.0;
862
863         if((transmission_type == 1) || (transmission_type == 3))
864                 ant_gain = _antenna_gain + 3.0; //pilot plane's antenna gain + ground station antenna gain
865         
866         double link_budget = tx_pow - _receiver_sensitivity + ant_gain; 
867
868         //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
869         
870         SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
871         
872         SGGeod sender_pos = pos;
873         
874         sender_alt_ft = sender_pos.getElevationFt();
875         sender_alt = sender_alt_ft * SG_FEET_TO_METER;
876         
877         receiver_height = own_alt;
878         transmitter_height = sender_alt;
879         
880         double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
881         
882         if(transmission_type == 1) 
883                 transmitter_height += ATC_HAAT;
884         else
885                 transmitter_height += Aircraft_HAAT;
886         
887         /** radio horizon calculation with wave bending k=4/3 */
888         double receiver_horizon = 4.12 * sqrt(receiver_height);
889         double transmitter_horizon = 4.12 * sqrt(transmitter_height);
890         double total_horizon = receiver_horizon + transmitter_horizon;
891         
892         if (distance_m > total_horizon) {
893                 return -1;
894         }
895         
896         // free-space loss (distance calculation should be changed)
897         dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55;
898         signal = link_budget - dbloss;
899         SG_LOG(SG_GENERAL, SG_BULK,
900                         "LOS:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm ");
901         //cerr << "LOS:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm " << endl;
902         return signal;
903         
904 }
905
906