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>
46 typedef vector<int> intVec;
47 typedef vector<int>::iterator intVecIterator;
50 class FGAIFlightPlan; // forward reference
51 class FGGroundNetwork; // forward reference
52 class FGAIAircraft; // forward reference
54 /**************************************************************************************
55 * class FGATCInstruction
56 * like class FGATC Controller, this class definition should go into its own file
57 * and or directory... For now, just testing this stuff out though...
58 *************************************************************************************/
59 class FGATCInstruction
67 bool resolveCircularWait;
75 bool hasInstruction ();
76 bool getHoldPattern () { return holdPattern; };
77 bool getHoldPosition () { return holdPosition; };
78 bool getChangeSpeed () { return changeSpeed; };
79 bool getChangeHeading () { return changeHeading; };
80 bool getChangeAltitude() { return changeAltitude; };
82 double getSpeed () { return speed; };
83 double getHeading () { return heading; };
84 double getAlt () { return alt; };
86 bool getCheckForCircularWait() { return resolveCircularWait; };
88 void setHoldPattern (bool val) { holdPattern = val; };
89 void setHoldPosition (bool val) { holdPosition = val; };
90 void setChangeSpeed (bool val) { changeSpeed = val; };
91 void setChangeHeading (bool val) { changeHeading = val; };
92 void setChangeAltitude(bool val) { changeAltitude = val; };
94 void setResolveCircularWait (bool val) { resolveCircularWait = val; };
96 void setSpeed (double val) { speed = val; };
97 void setHeading (double val) { heading = val; };
98 void setAlt (double val) { alt = val; };
105 /**************************************************************************************
106 * class FGTrafficRecord
107 *************************************************************************************/
108 class FGTrafficRecord
116 bool allowTransmission;
119 FGATCInstruction instruction;
120 double latitude, longitude, heading, speed, altitude, radius;
122 //FGAISchedule *trafficRef;
123 FGAIAircraft *aircraft;
129 void setId(int val) { id = val; };
130 void setRadius(double rad) { radius = rad;};
131 void setPositionAndIntentions(int pos, FGAIFlightPlan *route);
132 void setRunway(string rwy) { runway = rwy;};
133 void setLeg(int lg) { leg = lg;};
134 int getId() { return id;};
135 int getState() { return state;};
136 void setState(int s) { state = s;}
137 FGATCInstruction getInstruction() { return instruction;};
138 bool hasInstruction() { return instruction.hasInstruction(); };
139 void setPositionAndHeading(double lat, double lon, double hdg, double spd, double alt);
140 bool checkPositionAndIntentions(FGTrafficRecord &other);
141 int crosses (FGGroundNetwork *, FGTrafficRecord &other);
142 bool isOpposing (FGGroundNetwork *, FGTrafficRecord &other, int node);
144 bool onRoute(FGGroundNetwork *, FGTrafficRecord &other);
146 bool getSpeedAdjustment() { return instruction.getChangeSpeed(); };
148 double getLatitude () { return latitude ; };
149 double getLongitude() { return longitude; };
150 double getHeading () { return heading ; };
151 double getSpeed () { return speed ; };
152 double getAltitude () { return altitude ; };
153 double getRadius () { return radius ; };
155 int getWaitsForId () { return waitsForId; };
157 void setSpeedAdjustment(double spd);
158 void setHeadingAdjustment(double heading);
159 void clearSpeedAdjustment () { instruction.setChangeSpeed (false); };
160 void clearHeadingAdjustment() { instruction.setChangeHeading(false); };
162 bool hasHeadingAdjustment() { return instruction.getChangeHeading(); };
163 bool hasHoldPosition() { return instruction.getHoldPosition(); };
164 void setHoldPosition (bool inst) { instruction.setHoldPosition(inst); };
166 void setWaitsForId(int id) { waitsForId = id; };
168 void setResolveCircularWait() { instruction.setResolveCircularWait(true); };
169 void clearResolveCircularWait() { instruction.setResolveCircularWait(false); };
171 string getRunway() { return runway; };
172 //void setCallSign(string clsgn) { callsign = clsgn; };
173 void setAircraft(FGAIAircraft *ref) { aircraft = ref;};
174 void updateState() { state++; allowTransmission=true; };
175 //string getCallSign() { return callsign; };
176 FGAIAircraft *getAircraft() { return aircraft;};
177 int getTime() { return timer; };
178 int getLeg() { return leg; };
179 void setTime(time_t time) { timer = time; };
181 bool pushBackAllowed();
182 bool allowTransmissions() { return allowTransmission; };
183 void suppressRepeatedTransmissions () { allowTransmission=false; };
184 void allowRepeatedTransmissions () { allowTransmission=true; };
185 void nextFrequency() { frequencyId++; };
186 int getNextFrequency() { return frequencyId; };
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
232 time_t lastTransmission;
237 string formatATCFrequency3_2(int );
238 string genTransponderCode(string fltRules);
242 MSG_ANNOUNCE_ENGINE_START,
243 MSG_REQUEST_ENGINE_START,
244 MSG_PERMIT_ENGINE_START,
245 MSG_DENY_ENGINE_START,
246 MSG_ACKNOWLEDGE_ENGINE_START,
247 MSG_REQUEST_PUSHBACK_CLEARANCE,
248 MSG_PERMIT_PUSHBACK_CLEARANCE,
249 MSG_HOLD_PUSHBACK_CLEARANCE,
250 MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
251 MSG_INITIATE_CONTACT,
252 MSG_ACKNOWLEDGE_INITIATE_CONTACT,
253 MSG_REQUEST_TAXI_CLEARANCE,
254 MSG_ISSUE_TAXI_CLEARANCE,
255 MSG_ACKNOWLEDGE_TAXI_CLEARANCE,
257 MSG_ACKNOWLEDGE_HOLD_POSITION,
259 MSG_ACKNOWLEDGE_RESUME_TAXI } AtcMsgId;
262 ATC_GROUND_TO_AIR } AtcMsgDir;
264 virtual ~FGATCController();
267 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
268 double lat, double lon,
269 double hdg, double spd, double alt, double radius, int leg,
270 FGAIAircraft *aircraft) = 0;
271 virtual void signOff(int id) = 0;
272 virtual void updateAircraftInformation(int id, double lat, double lon,
273 double heading, double speed, double alt, double dt) = 0;
274 virtual bool hasInstruction(int id) = 0;
275 virtual FGATCInstruction getInstruction(int id) = 0;
277 double getDt() { return dt_count; };
278 void setDt(double dt) { dt_count = dt;};
279 void transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir);
280 string getGateName(FGAIAircraft *aircraft);
283 /******************************************************************************
284 * class FGTowerControl
285 *****************************************************************************/
286 class FGTowerController : public FGATCController
289 TrafficVector activeTraffic;
290 ActiveRunwayVec activeRunways;
294 virtual ~FGTowerController() {};
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 updateAircraftInformation(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);
305 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
306 TrafficVector &getActiveTraffic() { return activeTraffic; };
309 /******************************************************************************
310 * class FGStartupController
312 *****************************************************************************/
314 class FGStartupController : public FGATCController
317 TrafficVector activeTraffic;
318 //ActiveRunwayVec activeRunways;
321 FGStartupController();
322 virtual ~FGStartupController() {};
323 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
324 double lat, double lon,
325 double hdg, double spd, double alt, double radius, int leg,
326 FGAIAircraft *aircraft);
327 virtual void signOff(int id);
328 virtual void updateAircraftInformation(int id, double lat, double lon,
329 double heading, double speed, double alt, double dt);
330 virtual bool hasInstruction(int id);
331 virtual FGATCInstruction getInstruction(int id);
333 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
334 TrafficVector &getActiveTraffic() { return activeTraffic; };
338 /******************************************************************************
339 * class FGTowerControl
340 *****************************************************************************/
341 class FGApproachController : public FGATCController
344 TrafficVector activeTraffic;
345 ActiveRunwayVec activeRunways;
348 FGApproachController();
349 virtual ~FGApproachController() {};
350 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
351 double lat, double lon,
352 double hdg, double spd, double alt, double radius, int leg,
353 FGAIAircraft *aircraft);
354 virtual void signOff(int id);
355 virtual void updateAircraftInformation(int id, double lat, double lon,
356 double heading, double speed, double alt, double dt);
357 virtual bool hasInstruction(int id);
358 virtual FGATCInstruction getInstruction(int id);
360 ActiveRunway* getRunway(string name);
362 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
363 TrafficVector &getActiveTraffic() { return activeTraffic; };
367 #endif // _TRAFFIC_CONTROL_HXX