]> git.mxchange.org Git - flightgear.git/blob - src/ATC/trafficcontrol.hxx
Removal of some debugging statements.
[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 #include <string>
42 #include <vector>
43
44 using std::string;
45 using std::vector;
46
47
48 typedef vector<int> intVec;
49 typedef vector<int>::iterator intVecIterator;
50
51
52 class FGAIFlightPlan;  // forward reference
53 class FGGroundNetwork; // forward reference
54 class FGAIAircraft;    // forward reference
55 class FGAirportDynamics;
56
57 /**************************************************************************************
58  * class FGATCInstruction
59  * like class FGATC Controller, this class definition should go into its own file
60  * and or directory... For now, just testing this stuff out though...
61  *************************************************************************************/
62 class FGATCInstruction
63 {
64 private:
65   bool holdPattern;
66   bool holdPosition;
67   bool changeSpeed;
68   bool changeHeading;
69   bool changeAltitude;
70   bool resolveCircularWait;
71
72   double speed;
73   double heading;
74   double alt;
75 public:
76
77   FGATCInstruction();
78   bool hasInstruction   ();
79   bool getHoldPattern   () { return holdPattern;    };
80   bool getHoldPosition  () { return holdPosition;   };
81   bool getChangeSpeed   () { return changeSpeed;    };
82   bool getChangeHeading () { return changeHeading;  };
83   bool getChangeAltitude() { return changeAltitude; };
84
85   double getSpeed       () { return speed; };
86   double getHeading     () { return heading; };
87   double getAlt         () { return alt; };
88
89   bool getCheckForCircularWait() { return resolveCircularWait; };
90
91   void setHoldPattern   (bool val) { holdPattern    = val; };
92   void setHoldPosition  (bool val) { holdPosition   = val; };
93   void setChangeSpeed   (bool val) { changeSpeed    = val; };
94   void setChangeHeading (bool val) { changeHeading  = val; };
95   void setChangeAltitude(bool val) { changeAltitude = val; };
96
97   void setResolveCircularWait (bool val) { resolveCircularWait = val; }; 
98
99   void setSpeed       (double val) { speed   = val; };
100   void setHeading     (double val) { heading = val; };
101   void setAlt         (double val) { alt     = val; };
102 };
103
104
105
106
107
108 /**************************************************************************************
109  * class FGTrafficRecord
110  *************************************************************************************/
111 class FGTrafficRecord
112 {
113 private:
114   int id, waitsForId;
115   int currentPos;
116   int leg;
117   int frequencyId;
118   int state;
119   bool allowTransmission;
120   time_t timer;
121   intVec intentions;
122   FGATCInstruction instruction;
123   double latitude, longitude, heading, speed, altitude, radius;
124   string runway;
125   //FGAISchedule *trafficRef;
126   FGAIAircraft *aircraft;
127   
128   
129 public:
130   FGTrafficRecord();
131   
132   void setId(int val)  { id = val; };
133   void setRadius(double rad) { radius = rad;};
134   void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
135   void setRunway(string rwy) { runway = rwy;};
136   void setLeg(int lg) { leg = lg;};
137   int getId() { return id;};
138   int getState() { return state;};
139   void setState(int s) { state = s;}
140   FGATCInstruction getInstruction() { return instruction;};
141   bool hasInstruction() { return instruction.hasInstruction(); };
142   void setPositionAndHeading(double lat, double lon, double hdg, double spd, double alt);
143   bool checkPositionAndIntentions(FGTrafficRecord &other);
144   int  crosses                   (FGGroundNetwork *, FGTrafficRecord &other); 
145   bool isOpposing                (FGGroundNetwork *, FGTrafficRecord &other, int node);
146
147   bool onRoute(FGGroundNetwork *, FGTrafficRecord &other);
148
149   bool getSpeedAdjustment() { return instruction.getChangeSpeed(); };
150   
151   double getLatitude () { return latitude ; };
152   double getLongitude() { return longitude; };
153   double getHeading  () { return heading  ; };
154   double getSpeed    () { return speed    ; };
155   double getAltitude () { return altitude ; };
156   double getRadius   () { return radius   ; };
157
158   int getWaitsForId  () { return waitsForId; };
159
160   void setSpeedAdjustment(double spd);
161   void setHeadingAdjustment(double heading);
162   void clearSpeedAdjustment  () { instruction.setChangeSpeed  (false); };
163   void clearHeadingAdjustment() { instruction.setChangeHeading(false); };
164
165   bool hasHeadingAdjustment() { return instruction.getChangeHeading(); };
166   bool hasHoldPosition() { return instruction.getHoldPosition(); };
167   void setHoldPosition (bool inst) { instruction.setHoldPosition(inst); };
168
169   void setWaitsForId(int id) { waitsForId = id; };
170
171   void setResolveCircularWait()   { instruction.setResolveCircularWait(true);  };
172   void clearResolveCircularWait() { instruction.setResolveCircularWait(false); };
173
174   string getRunway() { return runway; };
175   //void setCallSign(string clsgn) { callsign = clsgn; };
176   void setAircraft(FGAIAircraft *ref) { aircraft = ref;};
177   void updateState() { state++; allowTransmission=true; };
178   //string getCallSign() { return callsign; };
179   FGAIAircraft *getAircraft() { return aircraft;};
180   int getTime() { return timer; };
181   int getLeg() { return leg; };
182   void setTime(time_t time) { timer = time; };
183
184   bool pushBackAllowed();
185   bool allowTransmissions() { return allowTransmission; };
186   void suppressRepeatedTransmissions () { allowTransmission=false; };
187   void allowRepeatedTransmissions () { allowTransmission=true; };
188   void nextFrequency() { frequencyId++; };
189   int  getNextFrequency() { return frequencyId; };
190   intVec& getIntentions() { return intentions; };
191   int getCurrentPosition() { return currentPos; };
192 };
193
194 typedef vector<FGTrafficRecord> TrafficVector;
195 typedef vector<FGTrafficRecord>::iterator TrafficVectorIterator;
196
197 typedef vector<time_t> TimeVector;
198 typedef vector<time_t>::iterator TimeVectorIterator;
199
200
201 /***********************************************************************
202  * Active runway, a utility class to keep track of which aircraft has
203  * clearance for a given runway.
204  **********************************************************************/
205 class ActiveRunway
206 {
207 private:
208   string rwy;
209   int currentlyCleared;
210   double distanceToFinal;
211   TimeVector estimatedArrivalTimes;
212 public:
213   ActiveRunway(string r, int cc) { rwy = r; currentlyCleared = cc; distanceToFinal = 6.0 * SG_NM_TO_METER; };
214   
215   string getRunwayName() { return rwy; };
216   int    getCleared   () { return currentlyCleared; };
217   double getApproachDistance() { return distanceToFinal; };
218   //time_t getEstApproachTime() { return estimatedArrival; };
219
220   //void setEstApproachTime(time_t time) { estimatedArrival = time; };
221   time_t requestTimeSlot(time_t eta);
222 };
223
224 typedef vector<ActiveRunway> ActiveRunwayVec;
225 typedef vector<ActiveRunway>::iterator ActiveRunwayVecIterator;
226
227 /**
228  * class FGATCController
229  * NOTE: this class serves as an abstraction layer for all sorts of ATC controllers. 
230  *************************************************************************************/
231 class FGATCController
232 {
233 private:
234      bool initialized;
235
236 protected:
237   bool available;
238   time_t lastTransmission;
239
240   double dt_count;
241   osg::Group* group;
242
243   string formatATCFrequency3_2(int );
244   string genTransponderCode(string fltRules);
245   bool isUserAircraft(FGAIAircraft*); 
246
247 public:
248   typedef enum {
249       MSG_ANNOUNCE_ENGINE_START,
250       MSG_REQUEST_ENGINE_START,
251       MSG_PERMIT_ENGINE_START,
252       MSG_DENY_ENGINE_START,
253       MSG_ACKNOWLEDGE_ENGINE_START,
254       MSG_REQUEST_PUSHBACK_CLEARANCE,
255       MSG_PERMIT_PUSHBACK_CLEARANCE,
256       MSG_HOLD_PUSHBACK_CLEARANCE,
257       MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
258       MSG_INITIATE_CONTACT,
259       MSG_ACKNOWLEDGE_INITIATE_CONTACT,
260       MSG_REQUEST_TAXI_CLEARANCE,
261       MSG_ISSUE_TAXI_CLEARANCE,
262       MSG_ACKNOWLEDGE_TAXI_CLEARANCE,
263       MSG_HOLD_POSITION,
264       MSG_ACKNOWLEDGE_HOLD_POSITION,
265       MSG_RESUME_TAXI,
266       MSG_ACKNOWLEDGE_RESUME_TAXI } AtcMsgId;
267   typedef enum {
268       ATC_AIR_TO_GROUND,
269       ATC_GROUND_TO_AIR } AtcMsgDir;
270   FGATCController();
271   virtual ~FGATCController();
272   void init();
273
274   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
275                                 double lat, double lon,
276                                 double hdg, double spd, double alt, double radius, int leg,
277                                 FGAIAircraft *aircraft) = 0;
278   virtual void             signOff(int id) = 0;
279   virtual void             updateAircraftInformation(int id, double lat, double lon, 
280                                                      double heading, double speed, double alt, double dt) = 0;
281   virtual bool             hasInstruction(int id) = 0;
282   virtual FGATCInstruction getInstruction(int id) = 0;
283
284   double getDt() { return dt_count; };
285   void   setDt(double dt) { dt_count = dt;};
286   void transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir, bool audible);
287   string getGateName(FGAIAircraft *aircraft);
288   virtual void render(bool) = 0;
289   virtual string getName()  = 0;
290
291
292 private:
293
294  AtcMsgDir lastTransmissionDirection;
295 };
296
297 /******************************************************************************
298  * class FGTowerControl
299  *****************************************************************************/
300 class FGTowerController : public FGATCController
301 {
302 private:
303   TrafficVector activeTraffic;
304   ActiveRunwayVec activeRunways;
305   FGAirportDynamics *parent;
306   
307 public:
308   FGTowerController(FGAirportDynamics *parent);
309   virtual ~FGTowerController() {};
310   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
311                                 double lat, double lon,
312                                 double hdg, double spd, double alt, double radius, int leg,
313                                 FGAIAircraft *aircraft);
314   virtual void             signOff(int id);
315   virtual void             updateAircraftInformation(int id, double lat, double lon, 
316                                   double heading, double speed, double alt, double dt);
317   virtual bool             hasInstruction(int id);
318   virtual FGATCInstruction getInstruction(int id);
319
320   virtual void render(bool);
321   virtual string getName();
322   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
323   TrafficVector &getActiveTraffic() { return activeTraffic; };
324 };
325
326 /******************************************************************************
327  * class FGStartupController
328  * handle 
329  *****************************************************************************/
330
331 class FGStartupController : public FGATCController
332 {
333 private:
334   TrafficVector activeTraffic;
335   //ActiveRunwayVec activeRunways;
336   FGAirportDynamics *parent;
337   
338 public:
339   FGStartupController(FGAirportDynamics *parent);
340   virtual ~FGStartupController() {};
341   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
342                                 double lat, double lon,
343                                 double hdg, double spd, double alt, double radius, int leg,
344                                 FGAIAircraft *aircraft);
345   virtual void             signOff(int id);
346   virtual void             updateAircraftInformation(int id, double lat, double lon, 
347                                   double heading, double speed, double alt, double dt);
348   virtual bool             hasInstruction(int id);
349   virtual FGATCInstruction getInstruction(int id);
350
351   virtual void render(bool);
352   virtual string getName();
353
354   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
355   TrafficVector &getActiveTraffic() { return activeTraffic; };
356
357   // 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.
358   bool checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
359                                AtcMsgDir msgDir);
360
361 }; 
362
363 /******************************************************************************
364  * class FGTowerControl
365  *****************************************************************************/
366 class FGApproachController : public FGATCController
367 {
368 private:
369   TrafficVector activeTraffic;
370   ActiveRunwayVec activeRunways;
371   FGAirportDynamics *parent;
372   
373 public:
374   FGApproachController(FGAirportDynamics * parent);
375   virtual ~FGApproachController() { };
376   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
377                                 double lat, double lon,
378                                 double hdg, double spd, double alt, double radius, int leg,
379                                 FGAIAircraft *aircraft);
380   virtual void             signOff(int id);
381   virtual void             updateAircraftInformation(int id, double lat, double lon, 
382                                   double heading, double speed, double alt, double dt);
383   virtual bool             hasInstruction(int id);
384   virtual FGATCInstruction getInstruction(int id);
385
386   virtual void render(bool);
387   virtual string getName();
388
389   ActiveRunway* getRunway(string name);
390
391   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
392   TrafficVector &getActiveTraffic() { return activeTraffic; };
393 };
394
395
396 #endif // _TRAFFIC_CONTROL_HXX