]> git.mxchange.org Git - flightgear.git/blob - src/ATC/trafficcontrol.hxx
ef984efcac42091e97c0d628db61e47998eec51e
[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 state;
111   time_t timer;
112   intVec intentions;
113   FGATCInstruction instruction;
114   double latitude, longitude, heading, speed, altitude, radius;
115   string runway;
116   //FGAISchedule *trafficRef;
117   FGAIAircraft *aircraft;
118   
119   
120 public:
121   FGTrafficRecord();
122   
123   void setId(int val)  { id = val; };
124   void setRadius(double rad) { radius = rad;};
125   void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
126   void setRunway(string rwy) { runway = rwy;};
127   void setLeg(int lg) { leg = lg;};
128   int getId() { return id;};
129   int getState() { return state;};
130   FGATCInstruction getInstruction() { return instruction;};
131   bool hasInstruction() { return instruction.hasInstruction(); };
132   void setPositionAndHeading(double lat, double lon, double hdg, double spd, double alt);
133   bool checkPositionAndIntentions(FGTrafficRecord &other);
134   int  crosses                   (FGGroundNetwork *, FGTrafficRecord &other); 
135   bool isOpposing                (FGGroundNetwork *, FGTrafficRecord &other, int node);
136
137   bool onRoute(FGGroundNetwork *, FGTrafficRecord &other);
138
139   bool getSpeedAdjustment() { return instruction.getChangeSpeed(); };
140   
141   double getLatitude () { return latitude ; };
142   double getLongitude() { return longitude; };
143   double getHeading  () { return heading  ; };
144   double getSpeed    () { return speed    ; };
145   double getAltitude () { return altitude ; };
146   double getRadius   () { return radius   ; };
147
148   int getWaitsForId  () { return waitsForId; };
149
150   void setSpeedAdjustment(double spd);
151   void setHeadingAdjustment(double heading);
152   void clearSpeedAdjustment  () { instruction.setChangeSpeed  (false); };
153   void clearHeadingAdjustment() { instruction.setChangeHeading(false); };
154
155   bool hasHeadingAdjustment() { return instruction.getChangeHeading(); };
156   bool hasHoldPosition() { return instruction.getHoldPosition(); };
157   void setHoldPosition (bool inst) { instruction.setHoldPosition(inst); };
158
159   void setWaitsForId(int id) { waitsForId = id; };
160
161   void setResolveCircularWait()   { instruction.setResolveCircularWait(true);  };
162   void clearResolveCircularWait() { instruction.setResolveCircularWait(false); };
163
164   string getRunway() { return runway; };
165   //void setCallSign(string clsgn) { callsign = clsgn; };
166   void setAircraft(FGAIAircraft *ref) { aircraft = ref;};
167   void updateState() { state++;};
168   //string getCallSign() { return callsign; };
169   FGAIAircraft *getAircraft() { return aircraft;};
170   int getTime() { return timer; };
171   int getLeg() { return leg; };
172   void setTime(time_t time) { timer = time; };
173 };
174
175 typedef vector<FGTrafficRecord> TrafficVector;
176 typedef vector<FGTrafficRecord>::iterator TrafficVectorIterator;
177
178
179 /***********************************************************************
180  * Active runway, a utility class to keep track of which aircraft has
181  * clearance for a given runway.
182  **********************************************************************/
183 class ActiveRunway
184 {
185 private:
186   string rwy;
187   int currentlyCleared;
188 public:
189   ActiveRunway(string r, int cc) { rwy = r; currentlyCleared = cc; };
190   
191   string getRunwayName() { return rwy; };
192   int    getCleared   () { return currentlyCleared; };
193 };
194
195 typedef vector<ActiveRunway> ActiveRunwayVec;
196 typedef vector<ActiveRunway>::iterator ActiveRunwayVecIterator;
197
198 /**
199  * class FGATCController
200  * NOTE: this class serves as an abstraction layer for all sorts of ATC controller. 
201  *************************************************************************************/
202 class FGATCController
203 {
204 private:
205   double dt_count;
206
207
208   string formatATCFrequency3_2(int );
209   string genTransponderCode(string fltRules);
210
211 public:
212   typedef enum {
213       MSG_ANNOUNCE_ENGINE_START,
214       MSG_REQUEST_ENGINE_START, 
215       MSG_PERMIT_ENGINE_START,
216       MSG_DENY_ENGINE_START,
217       MSG_ACKNOWLEDGE_ENGINE_START } AtcMsgId;
218   typedef enum {
219       ATC_AIR_TO_GROUND,
220       ATC_GROUND_TO_AIR } AtcMsgDir;
221   FGATCController() { dt_count = 0;};
222   virtual ~FGATCController() {};
223   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
224                                 double lat, double lon,
225                                 double hdg, double spd, double alt, double radius, int leg,
226                                 FGAIAircraft *aircraft) = 0;
227   virtual void             signOff(int id) = 0;
228   virtual void             update(int id, double lat, double lon, 
229                                   double heading, double speed, double alt, double dt) = 0;
230   virtual bool             hasInstruction(int id) = 0;
231   virtual FGATCInstruction getInstruction(int id) = 0;
232
233   double getDt() { return dt_count; };
234   void   setDt(double dt) { dt_count = dt;};
235   void transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir);
236   string getGateName(FGAIAircraft *aircraft);
237 };
238
239 /******************************************************************************
240  * class FGTowerControl
241  *****************************************************************************/
242 class FGTowerController : public FGATCController
243 {
244 private:
245   TrafficVector activeTraffic;
246   ActiveRunwayVec activeRunways;
247   
248 public:
249   FGTowerController();
250   virtual ~FGTowerController() {};
251   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
252                                 double lat, double lon,
253                                 double hdg, double spd, double alt, double radius, int leg,
254                                 FGAIAircraft *aircraft);
255   virtual void             signOff(int id);
256   virtual void             update(int id, double lat, double lon, 
257                                   double heading, double speed, double alt, double dt);
258   virtual bool             hasInstruction(int id);
259   virtual FGATCInstruction getInstruction(int id);
260
261   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
262   TrafficVector &getActiveTraffic() { return activeTraffic; };
263 };
264
265 /******************************************************************************
266  * class FGStartupController
267  * handle 
268  *****************************************************************************/
269
270 class FGStartupController : public FGATCController
271 {
272 private:
273   TrafficVector activeTraffic;
274   bool available;
275   time_t lastTransmission;
276   //ActiveRunwayVec activeRunways;
277   
278 public:
279   FGStartupController();
280   virtual ~FGStartupController() {};
281   virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
282                                 double lat, double lon,
283                                 double hdg, double spd, double alt, double radius, int leg,
284                                 FGAIAircraft *aircraft);
285   virtual void             signOff(int id);
286   virtual void             update(int id, double lat, double lon, 
287                                   double heading, double speed, double alt, double dt);
288   virtual bool             hasInstruction(int id);
289   virtual FGATCInstruction getInstruction(int id);
290
291   bool hasActiveTraffic() { return activeTraffic.size() != 0; };
292   TrafficVector &getActiveTraffic() { return activeTraffic; };
293
294 }; 
295
296 #endif // _TRAFFIC_CONTROL_HXX