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 SGSharedPtr<FGAIAircraft> aircraft;
169 virtual ~FGTrafficRecord();
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);
273 allowTransmission=true;
275 //string getCallSign() { return callsign; };
276 FGAIAircraft *getAircraft() const;
278 int getTime() const {
284 void setTime(time_t time) {
288 bool pushBackAllowed() const;
289 bool allowTransmissions() const {
290 return allowTransmission;
292 void allowPushBack() { allowPushback =true;};
293 void denyPushBack () { allowPushback = false;};
294 void suppressRepeatedTransmissions () {
295 allowTransmission=false;
297 void allowRepeatedTransmissions () {
298 allowTransmission=true;
300 void nextFrequency() {
303 int getNextFrequency() const {
306 intVec& getIntentions() {
309 int getCurrentPosition() const {
312 void setPriority(int p) { priority = p; };
313 int getPriority() const { return priority; };
316 /***********************************************************************
317 * Active runway, a utility class to keep track of which aircraft has
318 * clearance for a given runway.
319 **********************************************************************/
324 int currentlyCleared;
325 double distanceToFinal;
326 TimeVector estimatedArrivalTimes;
327 AircraftVec departureCue;
330 ActiveRunway(const std::string& r, int cc) {
332 currentlyCleared = cc;
333 distanceToFinal = 6.0 * SG_NM_TO_METER;
336 std::string getRunwayName() {
340 return currentlyCleared;
342 double getApproachDistance() {
343 return distanceToFinal;
345 //time_t getEstApproachTime() { return estimatedArrival; };
347 //void setEstApproachTime(time_t time) { estimatedArrival = time; };
348 void addToDepartureCue(FGAIAircraft *ac) {
349 departureCue.push_back(ac);
351 void setCleared(int number) {
352 currentlyCleared = number;
354 time_t requestTimeSlot(time_t eta);
356 int getDepartureCueSize() {
357 return departureCue.size();
359 FGAIAircraft* getFirstAircraftInDepartureCue() {
360 return departureCue.size() ? *(departureCue.begin()) : NULL;
362 FGAIAircraft* getFirstOfStatus(int stat);
363 void updateDepartureCue() {
364 departureCue.erase(departureCue.begin());
366 void printDepartureCue();
370 * class FGATCController
371 * NOTE: this class serves as an abstraction layer for all sorts of ATC controllers.
372 *************************************************************************************/
373 class FGATCController
381 time_t lastTransmission;
386 std::string formatATCFrequency3_2(int );
387 std::string genTransponderCode(const std::string& fltRules);
388 bool isUserAircraft(FGAIAircraft*);
392 MSG_ANNOUNCE_ENGINE_START,
393 MSG_REQUEST_ENGINE_START,
394 MSG_PERMIT_ENGINE_START,
395 MSG_DENY_ENGINE_START,
396 MSG_ACKNOWLEDGE_ENGINE_START,
397 MSG_REQUEST_PUSHBACK_CLEARANCE,
398 MSG_PERMIT_PUSHBACK_CLEARANCE,
399 MSG_HOLD_PUSHBACK_CLEARANCE,
400 MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
401 MSG_INITIATE_CONTACT,
402 MSG_ACKNOWLEDGE_INITIATE_CONTACT,
403 MSG_REQUEST_TAXI_CLEARANCE,
404 MSG_ISSUE_TAXI_CLEARANCE,
405 MSG_ACKNOWLEDGE_TAXI_CLEARANCE,
407 MSG_ACKNOWLEDGE_HOLD_POSITION,
409 MSG_ACKNOWLEDGE_RESUME_TAXI,
410 MSG_REPORT_RUNWAY_HOLD_SHORT,
411 MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT,
412 MSG_SWITCH_TOWER_FREQUENCY,
413 MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY
421 virtual ~FGATCController();
424 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
425 double lat, double lon,
426 double hdg, double spd, double alt, double radius, int leg,
427 FGAIAircraft *aircraft) = 0;
428 virtual void signOff(int id) = 0;
429 virtual void updateAircraftInformation(int id, double lat, double lon,
430 double heading, double speed, double alt, double dt) = 0;
431 virtual bool hasInstruction(int id) = 0;
432 virtual FGATCInstruction getInstruction(int id) = 0;
437 void setDt(double dt) {
440 void transmit(FGTrafficRecord *rec, FGAirportDynamics *parent, AtcMsgId msgId, AtcMsgDir msgDir, bool audible);
441 std::string getGateName(FGAIAircraft *aircraft);
442 virtual void render(bool) = 0;
443 virtual std::string getName() = 0;
445 virtual void update(double) = 0;
450 AtcMsgDir lastTransmissionDirection;
453 /******************************************************************************
454 * class FGTowerControl
455 *****************************************************************************/
456 class FGTowerController : public FGATCController
459 TrafficVector activeTraffic;
460 ActiveRunwayVec activeRunways;
461 FGAirportDynamics *parent;
464 FGTowerController(FGAirportDynamics *parent);
465 virtual ~FGTowerController() {};
466 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
467 double lat, double lon,
468 double hdg, double spd, double alt, double radius, int leg,
469 FGAIAircraft *aircraft);
470 virtual void signOff(int id);
471 virtual void updateAircraftInformation(int id, double lat, double lon,
472 double heading, double speed, double alt, double dt);
473 virtual bool hasInstruction(int id);
474 virtual FGATCInstruction getInstruction(int id);
476 virtual void render(bool);
477 virtual std::string getName();
478 virtual void update(double dt);
479 bool hasActiveTraffic() {
480 return ! activeTraffic.empty();
482 TrafficVector &getActiveTraffic() {
483 return activeTraffic;
487 /******************************************************************************
488 * class FGStartupController
490 *****************************************************************************/
492 class FGStartupController : public FGATCController
495 TrafficVector activeTraffic;
496 //ActiveRunwayVec activeRunways;
497 FGAirportDynamics *parent;
500 FGStartupController(FGAirportDynamics *parent);
501 virtual ~FGStartupController() {};
502 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
503 double lat, double lon,
504 double hdg, double spd, double alt, double radius, int leg,
505 FGAIAircraft *aircraft);
506 virtual void signOff(int id);
507 virtual void updateAircraftInformation(int id, double lat, double lon,
508 double heading, double speed, double alt, double dt);
509 virtual bool hasInstruction(int id);
510 virtual FGATCInstruction getInstruction(int id);
512 virtual void render(bool);
513 virtual std::string getName();
514 virtual void update(double dt);
516 bool hasActiveTraffic() {
517 return ! activeTraffic.empty();
519 TrafficVector &getActiveTraffic() {
520 return activeTraffic;
523 // 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.
524 bool checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
529 /******************************************************************************
530 * class FGTowerControl
531 *****************************************************************************/
532 class FGApproachController : public FGATCController
535 TrafficVector activeTraffic;
536 ActiveRunwayVec activeRunways;
537 FGAirportDynamics *parent;
540 FGApproachController(FGAirportDynamics * parent);
541 virtual ~FGApproachController() { };
542 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
543 double lat, double lon,
544 double hdg, double spd, double alt, double radius, int leg,
545 FGAIAircraft *aircraft);
546 virtual void signOff(int id);
547 virtual void updateAircraftInformation(int id, double lat, double lon,
548 double heading, double speed, double alt, double dt);
549 virtual bool hasInstruction(int id);
550 virtual FGATCInstruction getInstruction(int id);
552 virtual void render(bool);
553 virtual std::string getName();
554 virtual void update(double dt);
556 ActiveRunway* getRunway(const std::string& name);
558 bool hasActiveTraffic() {
559 return ! activeTraffic.empty();
561 TrafficVector &getActiveTraffic() {
562 return activeTraffic;
567 #endif // _TRAFFIC_CONTROL_HXX