1 // dynamics.cxx - Code to manage the higher order airport ground activities
2 // Written by Durk Talsma, started December 2004.
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.
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.
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.
27 #include <simgear/compiler.h>
31 #include <Environment/environment_mgr.hxx>
32 #include <Environment/environment.hxx>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/props/props.hxx>
35 #include <simgear/structure/subsystem_mgr.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/route/waypoint.hxx>
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
40 #include <Airports/runways.hxx>
48 using std::random_shuffle;
51 #include "dynamics.hxx"
53 FGAirportDynamics::FGAirportDynamics(FGAirport * ap):
54 _ap(ap), rwyPrefs(ap), SIDs(ap)
58 // For testing only. This needs to be refined when we move ATIS functionality over.
59 atisInformation = "Sierra";
62 // Note that the ground network should also be copied
64 FGAirportDynamics(const FGAirportDynamics & other):rwyPrefs(other.
68 for (FGParkingVecConstIterator ip = other.parkings.begin();
69 ip != other.parkings.end(); ip++)
70 parkings.push_back(*(ip));
71 // rwyPrefs = other.rwyPrefs;
72 lastUpdate = other.lastUpdate;
74 stringVecConstIterator il;
75 for (il = other.landing.begin(); il != other.landing.end(); il++)
76 landing.push_back(*il);
77 for (il = other.takeoff.begin(); il != other.takeoff.end(); il++)
78 takeoff.push_back(*il);
79 lastUpdate = other.lastUpdate;
80 atisInformation = other.atisInformation;
84 FGAirportDynamics::~FGAirportDynamics()
89 // Initialization required after XMLRead
90 void FGAirportDynamics::init()
92 // This may seem a bit weird to first randomly shuffle the parkings
93 // and then sort them again. However, parkings are sorted here by ascending
94 // radius. Since many parkings have similar radii, with each radius class they will
95 // still be allocated relatively systematically. Randomizing prior to sorting will
96 // prevent any initial orderings to be destroyed, leading (hopefully) to a more
97 // naturalistic gate assignment.
98 random_shuffle(parkings.begin(), parkings.end());
99 sort(parkings.begin(), parkings.end());
100 // add the gate positions to the ground network.
101 groundNetwork.addNodes(&parkings);
102 groundNetwork.init();
103 groundNetwork.setTowerController(&towerController);
104 groundNetwork.setParent(_ap);
107 bool FGAirportDynamics::getAvailableParking(double *lat, double *lon,
108 double *heading, int *gateId,
110 const string & flType,
111 const string & acType,
112 const string & airline)
115 bool available = false;
118 FGParkingVecIterator i;
119 if (parkings.begin() == parkings.end()) {
120 //cerr << "Could not find parking spot at " << _ap->getId() << endl;
121 *lat = _ap->getLatitude();
122 *lon = _ap->getLongitude();
126 // First try finding a parking with a designated airline code
127 for (i = parkings.begin(); !(i == parkings.end() || found); i++) {
129 // Taken by another aircraft
130 if (!(i->isAvailable())) {
134 // No airline codes, so skip
135 if (i->getCodes().empty()) {
138 } else { // Airline code doesn't match
139 //cerr << "Code = " << airline << ": Codes " << i->getCodes();
140 if (i->getCodes().find(airline, 0) == string::npos) {
142 //cerr << "Unavailable" << endl;
145 //cerr << "Available" << endl;
148 // Type doesn't match
149 if (i->getType() != flType) {
154 if (i->getRadius() < rad) {
160 *lat = i->getLatitude();
161 *lon = i->getLongitude();
162 *heading = i->getHeading();
163 *gateId = i->getIndex();
164 i->setAvailable(false);
168 // then try again for those without codes.
169 for (i = parkings.begin(); !(i == parkings.end() || found); i++) {
171 if (!(i->isAvailable())) {
175 if (!(i->getCodes().empty())) {
176 if ((i->getCodes().find(airline, 0) == string::npos)) {
181 if (i->getType() != flType) {
186 if (i->getRadius() < rad) {
192 *lat = i->getLatitude();
193 *lon = i->getLongitude();
194 *heading = i->getHeading();
195 *gateId = i->getIndex();
196 i->setAvailable(false);
200 // And finally once more if that didn't work. Now ignore the airline codes, as a last resort
201 for (i = parkings.begin(); !(i == parkings.end() || found); i++) {
203 if (!(i->isAvailable())) {
207 if (i->getType() != flType) {
212 if (i->getRadius() < rad) {
218 *lat = i->getLatitude();
219 *lon = i->getLongitude();
220 *heading = i->getHeading();
221 *gateId = i->getIndex();
222 i->setAvailable(false);
228 //cerr << "Traffic overflow at" << _ap->getId()
229 // << ". flType = " << flType
230 // << ". airline = " << airline
231 // << " Radius = " <<rad
233 *lat = _ap->getLatitude();
234 *lon = _ap->getLongitude();
242 void FGAirportDynamics::getParking(int id, double *lat, double *lon,
246 *lat = _ap->getLatitude();
247 *lon = _ap->getLongitude();
250 FGParkingVecIterator i = parkings.begin();
251 for (i = parkings.begin(); i != parkings.end(); i++) {
252 if (id == i->getIndex()) {
253 *lat = i->getLatitude();
254 *lon = i->getLongitude();
255 *heading = i->getHeading();
261 FGParking *FGAirportDynamics::getParking(int id)
263 FGParkingVecIterator i = parkings.begin();
264 for (i = parkings.begin(); i != parkings.end(); i++) {
265 if (id == i->getIndex()) {
272 string FGAirportDynamics::getParkingName(int id)
274 FGParkingVecIterator i = parkings.begin();
275 for (i = parkings.begin(); i != parkings.end(); i++) {
276 if (id == i->getIndex()) {
281 return string("overflow");
284 void FGAirportDynamics::releaseParking(int id)
288 FGParkingVecIterator i = parkings.begin();
289 for (i = parkings.begin(); i != parkings.end(); i++) {
290 if (id == i->getIndex()) {
291 i->setAvailable(true);
297 void FGAirportDynamics::setRwyUse(const FGRunwayPreference & ref)
300 //cerr << "Exiting due to not implemented yet" << endl;
304 bool FGAirportDynamics::innerGetActiveRunway(const string & trafficType,
305 int action, string & runway,
315 if (!rwyPrefs.available()) {
319 RunwayGroup *currRunwayGroup = 0;
320 int nrActiveRunways = 0;
321 time_t dayStart = fgGetLong("/sim/time/utc/day-seconds");
322 if ((abs((long) (dayStart - lastUpdate)) > 600)
323 || trafficType != prevTrafficType) {
326 lastUpdate = dayStart;
327 prevTrafficType = trafficType;
331 ((FGEnvironmentMgr *) globals->get_subsystem("environment"))
332 ->getEnvironment(getLatitude(), getLongitude(),
335 windSpeed = fgGetInt("/environment/metar/base-wind-speed-kt"); //stationweather.get_wind_speed_kt();
336 windHeading = fgGetInt("/environment/metar/base-wind-dir-deg");
337 //stationweather.get_wind_from_heading_deg();
339 //cerr << "finding active Runway for : " << _ap->getId() << endl;
340 //cerr << "Wind Heading : " << windHeading << endl;
341 //cerr << "Wind Speed : " << windSpeed << endl;
343 //cerr << "Nr of seconds since day start << " << dayStart << endl;
345 ScheduleTime *currSched;
346 //cerr << "A"<< endl;
347 currSched = rwyPrefs.getSchedule(trafficType.c_str());
350 //cerr << "B"<< endl;
351 scheduleName = currSched->getName(dayStart);
352 maxTail = currSched->getTailWind();
353 maxCross = currSched->getCrossWind();
354 //cerr << "Current Schedule = : " << scheduleName << endl;
355 if (scheduleName.empty())
357 //cerr << "C"<< endl;
358 currRunwayGroup = rwyPrefs.getGroup(scheduleName);
359 //cerr << "D"<< endl;
360 if (!(currRunwayGroup))
362 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
364 // Keep a history of the currently active runways, to ensure
365 // that an already established selection of runways will not
366 // be overridden once a more preferred selection becomes
367 // available as that can lead to random runway swapping.
368 if (trafficType == "com") {
369 currentlyActive = &comActive;
370 } else if (trafficType == "gen") {
371 currentlyActive = &genActive;
372 } else if (trafficType == "mil") {
373 currentlyActive = &milActive;
374 } else if (trafficType == "ul") {
375 currentlyActive = &ulActive;
378 //cerr << "Durrently active selection for " << trafficType << ": ";
379 for (stringVecIterator it = currentlyActive->begin();
380 it != currentlyActive->end(); it++) {
381 //cerr << (*it) << " ";
385 currRunwayGroup->setActive(_ap,
388 maxTail, maxCross, currentlyActive);
390 // Note that I SHOULD keep multiple lists in memory, one for
391 // general aviation, one for commercial and one for military
393 currentlyActive->clear();
394 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
395 //cerr << "Choosing runway for " << trafficType << endl;
396 for (int i = 0; i < nrActiveRunways; i++) {
397 type = "unknown"; // initialize to something other than landing or takeoff
398 currRunwayGroup->getActive(i, name, type);
399 if (type == "landing") {
400 landing.push_back(name);
401 currentlyActive->push_back(name);
402 //cerr << "Landing " << name << endl;
404 if (type == "takeoff") {
405 takeoff.push_back(name);
406 currentlyActive->push_back(name);
407 //cerr << "takeoff " << name << endl;
413 if (action == 1) // takeoff
415 int nr = takeoff.size();
417 // Note that the randomization below, is just a placeholder to choose between
418 // multiple active runways for this action. This should be
419 // under ATC control.
420 runway = chooseRwyByHeading(takeoff, heading);
422 runway = chooseRunwayFallback();
426 if (action == 2) // landing
428 int nr = landing.size();
430 runway = chooseRwyByHeading(landing, heading);
432 runway = chooseRunwayFallback();
439 string FGAirportDynamics::chooseRwyByHeading(stringVec rwys,
442 double bestError = 360.0;
443 double rwyHeading, headingError;
445 for (stringVecIterator i = rwys.begin(); i != rwys.end(); i++) {
446 if (!_ap->hasRunwayWithIdent(*i)) {
447 SG_LOG(SG_ATC, SG_WARN, "chooseRwyByHeading: runway " << *i <<
448 " not found at " << _ap->ident());
452 FGRunway *rwy = _ap->getRunwayByIdent((*i));
453 rwyHeading = rwy->headingDeg();
454 headingError = fabs(heading - rwyHeading);
455 if (headingError > 180)
456 headingError = fabs(headingError - 360);
457 if (headingError < bestError) {
459 bestError = headingError;
462 //cerr << "Using active runway " << runway << " for heading " << heading << endl;
466 void FGAirportDynamics::getActiveRunway(const string & trafficType,
467 int action, string & runway,
470 bool ok = innerGetActiveRunway(trafficType, action, runway, heading);
472 runway = chooseRunwayFallback();
476 string FGAirportDynamics::chooseRunwayFallback()
478 FGRunway *rwy = _ap->getActiveRunwayForUsage();
482 void FGAirportDynamics::addParking(FGParking & park)
484 parkings.push_back(park);
487 double FGAirportDynamics::getLatitude() const
489 return _ap->getLatitude();
492 double FGAirportDynamics::getLongitude() const
494 return _ap->getLongitude();
497 double FGAirportDynamics::getElevation() const
499 return _ap->getElevation();
502 const string & FGAirportDynamics::getId() const
507 // Experimental: Return a different ground frequency depending on the leg of the
508 // Flight. Leg should always have a minimum value of two when this function is called.
509 // Note that in this scheme, the assignment of various frequencies to various ground
510 // operations is completely arbitrary. As such, is a short cut I need to take now,
511 // so that at least I can start working on assigning different frequencies to different
514 int FGAirportDynamics::getGroundFrequency(unsigned leg)
516 //return freqGround.size() ? freqGround[0] : 0; };
519 SG_LOG(SG_ATC, SG_ALERT,
520 "Leg value is smaller than two at " << SG_ORIGIN);
522 if (freqGround.size() == 0) {
525 if ((freqGround.size() > leg - 1) && (leg > 1)) {
526 groundFreq = freqGround[leg - 1];
528 if ((freqGround.size() < leg - 1) && (leg > 1)) {
531 (leg - 1)) ? freqGround[freqGround.size() -
532 1] : freqGround[leg - 2];
534 if ((freqGround.size() >= leg - 1) && (leg > 1)) {
535 groundFreq = freqGround[leg - 2];
540 FGAIFlightPlan *FGAirportDynamics::getSID(string activeRunway,
543 return SIDs.getBest(activeRunway, heading);