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>
34 #include <simgear/structure/SGReferenced.hxx>
35 #include <simgear/structure/SGSharedPtr.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; };
185 intVec& getIntentions() { return intentions; };
186 int getCurrentPosition() { return currentPos; };
189 typedef vector<FGTrafficRecord> TrafficVector;
190 typedef vector<FGTrafficRecord>::iterator TrafficVectorIterator;
192 typedef vector<time_t> TimeVector;
193 typedef vector<time_t>::iterator TimeVectorIterator;
196 /***********************************************************************
197 * Active runway, a utility class to keep track of which aircraft has
198 * clearance for a given runway.
199 **********************************************************************/
204 int currentlyCleared;
205 double distanceToFinal;
206 TimeVector estimatedArrivalTimes;
208 ActiveRunway(string r, int cc) { rwy = r; currentlyCleared = cc; distanceToFinal = 6.0 * SG_NM_TO_METER; };
210 string getRunwayName() { return rwy; };
211 int getCleared () { return currentlyCleared; };
212 double getApproachDistance() { return distanceToFinal; };
213 //time_t getEstApproachTime() { return estimatedArrival; };
215 //void setEstApproachTime(time_t time) { estimatedArrival = time; };
216 time_t requestTimeSlot(time_t eta);
219 typedef vector<ActiveRunway> ActiveRunwayVec;
220 typedef vector<ActiveRunway>::iterator ActiveRunwayVecIterator;
223 * class FGATCController
224 * NOTE: this class serves as an abstraction layer for all sorts of ATC controllers.
225 *************************************************************************************/
226 class FGATCController
233 time_t lastTransmission;
238 string formatATCFrequency3_2(int );
239 string genTransponderCode(string fltRules);
240 bool isUserAircraft(FGAIAircraft*);
244 MSG_ANNOUNCE_ENGINE_START,
245 MSG_REQUEST_ENGINE_START,
246 MSG_PERMIT_ENGINE_START,
247 MSG_DENY_ENGINE_START,
248 MSG_ACKNOWLEDGE_ENGINE_START,
249 MSG_REQUEST_PUSHBACK_CLEARANCE,
250 MSG_PERMIT_PUSHBACK_CLEARANCE,
251 MSG_HOLD_PUSHBACK_CLEARANCE,
252 MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
253 MSG_INITIATE_CONTACT,
254 MSG_ACKNOWLEDGE_INITIATE_CONTACT,
255 MSG_REQUEST_TAXI_CLEARANCE,
256 MSG_ISSUE_TAXI_CLEARANCE,
257 MSG_ACKNOWLEDGE_TAXI_CLEARANCE,
259 MSG_ACKNOWLEDGE_HOLD_POSITION,
261 MSG_ACKNOWLEDGE_RESUME_TAXI } AtcMsgId;
264 ATC_GROUND_TO_AIR } AtcMsgDir;
266 virtual ~FGATCController();
269 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
270 double lat, double lon,
271 double hdg, double spd, double alt, double radius, int leg,
272 FGAIAircraft *aircraft) = 0;
273 virtual void signOff(int id) = 0;
274 virtual void updateAircraftInformation(int id, double lat, double lon,
275 double heading, double speed, double alt, double dt) = 0;
276 virtual bool hasInstruction(int id) = 0;
277 virtual FGATCInstruction getInstruction(int id) = 0;
279 double getDt() { return dt_count; };
280 void setDt(double dt) { dt_count = dt;};
281 void transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir, bool audible);
282 string getGateName(FGAIAircraft *aircraft);
285 AtcMsgDir lastTransmissionDirection;
288 /******************************************************************************
289 * class FGTowerControl
290 *****************************************************************************/
291 class FGTowerController : public FGATCController
294 TrafficVector activeTraffic;
295 ActiveRunwayVec activeRunways;
299 virtual ~FGTowerController() {};
300 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
301 double lat, double lon,
302 double hdg, double spd, double alt, double radius, int leg,
303 FGAIAircraft *aircraft);
304 virtual void signOff(int id);
305 virtual void updateAircraftInformation(int id, double lat, double lon,
306 double heading, double speed, double alt, double dt);
307 virtual bool hasInstruction(int id);
308 virtual FGATCInstruction getInstruction(int id);
310 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
311 TrafficVector &getActiveTraffic() { return activeTraffic; };
314 /******************************************************************************
315 * class FGStartupController
317 *****************************************************************************/
319 class FGStartupController : public FGATCController
322 TrafficVector activeTraffic;
323 //ActiveRunwayVec activeRunways;
326 FGStartupController();
327 virtual ~FGStartupController() {};
328 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
329 double lat, double lon,
330 double hdg, double spd, double alt, double radius, int leg,
331 FGAIAircraft *aircraft);
332 virtual void signOff(int id);
333 virtual void updateAircraftInformation(int id, double lat, double lon,
334 double heading, double speed, double alt, double dt);
335 virtual bool hasInstruction(int id);
336 virtual FGATCInstruction getInstruction(int id);
338 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
339 TrafficVector &getActiveTraffic() { return activeTraffic; };
341 // Hpoefully, we can move this function to the base class, but I need to verify what is needed for the other controllers before doing so.
342 bool checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
347 /******************************************************************************
348 * class FGTowerControl
349 *****************************************************************************/
350 class FGApproachController : public FGATCController
353 TrafficVector activeTraffic;
354 ActiveRunwayVec activeRunways;
357 FGApproachController();
358 virtual ~FGApproachController() {};
359 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
360 double lat, double lon,
361 double hdg, double spd, double alt, double radius, int leg,
362 FGAIAircraft *aircraft);
363 virtual void signOff(int id);
364 virtual void updateAircraftInformation(int id, double lat, double lon,
365 double heading, double speed, double alt, double dt);
366 virtual bool hasInstruction(int id);
367 virtual FGATCInstruction getInstruction(int id);
369 ActiveRunway* getRunway(string name);
371 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
372 TrafficVector &getActiveTraffic() { return activeTraffic; };
376 #endif // _TRAFFIC_CONTROL_HXX