]> git.mxchange.org Git - flightgear.git/blob - src/ATC/trafficcontrol.hxx
Fix a typo breaking some takeoff-state logic.
[flightgear.git] / src / ATC / trafficcontrol.hxx
1 // trafficcontrol.hxx - classes to manage AIModels based air traffic control
2 // Written by Durk Talsma, started September 2006.
3 //
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.
8 //
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.
13 //
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.
17 //
18 // $Id$
19
20
21 #ifndef _TRAFFIC_CONTROL_HXX_
22 #define _TRAFFIC_CONTROL_HXX_
23
24 #include <Airports/airports_fwd.hxx>
25
26 #include <osg/Geode>
27 #include <osg/Geometry>
28 #include <osg/MatrixTransform>
29 #include <osg/Shape>
30
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>
37
38 class FGAIAircraft;
39 typedef std::vector<FGAIAircraft*> AircraftVec;
40 typedef std::vector<FGAIAircraft*>::iterator AircraftVecIterator;
41
42 class FGAIFlightPlan;
43 typedef std::vector<FGAIFlightPlan*>           FlightPlanVec;
44 typedef std::vector<FGAIFlightPlan*>::iterator FlightPlanVecIterator;
45 typedef std::map<std::string, FlightPlanVec>   FlightPlanVecMap;
46
47 class FGTrafficRecord;
48 typedef std::list<FGTrafficRecord> TrafficVector;
49 typedef std::list<FGTrafficRecord>::iterator TrafficVectorIterator;
50
51 class ActiveRunway;
52 typedef std::vector<ActiveRunway> ActiveRunwayVec;
53 typedef std::vector<ActiveRunway>::iterator ActiveRunwayVecIterator;
54
55 typedef std::vector<int> intVec;
56 typedef std::vector<int>::iterator intVecIterator;
57
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
64 {
65 private:
66     bool holdPattern;
67     bool holdPosition;
68     bool changeSpeed;
69     bool changeHeading;
70     bool changeAltitude;
71     bool resolveCircularWait;
72
73     double speed;
74     double heading;
75     double alt;
76 public:
77
78     FGATCInstruction();
79     bool hasInstruction   () const;
80     bool getHoldPattern   () const {
81         return holdPattern;
82     };
83     bool getHoldPosition  () const {
84         return holdPosition;
85     };
86     bool getChangeSpeed   () const {
87         return changeSpeed;
88     };
89     bool getChangeHeading () const {
90         return changeHeading;
91     };
92     bool getChangeAltitude() const {
93         return changeAltitude;
94     };
95
96     double getSpeed       () const {
97         return speed;
98     };
99     double getHeading     () const {
100         return heading;
101     };
102     double getAlt         () const {
103         return alt;
104     };
105
106     bool getCheckForCircularWait() const {
107         return resolveCircularWait;
108     };
109
110     void setHoldPattern   (bool val) {
111         holdPattern    = val;
112     };
113     void setHoldPosition  (bool val) {
114         holdPosition   = val;
115     };
116     void setChangeSpeed   (bool val) {
117         changeSpeed    = val;
118     };
119     void setChangeHeading (bool val) {
120         changeHeading  = val;
121     };
122     void setChangeAltitude(bool val) {
123         changeAltitude = val;
124     };
125
126     void setResolveCircularWait (bool val) {
127         resolveCircularWait = val;
128     };
129
130     void setSpeed       (double val) {
131         speed   = val;
132     };
133     void setHeading     (double val) {
134         heading = val;
135     };
136     void setAlt         (double val) {
137         alt     = val;
138     };
139 };
140
141
142
143
144
145 /**************************************************************************************
146  * class FGTrafficRecord
147  *************************************************************************************/
148 class FGTrafficRecord
149 {
150 private:
151     int id, waitsForId;
152     int currentPos;
153     int leg;
154     int frequencyId;
155     int state;
156     bool allowTransmission;
157     bool allowPushback;
158     int priority;
159     time_t timer;
160     intVec intentions;
161     FGATCInstruction instruction;
162     double latitude, longitude, heading, speed, altitude, radius;
163     std::string runway;
164     SGSharedPtr<FGAIAircraft> aircraft;
165
166
167 public:
168     FGTrafficRecord();
169     virtual ~FGTrafficRecord();
170
171     void setId(int val)  {
172         id = val;
173     };
174     void setRadius(double rad) {
175         radius = rad;
176     };
177     void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
178     void setRunway(const std::string& rwy) {
179         runway = rwy;
180     };
181     void setLeg(int lg) {
182         leg = lg;
183     };
184     int getId() {
185         return id;
186     };
187     int getState() {
188         return state;
189     };
190     void setState(int s) {
191         state = s;
192     }
193     FGATCInstruction getInstruction() {
194         return instruction;
195     };
196     bool hasInstruction() {
197         return instruction.hasInstruction();
198     };
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);
203     
204     bool isActive(int margin) const;
205
206     bool onRoute(FGGroundNetwork *, FGTrafficRecord &other);
207
208     bool getSpeedAdjustment() const {
209         return instruction.getChangeSpeed();
210     };
211
212     double getLatitude () const {
213         return latitude ;
214     };
215     double getLongitude() const {
216         return longitude;
217     };
218     double getHeading  () const {
219         return heading  ;
220     };
221     double getSpeed    () const {
222         return speed    ;
223     };
224     double getAltitude () const {
225         return altitude ;
226     };
227     double getRadius   () const {
228         return radius   ;
229     };
230
231     int getWaitsForId  () const {
232         return waitsForId;
233     };
234
235     void setSpeedAdjustment(double spd);
236     void setHeadingAdjustment(double heading);
237     void clearSpeedAdjustment  () {
238         instruction.setChangeSpeed  (false);
239     };
240     void clearHeadingAdjustment() {
241         instruction.setChangeHeading(false);
242     };
243
244     bool hasHeadingAdjustment() const {
245         return instruction.getChangeHeading();
246     };
247     bool hasHoldPosition() const {
248         return instruction.getHoldPosition();
249     };
250     void setHoldPosition (bool inst) {
251         instruction.setHoldPosition(inst);
252     };
253
254     void setWaitsForId(int id) {
255         waitsForId = id;
256     };
257
258     void setResolveCircularWait()   {
259         instruction.setResolveCircularWait(true);
260     };
261     void clearResolveCircularWait() {
262         instruction.setResolveCircularWait(false);
263     };
264
265     const std::string& getRunway() const {
266         return runway;
267     };
268     //void setCallSign(string clsgn) { callsign = clsgn; };
269     void setAircraft(FGAIAircraft *ref);
270
271     void updateState() {
272         state++;
273         allowTransmission=true;
274     };
275     //string getCallSign() { return callsign; };
276     FGAIAircraft *getAircraft() const;
277
278     int getTime() const {
279         return timer;
280     };
281     int getLeg() const {
282         return leg;
283     };
284     void setTime(time_t time) {
285         timer = time;
286     };
287
288     bool pushBackAllowed() const;
289     bool allowTransmissions() const {
290         return allowTransmission;
291     };
292     void allowPushBack() { allowPushback =true;};
293     void denyPushBack () { allowPushback = false;};
294     void suppressRepeatedTransmissions () {
295         allowTransmission=false;
296     };
297     void allowRepeatedTransmissions () {
298         allowTransmission=true;
299     };
300     void nextFrequency() {
301         frequencyId++;
302     };
303     int  getNextFrequency() const {
304         return frequencyId;
305     };
306     intVec& getIntentions() {
307         return intentions;
308     };
309     int getCurrentPosition() const {
310         return currentPos;
311     };
312     void setPriority(int p) { priority = p; };
313     int getPriority() const { return priority; };
314 };
315
316 /***********************************************************************
317  * Active runway, a utility class to keep track of which aircraft has
318  * clearance for a given runway.
319  **********************************************************************/
320 class ActiveRunway
321 {
322 private:
323     std::string rwy;
324     int currentlyCleared;
325     double distanceToFinal;
326     TimeVector estimatedArrivalTimes;
327     AircraftVec departureCue;
328
329 public:
330     ActiveRunway(const std::string& r, int cc) {
331         rwy = r;
332         currentlyCleared = cc;
333         distanceToFinal = 6.0 * SG_NM_TO_METER;
334     };
335
336     std::string getRunwayName() {
337         return rwy;
338     };
339     int    getCleared   () {
340         return currentlyCleared;
341     };
342     double getApproachDistance() {
343         return distanceToFinal;
344     };
345     //time_t getEstApproachTime() { return estimatedArrival; };
346
347     //void setEstApproachTime(time_t time) { estimatedArrival = time; };
348     void addToDepartureCue(FGAIAircraft *ac) {
349         departureCue.push_back(ac);
350     };
351     void setCleared(int number) {
352         currentlyCleared = number;
353     };
354     time_t requestTimeSlot(time_t eta);
355
356     int getDepartureCueSize() {
357         return departureCue.size();
358     };
359     FGAIAircraft* getFirstAircraftInDepartureCue() {
360         return departureCue.size() ? *(departureCue.begin()) : NULL;
361     };
362     FGAIAircraft* getFirstOfStatus(int stat);
363     void updateDepartureCue() {
364         departureCue.erase(departureCue.begin());
365     }
366     void printDepartureCue();
367 };
368
369 /**
370  * class FGATCController
371  * NOTE: this class serves as an abstraction layer for all sorts of ATC controllers.
372  *************************************************************************************/
373 class FGATCController
374 {
375 private:
376
377
378 protected:
379     bool initialized;
380     bool available;
381     time_t lastTransmission;
382
383     double dt_count;
384     osg::Group* group;
385
386     std::string formatATCFrequency3_2(int );
387     std::string genTransponderCode(const std::string& fltRules);
388     bool isUserAircraft(FGAIAircraft*);
389
390 public:
391     typedef enum {
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,
406         MSG_HOLD_POSITION,
407         MSG_ACKNOWLEDGE_HOLD_POSITION,
408         MSG_RESUME_TAXI,
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
414     } AtcMsgId;
415
416     typedef enum {
417         ATC_AIR_TO_GROUND,
418         ATC_GROUND_TO_AIR
419     } AtcMsgDir;
420     FGATCController();
421     virtual ~FGATCController();
422     void init();
423
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;
433
434     double getDt() {
435         return dt_count;
436     };
437     void   setDt(double dt) {
438         dt_count = dt;
439     };
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;
444
445     virtual void update(double) = 0;
446
447
448 private:
449
450     AtcMsgDir lastTransmissionDirection;
451 };
452
453 /******************************************************************************
454  * class FGTowerControl
455  *****************************************************************************/
456 class FGTowerController : public FGATCController
457 {
458 private:
459     TrafficVector activeTraffic;
460     ActiveRunwayVec activeRunways;
461     FGAirportDynamics *parent;
462
463 public:
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);
475
476     virtual void render(bool);
477     virtual std::string getName();
478     virtual void update(double dt);
479     bool hasActiveTraffic() {
480         return ! activeTraffic.empty();
481     };
482     TrafficVector &getActiveTraffic() {
483         return activeTraffic;
484     };
485 };
486
487 /******************************************************************************
488  * class FGStartupController
489  * handle
490  *****************************************************************************/
491
492 class FGStartupController : public FGATCController
493 {
494 private:
495     TrafficVector activeTraffic;
496     //ActiveRunwayVec activeRunways;
497     FGAirportDynamics *parent;
498
499 public:
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);
511
512     virtual void render(bool);
513     virtual std::string getName();
514     virtual void update(double dt);
515
516     bool hasActiveTraffic() {
517         return ! activeTraffic.empty();
518     };
519     TrafficVector &getActiveTraffic() {
520         return activeTraffic;
521     };
522
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,
525                                 AtcMsgDir msgDir);
526
527 };
528
529 /******************************************************************************
530  * class FGTowerControl
531  *****************************************************************************/
532 class FGApproachController : public FGATCController
533 {
534 private:
535     TrafficVector activeTraffic;
536     ActiveRunwayVec activeRunways;
537     FGAirportDynamics *parent;
538
539 public:
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);
551
552     virtual void render(bool);
553     virtual std::string getName();
554     virtual void update(double dt);
555
556     ActiveRunway* getRunway(const std::string& name);
557
558     bool hasActiveTraffic() {
559         return ! activeTraffic.empty();
560     };
561     TrafficVector &getActiveTraffic() {
562         return activeTraffic;
563     };
564 };
565
566
567 #endif // _TRAFFIC_CONTROL_HXX