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
230 time_t lastTransmission;
235 string formatATCFrequency3_2(int );
236 string genTransponderCode(string fltRules);
240 MSG_ANNOUNCE_ENGINE_START,
241 MSG_REQUEST_ENGINE_START,
242 MSG_PERMIT_ENGINE_START,
243 MSG_DENY_ENGINE_START,
244 MSG_ACKNOWLEDGE_ENGINE_START,
245 MSG_REQUEST_PUSHBACK_CLEARANCE,
246 MSG_PERMIT_PUSHBACK_CLEARANCE,
247 MSG_HOLD_PUSHBACK_CLEARANCE,
248 MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY,
249 MSG_INITIATE_CONTACT,
250 MSG_ACKNOWLEDGE_INITIATE_CONTACT,
251 MSG_REQUEST_TAXI_CLEARANCE,
252 MSG_ISSUE_TAXI_CLEARANCE,
253 MSG_ACKNOWLEDGE_TAXI_CLEARANCE,
255 MSG_ACKNOWLEDGE_HOLD_POSITION,
257 MSG_ACKNOWLEDGE_RESUME_TAXI } AtcMsgId;
260 ATC_GROUND_TO_AIR } AtcMsgDir;
262 virtual ~FGATCController();
263 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
264 double lat, double lon,
265 double hdg, double spd, double alt, double radius, int leg,
266 FGAIAircraft *aircraft) = 0;
267 virtual void signOff(int id) = 0;
268 virtual void updateAircraftInformation(int id, double lat, double lon,
269 double heading, double speed, double alt, double dt) = 0;
270 virtual bool hasInstruction(int id) = 0;
271 virtual FGATCInstruction getInstruction(int id) = 0;
273 double getDt() { return dt_count; };
274 void setDt(double dt) { dt_count = dt;};
275 void transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir);
276 string getGateName(FGAIAircraft *aircraft);
279 /******************************************************************************
280 * class FGTowerControl
281 *****************************************************************************/
282 class FGTowerController : public FGATCController
285 TrafficVector activeTraffic;
286 ActiveRunwayVec activeRunways;
290 virtual ~FGTowerController() {};
291 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
292 double lat, double lon,
293 double hdg, double spd, double alt, double radius, int leg,
294 FGAIAircraft *aircraft);
295 virtual void signOff(int id);
296 virtual void updateAircraftInformation(int id, double lat, double lon,
297 double heading, double speed, double alt, double dt);
298 virtual bool hasInstruction(int id);
299 virtual FGATCInstruction getInstruction(int id);
301 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
302 TrafficVector &getActiveTraffic() { return activeTraffic; };
305 /******************************************************************************
306 * class FGStartupController
308 *****************************************************************************/
310 class FGStartupController : public FGATCController
313 TrafficVector activeTraffic;
314 //ActiveRunwayVec activeRunways;
317 FGStartupController();
318 virtual ~FGStartupController() {};
319 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
320 double lat, double lon,
321 double hdg, double spd, double alt, double radius, int leg,
322 FGAIAircraft *aircraft);
323 virtual void signOff(int id);
324 virtual void updateAircraftInformation(int id, double lat, double lon,
325 double heading, double speed, double alt, double dt);
326 virtual bool hasInstruction(int id);
327 virtual FGATCInstruction getInstruction(int id);
329 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
330 TrafficVector &getActiveTraffic() { return activeTraffic; };
334 /******************************************************************************
335 * class FGTowerControl
336 *****************************************************************************/
337 class FGApproachController : public FGATCController
340 TrafficVector activeTraffic;
341 ActiveRunwayVec activeRunways;
344 FGApproachController();
345 virtual ~FGApproachController() {};
346 virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
347 double lat, double lon,
348 double hdg, double spd, double alt, double radius, int leg,
349 FGAIAircraft *aircraft);
350 virtual void signOff(int id);
351 virtual void updateAircraftInformation(int id, double lat, double lon,
352 double heading, double speed, double alt, double dt);
353 virtual bool hasInstruction(int id);
354 virtual FGATCInstruction getInstruction(int id);
356 ActiveRunway* getRunway(string name);
358 bool hasActiveTraffic() { return activeTraffic.size() != 0; };
359 TrafficVector &getActiveTraffic() { return activeTraffic; };
363 #endif // _TRAFFIC_CONTROL_HXX