]> git.mxchange.org Git - flightgear.git/blob - src/ATC/trafficcontrol.hxx
More work on AI/ATC integration:
[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   intVec& getIntentions() { return intentions; };
186   int getCurrentPosition() { return currentPos; };
187 };
188
189 typedef vector<FGTrafficRecord> TrafficVector;
190 typedef vector<FGTrafficRecord>::iterator TrafficVectorIterator;
191
192 typedef vector<time_t> TimeVector;
193 typedef vector<time_t>::iterator TimeVectorIterator;
194
195
196 /***********************************************************************
197  * Active runway, a utility class to keep track of which aircraft has
198  * clearance for a given runway.
199  **********************************************************************/
200 class ActiveRunway
201 {
202 private:
203   string rwy;
204   int currentlyCleared;
205   double distanceToFinal;
206   TimeVector estimatedArrivalTimes;
207 public:
208   ActiveRunway(string r, int cc) { rwy = r; currentlyCleared = cc; distanceToFinal = 6.0 * SG_NM_TO_METER; };
209   
210   string getRunwayName() { return rwy; };
211   int    getCleared   () { return currentlyCleared; };
212   double getApproachDistance() { return distanceToFinal; };
213   //time_t getEstApproachTime() { return estimatedArrival; };
214
215   //void setEstApproachTime(time_t time) { estimatedArrival = time; };
216   time_t requestTimeSlot(time_t eta);
217 };
218
219 typedef vector<ActiveRunway> ActiveRunwayVec;
220 typedef vector<ActiveRunway>::iterator ActiveRunwayVecIterator;
221
222 /**
223  * class FGATCController
224  * NOTE: this class serves as an abstraction layer for all sorts of ATC controllers. 
225  *************************************************************************************/
226 class FGATCController
227 {
228 private:
229      bool initialized;
230
231 protected:
232   bool available;
233   time_t lastTransmission;
234
235   double dt_count;
236
237
238   string formatATCFrequency3_2(int );
239   string genTransponderCode(string fltRules);
240   bool isUserAircraft(FGAIAircraft*); 
241
242 public:
243   typedef enum {
244       MSG_ANNOUNCE_ENGINE_START,
245       MSG_REQUEST_ENGINE_START,
246       MSG_PERMIT_ENGINE_START,
247       MSG_DENY_ENGINE_START,
248       MSG_ACKNOWLEDGE_ENGINE_START,
249       MSG_REQUEST_PUSHBACK_CLEARANCE,
250       MSG_PERMIT_PUSHBACK_CLEARANCE,
251       MSG_HOLD_PUSHBACK_CLEARANCE,
252       MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
253       MSG_INITIATE_CONTACT,
254       MSG_ACKNOWLEDGE_INITIATE_CONTACT,
255       MSG_REQUEST_TAXI_CLEARANCE,
256       MSG_ISSUE_TAXI_CLEARANCE,
257       MSG_ACKNOWLEDGE_TAXI_CLEARANCE,
258       MSG_HOLD_POSITION,
259       MSG_ACKNOWLEDGE_HOLD_POSITION,
260       MSG_RESUME_TAXI,
261       MSG_ACKNOWLEDGE_RESUME_TAXI } AtcMsgId;
262   typedef enum {
263       ATC_AIR_TO_GROUND,
264       ATC_GROUND_TO_AIR } AtcMsgDir;
265   FGATCController();
266   virtual ~FGATCController();
267   void init();
268
269   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
270                                 double lat, double lon,
271                                 double hdg, double spd, double alt, double radius, int leg,
272                                 FGAIAircraft *aircraft) = 0;
273   virtual void             signOff(int id) = 0;
274   virtual void             updateAircraftInformation(int id, double lat, double lon, 
275                                                      double heading, double speed, double alt, double dt) = 0;
276   virtual bool             hasInstruction(int id) = 0;
277   virtual FGATCInstruction getInstruction(int id) = 0;
278
279   double getDt() { return dt_count; };
280   void   setDt(double dt) { dt_count = dt;};
281   void transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir, bool audible);
282   string getGateName(FGAIAircraft *aircraft);
283
284 private:
285  AtcMsgDir lastTransmissionDirection;
286 };
287
288 /******************************************************************************
289  * class FGTowerControl
290  *****************************************************************************/
291 class FGTowerController : public FGATCController
292 {
293 private:
294   TrafficVector activeTraffic;
295   ActiveRunwayVec activeRunways;
296   
297 public:
298   FGTowerController();
299   virtual ~FGTowerController() {};
300   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
301                                 double lat, double lon,
302                                 double hdg, double spd, double alt, double radius, int leg,
303                                 FGAIAircraft *aircraft);
304   virtual void             signOff(int id);
305   virtual void             updateAircraftInformation(int id, double lat, double lon, 
306                                   double heading, double speed, double alt, double dt);
307   virtual bool             hasInstruction(int id);
308   virtual FGATCInstruction getInstruction(int id);
309
310   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
311   TrafficVector &getActiveTraffic() { return activeTraffic; };
312 };
313
314 /******************************************************************************
315  * class FGStartupController
316  * handle 
317  *****************************************************************************/
318
319 class FGStartupController : public FGATCController
320 {
321 private:
322   TrafficVector activeTraffic;
323   //ActiveRunwayVec activeRunways;
324   
325 public:
326   FGStartupController();
327   virtual ~FGStartupController() {};
328   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
329                                 double lat, double lon,
330                                 double hdg, double spd, double alt, double radius, int leg,
331                                 FGAIAircraft *aircraft);
332   virtual void             signOff(int id);
333   virtual void             updateAircraftInformation(int id, double lat, double lon, 
334                                   double heading, double speed, double alt, double dt);
335   virtual bool             hasInstruction(int id);
336   virtual FGATCInstruction getInstruction(int id);
337
338   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
339   TrafficVector &getActiveTraffic() { return activeTraffic; };
340
341   // 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.
342   bool checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
343                                AtcMsgDir msgDir);
344
345 }; 
346
347 /******************************************************************************
348  * class FGTowerControl
349  *****************************************************************************/
350 class FGApproachController : public FGATCController
351 {
352 private:
353   TrafficVector activeTraffic;
354   ActiveRunwayVec activeRunways;
355   
356 public:
357   FGApproachController();
358   virtual ~FGApproachController() {};
359   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
360                                 double lat, double lon,
361                                 double hdg, double spd, double alt, double radius, int leg,
362                                 FGAIAircraft *aircraft);
363   virtual void             signOff(int id);
364   virtual void             updateAircraftInformation(int id, double lat, double lon, 
365                                   double heading, double speed, double alt, double dt);
366   virtual bool             hasInstruction(int id);
367   virtual FGATCInstruction getInstruction(int id);
368
369   ActiveRunway* getRunway(string name);
370
371   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
372   TrafficVector &getActiveTraffic() { return activeTraffic; };
373 };
374
375
376 #endif // _TRAFFIC_CONTROL_HXX