| 1 | // Extracted from trafficrecord.cxx - Implementation of AIModels ATC code. |
| 2 | // |
| 3 | // Written by Durk Talsma, started September 2006. |
| 4 | // |
| 5 | // Copyright (C) 2006 Durk Talsma. |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | // $Id$ |
| 22 | |
| 23 | #include <config.h> |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include <cstdio> |
| 27 | #include <random> |
| 28 | |
| 29 | #include <osg/Geode> |
| 30 | #include <osg/Geometry> |
| 31 | #include <osg/MatrixTransform> |
| 32 | #include <osg/Shape> |
| 33 | |
| 34 | #include <simgear/scene/material/EffectGeode.hxx> |
| 35 | #include <simgear/scene/material/matlib.hxx> |
| 36 | #include <simgear/scene/material/mat.hxx> |
| 37 | #include <simgear/scene/util/OsgMath.hxx> |
| 38 | #include <simgear/timing/sg_time.hxx> |
| 39 | |
| 40 | #include <Scenery/scenery.hxx> |
| 41 | |
| 42 | #include "trafficcontrol.hxx" |
| 43 | #include "atc_mgr.hxx" |
| 44 | #include <AIModel/AIAircraft.hxx> |
| 45 | #include <AIModel/AIFlightPlan.hxx> |
| 46 | #include <AIModel/performancedata.hxx> |
| 47 | #include <Traffic/TrafficMgr.hxx> |
| 48 | #include <Airports/groundnetwork.hxx> |
| 49 | #include <Airports/dynamics.hxx> |
| 50 | #include <Airports/airport.hxx> |
| 51 | #include <Radio/radio.hxx> |
| 52 | #include <signal.h> |
| 53 | |
| 54 | #include <ATC/atc_mgr.hxx> |
| 55 | #include <ATC/trafficcontrol.hxx> |
| 56 | #include <ATC/ATCController.hxx> |
| 57 | #include <ATC/ApproachController.hxx> |
| 58 | |
| 59 | using std::sort; |
| 60 | using std::string; |
| 61 | |
| 62 | /*************************************************************************** |
| 63 | * class FGApproachController |
| 64 | * subclass of FGATCController |
| 65 | **************************************************************************/ |
| 66 | |
| 67 | FGApproachController::FGApproachController(FGAirportDynamics *par): |
| 68 | FGATCController() |
| 69 | { |
| 70 | parent = par; |
| 71 | } |
| 72 | |
| 73 | FGApproachController::~FGApproachController() |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void FGApproachController::announcePosition(int id, |
| 79 | FGAIFlightPlan * intendedRoute, |
| 80 | int currentPosition, |
| 81 | double lat, double lon, |
| 82 | double heading, double speed, |
| 83 | double alt, double radius, |
| 84 | int leg, FGAIAircraft * ref) |
| 85 | { |
| 86 | init(); |
| 87 | |
| 88 | // Search activeTraffic for a record matching our id |
| 89 | TrafficVectorIterator i = FGATCController::searchActiveTraffic(id); |
| 90 | |
| 91 | // Add a new TrafficRecord if no one exsists for this aircraft. |
| 92 | if (i == activeTraffic.end() || activeTraffic.empty()) { |
| 93 | FGTrafficRecord rec; |
| 94 | rec.setId(id); |
| 95 | |
| 96 | rec.setPositionAndHeading(lat, lon, heading, speed, alt); |
| 97 | rec.setRunway(intendedRoute->getRunway()); |
| 98 | rec.setLeg(leg); |
| 99 | rec.setCallsign(ref->getCallSign()); |
| 100 | rec.setAircraft(ref); |
| 101 | rec.setPlannedArrivalTime(intendedRoute->getArrivalTime()); |
| 102 | activeTraffic.push_back(rec); |
| 103 | } else { |
| 104 | i->setPositionAndHeading(lat, lon, heading, speed, alt); |
| 105 | i->setPlannedArrivalTime(intendedRoute->getArrivalTime()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void FGApproachController::updateAircraftInformation(int id, SGGeod geod, |
| 110 | double heading, double speed, double alt, |
| 111 | double dt) |
| 112 | { |
| 113 | time_t now = globals->get_time_params()->get_cur_time(); |
| 114 | // Search activeTraffic for a record matching our id |
| 115 | TrafficVectorIterator i = FGATCController::searchActiveTraffic(id); |
| 116 | TrafficVectorIterator current; |
| 117 | |
| 118 | // update position of the current aircraft |
| 119 | if (i == activeTraffic.end() || activeTraffic.empty()) { |
| 120 | SG_LOG(SG_ATC, SG_ALERT, |
| 121 | "FGApproachController updating aircraft without traffic record at " << SG_ORIGIN); |
| 122 | } else { |
| 123 | i->setPositionAndHeading(geod.getLatitudeDeg(), geod.getLongitudeDeg(), heading, speed, alt); |
| 124 | current = i; |
| 125 | if(current->getAircraft()) { |
| 126 | //FIXME No call to aircraft! -> set instruction |
| 127 | time_t time_diff = |
| 128 | current->getAircraft()-> |
| 129 | checkForArrivalTime(string("final001")); |
| 130 | if (time_diff != 0) { |
| 131 | SG_LOG(SG_ATC, SG_BULK, current->getCallsign() << "|ApproachController: checking for speed " << time_diff); |
| 132 | } |
| 133 | if (time_diff > 15) { |
| 134 | current->setSpeedAdjustment(current->getAircraft()-> |
| 135 | getPerformance()->vDescent() * |
| 136 | 1.35); |
| 137 | } else if (time_diff > 5) { |
| 138 | current->setSpeedAdjustment(current->getAircraft()-> |
| 139 | getPerformance()->vDescent() * |
| 140 | 1.2); |
| 141 | } else if (time_diff < -15) { |
| 142 | current->setSpeedAdjustment(current->getAircraft()-> |
| 143 | getPerformance()->vDescent() * |
| 144 | 0.65); |
| 145 | } else if (time_diff < -5) { |
| 146 | current->setSpeedAdjustment(current->getAircraft()-> |
| 147 | getPerformance()->vDescent() * |
| 148 | 0.8); |
| 149 | } else { |
| 150 | current->clearSpeedAdjustment(); |
| 151 | } |
| 152 | if ((now - lastTransmission) > 15) { |
| 153 | available = true; |
| 154 | } |
| 155 | //Start of our status runimplicit "announce arrival" |
| 156 | if (checkTransmissionState(ATCMessageState::NORMAL, ATCMessageState::NORMAL, current, now, MSG_ARRIVAL, ATC_AIR_TO_GROUND)) { |
| 157 | current->setRunwaySlot(getRunway(current->getRunway())->requestTimeSlot(current->getPlannedArrivalTime())); |
| 158 | current->setState(ATCMessageState::ACK_ARRIVAL); |
| 159 | } |
| 160 | if (checkTransmissionState(ATCMessageState::ACK_ARRIVAL, ATCMessageState::ACK_ARRIVAL, current, now, MSG_ACKNOWLEDGE_ARRIVAL, ATC_GROUND_TO_AIR)) { |
| 161 | if (current->getRunwaySlot() > current->getPlannedArrivalTime()) { |
| 162 | current->setState(ATCMessageState::HOLD); |
| 163 | } else { |
| 164 | current->setState(ATCMessageState::CLEARED_TO_LAND); |
| 165 | } |
| 166 | } |
| 167 | if (checkTransmissionState(ATCMessageState::HOLD, ATCMessageState::HOLD, current, now, MSG_ACKNOWLEDGE_HOLD, ATC_AIR_TO_GROUND)) { |
| 168 | current->setState(ATCMessageState::ACK_HOLD); |
| 169 | } |
| 170 | if (checkTransmissionState(ATCMessageState::CLEARED_TO_LAND, ATCMessageState::CLEARED_TO_LAND, current, now, MSG_CLEARED_TO_LAND, ATC_GROUND_TO_AIR)) { |
| 171 | current->setState(ATCMessageState::ACK_CLEARED_TO_LAND); |
| 172 | } |
| 173 | if (checkTransmissionState(ATCMessageState::ACK_CLEARED_TO_LAND, ATCMessageState::ACK_CLEARED_TO_LAND, current, now, MSG_ACKNOWLEDGE_CLEARED_TO_LAND, ATC_AIR_TO_GROUND)) { |
| 174 | current->setState(ATCMessageState::SWITCH_GROUND_TOWER); |
| 175 | } |
| 176 | if (checkTransmissionState(ATCMessageState::SWITCH_GROUND_TOWER, ATCMessageState::SWITCH_GROUND_TOWER, current, now, MSG_SWITCH_TOWER_FREQUENCY, ATC_GROUND_TO_AIR)) { |
| 177 | } |
| 178 | if (checkTransmissionState(ATCMessageState::ACK_SWITCH_GROUND_TOWER, ATCMessageState::ACK_SWITCH_GROUND_TOWER, current, now, MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY, ATC_AIR_TO_GROUND)) { |
| 179 | current->setState(ATCMessageState::LANDING_TAXI); |
| 180 | } |
| 181 | } |
| 182 | //current->setSpeedAdjustment(current->getAircraft()->getPerformance()->vDescent() + time_diff); |
| 183 | } |
| 184 | setDt(getDt() + dt); |
| 185 | } |
| 186 | |
| 187 | /** Periodically check for and remove dead traffic records */ |
| 188 | void FGApproachController::update(double dt) |
| 189 | { |
| 190 | FGATCController::eraseDeadTraffic(); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | |
| 195 | ActiveRunway *FGApproachController::getRunway(const string& name) |
| 196 | { |
| 197 | ActiveRunwayVecIterator rwy = activeRunways.begin(); |
| 198 | if (activeRunways.size()) { |
| 199 | while (rwy != activeRunways.end()) { |
| 200 | if (rwy->getRunwayName() == name) { |
| 201 | break; |
| 202 | } |
| 203 | ++rwy; |
| 204 | } |
| 205 | } |
| 206 | if (rwy == activeRunways.end()) { |
| 207 | ActiveRunway aRwy(name, 0); |
| 208 | activeRunways.push_back(aRwy); |
| 209 | rwy = activeRunways.end() - 1; |
| 210 | } |
| 211 | return &(*rwy); |
| 212 | } |
| 213 | |
| 214 | void FGApproachController::render(bool visible) { |
| 215 | // Must be BULK in order to prevent it being called each frame |
| 216 | SG_LOG(SG_ATC, SG_BULK, "FGApproachController::render function not yet implemented"); |
| 217 | } |
| 218 | |
| 219 | string FGApproachController::getName() { |
| 220 | return string(parent->parent()->getName() + "-approach"); |
| 221 | } |
| 222 | |
| 223 | int FGApproachController::getFrequency() { |
| 224 | int groundFreq = parent->getApproachFrequency(2); |
| 225 | int towerFreq = parent->getTowerFrequency(2); |
| 226 | return groundFreq>0?groundFreq:towerFreq; |
| 227 | } |