1 // trafficcontrol.hxx - classes to manage AIModels based air traffic control
2 // Written by Durk Talsma, started September 2006.
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.
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.
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.
21 #ifndef _TRAFFIC_CONTROL_HXX_
22 #define _TRAFFIC_CONTROL_HXX_
26 # error This library requires C++
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>
44 typedef vector<int> intVec;
45 typedef vector<int>::iterator intVecIterator;
48 class FGAIFlightPlan; // forward reference
49 class FGGroundNetwork; // forward reference
50 class FGAIAircraft; // forward reference
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
65 bool resolveCircularWait;
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; };
80 double getSpeed () { return speed; };
81 double getHeading () { return heading; };
82 double getAlt () { return alt; };
84 bool getCheckForCircularWait() { return resolveCircularWait; };
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; };
92 void setResolveCircularWait (bool val) { resolveCircularWait = val; };
94 void setSpeed (double val) { speed = val; };
95 void setHeading (double val) { heading = val; };
96 void setAlt (double val) { alt = val; };
103 /**************************************************************************************
104 * class FGTrafficRecord
105 *************************************************************************************/
106 class FGTrafficRecord
114 bool allowTransmission;
117 FGATCInstruction instruction;
118 double latitude, longitude, heading, speed, altitude, radius;
120 //FGAISchedule *trafficRef;
121 FGAIAircraft *aircraft;
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);
142 bool onRoute(FGGroundNetwork *, FGTrafficRecord &other);
144 bool getSpeedAdjustment() { return instruction.getChangeSpeed(); };
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 ; };
153 int getWaitsForId () { return waitsForId; };
155 void setSpeedAdjustment(double spd);
156 void setHeadingAdjustment(double heading);
157 void clearSpeedAdjustment () { instruction.setChangeSpeed (false); };
158 void clearHeadingAdjustment() { instruction.setChangeHeading(false); };
160 bool hasHeadingAdjustment() { return instruction.getChangeHeading(); };
161 bool hasHoldPosition() { return instruction.getHoldPosition(); };
162 void setHoldPosition (bool inst) { instruction.setHoldPosition(inst); };
164 void setWaitsForId(int id) { waitsForId = id; };
166 void setResolveCircularWait() { instruction.setResolveCircularWait(true); };
167 void clearResolveCircularWait() { instruction.setResolveCircularWait(false); };
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; };
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; };
187 typedef vector<FGTrafficRecord> TrafficVector;
188 typedef vector<FGTrafficRecord>::iterator TrafficVectorIterator;
190 typedef vector<time_t> TimeVector;
191 typedef vector<time_t>::iterator TimeVectorIterator;
194 /***********************************************************************
195 * Active runway, a utility class to keep track of which aircraft has
196 * clearance for a given runway.
197 **********************************************************************/
202 int currentlyCleared;
203 double distanceToFinal;
204 TimeVector estimatedArrivalTimes;
206 ActiveRunway(string r, int cc) { rwy = r; currentlyCleared = cc; distanceToFinal = 6.0 * SG_NM_TO_METER; };
208 string getRunwayName() { return rwy; };
209 int getCleared () { return currentlyCleared; };
210 double getApproachDistance() { return distanceToFinal; };
211 //time_t getEstApproachTime() { return estimatedArrival; };
213 //void setEstApproachTime(time_t time) { estimatedArrival = time; };
214 time_t requestTimeSlot(time_t eta);
217 typedef vector<ActiveRunway> ActiveRunwayVec;
218 typedef vector<ActiveRunway>::iterator ActiveRunwayVecIterator;
221 * class FGATCController
222 * NOTE: this class serves as an abstraction layer for all sorts of ATC controllers.
223 *************************************************************************************/
224 class FGATCController
228 time_t lastTransmission;
233 string formatATCFrequency3_2(int );
234 string genTransponderCode(string fltRules);
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,
253 MSG_ACKNOWLEDGE_HOLD_POSITION,
255 MSG_ACKNOWLEDGE_RESUME_TAXI } AtcMsgId;
258 ATC_GROUND_TO_AIR } AtcMsgDir;
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;
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);
277 /******************************************************************************
278 * class FGTowerControl
279 *****************************************************************************/
280 class FGTowerController : public FGATCController
283 TrafficVector activeTraffic;
284 ActiveRunwayVec activeRunways;
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);
299 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
300 TrafficVector &getActiveTraffic() { return activeTraffic; };
303 /******************************************************************************
304 * class FGStartupController
306 *****************************************************************************/
308 class FGStartupController : public FGATCController
311 TrafficVector activeTraffic;
312 //ActiveRunwayVec activeRunways;
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);
327 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
328 TrafficVector &getActiveTraffic() { return activeTraffic; };
332 /******************************************************************************
333 * class FGTowerControl
334 *****************************************************************************/
335 class FGApproachController : public FGATCController
338 TrafficVector activeTraffic;
339 ActiveRunwayVec activeRunways;
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);
354 ActiveRunway* getRunway(string name);
356 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
357 TrafficVector &getActiveTraffic() { return activeTraffic; };
361 #endif // _TRAFFIC_CONTROL_HXX