1 // trafficrecord.cxx - Implementation of AIModels ATC code.
3 // Written by Durk Talsma, started September 2006.
5 // Copyright (C) 2006 Durk Talsma.
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.
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.
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.
29 #include "trafficcontrol.hxx"
30 #include <AIModel/AIAircraft.hxx>
31 #include <AIModel/AIFlightPlan.hxx>
32 #include <AIModel/performancedata.hxx>
33 #include <AIModel/performancedb.hxx>
34 #include <Traffic/TrafficMgr.hxx>
35 #include <Airports/groundnetwork.hxx>
36 #include <Airports/dynamics.hxx>
37 #include <Airports/simple.hxx>
41 /***************************************************************************
43 **************************************************************************/
44 time_t ActiveRunway::requestTimeSlot(time_t eta)
47 time_t separation = 90;
49 if (estimatedArrivalTimes.size() == 0) {
50 estimatedArrivalTimes.push_back(eta);
53 TimeVectorIterator i = estimatedArrivalTimes.begin();
54 //cerr << "Checking eta slots " << eta << ": " << endl;
55 for (i = estimatedArrivalTimes.begin();
56 i != estimatedArrivalTimes.end(); i++) {
57 //cerr << "Stored time : " << (*i) << endl;
59 i = estimatedArrivalTimes.begin();
60 if ((eta + separation) < (*i)) {
63 //cerr << "Storing at beginning" << endl;
65 while ((i != estimatedArrivalTimes.end()) && (!found)) {
66 TimeVectorIterator j = i + 1;
67 if (j == estimatedArrivalTimes.end()) {
68 if (((*i) + separation) < eta) {
69 //cerr << "Storing at end" << endl;
72 newEta = (*i) + separation;
73 //cerr << "Storing at end + separation" << endl;
76 if ((((*j) - (*i)) > (separation * 2))) { // found a potential slot
77 // now check whether this slow is usable:
78 // 1) eta should fall between the two points
79 // i.e. eta > i AND eta < j
81 //cerr << "Found potential slot after " << (*i) << endl;
82 if (eta > (*i) && (eta < (*j))) {
84 if (eta < ((*i) + separation)) {
85 newEta = (*i) + separation;
86 //cerr << "Using original" << (*i) << " + separation " << endl;
89 //cerr << "Using original after " << (*i) << endl;
91 } else if (eta < (*i)) {
93 newEta = (*i) + separation;
94 //cerr << "Using delayed slot after " << (*i) << endl;
97 if (((*j) - separation) < eta) {
99 if (((*i) + separation) < eta) {
101 cerr << "Using original after " << (*i) << endl;
103 newEta = (*i) + separation;
104 cerr << "Using " << (*i) << " + separation " << endl;
112 //cerr << ". done. New ETA : " << newEta << endl;
114 estimatedArrivalTimes.push_back(newEta);
115 sort(estimatedArrivalTimes.begin(), estimatedArrivalTimes.end());
116 // do some housekeeping : remove any timestamps that are past
117 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
118 TimeVectorIterator i = estimatedArrivalTimes.begin();
119 while (i != estimatedArrivalTimes.end()) {
121 //cerr << "Deleting timestamp " << (*i) << " (now = " << now << "). " << endl;
122 estimatedArrivalTimes.erase(i);
123 i = estimatedArrivalTimes.begin();
131 /***************************************************************************
133 **************************************************************************/
134 FGTrafficRecord::FGTrafficRecord():
135 id(0), waitsForId(0),
140 allowTransmission(true),
141 latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0)
145 void FGTrafficRecord::setPositionAndIntentions(int pos,
146 FGAIFlightPlan * route)
150 if (intentions.size()) {
151 intVecIterator i = intentions.begin();
153 SG_LOG(SG_GENERAL, SG_ALERT,
154 "Error in FGTrafficRecord::setPositionAndIntentions");
155 //cerr << "Pos : " << pos << " Curr " << *(intentions.begin()) << endl;
156 for (intVecIterator i = intentions.begin();
157 i != intentions.end(); i++) {
158 //cerr << (*i) << " ";
164 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
165 int size = route->getNrOfWayPoints();
166 //cerr << "Setting pos" << pos << " ";
167 //cerr << "setting intentions ";
168 for (int i = 0; i < size; i++) {
169 int val = route->getRouteIndex(i);
171 if ((val) && (val != pos)) {
172 intentions.push_back(val);
177 //while (route->next(&legNr, &routeNr)) {
178 //intentions.push_back(routeNr);
180 //route->rewind(currentPos);
185 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord & other)
188 //cerr << "Start check 1" << endl;
189 if (currentPos == other.currentPos) {
190 //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
193 // else if (other.intentions.size())
195 // cerr << "Start check 2" << endl;
196 // intVecIterator i = other.intentions.begin();
197 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
199 // if (i != other.intentions.end()) {
200 // cerr << "Check Position and intentions: current matches other.intentions" << endl;
203 else if (intentions.size()) {
204 //cerr << "Start check 3" << endl;
205 intVecIterator i = intentions.begin();
206 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
207 while (i != intentions.end()) {
208 if ((*i) == other.currentPos) {
213 if (i != intentions.end()) {
214 //cerr << callsign << ": Check Position and intentions: .other.current matches" << other.callsign << "Index = " << (*i) << endl;
218 //cerr << "Done !!" << endl;
222 void FGTrafficRecord::setPositionAndHeading(double lat, double lon,
223 double hdg, double spd,
233 int FGTrafficRecord::crosses(FGGroundNetwork * net,
234 FGTrafficRecord & other)
236 if (checkPositionAndIntentions(other)
237 || (other.checkPositionAndIntentions(*this)))
240 int currentTargetNode = 0, otherTargetNode = 0;
242 currentTargetNode = net->findSegment(currentPos)->getEnd()->getIndex(); // OKAY,...
243 if (other.currentPos > 0)
244 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
245 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
246 return currentTargetNode;
247 if (intentions.size()) {
248 for (i = intentions.begin(); i != intentions.end(); i++) {
250 if ((currentTargetNode ==
251 net->findSegment(*i)->getEnd()->getIndex())) {
252 //cerr << "Current crosses at " << currentTargetNode <<endl;
253 return currentTargetNode;
258 if (other.intentions.size()) {
259 for (i = other.intentions.begin(); i != other.intentions.end();
262 if (otherTargetNode ==
263 net->findSegment(*i)->getEnd()->getIndex()) {
264 //cerr << "Other crosses at " << currentTargetNode <<endl;
265 return otherTargetNode;
270 if (intentions.size() && other.intentions.size()) {
271 for (i = intentions.begin(); i != intentions.end(); i++) {
272 for (j = other.intentions.begin(); j != other.intentions.end();
274 //cerr << "finding segment " << *i << " and " << *j << endl;
275 if (((*i) > 0) && ((*j) > 0)) {
277 net->findSegment(*i)->getEnd()->getIndex();
279 net->findSegment(*j)->getEnd()->getIndex();
280 if (currentTargetNode == otherTargetNode) {
281 //cerr << "Routes will cross at " << currentTargetNode << endl;
282 return currentTargetNode;
291 bool FGTrafficRecord::onRoute(FGGroundNetwork * net,
292 FGTrafficRecord & other)
294 int node = -1, othernode = -1;
296 node = net->findSegment(currentPos)->getEnd()->getIndex();
297 if (other.currentPos > 0)
299 net->findSegment(other.currentPos)->getEnd()->getIndex();
300 if ((node == othernode) && (node != -1))
302 if (other.intentions.size()) {
303 for (intVecIterator i = other.intentions.begin();
304 i != other.intentions.end(); i++) {
306 othernode = net->findSegment(*i)->getEnd()->getIndex();
307 if ((node == othernode) && (node > -1))
312 //if (other.currentPos > 0)
313 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
314 //if (intentions.size())
316 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
320 // node = net->findSegment(*i)->getEnd()->getIndex();
321 // if ((node == othernode) && (node > -1))
330 bool FGTrafficRecord::isOpposing(FGGroundNetwork * net,
331 FGTrafficRecord & other, int node)
333 // Check if current segment is the reverse segment for the other aircraft
335 //cerr << "Current segment " << currentPos << endl;
336 if ((currentPos > 0) && (other.currentPos > 0)) {
337 opp = net->findSegment(currentPos)->opposite();
339 if (opp->getIndex() == other.currentPos)
343 for (intVecIterator i = intentions.begin(); i != intentions.end();
345 if ((opp = net->findSegment(other.currentPos)->opposite())) {
347 if (opp->getIndex() ==
348 net->findSegment(*i)->getIndex()) {
349 if (net->findSegment(*i)->getStart()->getIndex() ==
352 //cerr << "Found the node " << node << endl;
358 if (other.intentions.size()) {
359 for (intVecIterator j = other.intentions.begin();
360 j != other.intentions.end(); j++) {
361 // cerr << "Current segment 1 " << (*i) << endl;
363 if ((opp = net->findSegment(*i)->opposite())) {
364 if (opp->getIndex() ==
365 net->findSegment(*j)->getIndex()) {
366 //cerr << "Nodes " << net->findSegment(*i)->getIndex()
367 // << " and " << net->findSegment(*j)->getIndex()
368 // << " are opposites " << endl;
369 if (net->findSegment(*i)->getStart()->
370 getIndex() == node) {
372 //cerr << "Found the node " << node << endl;
386 void FGTrafficRecord::setSpeedAdjustment(double spd)
388 instruction.setChangeSpeed(true);
389 instruction.setSpeed(spd);
392 void FGTrafficRecord::setHeadingAdjustment(double heading)
394 instruction.setChangeHeading(true);
395 instruction.setHeading(heading);
398 bool FGTrafficRecord::pushBackAllowed()
400 double course, az2, dist;
401 SGGeod curr(SGGeod::fromDegM(getLongitude(),
402 getLatitude(), getAltitude()));
404 double userLatitude = fgGetDouble("/position/latitude-deg");
405 double userLongitude = fgGetDouble("/position/longitude-deg");
406 SGGeod user(SGGeod::fromDeg(userLongitude, userLatitude));
407 SGGeodesy::inverse(curr, user, course, az2, dist);
408 //cerr << "Distance to user : " << dist << endl;
415 /***************************************************************************
418 **************************************************************************/
419 FGATCInstruction::FGATCInstruction()
422 holdPosition = false;
424 changeHeading = false;
425 changeAltitude = false;
426 resolveCircularWait = false;
434 bool FGATCInstruction::hasInstruction()
436 return (holdPattern || holdPosition || changeSpeed || changeHeading
437 || changeAltitude || resolveCircularWait);
440 /***************************************************************************
443 **************************************************************************/
448 FGATCController::FGATCController()
452 lastTransmission = 0;
455 string FGATCController::getGateName(FGAIAircraft * ref)
457 return ref->atGate();
460 void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
463 string sender, receiver;
467 string atisInformation;
475 string transponderCode;
480 sender = rec->getAircraft()->getTrafficRef()->getCallSign();
481 //cerr << "transmitting for: " << sender << "Leg = " << rec->getLeg() << endl;
482 switch (rec->getLeg()) {
485 freqId = rec->getNextFrequency();
487 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
488 getDynamics()->getGroundFrequency(rec->getLeg() + freqId);
490 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
491 getDynamics()->getGroundFrequency(3);
493 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
494 getName() + "-Ground";
496 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
497 getDynamics()->getAtisInformation();
501 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
502 getName() + "-Tower";
505 // Swap sender and receiver value in case of a ground to air transmission
506 if (msgDir == ATC_GROUND_TO_AIR) {
512 case MSG_ANNOUNCE_ENGINE_START:
513 text = sender + ". Ready to Start up";
515 case MSG_REQUEST_ENGINE_START:
517 receiver + ", This is " + sender + ". Position " +
518 getGateName(rec->getAircraft()) + ". Information " +
519 atisInformation + ". " +
520 rec->getAircraft()->getTrafficRef()->getFlightRules() +
522 rec->getAircraft()->getTrafficRef()->getArrivalAirport()->
523 getName() + ". Request start-up";
525 // Acknowledge engine startup permission
526 // Assign departure runway
527 // Assign SID, if necessery (TODO)
528 case MSG_PERMIT_ENGINE_START:
529 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
531 heading = rec->getAircraft()->getTrafficRef()->getCourse();
532 fltType = rec->getAircraft()->getTrafficRef()->getFlightType();
534 rec->getAircraft()->GetFlightPlan()->
535 getRunwayClassFromTrafficType(fltType);
537 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
538 getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
540 rec->getAircraft()->GetFlightPlan()->setRunway(activeRunway);
541 fp = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
542 getDynamics()->getSID(activeRunway, heading);
543 rec->getAircraft()->GetFlightPlan()->setSID(fp);
545 SID = fp->getName() + " departure";
547 SID = "fly runway heading ";
549 //snprintf(buffer, 7, "%3.2f", heading);
550 fltRules = rec->getAircraft()->getTrafficRef()->getFlightRules();
551 transponderCode = genTransponderCode(fltRules);
552 rec->getAircraft()->SetTransponderCode(transponderCode);
554 receiver + ". Start-up approved. " + atisInformation +
555 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
556 transponderCode + ". " +
557 "For push-back and taxi clearance call " + taxiFreqStr + ". " +
558 sender + " control.";
560 case MSG_DENY_ENGINE_START:
561 text = receiver + ". Standby";
563 case MSG_ACKNOWLEDGE_ENGINE_START:
564 fp = rec->getAircraft()->GetFlightPlan()->getSID();
567 rec->getAircraft()->GetFlightPlan()->getSID()->getName() +
570 SID = "fly runway heading ";
572 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
573 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
574 transponderCode = rec->getAircraft()->GetTransponderCode();
576 receiver + ". Start-up approved. " + atisInformation +
577 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
578 transponderCode + ". " +
579 "For push-back and taxi clearance call " + taxiFreqStr + ". " +
582 case MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY:
583 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
584 text = receiver + ". Switching to " + taxiFreqStr + ". " + sender;
586 case MSG_INITIATE_CONTACT:
587 text = receiver + ". With you. " + sender;
589 case MSG_ACKNOWLEDGE_INITIATE_CONTACT:
590 text = receiver + ". Roger. " + sender;
592 case MSG_REQUEST_PUSHBACK_CLEARANCE:
593 text = receiver + ". Request push-back. " + sender;
595 case MSG_PERMIT_PUSHBACK_CLEARANCE:
596 text = receiver + ". Push-back approved. " + sender;
598 case MSG_HOLD_PUSHBACK_CLEARANCE:
599 text = receiver + ". Standby. " + sender;
601 case MSG_REQUEST_TAXI_CLEARANCE:
602 text = receiver + ". Ready to Taxi. " + sender;
604 case MSG_ISSUE_TAXI_CLEARANCE:
605 text = receiver + ". Cleared to taxi. " + sender;
607 case MSG_ACKNOWLEDGE_TAXI_CLEARANCE:
608 text = receiver + ". Cleared to taxi. " + sender;
610 case MSG_HOLD_POSITION:
611 text = receiver + ". Hold Position. " + sender;
613 case MSG_ACKNOWLEDGE_HOLD_POSITION:
614 text = receiver + ". Holding Position. " + sender;
616 case MSG_RESUME_TAXI:
617 text = receiver + ". Resume Taxiing. " + sender;
619 case MSG_ACKNOWLEDGE_RESUME_TAXI:
620 text = receiver + ". Continuing Taxi. " + sender;
623 text = text + sender + ". Transmitting unknown Message";
626 double onBoardRadioFreq0 =
627 fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
628 double onBoardRadioFreq1 =
629 fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
630 int onBoardRadioFreqI0 = (int) floor(onBoardRadioFreq0 * 100 + 0.5);
631 int onBoardRadioFreqI1 = (int) floor(onBoardRadioFreq1 * 100 + 0.5);
632 //cerr << "Using " << onBoardRadioFreq0 << ", " << onBoardRadioFreq1 << " and " << stationFreq << " for " << text << endl;
634 // Display ATC message only when one of the radios is tuned
635 // the relevant frequency.
636 // Note that distance attenuation is currently not yet implemented
637 if ((onBoardRadioFreqI0 == stationFreq)
638 || (onBoardRadioFreqI1 == stationFreq)) {
639 if (rec->allowTransmissions()) {
640 fgSetString("/sim/messages/atc", text.c_str());
645 string FGATCController::formatATCFrequency3_2(int freq)
648 snprintf(buffer, 7, "%3.2f", ((float) freq / 100.0));
649 return string(buffer);
652 // TODO: Set transponder codes according to real-world routes.
653 // The current version just returns a random string of four octal numbers.
654 string FGATCController::genTransponderCode(string fltRules)
656 if (fltRules == "VFR") {
657 return string("1200");
660 snprintf(buffer, 5, "%d%d%d%d", rand() % 8, rand() % 8, rand() % 8,
662 return string(buffer);
666 /***************************************************************************
667 * class FGTowerController
669 **************************************************************************/
670 FGTowerController::FGTowerController():
676 void FGTowerController::announcePosition(int id,
677 FGAIFlightPlan * intendedRoute,
678 int currentPosition, double lat,
679 double lon, double heading,
680 double speed, double alt,
681 double radius, int leg,
684 TrafficVectorIterator i = activeTraffic.begin();
685 // Search whether the current id alread has an entry
686 // This might be faster using a map instead of a vector, but let's start by taking a safe route
687 if (activeTraffic.size()) {
688 //while ((i->getId() != id) && i != activeTraffic.end()) {
689 while (i != activeTraffic.end()) {
690 if (i->getId() == id) {
696 // Add a new TrafficRecord if no one exsists for this aircraft.
697 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
701 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
702 rec.setRunway(intendedRoute->getRunway());
704 //rec.setCallSign(callsign);
705 rec.setAircraft(ref);
706 activeTraffic.push_back(rec);
708 i->setPositionAndHeading(lat, lon, heading, speed, alt);
712 void FGTowerController::update(int id, double lat, double lon,
713 double heading, double speed, double alt,
716 TrafficVectorIterator i = activeTraffic.begin();
717 // Search whether the current id has an entry
718 // This might be faster using a map instead of a vector, but let's start by taking a safe route
719 TrafficVectorIterator current, closest;
720 if (activeTraffic.size()) {
721 //while ((i->getId() != id) && i != activeTraffic.end()) {
722 while (i != activeTraffic.end()) {
723 if (i->getId() == id) {
729 // // update position of the current aircraft
730 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
731 SG_LOG(SG_GENERAL, SG_ALERT,
732 "AI error: updating aircraft without traffic record");
734 i->setPositionAndHeading(lat, lon, heading, speed, alt);
739 // // see if we already have a clearance record for the currently active runway
740 ActiveRunwayVecIterator rwy = activeRunways.begin();
741 // again, a map might be more efficient here
742 if (activeRunways.size()) {
743 //while ((rwy->getRunwayName() != current->getRunway()) && (rwy != activeRunways.end())) {
744 while (rwy != activeRunways.end()) {
745 if (rwy->getRunwayName() == current->getRunway()) {
751 if (rwy == activeRunways.end()) {
752 ActiveRunway aRwy(current->getRunway(), id);
753 activeRunways.push_back(aRwy); // Since there are no clearance records for this runway yet
754 current->setHoldPosition(false); // Clear the current aircraft to continue
756 // Okay, we have a clearance record for this runway, so check
757 // whether the clearence ID matches that of the current aircraft
758 if (id == rwy->getCleared()) {
759 current->setHoldPosition(false);
761 current->setHoldPosition(true);
767 void FGTowerController::signOff(int id)
769 TrafficVectorIterator i = activeTraffic.begin();
770 // Search search if the current id alread has an entry
771 // This might be faster using a map instead of a vector, but let's start by taking a safe route
772 if (activeTraffic.size()) {
773 //while ((i->getId() != id) && i != activeTraffic.end()) {
774 while (i != activeTraffic.end()) {
775 if (i->getId() == id) {
781 // If this aircraft has left the runway, we can clear the departure record for this runway
782 ActiveRunwayVecIterator rwy = activeRunways.begin();
783 if (activeRunways.size()) {
784 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
785 while (rwy != activeRunways.end()) {
786 if (rwy->getRunwayName() == i->getRunway()) {
791 if (rwy != activeRunways.end()) {
792 rwy = activeRunways.erase(rwy);
794 SG_LOG(SG_GENERAL, SG_ALERT,
795 "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff");
798 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
799 SG_LOG(SG_GENERAL, SG_ALERT,
800 "AI error: Aircraft without traffic record is signing off from tower");
802 i = activeTraffic.erase(i);
807 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
808 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
809 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
810 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
811 // Note that this function is probably obsolete
812 bool FGTowerController::hasInstruction(int id)
814 TrafficVectorIterator i = activeTraffic.begin();
815 // Search search if the current id has an entry
816 // This might be faster using a map instead of a vector, but let's start by taking a safe route
817 if (activeTraffic.size()) {
818 //while ((i->getId() != id) && i != activeTraffic.end()) {
819 while (i != activeTraffic.end()) {
820 if (i->getId() == id) {
826 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
827 SG_LOG(SG_GENERAL, SG_ALERT,
828 "AI error: checking ATC instruction for aircraft without traffic record");
830 return i->hasInstruction();
836 FGATCInstruction FGTowerController::getInstruction(int id)
838 TrafficVectorIterator i = activeTraffic.begin();
839 // Search search if the current id has an entry
840 // This might be faster using a map instead of a vector, but let's start by taking a safe route
841 if (activeTraffic.size()) {
842 //while ((i->getId() != id) && i != activeTraffic.end()) {
843 while (i != activeTraffic.end()) {
844 if (i->getId() == id) {
850 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
851 SG_LOG(SG_GENERAL, SG_ALERT,
852 "AI error: requesting ATC instruction for aircraft without traffic record");
854 return i->getInstruction();
856 return FGATCInstruction();
859 /***************************************************************************
860 * class FGStartupController
862 **************************************************************************/
863 FGStartupController::FGStartupController():
868 void FGStartupController::announcePosition(int id,
869 FGAIFlightPlan * intendedRoute,
870 int currentPosition, double lat,
871 double lon, double heading,
872 double speed, double alt,
873 double radius, int leg,
876 TrafficVectorIterator i = activeTraffic.begin();
877 // Search whether the current id alread has an entry
878 // This might be faster using a map instead of a vector, but let's start by taking a safe route
879 if (activeTraffic.size()) {
880 //while ((i->getId() != id) && i != activeTraffic.end()) {
881 while (i != activeTraffic.end()) {
882 if (i->getId() == id) {
888 // Add a new TrafficRecord if no one exsists for this aircraft.
889 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
893 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
894 rec.setRunway(intendedRoute->getRunway());
896 //rec.setCallSign(callsign);
897 rec.setAircraft(ref);
898 rec.setHoldPosition(true);
899 activeTraffic.push_back(rec);
901 i->setPositionAndHeading(lat, lon, heading, speed, alt);
907 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
908 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
909 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
910 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
911 // Note that this function is probably obsolete
912 bool FGStartupController::hasInstruction(int id)
914 TrafficVectorIterator i = activeTraffic.begin();
915 // Search search if the current id has an entry
916 // This might be faster using a map instead of a vector, but let's start by taking a safe route
917 if (activeTraffic.size()) {
918 //while ((i->getId() != id) && i != activeTraffic.end()) {
919 while (i != activeTraffic.end()) {
920 if (i->getId() == id) {
926 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
927 SG_LOG(SG_GENERAL, SG_ALERT,
928 "AI error: checking ATC instruction for aircraft without traffic record");
930 return i->hasInstruction();
936 FGATCInstruction FGStartupController::getInstruction(int id)
938 TrafficVectorIterator i = activeTraffic.begin();
939 // Search search if the current id has an entry
940 // This might be faster using a map instead of a vector, but let's start by taking a safe route
941 if (activeTraffic.size()) {
942 //while ((i->getId() != id) && i != activeTraffic.end()) {
943 while (i != activeTraffic.end()) {
944 if (i->getId() == id) {
950 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
951 SG_LOG(SG_GENERAL, SG_ALERT,
952 "AI error: requesting ATC instruction for aircraft without traffic record");
954 return i->getInstruction();
956 return FGATCInstruction();
959 void FGStartupController::signOff(int id)
961 TrafficVectorIterator i = activeTraffic.begin();
962 // Search search if the current id alread has an entry
963 // This might be faster using a map instead of a vector, but let's start by taking a safe route
964 if (activeTraffic.size()) {
965 //while ((i->getId() != id) && i != activeTraffic.end()) {
966 while (i != activeTraffic.end()) {
967 if (i->getId() == id) {
973 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
974 SG_LOG(SG_GENERAL, SG_ALERT,
975 "AI error: Aircraft without traffic record is signing off from tower");
977 i = activeTraffic.erase(i);
981 void FGStartupController::update(int id, double lat, double lon,
982 double heading, double speed, double alt,
985 TrafficVectorIterator i = activeTraffic.begin();
986 // Search search if the current id has an entry
987 // This might be faster using a map instead of a vector, but let's start by taking a safe route
988 TrafficVectorIterator current, closest;
989 if (activeTraffic.size()) {
990 //while ((i->getId() != id) && i != activeTraffic.end()) {
991 while (i != activeTraffic.end()) {
992 if (i->getId() == id) {
998 // // update position of the current aircraft
999 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1000 SG_LOG(SG_GENERAL, SG_ALERT,
1001 "AI error: updating aircraft without traffic record");
1003 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1006 setDt(getDt() + dt);
1008 int state = i->getState();
1010 i->getAircraft()->getTrafficRef()->getDepartureTime();
1011 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1012 //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
1013 // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
1014 // << " at parking " << getGateName(i->getAircraft()) << endl;
1016 if ((now - lastTransmission) > 3 + (rand() % 15)) {
1020 if ((state == 0) && available) {
1021 if (now > startTime) {
1022 //cerr << "Transmitting startup msg" << endl;
1023 transmit(&(*i), MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND);
1025 lastTransmission = now;
1029 if ((state == 1) && available) {
1030 if (now > startTime + 60) {
1031 transmit(&(*i), MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND);
1033 lastTransmission = now;
1037 if ((state == 2) && available) {
1038 if (now > startTime + 80) {
1039 transmit(&(*i), MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR);
1041 lastTransmission = now;
1045 if ((state == 3) && available) {
1046 if (now > startTime + 100) {
1047 transmit(&(*i), MSG_ACKNOWLEDGE_ENGINE_START,
1050 lastTransmission = now;
1054 // Note: The next four stages are only necessesary when Startup control is
1055 // on a different frequency, compared to ground control
1056 if ((state == 4) && available) {
1057 if (now > startTime + 130) {
1058 transmit(&(*i), MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
1062 lastTransmission = now;
1066 if ((state == 5) && available) {
1067 if (now > startTime + 140) {
1068 transmit(&(*i), MSG_INITIATE_CONTACT, ATC_AIR_TO_GROUND);
1070 lastTransmission = now;
1074 if ((state == 6) && available) {
1075 if (now > startTime + 150) {
1076 transmit(&(*i), MSG_ACKNOWLEDGE_INITIATE_CONTACT,
1079 lastTransmission = now;
1084 // TODO: Switch to APRON control and request pushback Clearance.
1085 // Get Push back clearance
1086 if ((state == 7) && available) {
1087 if (now > startTime + 180) {
1088 transmit(&(*i), MSG_REQUEST_PUSHBACK_CLEARANCE,
1091 lastTransmission = now;
1095 if ((state == 8) && available) {
1096 if (now > startTime + 200) {
1097 if (i->pushBackAllowed()) {
1098 i->allowRepeatedTransmissions();
1099 transmit(&(*i), MSG_PERMIT_PUSHBACK_CLEARANCE,
1103 transmit(&(*i), MSG_HOLD_PUSHBACK_CLEARANCE,
1105 i->suppressRepeatedTransmissions();
1107 lastTransmission = now;
1111 if ((state == 9) && available) {
1112 i->setHoldPosition(false);
1117 /***************************************************************************
1118 * class FGApproachController
1120 **************************************************************************/
1121 FGApproachController::FGApproachController():
1127 void FGApproachController::announcePosition(int id,
1128 FGAIFlightPlan * intendedRoute,
1129 int currentPosition,
1130 double lat, double lon,
1131 double heading, double speed,
1132 double alt, double radius,
1133 int leg, FGAIAircraft * ref)
1135 TrafficVectorIterator i = activeTraffic.begin();
1136 // Search whether the current id alread has an entry
1137 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1138 if (activeTraffic.size()) {
1139 //while ((i->getId() != id) && i != activeTraffic.end()) {
1140 while (i != activeTraffic.end()) {
1141 if (i->getId() == id) {
1147 // Add a new TrafficRecord if no one exsists for this aircraft.
1148 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1149 FGTrafficRecord rec;
1152 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1153 rec.setRunway(intendedRoute->getRunway());
1155 //rec.setCallSign(callsign);
1156 rec.setAircraft(ref);
1157 activeTraffic.push_back(rec);
1159 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1163 void FGApproachController::update(int id, double lat, double lon,
1164 double heading, double speed, double alt,
1167 TrafficVectorIterator i = activeTraffic.begin();
1168 // Search search if the current id has an entry
1169 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1170 TrafficVectorIterator current, closest;
1171 if (activeTraffic.size()) {
1172 //while ((i->getId() != id) && i != activeTraffic.end()) {
1173 while (i != activeTraffic.end()) {
1174 if (i->getId() == id) {
1180 // // update position of the current aircraft
1181 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1182 SG_LOG(SG_GENERAL, SG_ALERT,
1183 "AI error: updating aircraft without traffic record");
1185 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1187 //cerr << "ApproachController: checking for speed" << endl;
1189 current->getAircraft()->
1190 checkForArrivalTime(string("final001"));
1191 if (time_diff > 15) {
1192 current->setSpeedAdjustment(current->getAircraft()->
1193 getPerformance()->vDescent() *
1195 } else if (time_diff > 5) {
1196 current->setSpeedAdjustment(current->getAircraft()->
1197 getPerformance()->vDescent() *
1199 } else if (time_diff < -15) {
1200 current->setSpeedAdjustment(current->getAircraft()->
1201 getPerformance()->vDescent() *
1203 } else if (time_diff < -5) {
1204 current->setSpeedAdjustment(current->getAircraft()->
1205 getPerformance()->vDescent() *
1208 current->clearSpeedAdjustment();
1210 //current->setSpeedAdjustment(current->getAircraft()->getPerformance()->vDescent() + time_diff);
1212 setDt(getDt() + dt);
1215 void FGApproachController::signOff(int id)
1217 TrafficVectorIterator i = activeTraffic.begin();
1218 // Search search if the current id alread has an entry
1219 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1220 if (activeTraffic.size()) {
1221 //while ((i->getId() != id) && i != activeTraffic.end()) {
1222 while (i != activeTraffic.end()) {
1223 if (i->getId() == id) {
1229 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1230 SG_LOG(SG_GENERAL, SG_ALERT,
1231 "AI error: Aircraft without traffic record is signing off from approach");
1233 i = activeTraffic.erase(i);
1240 bool FGApproachController::hasInstruction(int id)
1242 TrafficVectorIterator i = activeTraffic.begin();
1243 // Search search if the current id has an entry
1244 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1245 if (activeTraffic.size()) {
1246 //while ((i->getId() != id) && i != activeTraffic.end()) {
1247 while (i != activeTraffic.end()) {
1248 if (i->getId() == id) {
1254 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1255 SG_LOG(SG_GENERAL, SG_ALERT,
1256 "AI error: checking ATC instruction for aircraft without traffic record");
1258 return i->hasInstruction();
1264 FGATCInstruction FGApproachController::getInstruction(int id)
1266 TrafficVectorIterator i = activeTraffic.begin();
1267 // Search search if the current id has an entry
1268 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1269 if (activeTraffic.size()) {
1270 //while ((i->getId() != id) && i != activeTraffic.end()) {
1271 while (i != activeTraffic.end()) {
1272 if (i->getId() == id) {
1278 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1279 SG_LOG(SG_GENERAL, SG_ALERT,
1280 "AI error: requesting ATC instruction for aircraft without traffic record");
1282 return i->getInstruction();
1284 return FGATCInstruction();
1288 ActiveRunway *FGApproachController::getRunway(string name)
1290 ActiveRunwayVecIterator rwy = activeRunways.begin();
1291 if (activeRunways.size()) {
1292 while (rwy != activeRunways.end()) {
1293 if (rwy->getRunwayName() == name) {
1299 if (rwy == activeRunways.end()) {
1300 ActiveRunway aRwy(name, 0);
1301 activeRunways.push_back(aRwy);
1302 rwy = activeRunways.end() - 1;