1 // trafficcontrol.hxx - classes to manage AIModels based air traffic control
2 // Written by Durk Talsma, started September 2006.
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #ifndef _TRAFFIC_CONTROL_HXX_
22 #define _TRAFFIC_CONTROL_HXX_
24 #include <Airports/airports_fwd.hxx>
27 #include <osg/Geometry>
28 #include <osg/MatrixTransform>
31 #include <simgear/compiler.h>
32 // There is probably a better include than sg_geodesy to get the SG_NM_TO_METER...
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/structure/SGReferenced.hxx>
36 #include <simgear/structure/SGSharedPtr.hxx>
39 typedef std::vector<FGAIAircraft*> AircraftVec;
40 typedef std::vector<FGAIAircraft*>::iterator AircraftVecIterator;
43 typedef std::vector<FGAIFlightPlan*> FlightPlanVec;
44 typedef std::vector<FGAIFlightPlan*>::iterator FlightPlanVecIterator;
45 typedef std::map<std::string, FlightPlanVec> FlightPlanVecMap;
47 class FGTrafficRecord;
48 typedef std::list<FGTrafficRecord> TrafficVector;
49 typedef std::list<FGTrafficRecord>::iterator TrafficVectorIterator;
52 typedef std::vector<ActiveRunway> ActiveRunwayVec;
53 typedef std::vector<ActiveRunway>::iterator ActiveRunwayVecIterator;
55 typedef std::vector<int> intVec;
56 typedef std::vector<int>::iterator intVecIterator;
58 /**************************************************************************************
59 * class FGATCInstruction
60 * like class FGATC Controller, this class definition should go into its own file
61 * and or directory... For now, just testing this stuff out though...
62 *************************************************************************************/
63 class FGATCInstruction
71 bool resolveCircularWait;
79 bool hasInstruction () const;
80 bool getHoldPattern () const {
83 bool getHoldPosition () const {
86 bool getChangeSpeed () const {
89 bool getChangeHeading () const {
92 bool getChangeAltitude() const {
93 return changeAltitude;
96 double getSpeed () const {
99 double getHeading () const {
102 double getAlt () const {
106 bool getCheckForCircularWait() const {
107 return resolveCircularWait;
110 void setHoldPattern (bool val) {
113 void setHoldPosition (bool val) {
116 void setChangeSpeed (bool val) {
119 void setChangeHeading (bool val) {
122 void setChangeAltitude(bool val) {
123 changeAltitude = val;
126 void setResolveCircularWait (bool val) {
127 resolveCircularWait = val;
130 void setSpeed (double val) {
133 void setHeading (double val) {
136 void setAlt (double val) {
145 /**************************************************************************************
146 * class FGTrafficRecord
147 *************************************************************************************/
148 class FGTrafficRecord
156 bool allowTransmission;
161 FGATCInstruction instruction;
162 double latitude, longitude, heading, speed, altitude, radius;
164 //FGAISchedule *trafficRef;
165 FGAIAircraft *aircraft;
171 void setId(int val) {
174 void setRadius(double rad) {
177 void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
178 void setRunway(const std::string& rwy) {
181 void setLeg(int lg) {
190 void setState(int s) {
193 FGATCInstruction getInstruction() {
196 bool hasInstruction() {
197 return instruction.hasInstruction();
199 void setPositionAndHeading(double lat, double lon, double hdg, double spd, double alt);
200 bool checkPositionAndIntentions(FGTrafficRecord &other);
201 int crosses (FGGroundNetwork *, FGTrafficRecord &other);
202 bool isOpposing (FGGroundNetwork *, FGTrafficRecord &other, int node);
204 bool isActive(int margin) const;
206 bool onRoute(FGGroundNetwork *, FGTrafficRecord &other);
208 bool getSpeedAdjustment() const {
209 return instruction.getChangeSpeed();
212 double getLatitude () const {
215 double getLongitude() const {
218 double getHeading () const {
221 double getSpeed () const {
224 double getAltitude () const {
227 double getRadius () const {
231 int getWaitsForId () const {
235 void setSpeedAdjustment(double spd);
236 void setHeadingAdjustment(double heading);
237 void clearSpeedAdjustment () {
238 instruction.setChangeSpeed (false);
240 void clearHeadingAdjustment() {
241 instruction.setChangeHeading(false);
244 bool hasHeadingAdjustment() const {
245 return instruction.getChangeHeading();
247 bool hasHoldPosition() const {
248 return instruction.getHoldPosition();
250 void setHoldPosition (bool inst) {
251 instruction.setHoldPosition(inst);
254 void setWaitsForId(int id) {
258 void setResolveCircularWait() {
259 instruction.setResolveCircularWait(true);
261 void clearResolveCircularWait() {
262 instruction.setResolveCircularWait(false);
265 const std::string& getRunway() const {
268 //void setCallSign(string clsgn) { callsign = clsgn; };
269 void setAircraft(FGAIAircraft *ref) {
274 allowTransmission=true;
276 //string getCallSign() { return callsign; };
277 FGAIAircraft *getAircraft() const {
280 int getTime() const {
286 void setTime(time_t time) {
290 bool pushBackAllowed() const;
291 bool allowTransmissions() const {
292 return allowTransmission;
294 void allowPushBack() { allowPushback =true;};
295 void denyPushBack () { allowPushback = false;};
296 void suppressRepeatedTransmissions () {
297 allowTransmission=false;
299 void allowRepeatedTransmissions () {
300 allowTransmission=true;
302 void nextFrequency() {
305 int getNextFrequency() const {
308 intVec& getIntentions() {
311 int getCurrentPosition() const {
314 void setPriority(int p) { priority = p; };
315 int getPriority() const { return priority; };
318 /***********************************************************************
319 * Active runway, a utility class to keep track of which aircraft has
320 * clearance for a given runway.
321 **********************************************************************/
326 int currentlyCleared;
327 double distanceToFinal;
328 TimeVector estimatedArrivalTimes;
329 AircraftVec departureCue;
332 ActiveRunway(const std::string& r, int cc) {
334 currentlyCleared = cc;
335 distanceToFinal = 6.0 * SG_NM_TO_METER;
338 std::string getRunwayName() {
342 return currentlyCleared;
344 double getApproachDistance() {
345 return distanceToFinal;
347 //time_t getEstApproachTime() { return estimatedArrival; };
349 //void setEstApproachTime(time_t time) { estimatedArrival = time; };
350 void addToDepartureCue(FGAIAircraft *ac) {
351 departureCue.push_back(ac);
353 void setCleared(int number) {
354 currentlyCleared = number;
356 time_t requestTimeSlot(time_t eta);
358 int getDepartureCueSize() {
359 return departureCue.size();
361 FGAIAircraft* getFirstAircraftInDepartureCue() {
362 return departureCue.size() ? *(departureCue.begin()) : NULL;
364 FGAIAircraft* getFirstOfStatus(int stat);
365 void updateDepartureCue() {
366 departureCue.erase(departureCue.begin());
368 void printDepartureCue();
372 * class FGATCController
373 * NOTE: this class serves as an abstraction layer for all sorts of ATC controllers.
374 *************************************************************************************/
375 class FGATCController
383 time_t lastTransmission;
388 std::string formatATCFrequency3_2(int );
389 std::string genTransponderCode(const std::string& fltRules);
390 bool isUserAircraft(FGAIAircraft*);
394 MSG_ANNOUNCE_ENGINE_START,
395 MSG_REQUEST_ENGINE_START,
396 MSG_PERMIT_ENGINE_START,
397 MSG_DENY_ENGINE_START,
398 MSG_ACKNOWLEDGE_ENGINE_START,
399 MSG_REQUEST_PUSHBACK_CLEARANCE,
400 MSG_PERMIT_PUSHBACK_CLEARANCE,
401 MSG_HOLD_PUSHBACK_CLEARANCE,
402 MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
403 MSG_INITIATE_CONTACT,
404 MSG_ACKNOWLEDGE_INITIATE_CONTACT,
405 MSG_REQUEST_TAXI_CLEARANCE,
406 MSG_ISSUE_TAXI_CLEARANCE,
407 MSG_ACKNOWLEDGE_TAXI_CLEARANCE,
409 MSG_ACKNOWLEDGE_HOLD_POSITION,
411 MSG_ACKNOWLEDGE_RESUME_TAXI,
412 MSG_REPORT_RUNWAY_HOLD_SHORT,
413 MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT,
414 MSG_SWITCH_TOWER_FREQUENCY,
415 MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY
423 virtual ~FGATCController();
426 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
427 double lat, double lon,
428 double hdg, double spd, double alt, double radius, int leg,
429 FGAIAircraft *aircraft) = 0;
430 virtual void signOff(int id) = 0;
431 virtual void updateAircraftInformation(int id, double lat, double lon,
432 double heading, double speed, double alt, double dt) = 0;
433 virtual bool hasInstruction(int id) = 0;
434 virtual FGATCInstruction getInstruction(int id) = 0;
439 void setDt(double dt) {
442 void transmit(FGTrafficRecord *rec, FGAirportDynamics *parent, AtcMsgId msgId, AtcMsgDir msgDir, bool audible);
443 std::string getGateName(FGAIAircraft *aircraft);
444 virtual void render(bool) = 0;
445 virtual std::string getName() = 0;
447 virtual void update(double) = 0;
452 AtcMsgDir lastTransmissionDirection;
455 /******************************************************************************
456 * class FGTowerControl
457 *****************************************************************************/
458 class FGTowerController : public FGATCController
461 TrafficVector activeTraffic;
462 ActiveRunwayVec activeRunways;
463 FGAirportDynamics *parent;
466 FGTowerController(FGAirportDynamics *parent);
467 virtual ~FGTowerController() {};
468 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
469 double lat, double lon,
470 double hdg, double spd, double alt, double radius, int leg,
471 FGAIAircraft *aircraft);
472 virtual void signOff(int id);
473 virtual void updateAircraftInformation(int id, double lat, double lon,
474 double heading, double speed, double alt, double dt);
475 virtual bool hasInstruction(int id);
476 virtual FGATCInstruction getInstruction(int id);
478 virtual void render(bool);
479 virtual std::string getName();
480 virtual void update(double dt);
481 bool hasActiveTraffic() {
482 return activeTraffic.size() != 0;
484 TrafficVector &getActiveTraffic() {
485 return activeTraffic;
489 /******************************************************************************
490 * class FGStartupController
492 *****************************************************************************/
494 class FGStartupController : public FGATCController
497 TrafficVector activeTraffic;
498 //ActiveRunwayVec activeRunways;
499 FGAirportDynamics *parent;
502 FGStartupController(FGAirportDynamics *parent);
503 virtual ~FGStartupController() {};
504 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
505 double lat, double lon,
506 double hdg, double spd, double alt, double radius, int leg,
507 FGAIAircraft *aircraft);
508 virtual void signOff(int id);
509 virtual void updateAircraftInformation(int id, double lat, double lon,
510 double heading, double speed, double alt, double dt);
511 virtual bool hasInstruction(int id);
512 virtual FGATCInstruction getInstruction(int id);
514 virtual void render(bool);
515 virtual std::string getName();
516 virtual void update(double dt);
518 bool hasActiveTraffic() {
519 return activeTraffic.size() != 0;
521 TrafficVector &getActiveTraffic() {
522 return activeTraffic;
525 // Hpoefully, we can move this function to the base class, but I need to verify what is needed for the other controllers before doing so.
526 bool checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
531 /******************************************************************************
532 * class FGTowerControl
533 *****************************************************************************/
534 class FGApproachController : public FGATCController
537 TrafficVector activeTraffic;
538 ActiveRunwayVec activeRunways;
539 FGAirportDynamics *parent;
542 FGApproachController(FGAirportDynamics * parent);
543 virtual ~FGApproachController() { };
544 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
545 double lat, double lon,
546 double hdg, double spd, double alt, double radius, int leg,
547 FGAIAircraft *aircraft);
548 virtual void signOff(int id);
549 virtual void updateAircraftInformation(int id, double lat, double lon,
550 double heading, double speed, double alt, double dt);
551 virtual bool hasInstruction(int id);
552 virtual FGATCInstruction getInstruction(int id);
554 virtual void render(bool);
555 virtual std::string getName();
556 virtual void update(double dt);
558 ActiveRunway* getRunway(const std::string& name);
560 bool hasActiveTraffic() {
561 return activeTraffic.size() != 0;
563 TrafficVector &getActiveTraffic() {
564 return activeTraffic;
569 #endif // _TRAFFIC_CONTROL_HXX