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_
29 #include <osg/Geometry>
30 #include <osg/MatrixTransform>
33 #include <simgear/compiler.h>
34 // There is probably a better include than sg_geodesy to get the SG_NM_TO_METER...
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/structure/SGReferenced.hxx>
38 #include <simgear/structure/SGSharedPtr.hxx>
40 typedef std::vector<int> intVec;
41 typedef std::vector<int>::iterator intVecIterator;
44 class FGAIFlightPlan; // forward reference
45 class FGGroundNetwork; // forward reference
46 class FGAIAircraft; // forward reference
47 class FGAirportDynamics;
49 /**************************************************************************************
50 * class FGATCInstruction
51 * like class FGATC Controller, this class definition should go into its own file
52 * and or directory... For now, just testing this stuff out though...
53 *************************************************************************************/
54 class FGATCInstruction
62 bool resolveCircularWait;
70 bool hasInstruction ();
71 bool getHoldPattern () {
74 bool getHoldPosition () {
77 bool getChangeSpeed () {
80 bool getChangeHeading () {
83 bool getChangeAltitude() {
84 return changeAltitude;
90 double getHeading () {
97 bool getCheckForCircularWait() {
98 return resolveCircularWait;
101 void setHoldPattern (bool val) {
104 void setHoldPosition (bool val) {
107 void setChangeSpeed (bool val) {
110 void setChangeHeading (bool val) {
113 void setChangeAltitude(bool val) {
114 changeAltitude = val;
117 void setResolveCircularWait (bool val) {
118 resolveCircularWait = val;
121 void setSpeed (double val) {
124 void setHeading (double val) {
127 void setAlt (double val) {
136 /**************************************************************************************
137 * class FGTrafficRecord
138 *************************************************************************************/
139 class FGTrafficRecord
147 bool allowTransmission;
152 FGATCInstruction instruction;
153 double latitude, longitude, heading, speed, altitude, radius;
155 //FGAISchedule *trafficRef;
156 FGAIAircraft *aircraft;
162 void setId(int val) {
165 void setRadius(double rad) {
168 void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
169 void setRunway(const std::string& rwy) {
172 void setLeg(int lg) {
181 void setState(int s) {
184 FGATCInstruction getInstruction() {
187 bool hasInstruction() {
188 return instruction.hasInstruction();
190 void setPositionAndHeading(double lat, double lon, double hdg, double spd, double alt);
191 bool checkPositionAndIntentions(FGTrafficRecord &other);
192 int crosses (FGGroundNetwork *, FGTrafficRecord &other);
193 bool isOpposing (FGGroundNetwork *, FGTrafficRecord &other, int node);
195 bool isActive(int margin);
197 bool onRoute(FGGroundNetwork *, FGTrafficRecord &other);
199 bool getSpeedAdjustment() {
200 return instruction.getChangeSpeed();
203 double getLatitude () {
206 double getLongitude() {
209 double getHeading () {
215 double getAltitude () {
218 double getRadius () {
222 int getWaitsForId () {
226 void setSpeedAdjustment(double spd);
227 void setHeadingAdjustment(double heading);
228 void clearSpeedAdjustment () {
229 instruction.setChangeSpeed (false);
231 void clearHeadingAdjustment() {
232 instruction.setChangeHeading(false);
235 bool hasHeadingAdjustment() {
236 return instruction.getChangeHeading();
238 bool hasHoldPosition() {
239 return instruction.getHoldPosition();
241 void setHoldPosition (bool inst) {
242 instruction.setHoldPosition(inst);
245 void setWaitsForId(int id) {
249 void setResolveCircularWait() {
250 instruction.setResolveCircularWait(true);
252 void clearResolveCircularWait() {
253 instruction.setResolveCircularWait(false);
256 const std::string& getRunway() {
259 //void setCallSign(string clsgn) { callsign = clsgn; };
260 void setAircraft(FGAIAircraft *ref) {
265 allowTransmission=true;
267 //string getCallSign() { return callsign; };
268 FGAIAircraft *getAircraft() {
277 void setTime(time_t time) {
281 bool pushBackAllowed();
282 bool allowTransmissions() {
283 return allowTransmission;
285 void allowPushBack() { allowPushback =true;};
286 void denyPushBack () { allowPushback = false;};
287 void suppressRepeatedTransmissions () {
288 allowTransmission=false;
290 void allowRepeatedTransmissions () {
291 allowTransmission=true;
293 void nextFrequency() {
296 int getNextFrequency() {
299 intVec& getIntentions() {
302 int getCurrentPosition() {
305 void setPriority(int p) { priority = p; };
306 int getPriority() { return priority; };
309 typedef std::list<FGTrafficRecord> TrafficVector;
310 typedef std::list<FGTrafficRecord>::iterator TrafficVectorIterator;
312 typedef std::vector<time_t> TimeVector;
313 typedef std::vector<time_t>::iterator TimeVectorIterator;
315 typedef std::vector<FGAIAircraft*> AircraftVec;
316 typedef std::vector<FGAIAircraft*>::iterator AircraftVecIterator;
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();
371 typedef std::vector<ActiveRunway> ActiveRunwayVec;
372 typedef std::vector<ActiveRunway>::iterator ActiveRunwayVecIterator;
375 * class FGATCController
376 * NOTE: this class serves as an abstraction layer for all sorts of ATC controllers.
377 *************************************************************************************/
378 class FGATCController
386 time_t lastTransmission;
391 std::string formatATCFrequency3_2(int );
392 std::string genTransponderCode(const std::string& fltRules);
393 bool isUserAircraft(FGAIAircraft*);
397 MSG_ANNOUNCE_ENGINE_START,
398 MSG_REQUEST_ENGINE_START,
399 MSG_PERMIT_ENGINE_START,
400 MSG_DENY_ENGINE_START,
401 MSG_ACKNOWLEDGE_ENGINE_START,
402 MSG_REQUEST_PUSHBACK_CLEARANCE,
403 MSG_PERMIT_PUSHBACK_CLEARANCE,
404 MSG_HOLD_PUSHBACK_CLEARANCE,
405 MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
406 MSG_INITIATE_CONTACT,
407 MSG_ACKNOWLEDGE_INITIATE_CONTACT,
408 MSG_REQUEST_TAXI_CLEARANCE,
409 MSG_ISSUE_TAXI_CLEARANCE,
410 MSG_ACKNOWLEDGE_TAXI_CLEARANCE,
412 MSG_ACKNOWLEDGE_HOLD_POSITION,
414 MSG_ACKNOWLEDGE_RESUME_TAXI,
415 MSG_REPORT_RUNWAY_HOLD_SHORT,
416 MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT,
417 MSG_SWITCH_TOWER_FREQUENCY,
418 MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY
426 virtual ~FGATCController();
429 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
430 double lat, double lon,
431 double hdg, double spd, double alt, double radius, int leg,
432 FGAIAircraft *aircraft) = 0;
433 virtual void signOff(int id) = 0;
434 virtual void updateAircraftInformation(int id, double lat, double lon,
435 double heading, double speed, double alt, double dt) = 0;
436 virtual bool hasInstruction(int id) = 0;
437 virtual FGATCInstruction getInstruction(int id) = 0;
442 void setDt(double dt) {
445 void transmit(FGTrafficRecord *rec, FGAirportDynamics *parent, AtcMsgId msgId, AtcMsgDir msgDir, bool audible);
446 std::string getGateName(FGAIAircraft *aircraft);
447 virtual void render(bool) = 0;
448 virtual std::string getName() = 0;
450 virtual void update(double) = 0;
455 AtcMsgDir lastTransmissionDirection;
458 /******************************************************************************
459 * class FGTowerControl
460 *****************************************************************************/
461 class FGTowerController : public FGATCController
464 TrafficVector activeTraffic;
465 ActiveRunwayVec activeRunways;
466 FGAirportDynamics *parent;
469 FGTowerController(FGAirportDynamics *parent);
470 virtual ~FGTowerController() {};
471 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
472 double lat, double lon,
473 double hdg, double spd, double alt, double radius, int leg,
474 FGAIAircraft *aircraft);
475 virtual void signOff(int id);
476 virtual void updateAircraftInformation(int id, double lat, double lon,
477 double heading, double speed, double alt, double dt);
478 virtual bool hasInstruction(int id);
479 virtual FGATCInstruction getInstruction(int id);
481 virtual void render(bool);
482 virtual std::string getName();
483 virtual void update(double dt);
484 bool hasActiveTraffic() {
485 return activeTraffic.size() != 0;
487 TrafficVector &getActiveTraffic() {
488 return activeTraffic;
492 /******************************************************************************
493 * class FGStartupController
495 *****************************************************************************/
497 class FGStartupController : public FGATCController
500 TrafficVector activeTraffic;
501 //ActiveRunwayVec activeRunways;
502 FGAirportDynamics *parent;
505 FGStartupController(FGAirportDynamics *parent);
506 virtual ~FGStartupController() {};
507 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
508 double lat, double lon,
509 double hdg, double spd, double alt, double radius, int leg,
510 FGAIAircraft *aircraft);
511 virtual void signOff(int id);
512 virtual void updateAircraftInformation(int id, double lat, double lon,
513 double heading, double speed, double alt, double dt);
514 virtual bool hasInstruction(int id);
515 virtual FGATCInstruction getInstruction(int id);
517 virtual void render(bool);
518 virtual std::string getName();
519 virtual void update(double dt);
521 bool hasActiveTraffic() {
522 return activeTraffic.size() != 0;
524 TrafficVector &getActiveTraffic() {
525 return activeTraffic;
528 // 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.
529 bool checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
534 /******************************************************************************
535 * class FGTowerControl
536 *****************************************************************************/
537 class FGApproachController : public FGATCController
540 TrafficVector activeTraffic;
541 ActiveRunwayVec activeRunways;
542 FGAirportDynamics *parent;
545 FGApproachController(FGAirportDynamics * parent);
546 virtual ~FGApproachController() { };
547 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
548 double lat, double lon,
549 double hdg, double spd, double alt, double radius, int leg,
550 FGAIAircraft *aircraft);
551 virtual void signOff(int id);
552 virtual void updateAircraftInformation(int id, double lat, double lon,
553 double heading, double speed, double alt, double dt);
554 virtual bool hasInstruction(int id);
555 virtual FGATCInstruction getInstruction(int id);
557 virtual void render(bool);
558 virtual std::string getName();
559 virtual void update(double dt);
561 ActiveRunway* getRunway(const std::string& name);
563 bool hasActiveTraffic() {
564 return activeTraffic.size() != 0;
566 TrafficVector &getActiveTraffic() {
567 return activeTraffic;
572 #endif // _TRAFFIC_CONTROL_HXX