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