]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/trafficcontrol.cxx
functional radio signal attenuation
[flightgear.git] / src / ATC / trafficcontrol.cxx
index 02d5a5c81e9a18d8b927e40f26e1dd950dbfdea2..15520295a5ec24296b6b9fd84acfa95c08c80406 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <algorithm>
 #include <math.h>
+#include <stdlib.h>
 #include <deque>
 
 #include <osg/Geode>
@@ -49,7 +50,7 @@
 #include <Airports/groundnetwork.hxx>
 #include <Airports/dynamics.hxx>
 #include <Airports/simple.hxx>
-
+#define WITH_POINT_TO_POINT
 #include "itm.cpp"
 
 using std::sort;
@@ -521,6 +522,7 @@ void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent,
     FGAIFlightPlan *fp;
     string fltRules;
     string instructionText;
+    int ground_to_air=0;
 
     //double commFreqD;
     sender = rec->getAircraft()->getTrafficRef()->getCallSign();
@@ -561,6 +563,7 @@ void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent,
         string tmp = sender;
         sender = receiver;
         receiver = tmp;
+        ground_to_air=1;
     }
     switch (msgId) {
     case MSG_ANNOUNCE_ENGINE_START:
@@ -731,13 +734,20 @@ void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent,
                 
         if ((onBoardRadioFreqI0 == stationFreq)
             || (onBoardRadioFreqI1 == stationFreq)) {
-               double snr = calculate_attenuation(rec, parent, msgDir);
+               double snr = calculate_attenuation(rec, parent, ground_to_air);
                if (snr <= 0)
                        return;
-               if (snr > 0 && snr < 10) {
-                       //for low snr values implement a way to make the conversation
-                       //hard to understand (perhaps eliminate letters from words or such
-                       return;
+               if (snr > 0 && snr < 12) {
+                       //for low SNR values implement a way to make the conversation
+                       //hard to understand but audible
+                       string hash_noise = " ";
+                       int reps = fabs((int)snr - 11);
+                       int t_size = text.size();
+                       for (int n=1;n<=reps * 2;n++) {
+                               int pos = rand() % t_size -1;
+                               text.replace(pos,1, hash_noise);
+                       }
+                       
                }
                
             if (rec->allowTransmissions()) {
@@ -751,12 +761,12 @@ void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent,
     }
 }
 
-int calculate_attenuation(FGTrafficRecord * rec, FGAirportDynamics *parent,
-                               AtcMsgDir msgDir) {
-       /////////////////////////////////////////////////
-        ///  Implement radio attenuation
-        ///  based on the Longley-Rice propagation model
-        /////////////////////////////////////////////////
+double FGATCController::calculate_attenuation(FGTrafficRecord * rec, FGAirportDynamics *parent,
+                               int ground_to_air) {
+       //////////////////////////////////////////////////
+        ///  Implement radio attenuation               //
+        ///  based on the Longley-Rice propagation model//
+        //////////////////////////////////////////////////
         
         FGScenery * scenery = globals->get_scenery();
         // player aircraft position
@@ -765,74 +775,108 @@ int calculate_attenuation(FGTrafficRecord * rec, FGAirportDynamics *parent,
         double own_alt_ft = fgGetDouble("/position/altitude-ft");
         double own_alt= own_alt_ft * SG_FEET_TO_METER;
         
+        cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
+        
         SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
         SGGeod max_own_pos = SGGeod::fromDegM( own_lon, own_lat, SG_MAX_ELEVATION_M );
         SGGeoc center = SGGeoc::fromGeod( max_own_pos );
+        SGGeoc own_pos_c = SGGeoc::fromGeod( own_pos );
         
-        // position of sender
+        // position of sender radio antenna (HAAT)
         // sender can be aircraft or ground station
+        double ATC_HAAT = 30.0;
+        double Aircraft_HAAT = 7.0;
         double sender_alt_ft,sender_alt;
+        double transceiver_height=0.0;
+        double receiver_height=0.0;
         SGGeod sender_pos;
-        if(msgDir == ATC_GROUND_TO_AIR) {
+        if(ground_to_air) {
                sender_alt_ft = parent->getElevation();
-               sender_alt = sender_alt_ft * SG_FEET_TO_METER;
-               sender_pos= SGGeod::fromDegM( parent->getLongitude(), parent->getLatitude(), sender_alt );
+               sender_alt = sender_alt_ft * SG_FEET_TO_METER + ATC_HAAT;
+               sender_pos= SGGeod::fromDegM( parent->getLongitude(),
+                       parent->getLatitude(), sender_alt );
        }
        else {
                sender_alt_ft = rec->getAltitude();
-               sender_alt = sender_alt_ft * SG_FEET_TO_METER;
-               sender_pos= SGGeod::fromDegM( rec->getLongitude(), rec->getLatitude(), sender_alt );
+               sender_alt = sender_alt_ft * SG_FEET_TO_METER + Aircraft_HAAT;
+               sender_pos= SGGeod::fromDegM( rec->getLongitude(),
+                       rec->getLatitude(), sender_alt );
        }
-        double point_distance= 100.0; // regular SRTM is 90 meters
-        double course = SGGeodesy::courseDeg(own_pos, sender_pos);
+       SGGeoc sender_pos_c = SGGeoc::fromGeod( sender_pos );
+       cerr << "ITM:: sender Lat: " << parent->getLatitude() << ", Lon: " << parent->getLongitude() << ", Alt: " << sender_alt << endl;
+       
+        double point_distance= 90.0; // regular SRTM is 90 meters
+        double course = SGGeodesy::courseRad(own_pos_c, sender_pos_c);
         double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
+        double probe_distance = 0.0;
+        
+        cerr << "ITM:: Distance: " << distance_m << endl;
+        
         double max_points = distance_m / point_distance;
         deque<double> _elevations;
         
+        SGGeod probe_pilot = SGGeod::fromGeoc(center.advanceRadM( course, 0 ));
+        double elevation_under_pilot = 0.0;
+       if (scenery->get_elevation_m( probe_pilot, elevation_under_pilot, NULL )) {
+               receiver_height = own_alt - elevation_under_pilot;
+       }
+       _elevations.push_front(receiver_height);
+       
+       SGGeod probe_sender = SGGeod::fromGeoc(center.advanceRadM( course, distance_m ));
+        double elevation_under_sender = 0.0;
+       if (scenery->get_elevation_m( probe_sender, elevation_under_sender, NULL )) {
+               transceiver_height = sender_alt - elevation_under_sender;
+       }
+        
         // If distance larger than this value (400 km), assume reception imposssible
         // technically 400 km is no problem if LOS conditions exist,
         // but we do this to spare resources
         if (distance_m > 400000)
-               return -1;
+               return -1.0;
+        
+        int e_size = (deque<unsigned>::size_type)max_points;
         
-        while (_elevations.size() < (deque<double>::size_type)max_points) {
-               SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, point_distance ));
+        while (_elevations.size() < e_size) {
+               probe_distance += point_distance;
+               SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, probe_distance ));
                
                double elevation_m = 0.0;
        
                if (scenery->get_elevation_m( probe, elevation_m, NULL )) {
                         _elevations.push_front(elevation_m);
+                        //cerr << "ITM:: Probe elev: " << elevation_m << endl;
                }
        }
        
-       /*
+       _elevations.push_front(transceiver_height);
        double max_alt_between=0.0;
        for( deque<double>::size_type i = 0; i < _elevations.size(); i++ ) {
                if (_elevations[i] > max_alt_between) {
                        max_alt_between = _elevations[i];
                }
        }
-       */
        
-       double num_points= (int)_elevations.size();
-       _elevations.push_front(distance_m);
+       double num_points= (double)_elevations.size();
+       cerr << "ITM:: Max alt between: " << max_alt_between << ", num points:" << num_points << endl;
+       _elevations.push_front(point_distance);
        _elevations.push_front(num_points -1);
-       int size= _elevations.size();
-       double itm_elev[];
+       int size = _elevations.size();
+       double itm_elev[size];
        for(int i=0;i<size;i++) {
                itm_elev[i]=_elevations[i];
+               //cerr << "ITM:: itm_elev: " << _elevations[i] << endl;
        }
        
        ////////////// ITM default parameters //////////////
        // later perhaps take them from tile materials?
        double eps_dielect=15.0;
        double sgm_conductivity = 0.005;
-       double eno_ns_surfref = 301.0;
+       double eno = 301.0;
        double frq_mhz = 125.0;         // middle of bandplan
        int radio_climate = 5;          // continental temperate
        int pol=1;      // assuming vertical polarization
-       double conf = 0.70;     // my own tests in Radiomobile have worked best with these values
-       double rel = 0.70;      // ^^
+       double conf = 0.90;     // my own tests in Radiomobile have worked best with these values
+       double rel = 0.80;      // ^^
        double dbloss;
        char strmode[150];
        int errnum;
@@ -844,8 +888,16 @@ int calculate_attenuation(FGTrafficRecord * rec, FGAirportDynamics *parent,
        // !!! small aircraft have portable transmitters which operate at 36 dBm output (4 Watts)
        // later store this value in aircraft description
        // ATC comms usually operate high power equipment, thus making the link asymetrical; this is ignored for now
-       double transmitter_power = 43.0;
-       double link_budget = transmitter_power - receiver_sensitivity;  
+       if(ground_to_air)
+               double transmitter_power = 49.0;
+       else
+               double transmitter_power = 43.0;
+       if(ground_to_air)
+               double antenna_gain = 5.0; //pilot plane's antenna gain + Controller antenna gain
+       else
+               double antenna_gain = 2.0; //pilot plane's antenna gain + AI aircraft antenna gain
+       double link_budget = transmitter_power - receiver_sensitivity + antenna_gain;   
+       
        
        // first Fresnel zone radius
        // frequency in the middle of the bandplan, more accuracy is not necessary
@@ -857,12 +909,12 @@ int calculate_attenuation(FGTrafficRecord * rec, FGAirportDynamics *parent,
 
        point_to_point(itm_elev, sender_alt, own_alt,
                eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
-               pol, conf, rel, dbloss, strmode, errnum)
+               pol, conf, rel, dbloss, strmode, errnum);
 
-       cerr << "Attenuation: " << dbloss << ", Mode: " << strmode << ", Error: " << errnum << endl;
+       cerr << "ITM:: Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum << endl;
        
-       if (errnum !=0 && errnum !=1)
-               return -1;
+       //if (errnum !=0 && errnum !=1)
+       //      return -1;
        double snr = link_budget - dbloss;
        return snr;
 
@@ -1286,13 +1338,13 @@ bool FGStartupController::checkTransmissionState(int st, time_t now, time_t star
                  atc->getATCDialog()->removeEntry(1);
             } else {
                 //cerr << "creading message for " << i->getAircraft()->getCallSign() << endl;
-                transmit(&(*i), parent, msgId, msgDir, false);
+                transmit(&(*i), &(*parent), msgId, msgDir, false);
                 return false;
             }
         }
         if (now > startTime) {
             //cerr << "Transmitting startup msg" << endl;
-            transmit(&(*i), msgId, msgDir, true);
+            transmit(&(*i), &(*parent), msgId, msgDir, true);
             i->updateState();
             lastTransmission = now;
             available = false;
@@ -1361,11 +1413,11 @@ void FGStartupController::updateAircraftInformation(int id, double lat, double l
         if (now > startTime + 200) {
             if (i->pushBackAllowed()) {
                 i->allowRepeatedTransmissions();
-                transmit(&(*i), MSG_PERMIT_PUSHBACK_CLEARANCE,
+                transmit(&(*i), &(*parent), MSG_PERMIT_PUSHBACK_CLEARANCE,
                          ATC_GROUND_TO_AIR, true);
                 i->updateState();
             } else {
-                transmit(&(*i), MSG_HOLD_PUSHBACK_CLEARANCE,
+                transmit(&(*i), &(*parent), MSG_HOLD_PUSHBACK_CLEARANCE,
                          ATC_GROUND_TO_AIR, true);
                 i->suppressRepeatedTransmissions();
             }