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