]> git.mxchange.org Git - flightgear.git/blob - src/ATC/trafficcontrol.hxx
AI aircraft will need to switch frequencies before being able to request
[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 FGAISchedule;    // forward reference
49 class FGAIAircraft;    // forward reference
50
51 /**************************************************************************************
52  * class FGATCInstruction
53  * like class FGATC Controller, this class definition should go into its own file
54  * and or directory... For now, just testing this stuff out though...
55  *************************************************************************************/
56 class FGATCInstruction
57 {
58 private:
59   bool holdPattern;
60   bool holdPosition;
61   bool changeSpeed;
62   bool changeHeading;
63   bool changeAltitude;
64   bool resolveCircularWait;
65
66   double speed;
67   double heading;
68   double alt;
69 public:
70
71   FGATCInstruction();
72   bool hasInstruction   ();
73   bool getHoldPattern   () { return holdPattern;    };
74   bool getHoldPosition  () { return holdPosition;   };
75   bool getChangeSpeed   () { return changeSpeed;    };
76   bool getChangeHeading () { return changeHeading;  };
77   bool getChangeAltitude() { return changeAltitude; };
78
79   double getSpeed       () { return speed; };
80   double getHeading     () { return heading; };
81   double getAlt         () { return alt; };
82
83   bool getCheckForCircularWait() { return resolveCircularWait; };
84
85   void setHoldPattern   (bool val) { holdPattern    = val; };
86   void setHoldPosition  (bool val) { holdPosition   = val; };
87   void setChangeSpeed   (bool val) { changeSpeed    = val; };
88   void setChangeHeading (bool val) { changeHeading  = val; };
89   void setChangeAltitude(bool val) { changeAltitude = val; };
90
91   void setResolveCircularWait (bool val) { resolveCircularWait = val; }; 
92
93   void setSpeed       (double val) { speed   = val; };
94   void setHeading     (double val) { heading = val; };
95   void setAlt         (double val) { alt     = val; };
96 };
97
98
99
100
101
102 /**************************************************************************************
103  * class FGTrafficRecord
104  *************************************************************************************/
105 class FGTrafficRecord
106 {
107 private:
108   int id, waitsForId;
109   int currentPos;
110   int leg;
111   int frequencyId;
112   int state;
113   bool allowTransmission;
114   time_t timer;
115   intVec intentions;
116   FGATCInstruction instruction;
117   double latitude, longitude, heading, speed, altitude, radius;
118   string runway;
119   //FGAISchedule *trafficRef;
120   FGAIAircraft *aircraft;
121   
122   
123 public:
124   FGTrafficRecord();
125   
126   void setId(int val)  { id = val; };
127   void setRadius(double rad) { radius = rad;};
128   void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
129   void setRunway(string rwy) { runway = rwy;};
130   void setLeg(int lg) { leg = lg;};
131   int getId() { return id;};
132   int getState() { return state;};
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 private:
215   double dt_count;
216
217
218   string formatATCFrequency3_2(int );
219   string genTransponderCode(string fltRules);
220
221 public:
222   typedef enum {
223       MSG_ANNOUNCE_ENGINE_START,
224       MSG_REQUEST_ENGINE_START, 
225       MSG_PERMIT_ENGINE_START,
226       MSG_DENY_ENGINE_START,
227       MSG_ACKNOWLEDGE_ENGINE_START,
228       MSG_REQUEST_PUSHBACK_CLEARANCE,
229       MSG_PERMIT_PUSHBACK_CLEARANCE, 
230       MSG_HOLD_PUSHBACK_CLEARANCE,
231       MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY } AtcMsgId;
232   typedef enum {
233       ATC_AIR_TO_GROUND,
234       ATC_GROUND_TO_AIR } AtcMsgDir;
235   FGATCController() { dt_count = 0;};
236   virtual ~FGATCController() {};
237   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
238                                 double lat, double lon,
239                                 double hdg, double spd, double alt, double radius, int leg,
240                                 FGAIAircraft *aircraft) = 0;
241   virtual void             signOff(int id) = 0;
242   virtual void             update(int id, double lat, double lon, 
243                                   double heading, double speed, double alt, double dt) = 0;
244   virtual bool             hasInstruction(int id) = 0;
245   virtual FGATCInstruction getInstruction(int id) = 0;
246
247   double getDt() { return dt_count; };
248   void   setDt(double dt) { dt_count = dt;};
249   void transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir);
250   string getGateName(FGAIAircraft *aircraft);
251 };
252
253 /******************************************************************************
254  * class FGTowerControl
255  *****************************************************************************/
256 class FGTowerController : public FGATCController
257 {
258 private:
259   TrafficVector activeTraffic;
260   ActiveRunwayVec activeRunways;
261   
262 public:
263   FGTowerController();
264   virtual ~FGTowerController() {};
265   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
266                                 double lat, double lon,
267                                 double hdg, double spd, double alt, double radius, int leg,
268                                 FGAIAircraft *aircraft);
269   virtual void             signOff(int id);
270   virtual void             update(int id, double lat, double lon, 
271                                   double heading, double speed, double alt, double dt);
272   virtual bool             hasInstruction(int id);
273   virtual FGATCInstruction getInstruction(int id);
274
275   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
276   TrafficVector &getActiveTraffic() { return activeTraffic; };
277 };
278
279 /******************************************************************************
280  * class FGStartupController
281  * handle 
282  *****************************************************************************/
283
284 class FGStartupController : public FGATCController
285 {
286 private:
287   TrafficVector activeTraffic;
288   bool available;
289   time_t lastTransmission;
290   //ActiveRunwayVec activeRunways;
291   
292 public:
293   FGStartupController();
294   virtual ~FGStartupController() {};
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             update(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 #endif // _TRAFFIC_CONTROL_HXX