]> git.mxchange.org Git - flightgear.git/blob - src/ATC/trafficcontrol.hxx
Assigned an ATC controller to the user's Aircraft and change the comm1 radio frequenc...
[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
38
39 #include <string>
40 #include <vector>
41
42 using std::string;
43 using std::vector;
44
45
46 typedef vector<int> intVec;
47 typedef vector<int>::iterator intVecIterator;
48
49
50 class FGAIFlightPlan;  // forward reference
51 class FGGroundNetwork; // forward reference
52 class FGAIAircraft;    // forward reference
53
54 /**************************************************************************************
55  * class FGATCInstruction
56  * like class FGATC Controller, this class definition should go into its own file
57  * and or directory... For now, just testing this stuff out though...
58  *************************************************************************************/
59 class FGATCInstruction
60 {
61 private:
62   bool holdPattern;
63   bool holdPosition;
64   bool changeSpeed;
65   bool changeHeading;
66   bool changeAltitude;
67   bool resolveCircularWait;
68
69   double speed;
70   double heading;
71   double alt;
72 public:
73
74   FGATCInstruction();
75   bool hasInstruction   ();
76   bool getHoldPattern   () { return holdPattern;    };
77   bool getHoldPosition  () { return holdPosition;   };
78   bool getChangeSpeed   () { return changeSpeed;    };
79   bool getChangeHeading () { return changeHeading;  };
80   bool getChangeAltitude() { return changeAltitude; };
81
82   double getSpeed       () { return speed; };
83   double getHeading     () { return heading; };
84   double getAlt         () { return alt; };
85
86   bool getCheckForCircularWait() { return resolveCircularWait; };
87
88   void setHoldPattern   (bool val) { holdPattern    = val; };
89   void setHoldPosition  (bool val) { holdPosition   = val; };
90   void setChangeSpeed   (bool val) { changeSpeed    = val; };
91   void setChangeHeading (bool val) { changeHeading  = val; };
92   void setChangeAltitude(bool val) { changeAltitude = val; };
93
94   void setResolveCircularWait (bool val) { resolveCircularWait = val; }; 
95
96   void setSpeed       (double val) { speed   = val; };
97   void setHeading     (double val) { heading = val; };
98   void setAlt         (double val) { alt     = val; };
99 };
100
101
102
103
104
105 /**************************************************************************************
106  * class FGTrafficRecord
107  *************************************************************************************/
108 class FGTrafficRecord
109 {
110 private:
111   int id, waitsForId;
112   int currentPos;
113   int leg;
114   int frequencyId;
115   int state;
116   bool allowTransmission;
117   time_t timer;
118   intVec intentions;
119   FGATCInstruction instruction;
120   double latitude, longitude, heading, speed, altitude, radius;
121   string runway;
122   //FGAISchedule *trafficRef;
123   FGAIAircraft *aircraft;
124   
125   
126 public:
127   FGTrafficRecord();
128   
129   void setId(int val)  { id = val; };
130   void setRadius(double rad) { radius = rad;};
131   void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
132   void setRunway(string rwy) { runway = rwy;};
133   void setLeg(int lg) { leg = lg;};
134   int getId() { return id;};
135   int getState() { return state;};
136   void setState(int s) { state = s;}
137   FGATCInstruction getInstruction() { return instruction;};
138   bool hasInstruction() { return instruction.hasInstruction(); };
139   void setPositionAndHeading(double lat, double lon, double hdg, double spd, double alt);
140   bool checkPositionAndIntentions(FGTrafficRecord &other);
141   int  crosses                   (FGGroundNetwork *, FGTrafficRecord &other); 
142   bool isOpposing                (FGGroundNetwork *, FGTrafficRecord &other, int node);
143
144   bool onRoute(FGGroundNetwork *, FGTrafficRecord &other);
145
146   bool getSpeedAdjustment() { return instruction.getChangeSpeed(); };
147   
148   double getLatitude () { return latitude ; };
149   double getLongitude() { return longitude; };
150   double getHeading  () { return heading  ; };
151   double getSpeed    () { return speed    ; };
152   double getAltitude () { return altitude ; };
153   double getRadius   () { return radius   ; };
154
155   int getWaitsForId  () { return waitsForId; };
156
157   void setSpeedAdjustment(double spd);
158   void setHeadingAdjustment(double heading);
159   void clearSpeedAdjustment  () { instruction.setChangeSpeed  (false); };
160   void clearHeadingAdjustment() { instruction.setChangeHeading(false); };
161
162   bool hasHeadingAdjustment() { return instruction.getChangeHeading(); };
163   bool hasHoldPosition() { return instruction.getHoldPosition(); };
164   void setHoldPosition (bool inst) { instruction.setHoldPosition(inst); };
165
166   void setWaitsForId(int id) { waitsForId = id; };
167
168   void setResolveCircularWait()   { instruction.setResolveCircularWait(true);  };
169   void clearResolveCircularWait() { instruction.setResolveCircularWait(false); };
170
171   string getRunway() { return runway; };
172   //void setCallSign(string clsgn) { callsign = clsgn; };
173   void setAircraft(FGAIAircraft *ref) { aircraft = ref;};
174   void updateState() { state++; allowTransmission=true; };
175   //string getCallSign() { return callsign; };
176   FGAIAircraft *getAircraft() { return aircraft;};
177   int getTime() { return timer; };
178   int getLeg() { return leg; };
179   void setTime(time_t time) { timer = time; };
180
181   bool pushBackAllowed();
182   bool allowTransmissions() { return allowTransmission; };
183   void suppressRepeatedTransmissions () { allowTransmission=false; };
184   void allowRepeatedTransmissions () { allowTransmission=true; };
185   void nextFrequency() { frequencyId++; };
186   int  getNextFrequency() { return frequencyId; };
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 protected:
231   bool available;
232   time_t lastTransmission;
233
234   double dt_count;
235
236
237   string formatATCFrequency3_2(int );
238   string genTransponderCode(string fltRules);
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);
280   string getGateName(FGAIAircraft *aircraft);
281 };
282
283 /******************************************************************************
284  * class FGTowerControl
285  *****************************************************************************/
286 class FGTowerController : public FGATCController
287 {
288 private:
289   TrafficVector activeTraffic;
290   ActiveRunwayVec activeRunways;
291   
292 public:
293   FGTowerController();
294   virtual ~FGTowerController() {};
295   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
296                                 double lat, double lon,
297                                 double hdg, double spd, double alt, double radius, int leg,
298                                 FGAIAircraft *aircraft);
299   virtual void             signOff(int id);
300   virtual void             updateAircraftInformation(int id, double lat, double lon, 
301                                   double heading, double speed, double alt, double dt);
302   virtual bool             hasInstruction(int id);
303   virtual FGATCInstruction getInstruction(int id);
304
305   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
306   TrafficVector &getActiveTraffic() { return activeTraffic; };
307 };
308
309 /******************************************************************************
310  * class FGStartupController
311  * handle 
312  *****************************************************************************/
313
314 class FGStartupController : public FGATCController
315 {
316 private:
317   TrafficVector activeTraffic;
318   //ActiveRunwayVec activeRunways;
319   
320 public:
321   FGStartupController();
322   virtual ~FGStartupController() {};
323   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
324                                 double lat, double lon,
325                                 double hdg, double spd, double alt, double radius, int leg,
326                                 FGAIAircraft *aircraft);
327   virtual void             signOff(int id);
328   virtual void             updateAircraftInformation(int id, double lat, double lon, 
329                                   double heading, double speed, double alt, double dt);
330   virtual bool             hasInstruction(int id);
331   virtual FGATCInstruction getInstruction(int id);
332
333   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
334   TrafficVector &getActiveTraffic() { return activeTraffic; };
335
336 }; 
337
338 /******************************************************************************
339  * class FGTowerControl
340  *****************************************************************************/
341 class FGApproachController : public FGATCController
342 {
343 private:
344   TrafficVector activeTraffic;
345   ActiveRunwayVec activeRunways;
346   
347 public:
348   FGApproachController();
349   virtual ~FGApproachController() {};
350   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
351                                 double lat, double lon,
352                                 double hdg, double spd, double alt, double radius, int leg,
353                                 FGAIAircraft *aircraft);
354   virtual void             signOff(int id);
355   virtual void             updateAircraftInformation(int id, double lat, double lon, 
356                                   double heading, double speed, double alt, double dt);
357   virtual bool             hasInstruction(int id);
358   virtual FGATCInstruction getInstruction(int id);
359
360   ActiveRunway* getRunway(string name);
361
362   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
363   TrafficVector &getActiveTraffic() { return activeTraffic; };
364 };
365
366
367 #endif // _TRAFFIC_CONTROL_HXX