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