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>
32 #include <Environment/environment_mgr.hxx>
33 #include <Environment/environment.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/props/props.hxx>
36 #include <simgear/structure/subsystem_mgr.hxx>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/route/waypoint.hxx>
39 #include <Main/globals.hxx>
40 #include <Main/fg_props.hxx>
41 #include <Airports/runways.hxx>
49 SG_USING_STD(random_shuffle);
51 #include "dynamics.hxx"
53 /********** FGAirport Dynamics *********************************************/
55 FGAirportDynamics::FGAirportDynamics(double lat, double lon, double elev, string id) :
62 for (int i = 0; i < 10; i++)
64 avWindHeading [i] = 0;
70 // Note that the ground network should also be copied
71 FGAirportDynamics::FGAirportDynamics(const FGAirportDynamics& other)
73 for (FGParkingVecConstIterator ip= other.parkings.begin(); ip != other.parkings.end(); ip++)
74 parkings.push_back(*(ip));
75 rwyPrefs = other.rwyPrefs;
76 lastUpdate = other.lastUpdate;
78 stringVecConstIterator il;
79 for (il = other.landing.begin(); il != other.landing.end(); il++)
80 landing.push_back(*il);
81 for (il = other.takeoff.begin(); il != other.takeoff.end(); il++)
82 takeoff.push_back(*il);
83 lastUpdate = other.lastUpdate;
84 for (int i = 0; i < 10; i++)
86 avWindHeading [i] = other.avWindHeading[i];
87 avWindSpeed [i] = other.avWindSpeed [i];
92 FGAirportDynamics::~FGAirportDynamics()
98 // Initialization required after XMLRead
99 void FGAirportDynamics::init()
101 // This may seem a bit weird to first randomly shuffle the parkings
102 // and then sort them again. However, parkings are sorted here by ascending
103 // radius. Since many parkings have similar radii, with each radius class they will
104 // still be allocated relatively systematically. Randomizing prior to sorting will
105 // prevent any initial orderings to be destroyed, leading (hopefully) to a more
106 // naturalistic gate assignment.
107 random_shuffle(parkings.begin(), parkings.end());
108 sort(parkings.begin(), parkings.end());
109 // add the gate positions to the ground network.
110 groundNetwork.addNodes(&parkings);
111 groundNetwork.init();
114 bool FGAirportDynamics::getAvailableParking(double *lat, double *lon, double *heading, int *gateId, double rad, const string &flType, const string &acType, const string &airline)
117 bool available = false;
120 FGParkingVecIterator i;
121 // if (flType == "cargo")
123 // gateType = "RAMP_CARGO";
125 // else if (flType == "ga")
127 // gateType = "RAMP_GA";
129 // else gateType = "GATE";
131 if (parkings.begin() == parkings.end())
133 //cerr << "Could not find parking spot at " << _id << endl;
141 // First try finding a parking with a designated airline code
142 for (i = parkings.begin(); !(i == parkings.end() || found); i++)
144 //cerr << "Gate Id: " << i->getIndex()
145 // << " Type : " << i->getType()
146 // << " Codes : " << i->getCodes()
147 // << " Radius: " << i->getRadius()
148 // << " Name : " << i->getName()
149 // << " Available: " << i->isAvailable() << endl;
151 // Taken by another aircraft
152 if (!(i->isAvailable()))
157 // No airline codes, so skip
158 if (i->getCodes().empty())
163 else // Airline code doesn't match
164 if (i->getCodes().find(airline, 0) == string::npos)
169 // Type doesn't match
170 if (i->getType() != flType)
176 if (i->getRadius() < rad)
184 *lat = i->getLatitude ();
185 *lon = i->getLongitude();
186 *heading = i->getHeading ();
187 *gateId = i->getIndex ();
188 i->setAvailable(false);
192 // then try again for those without codes.
193 for (i = parkings.begin(); !(i == parkings.end() || found); i++)
196 if (!(i->isAvailable()))
201 if (!(i->getCodes().empty()))
203 if ((i->getCodes().find(airline,0) == string::npos))
209 if (i->getType() != flType)
215 if (i->getRadius() < rad)
223 *lat = i->getLatitude ();
224 *lon = i->getLongitude();
225 *heading = i->getHeading ();
226 *gateId = i->getIndex ();
227 i->setAvailable(false);
231 // And finally once more if that didn't work. Now ignore the airline codes, as a last resort
232 for (i = parkings.begin(); !(i == parkings.end() || found); i++)
235 if (!(i->isAvailable()))
240 if (i->getType() != flType)
246 if (i->getRadius() < rad)
254 *lat = i->getLatitude ();
255 *lon = i->getLongitude();
256 *heading = i->getHeading ();
257 *gateId = i->getIndex ();
258 i->setAvailable(false);
265 //cerr << "Traffic overflow at" << _id
266 // << ". flType = " << flType
267 // << ". airline = " << airline
268 // << " Radius = " <<rad
279 void FGAirportDynamics::getParking (int id, double *lat, double* lon, double *heading)
289 FGParkingVecIterator i = parkings.begin();
290 for (i = parkings.begin(); i != parkings.end(); i++)
292 if (id == i->getIndex())
294 *lat = i->getLatitude();
295 *lon = i->getLongitude();
296 *heading = i->getHeading();
302 FGParking *FGAirportDynamics::getParking(int i)
304 if (i < (int)parkings.size())
305 return &(parkings[i]);
309 string FGAirportDynamics::getParkingName(int i)
311 if (i < (int)parkings.size() && i >= 0)
312 return (parkings[i].getName());
314 return string("overflow");
316 void FGAirportDynamics::releaseParking(int id)
321 FGParkingVecIterator i = parkings.begin();
322 for (i = parkings.begin(); i != parkings.end(); i++)
324 if (id == i->getIndex())
326 i -> setAvailable(true);
332 void FGAirportDynamics::startXML () {
333 //cout << "Start XML" << endl;
336 void FGAirportDynamics::endXML () {
337 //cout << "End XML" << endl;
340 void FGAirportDynamics::startElement (const char * name, const XMLAttributes &atts) {
341 // const char *attval;
344 FGTaxiSegment taxiSegment;
346 taxiSegment.setIndex(index);
347 //cout << "Start element " << name << endl;
354 if (name == string("Parking"))
356 for (int i = 0; i < atts.size(); i++)
358 //cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
359 attname = atts.getName(i);
360 if (attname == string("index"))
361 park.setIndex(atoi(atts.getValue(i)));
362 else if (attname == string("type"))
363 park.setType(atts.getValue(i));
364 else if (attname == string("name"))
365 gateName = atts.getValue(i);
366 else if (attname == string("number"))
367 gateNumber = atts.getValue(i);
368 else if (attname == string("lat"))
369 park.setLatitude(atts.getValue(i));
370 else if (attname == string("lon"))
371 park.setLongitude(atts.getValue(i));
372 else if (attname == string("heading"))
373 park.setHeading(atof(atts.getValue(i)));
374 else if (attname == string("radius")) {
375 string radius = atts.getValue(i);
376 if (radius.find("M") != string::npos)
377 radius = radius.substr(0, radius.find("M",0));
378 //cerr << "Radius " << radius <<endl;
379 park.setRadius(atof(radius.c_str()));
381 else if (attname == string("airlineCodes"))
382 park.setCodes(atts.getValue(i));
384 park.setName((gateName+gateNumber));
385 parkings.push_back(park);
387 if (name == string("node"))
389 for (int i = 0; i < atts.size() ; i++)
391 attname = atts.getName(i);
392 if (attname == string("index"))
393 taxiNode.setIndex(atoi(atts.getValue(i)));
394 if (attname == string("lat"))
395 taxiNode.setLatitude(atts.getValue(i));
396 if (attname == string("lon"))
397 taxiNode.setLongitude(atts.getValue(i));
399 groundNetwork.addNode(taxiNode);
401 if (name == string("arc"))
403 taxiSegment.setIndex(++index);
404 for (int i = 0; i < atts.size() ; i++)
406 attname = atts.getName(i);
407 if (attname == string("begin"))
408 taxiSegment.setStartNodeRef(atoi(atts.getValue(i)));
409 if (attname == string("end"))
410 taxiSegment.setEndNodeRef(atoi(atts.getValue(i)));
412 groundNetwork.addSegment(taxiSegment);
414 // sort by radius, in asending order, so that smaller gates are first in the list
417 void FGAirportDynamics::endElement (const char * name) {
418 //cout << "End element " << name << endl;
422 void FGAirportDynamics::data (const char * s, int len) {
423 string token = string(s,len);
424 //cout << "Character data " << string(s,len) << endl;
425 //if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
428 //value = string("");
431 void FGAirportDynamics::pi (const char * target, const char * data) {
432 //cout << "Processing instruction " << target << ' ' << data << endl;
435 void FGAirportDynamics::warning (const char * message, int line, int column) {
436 SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
439 void FGAirportDynamics::error (const char * message, int line, int column) {
440 SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');
443 void FGAirportDynamics::setRwyUse(const FGRunwayPreference& ref)
446 //cerr << "Exiting due to not implemented yet" << endl;
449 void FGAirportDynamics::getActiveRunway(const string &trafficType, int action, string &runway)
458 if (!(rwyPrefs.available()))
460 runway = chooseRunwayFallback();
461 return; // generic fall back goes here
465 RunwayGroup *currRunwayGroup = 0;
466 int nrActiveRunways = 0;
467 time_t dayStart = fgGetLong("/sim/time/utc/day-seconds");
468 if ((abs(dayStart - lastUpdate) > 600) || trafficType != prevTrafficType)
472 lastUpdate = dayStart;
473 prevTrafficType = trafficType;
476 stationweather = ((FGEnvironmentMgr *) globals->get_subsystem("environment"))
477 ->getEnvironment(getLatitude(),
481 windSpeed = stationweather.get_wind_speed_kt();
482 windHeading = stationweather.get_wind_from_heading_deg();
483 // double averageWindSpeed = 0;
484 // double averageWindHeading = 0;
485 // double cosHeading = 0;
486 // double sinHeading = 0;
487 // // Initialize at the beginning of the next day or startup
488 // if ((lastUpdate == 0) || (dayStart < lastUpdate))
490 // for (int i = 0; i < 10; i++)
492 // avWindHeading [i] = windHeading;
493 // avWindSpeed [i] = windSpeed;
498 // if (windSpeed != avWindSpeed[9]) // update if new metar data
500 // // shift the running average
501 // for (int i = 0; i < 9 ; i++)
503 // avWindHeading[i] = avWindHeading[i+1];
504 // avWindSpeed [i] = avWindSpeed [i+1];
507 // avWindHeading[9] = windHeading;
508 // avWindSpeed [9] = windSpeed;
511 // for (int i = 0; i < 10; i++)
513 // averageWindSpeed += avWindSpeed [i];
514 // //averageWindHeading += avWindHeading [i];
515 // cosHeading += cos(avWindHeading[i] * SG_DEGREES_TO_RADIANS);
516 // sinHeading += sin(avWindHeading[i] * SG_DEGREES_TO_RADIANS);
518 // averageWindSpeed /= 10;
519 // //averageWindHeading /= 10;
522 // averageWindHeading = atan2(sinHeading, cosHeading) *SG_RADIANS_TO_DEGREES;
523 // if (averageWindHeading < 0)
524 // averageWindHeading += 360.0;
525 // //cerr << "Wind Heading " << windHeading << " average " << averageWindHeading << endl;
526 // //cerr << "Wind Speed " << windSpeed << " average " << averageWindSpeed << endl;
527 // lastUpdate = dayStart;
528 // //if (wind_speed == 0) {
529 // // wind_heading = 270; This forces West-facing rwys to be used in no-wind situations
530 // // which is consistent with Flightgear's initial setup.
533 //string rwy_no = globals->get_runways()->search(apt->getId(), int(wind_heading));
535 //cerr << "finding active Runway for" << _id << endl;
536 //cerr << "Nr of seconds since day start << " << dayStart << endl;
538 ScheduleTime *currSched;
539 //cerr << "A"<< endl;
540 currSched = rwyPrefs.getSchedule(trafficType.c_str());
543 //cerr << "B"<< endl;
544 scheduleName = currSched->getName(dayStart);
545 maxTail = currSched->getTailWind ();
546 maxCross = currSched->getCrossWind ();
547 //cerr << "SChedule anme = " << scheduleName << endl;
548 if (scheduleName.empty())
550 //cerr << "C"<< endl;
551 currRunwayGroup = rwyPrefs.getGroup(scheduleName);
552 //cerr << "D"<< endl;
553 if (!(currRunwayGroup))
555 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
556 //cerr << "Nr of Active Runways = " << nrActiveRunways << endl;
559 currRunwayGroup->setActive(_id,
566 // Note that I SHOULD keep three lists in memory, one for
567 // general aviation, one for commercial and one for military
569 currentlyActive.clear();
570 nrActiveRunways = currRunwayGroup->getNrActiveRunways();
571 for (int i = 0; i < nrActiveRunways; i++)
573 type = "unknown"; // initialize to something other than landing or takeoff
574 currRunwayGroup->getActive(i, name, type);
575 if (type == "landing")
577 landing.push_back(name);
578 currentlyActive.push_back(name);
579 //cerr << "Landing " << name << endl;
581 if (type == "takeoff")
583 takeoff.push_back(name);
584 currentlyActive.push_back(name);
585 //cerr << "takeoff " << name << endl;
589 if (action == 1) // takeoff
591 int nr = takeoff.size();
594 // Note that the randomization below, is just a placeholder to choose between
595 // multiple active runways for this action. This should be
596 // under ATC control.
597 runway = takeoff[(rand() % nr)];
601 runway = chooseRunwayFallback();
604 if (action == 2) // landing
606 int nr = landing.size();
609 runway = landing[(rand() % nr)];
613 runway = chooseRunwayFallback();
617 //runway = globals->get_runways()->search(_id, int(windHeading));
618 //cerr << "Seleceted runway: " << runway << endl;
622 string FGAirportDynamics::chooseRunwayFallback()
625 stationweather = ((FGEnvironmentMgr *) globals->get_subsystem("environment"))
626 ->getEnvironment(getLatitude(),
630 double windSpeed = stationweather.get_wind_speed_kt();
631 double windHeading = stationweather.get_wind_from_heading_deg();
632 if (windSpeed == 0) {
633 windHeading = 270; // This forces West-facing rwys to be used in no-wind situations
634 //which is consistent with Flightgear's initial setup.
637 return globals->get_runways()->search(_id, int(windHeading));