]> git.mxchange.org Git - flightgear.git/blob - src/ATC/trafficcontrol.hxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[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
35
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 protected:
227   bool available;
228   time_t lastTransmission;
229
230   double dt_count;
231
232
233   string formatATCFrequency3_2(int );
234   string genTransponderCode(string fltRules);
235
236 public:
237   typedef enum {
238       MSG_ANNOUNCE_ENGINE_START,
239       MSG_REQUEST_ENGINE_START,
240       MSG_PERMIT_ENGINE_START,
241       MSG_DENY_ENGINE_START,
242       MSG_ACKNOWLEDGE_ENGINE_START,
243       MSG_REQUEST_PUSHBACK_CLEARANCE,
244       MSG_PERMIT_PUSHBACK_CLEARANCE,
245       MSG_HOLD_PUSHBACK_CLEARANCE,
246       MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
247       MSG_INITIATE_CONTACT,
248       MSG_ACKNOWLEDGE_INITIATE_CONTACT,
249       MSG_REQUEST_TAXI_CLEARANCE,
250       MSG_ISSUE_TAXI_CLEARANCE,
251       MSG_ACKNOWLEDGE_TAXI_CLEARANCE,
252       MSG_HOLD_POSITION,
253       MSG_ACKNOWLEDGE_HOLD_POSITION,
254       MSG_RESUME_TAXI,
255       MSG_ACKNOWLEDGE_RESUME_TAXI } AtcMsgId;
256   typedef enum {
257       ATC_AIR_TO_GROUND,
258       ATC_GROUND_TO_AIR } AtcMsgDir;
259   FGATCController();
260   virtual ~FGATCController() {};
261   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
262                                 double lat, double lon,
263                                 double hdg, double spd, double alt, double radius, int leg,
264                                 FGAIAircraft *aircraft) = 0;
265   virtual void             signOff(int id) = 0;
266   virtual void             update(int id, double lat, double lon, 
267                                   double heading, double speed, double alt, double dt) = 0;
268   virtual bool             hasInstruction(int id) = 0;
269   virtual FGATCInstruction getInstruction(int id) = 0;
270
271   double getDt() { return dt_count; };
272   void   setDt(double dt) { dt_count = dt;};
273   void transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir);
274   string getGateName(FGAIAircraft *aircraft);
275 };
276
277 /******************************************************************************
278  * class FGTowerControl
279  *****************************************************************************/
280 class FGTowerController : public FGATCController
281 {
282 private:
283   TrafficVector activeTraffic;
284   ActiveRunwayVec activeRunways;
285   
286 public:
287   FGTowerController();
288   virtual ~FGTowerController() {};
289   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
290                                 double lat, double lon,
291                                 double hdg, double spd, double alt, double radius, int leg,
292                                 FGAIAircraft *aircraft);
293   virtual void             signOff(int id);
294   virtual void             update(int id, double lat, double lon, 
295                                   double heading, double speed, double alt, double dt);
296   virtual bool             hasInstruction(int id);
297   virtual FGATCInstruction getInstruction(int id);
298
299   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
300   TrafficVector &getActiveTraffic() { return activeTraffic; };
301 };
302
303 /******************************************************************************
304  * class FGStartupController
305  * handle 
306  *****************************************************************************/
307
308 class FGStartupController : public FGATCController
309 {
310 private:
311   TrafficVector activeTraffic;
312   //ActiveRunwayVec activeRunways;
313   
314 public:
315   FGStartupController();
316   virtual ~FGStartupController() {};
317   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
318                                 double lat, double lon,
319                                 double hdg, double spd, double alt, double radius, int leg,
320                                 FGAIAircraft *aircraft);
321   virtual void             signOff(int id);
322   virtual void             update(int id, double lat, double lon, 
323                                   double heading, double speed, double alt, double dt);
324   virtual bool             hasInstruction(int id);
325   virtual FGATCInstruction getInstruction(int id);
326
327   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
328   TrafficVector &getActiveTraffic() { return activeTraffic; };
329
330 }; 
331
332 /******************************************************************************
333  * class FGTowerControl
334  *****************************************************************************/
335 class FGApproachController : public FGATCController
336 {
337 private:
338   TrafficVector activeTraffic;
339   ActiveRunwayVec activeRunways;
340   
341 public:
342   FGApproachController();
343   virtual ~FGApproachController() {};
344   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
345                                 double lat, double lon,
346                                 double hdg, double spd, double alt, double radius, int leg,
347                                 FGAIAircraft *aircraft);
348   virtual void             signOff(int id);
349   virtual void             update(int id, double lat, double lon, 
350                                   double heading, double speed, double alt, double dt);
351   virtual bool             hasInstruction(int id);
352   virtual FGATCInstruction getInstruction(int id);
353
354   ActiveRunway* getRunway(string name);
355
356   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
357   TrafficVector &getActiveTraffic() { return activeTraffic; };
358 };
359
360
361 #endif // _TRAFFIC_CONTROL_HXX