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