]> git.mxchange.org Git - flightgear.git/blob - src/Radio/radio.cxx
Merge branch 'next' into comm-subsystem
[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 <Scenery/scenery.hxx>
30
31 #define WITH_POINT_TO_POINT 1
32 #include "itm.cpp"
33
34
35 FGRadio::FGRadio() {
36         
37         /////////// radio parameters ///////////
38         _receiver_sensitivity = -110.0; // typical AM receiver sensitivity seems to be 0.8 microVolt at 12dB SINAD
39         // AM transmitter power in dBm.
40         // Note this value is calculated from the typical final transistor stage output
41         // !!! small aircraft have portable transmitters which operate at 36 dBm output (4 Watts)
42         // later store this value in aircraft description
43         // ATC comms usually operate high power equipment, thus making the link asymetrical; this is ignored for now
44         _transmitter_power = 43.0;
45         //pilot plane's antenna gain + AI aircraft antenna gain
46         //real-life gain for conventional monopole/dipole antenna
47         _antenna_gain = 2.0;
48         _propagation_model = 2; //  choose between models via option: realistic radio on/off
49         
50 }
51
52 FGRadio::~FGRadio() 
53 {
54 }
55
56
57 double FGRadio::getFrequency(int radio) {
58         double freq = 118.0;
59         switch (radio) {
60                 case 1:
61                         freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
62                         break;
63                 case 2:
64                         freq = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
65                         break;
66                 default:
67                         freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
68                         
69         }
70         return freq;
71 }
72
73
74
75
76 void FGRadio::receiveText(SGGeod tx_pos, double freq, string text,
77         int ground_to_air) {
78
79         /*
80         double comm1 = getFrequency(1);
81         double comm2 = getFrequency(2);
82         if ( (freq != comm1) &&  (freq != comm2) ) {
83                 cerr << "Frequency not tuned: " << freq << " Radio1: " << comm1 << " Radio2: " << comm2 << endl;
84                 return;
85         }
86         else {
87         */
88                 double signal = ITM_calculate_attenuation(tx_pos, freq, ground_to_air);
89                 //cerr << "Signal: " << signal << endl;
90                 if (signal <= 0.0) {
91                         //cerr << "Signal below sensitivity: " << signal << " dBm" << endl;
92                         return;
93                 }
94                 if ((signal > 0.0) && (signal < 12.0)) {
95                         //for low SNR values implement a way to make the conversation
96                         //hard to understand but audible
97                         //how this works in the real world, is the receiver AGC fails to capture the slope
98                         //and the signal, due to being amplitude modulated, decreases volume after demodulation
99                         //the implementation below is more akin to what would happen on a FM transmission
100                         //therefore the correct way would be to work on the volume
101                         /*
102                         string hash_noise = " ";
103                         int reps = (int) (fabs(floor(signal - 11.0)) * 2);
104                         //cerr << "Reps: " << reps << endl;
105                         int t_size = text.size();
106                         for (int n = 1; n <= reps; ++n) {
107                                 int pos = rand() % (t_size -1);
108                                 //cerr << "Pos: " << pos << endl;
109                                 text.replace(pos,1, hash_noise);
110                         }
111                         */
112                         double volume = (fabs(signal - 12.0) / 12);
113                         double old_volume = fgGetDouble("/sim/sound/voices/voice/volume");
114                         //cerr << "Usable signal at limit: " << signal << endl;
115                         fgSetDouble("/sim/sound/voices/voice/volume", volume);
116                         fgSetString("/sim/messages/atc", text.c_str());
117                         fgSetDouble("/sim/sound/voices/voice/volume", old_volume);
118                 }
119                 else {
120                         //cerr << "Signal completely readable: " << signal << " dBm" << endl;
121                         fgSetString("/sim/messages/atc", text.c_str());
122                 }
123                 
124         //}
125         
126 }
127
128 double FGRadio::ITM_calculate_attenuation(SGGeod pos, double freq,
129                                int transmission_type) {
130
131         ///  Implement radio attenuation                
132         ///  based on the Longley-Rice propagation model
133         
134         ////////////// ITM default parameters //////////////
135         // in the future perhaps take them from tile materials?
136         double eps_dielect=15.0;
137         double sgm_conductivity = 0.005;
138         double eno = 301.0;
139         double frq_mhz;
140         if( (freq < 118.0) || (freq > 137.0) )
141                 frq_mhz = 125.0;        // sane value, middle of bandplan
142         else
143                 frq_mhz = freq;
144         int radio_climate = 5;          // continental temperate
145         int pol=1;      // assuming vertical polarization although this is more complex in reality
146         double conf = 0.90;     // 90% of situations and time, take into account speed
147         double rel = 0.90;      
148         double dbloss;
149         char strmode[150];
150         int errnum;
151         
152         double tx_pow = _transmitter_power;
153         double ant_gain = _antenna_gain;
154         double signal = 0.0;
155         
156         if(transmission_type == 1)
157                 tx_pow = _transmitter_power + 6.0;
158
159         if((transmission_type == 1) || (transmission_type == 3))
160                 ant_gain = _antenna_gain + 3.0; //pilot plane's antenna gain + ground station antenna gain
161         
162         double link_budget = tx_pow - _receiver_sensitivity + ant_gain; 
163
164         FGScenery * scenery = globals->get_scenery();
165         
166         double own_lat = fgGetDouble("/position/latitude-deg");
167         double own_lon = fgGetDouble("/position/longitude-deg");
168         double own_alt_ft = fgGetDouble("/position/altitude-ft");
169         double own_alt= own_alt_ft * SG_FEET_TO_METER;
170         
171         
172         //cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
173         
174         SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
175         SGGeod max_own_pos = SGGeod::fromDegM( own_lon, own_lat, SG_MAX_ELEVATION_M );
176         SGGeoc center = SGGeoc::fromGeod( max_own_pos );
177         SGGeoc own_pos_c = SGGeoc::fromGeod( own_pos );
178         
179         // position of sender radio antenna (HAAT)
180         // sender can be aircraft or ground station
181         double ATC_HAAT = 30.0;
182         double Aircraft_HAAT = 5.0;
183         double sender_alt_ft,sender_alt;
184         double transmitter_height=0.0;
185         double receiver_height=0.0;
186         SGGeod sender_pos = pos;
187         
188         sender_alt_ft = sender_pos.getElevationFt();
189         sender_alt = sender_alt_ft * SG_FEET_TO_METER;
190         SGGeod max_sender_pos = SGGeod::fromGeodM( pos, SG_MAX_ELEVATION_M );
191         SGGeoc sender_pos_c = SGGeoc::fromGeod( sender_pos );
192         //cerr << "ITM:: sender Lat: " << parent->getLatitude() << ", Lon: " << parent->getLongitude() << ", Alt: " << sender_alt << endl;
193         
194         double point_distance= 90.0; // regular SRTM is 90 meters
195         double course = SGGeodesy::courseRad(own_pos_c, sender_pos_c);
196         double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
197         double probe_distance = 0.0;
198         // If distance larger than this value (300 km), assume reception imposssible 
199         if (distance_m > 300000)
200                 return -1.0;
201         // If above 8000, consider LOS mode and calculate free-space att
202         if (own_alt > 8000) {
203                 dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55;
204                 cerr << "LOS-mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation" << endl;
205                 signal = link_budget - dbloss;
206                 return signal;
207         }
208         
209                 
210         double max_points = distance_m / point_distance;
211         deque<double> _elevations;
212
213         double elevation_under_pilot = 0.0;
214         if (scenery->get_elevation_m( max_own_pos, elevation_under_pilot, NULL )) {
215                 receiver_height = own_alt - elevation_under_pilot + 3; //assume antenna located 3 meters above ground
216         }
217
218         double elevation_under_sender = 0.0;
219         if (scenery->get_elevation_m( max_sender_pos, elevation_under_sender, NULL )) {
220                 transmitter_height = sender_alt - elevation_under_sender;
221         }
222         else {
223                 transmitter_height = sender_alt;
224         }
225         
226         if(transmission_type == 1) 
227                 transmitter_height += ATC_HAAT;
228         else
229                 transmitter_height += Aircraft_HAAT;
230         
231         cerr << "ITM:: RX-height: " << receiver_height << " meters, TX-height: " << transmitter_height << " meters, Distance: " << distance_m << " meters" << endl;
232         
233         unsigned int e_size = (deque<unsigned>::size_type)max_points;
234         
235         while (_elevations.size() <= e_size) {
236                 probe_distance += point_distance;
237                 SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, probe_distance ));
238                 
239                 double elevation_m = 0.0;
240         
241                 if (scenery->get_elevation_m( probe, elevation_m, NULL )) {
242                         if((transmission_type == 3) || (transmission_type == 4)) {
243                                 _elevations.push_back(elevation_m);
244                         }
245                         else {
246                                  _elevations.push_front(elevation_m);
247                         }
248                 }
249                 else {
250                         if((transmission_type == 3) || (transmission_type == 4)) {
251                                 _elevations.push_back(elevation_m);
252                         }
253                         else {
254                         _elevations.push_front(0.0);
255                         }
256                 }
257         }
258         if((transmission_type == 3) || (transmission_type == 4)) {
259                 _elevations.push_front(elevation_under_pilot);
260                 _elevations.push_back(elevation_under_sender);
261         }
262         else {
263                 _elevations.push_back(elevation_under_pilot);
264                 _elevations.push_front(elevation_under_sender);
265         }
266         
267         
268         double max_alt_between=0.0;
269         for( deque<double>::size_type i = 0; i < _elevations.size(); i++ ) {
270                 if (_elevations[i] > max_alt_between) {
271                         max_alt_between = _elevations[i];
272                 }
273         }
274         
275         double num_points= (double)_elevations.size();
276         //cerr << "ITM:: Max alt between: " << max_alt_between << ", num points:" << num_points << endl;
277         _elevations.push_front(point_distance);
278         _elevations.push_front(num_points -1);
279         int size = _elevations.size();
280         double itm_elev[size];
281         for(int i=0;i<size;i++) {
282                 itm_elev[i]=_elevations[i];
283                 //cerr << "ITM:: itm_elev: " << _elevations[i] << endl;
284         }
285
286         
287         // first Fresnel zone radius
288         // frequency in the middle of the bandplan, more accuracy is not necessary
289         double fz_clr= 8.657 * sqrt(distance_m / 0.125);
290         
291         // TODO: If we clear the first Fresnel zone, we are into line of sight territory
292
293         // else we need to calculate point to point link loss
294         if((transmission_type == 3) || (transmission_type == 4)) {
295                 // the sender and receiver roles are switched
296                 point_to_point(itm_elev, receiver_height, transmitter_height,
297                         eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
298                         pol, conf, rel, dbloss, strmode, errnum);
299                 
300         }
301         else {
302
303                 point_to_point(itm_elev, transmitter_height, receiver_height,
304                         eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
305                         pol, conf, rel, dbloss, strmode, errnum);
306         }
307
308         cerr << "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum << endl;
309         
310         //if (errnum == 4)
311         //      return -1;
312         signal = link_budget - dbloss;
313         return signal;
314
315 }