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