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