]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/trafficcontrol.cxx
fix + define WITH_POINT_TO_POINT
[flightgear.git] / src / ATC / trafficcontrol.cxx
index b80fcdefa052656a8b0f442eb9d09d32276beeca..90eba53a90e2c6223e14b8a3886aafa4c9f7009c 100644 (file)
@@ -25,6 +25,8 @@
 #endif
 
 #include <algorithm>
+#include <math.h>
+#include <deque>
 
 #include <osg/Geode>
 #include <osg/Geometry>
@@ -47,6 +49,8 @@
 #include <Airports/groundnetwork.hxx>
 #include <Airports/dynamics.hxx>
 #include <Airports/simple.hxx>
+#define WITH_POINT_TO_POINT
+#include "itm.cpp"
 
 using std::sort;
 
@@ -496,16 +500,18 @@ bool FGATCController::isUserAircraft(FGAIAircraft* ac)
     return (ac->getCallSign() == fgGetString("/sim/multiplay/callsign")) ? true : false; 
 };
 
-void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
+void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent, AtcMsgId msgId,
                                AtcMsgDir msgDir, bool audible)
 {
     string sender, receiver;
     int stationFreq = 0;
     int taxiFreq = 0;
+    int towerFreq = 0;
     int freqId = 0;
     string atisInformation;
     string text;
     string taxiFreqStr;
+    string towerFreqStr;
     double heading = 0;
     string activeRunway;
     string fltType;
@@ -515,6 +521,7 @@ void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
     FGAIFlightPlan *fp;
     string fltRules;
     string instructionText;
+    int ground_to_air=0;
 
     //double commFreqD;
     sender = rec->getAircraft()->getTrafficRef()->getCallSign();
@@ -534,6 +541,9 @@ void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
         taxiFreq =
             rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
             getDynamics()->getGroundFrequency(2);
+        towerFreq = 
+            rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
+            getDynamics()->getTowerFrequency(2);
         receiver =
             rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
             getName() + "-Ground";
@@ -552,6 +562,7 @@ void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
         string tmp = sender;
         sender = receiver;
         receiver = tmp;
+        ground_to_air=1;
     }
     switch (msgId) {
     case MSG_ANNOUNCE_ENGINE_START:
@@ -673,7 +684,37 @@ void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
     case MSG_ACKNOWLEDGE_RESUME_TAXI:
         text = receiver + ". Continuing Taxi. " + sender;
         break;
+    case MSG_REPORT_RUNWAY_HOLD_SHORT:
+        activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
+        //activeRunway = "test";
+        text = receiver + ". Holding short runway " 
+                        + activeRunway 
+                        + ". " + sender;
+        //text = "test1";
+        //cerr << "1 Currently at leg " << rec->getLeg() << endl;
+        break;
+    case MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT:
+        activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
+        text = receiver + "Roger. Holding short runway " 
+        //                + activeRunway 
+                        + ". " + sender;
+        //text = "test2";
+        //cerr << "2 Currently at leg " << rec->getLeg() << endl;
+        break;
+    case MSG_SWITCH_TOWER_FREQUENCY:
+        towerFreqStr = formatATCFrequency3_2(towerFreq);
+        text = receiver + "Contact Tower at " + towerFreqStr + ". " + sender;
+        //text = "test3";
+        //cerr << "3 Currently at leg " << rec->getLeg() << endl;
+        break;
+    case MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY:
+        towerFreqStr = formatATCFrequency3_2(towerFreq);
+        text = receiver + "Roger, switching to tower at " + towerFreqStr + ". " + sender;
+        //text = "test4";
+        //cerr << "4 Currently at leg " << rec->getLeg() << endl;
+        break;
     default:
+        //text = "test3";
         text = text + sender + ". Transmitting unknown Message";
         break;
     }
@@ -689,8 +730,18 @@ void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
         // Display ATC message only when one of the radios is tuned
         // the relevant frequency.
         // Note that distance attenuation is currently not yet implemented
+                
         if ((onBoardRadioFreqI0 == stationFreq)
             || (onBoardRadioFreqI1 == stationFreq)) {
+               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 (rec->allowTransmissions()) {
                 fgSetString("/sim/messages/atc", text.c_str());
             }
@@ -702,6 +753,125 @@ void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
     }
 }
 
+int 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
+        double own_lat = fgGetDouble("/position/latitude-deg");
+        double own_lon = fgGetDouble("/position/longitude-deg");
+        double own_alt_ft = fgGetDouble("/position/altitude-ft");
+        double own_alt= own_alt_ft * SG_FEET_TO_METER;
+        
+        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 );
+        
+        // position of sender
+        // sender can be aircraft or ground station
+        double sender_alt_ft,sender_alt;
+        SGGeod sender_pos;
+        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 );
+       }
+       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 );
+       }
+        double point_distance= 100.0; // regular SRTM is 90 meters
+        double course = SGGeodesy::courseDeg(own_pos, sender_pos);
+        double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
+        double max_points = distance_m / point_distance;
+        deque<double> _elevations;
+        
+        // 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;
+        
+        while (_elevations.size() < (deque<double>::size_type)max_points) {
+               SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, point_distance ));
+               
+               double elevation_m = 0.0;
+       
+               if (scenery->get_elevation_m( probe, elevation_m, NULL )) {
+                        _elevations.push_front(elevation_m);
+               }
+       }
+       
+       /*
+       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);
+       _elevations.push_front(num_points -1);
+       int size= _elevations.size();
+       double itm_elev[size];
+       for(int i=0;i<size;i++) {
+               itm_elev[i]=_elevations[i];
+       }
+       
+       ////////////// ITM default parameters //////////////
+       // later perhaps take them from tile materials?
+       double eps_dielect=15.0;
+       double sgm_conductivity = 0.005;
+       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 dbloss;
+       char strmode[150];
+       int errnum;
+       
+       /////////// radio parameters ///////////
+       double receiver_sensitivity = -112.0;   // typical AM receiver sensitivity in dBm
+       // AM transmitter power in dBm.
+       // Note this value is calculated from the typical final transistor stage output
+       // !!! 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;  
+       
+       // first Fresnel zone radius
+       // frequency in the middle of the bandplan, more accuracy is not necessary
+       double fz_clr= 8.657 * sqrt(distance_m / 0.125);
+       
+       // TODO: If we clear the first Fresnel zone, we are into line of sight teritory
+
+       // else we need to calculate point to point link loss
+
+       point_to_point(itm_elev, sender_alt, own_alt,
+               eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
+               pol, conf, rel, dbloss, strmode, errnum);
+
+       cerr << "Attenuation: " << dbloss << ", Mode: " << strmode << ", Error: " << errnum << endl;
+       
+       if (errnum !=0 && errnum !=1)
+               return -1;
+       double snr = link_budget - dbloss;
+       return snr;
+
+}
+
 string FGATCController::formatATCFrequency3_2(int freq)
 {
     char buffer[7];
@@ -795,7 +965,7 @@ void FGTowerController::announcePosition(int id,
             rwy->addToDepartureCue(ref);
         }
 
-        cerr << ref->getTrafficRef()->getCallSign() << " You are number " << rwy->getDepartureCueSize() <<  " for takeoff " << endl;
+        //cerr << ref->getTrafficRef()->getCallSign() << " You are number " << rwy->getDepartureCueSize() <<  " for takeoff " << endl;
     } else {
         i->setPositionAndHeading(lat, lon, heading, speed, alt);
     }
@@ -1120,13 +1290,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), 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;
@@ -1195,11 +1365,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();
             }
@@ -1214,7 +1384,7 @@ void FGStartupController::updateAircraftInformation(int id, double lat, double l
 
 // Note that this function is copied from simgear. for maintanance purposes, it's probabtl better to make a general function out of that.
 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
-                            double lon, double elev, double hdg)
+                            double lon, double elev, double hdg, double slope)
 {
     SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
     obj_pos = geod.makeZUpFrame();
@@ -1222,6 +1392,8 @@ static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
     // around the Z axis
     obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
                                         0.0, 0.0, 1.0));
+    obj_pos.preMult(osg::Matrix::rotate(slope * SGD_DEGREES_TO_RADIANS,
+                                        0.0, 1.0, 0.0));
 }
 
 
@@ -1234,7 +1406,7 @@ void FGStartupController::render(bool visible)
         globals->get_scenery()->get_scene_graph()->removeChild(group);
         //while (group->getNumChildren()) {
         //  cerr << "Number of children: " << group->getNumChildren() << endl;
-        simgear::EffectGeode* geode = (simgear::EffectGeode*) group->getChild(0);
+        //simgear::EffectGeode* geode = (simgear::EffectGeode*) group->getChild(0);
           //osg::MatrixTransform *obj_trans = (osg::MatrixTransform*) group->getChild(0);
            //geode->releaseGLObjects();
            //group->removeChild(geode);
@@ -1243,6 +1415,10 @@ void FGStartupController::render(bool visible)
     }
     if (visible) {
         group = new osg::Group;
+        FGScenery * local_scenery = globals->get_scenery();
+        double elevation_meters = 0.0;
+        double elevation_feet = 0.0;
+
 
         //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) {
         double dx = 0;
@@ -1269,8 +1445,36 @@ void FGStartupController::render(bool visible)
                 osg::Matrix obj_pos;
                 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
                 obj_trans->setDataVariance(osg::Object::STATIC);
+                // Experimental: Calculate slope here, based on length, and the individual elevations
+                double elevationStart;
+                if (isUserAircraft((i)->getAircraft())) {
+                    elevationStart = fgGetDouble("/position/ground-elev-m");
+                } else {
+                    elevationStart = ((i)->getAircraft()->_getAltitude()); 
+                }
+                double elevationEnd   = segment->getEnd()->getElevation();
+                if (elevationEnd == 0) {
+                    SGGeod center2 = end;
+                    center2.setElevationM(SG_MAX_ELEVATION_M);
+                    if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
+                        elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
+                            //elevation_meters += 0.5;
+                    }
+                    else { 
+                        elevationEnd = parent->getElevation()+8+dx;
+                    }
+                    segment->getEnd()->setElevation(elevationEnd);
+                }
+
+                double elevationMean  = (elevationStart + elevationEnd) / 2.0;
+                double elevDiff       = elevationEnd - elevationStart;
+               
+               double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
+                
+               //cerr << "1. Using mean elevation : " << elevationMean << " and " << slope << endl;
 
-                WorldCoordinate( obj_pos, center.getLatitudeDeg(), center.getLongitudeDeg(), parent->getElevation()+8+dx, -(heading) );
+                WorldCoordinate( obj_pos, center.getLatitudeDeg(), center.getLongitudeDeg(), elevationMean + 0.5, -(heading), slope );
+;
 
                 obj_trans->setMatrix( obj_pos );
                 //osg::Vec3 center(0, 0, 0)
@@ -1304,7 +1508,45 @@ void FGStartupController::render(bool visible)
                     osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
                     obj_trans->setDataVariance(osg::Object::STATIC);
                     FGTaxiSegment *segment  = parent->getGroundNetwork()->findSegment(k);
-                    WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), parent->getElevation()+8+dx, -(segment->getHeading()) );
+
+                    double elevationStart = segment->getStart()->getElevation();
+                    double elevationEnd   = segment->getEnd  ()->getElevation();
+                    if (elevationStart == 0) {
+                        SGGeod center2 = segment->getStart()->getGeod();
+                        center2.setElevationM(SG_MAX_ELEVATION_M);
+                        if (local_scenery->get_elevation_m( center2, elevationStart, NULL )) {
+                            elevation_feet = elevationStart * SG_METER_TO_FEET + 0.5;
+                            //elevation_meters += 0.5;
+                        }
+                        else { 
+                            elevationStart = parent->getElevation()+8+dx;
+                        }
+                        segment->getStart()->setElevation(elevationStart);
+                    }
+                    if (elevationEnd == 0) {
+                        SGGeod center2 = segment->getEnd()->getGeod();
+                        center2.setElevationM(SG_MAX_ELEVATION_M);
+                        if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
+                            elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
+                            //elevation_meters += 0.5;
+                        }
+                        else { 
+                            elevationEnd = parent->getElevation()+8+dx;
+                        }
+                        segment->getEnd()->setElevation(elevationEnd);
+                    }
+                    double elevationMean  = (elevationStart + elevationEnd) / 2.0;
+                    double elevDiff       = elevationEnd - elevationStart;
+                    double length         = segment->getLength();
+                    double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
+                
+                    //cerr << "2. Using mean elevation : " << elevationMean << " and " << slope << endl;
+
+
+                    WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), elevationMean + 0.5, -(segment->getHeading()), slope );
+
+                    //WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), parent->getElevation()+8+dx, -(segment->getHeading()) );
 
                     obj_trans->setMatrix( obj_pos );
                     //osg::Vec3 center(0, 0, 0)