1 // FGTower - a class to provide tower control at towered airports.
3 // Written by David Luff, started March 2002.
5 // Copyright (C) 2002 David C. Luff - david.luff@nottingham.ac.uk
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/compiler.h>
25 #include <simgear/math/point3d.hxx>
26 #include <simgear/misc/sgstream.hxx>
27 //#include <simgear/math/sg_geodesy.hxx>
29 //#include <Airports/runways.hxx>
38 //#include "ATCmgr.hxx"
40 #include "ATCProjection.hxx"
41 #include "AIPlane.hxx"
43 //DCL - a complete guess for now.
44 #define FG_TOWER_DEFAULT_RANGE 30
46 enum tower_traffic_type {
48 INBOUND, // CIRCUIT traffic gets changed to INBOUND when on final of the full-stop circuit.
50 TTT_UNKNOWN, // departure, but we don't know if for circuits or leaving properly
54 ostream& operator << (ostream& os, tower_traffic_type ttt);
56 enum tower_callback_type {
57 USER_REQUEST_VFR_DEPARTURE = 1,
58 USER_REQUEST_VFR_ARRIVAL = 2,
59 USER_REQUEST_VFR_ARRIVAL_FULL_STOP = 3,
60 USER_REQUEST_VFR_ARRIVAL_TOUCH_AND_GO = 4,
61 USER_REPORT_3_MILE_FINAL = 5,
62 USER_REPORT_DOWNWIND = 6,
63 USER_REPORT_RWY_VACATED = 7,
64 USER_REPORT_GOING_AROUND = 8
67 // TODO - need some differentiation of IFR and VFR traffic in order to give the former priority.
69 // Structure for holding details of a plane under tower control.
70 // Not fixed yet - may include more stuff later.
76 TowerPlaneRec(PlaneRec p);
77 TowerPlaneRec(Point3D pt);
78 TowerPlaneRec(PlaneRec p, Point3D pt);
80 FGAIPlane* planePtr; // This might move to the planeRec eventually
84 double eta; // seconds
85 double dist_out; // meters from theshold
88 bool clearedToTakeOff;
89 // ought to add time cleared to depart so we can nag if necessary
90 bool holdShortReported;
91 bool downwindReported;
92 bool longFinalReported;
93 bool longFinalAcknowledged;
95 bool finalAcknowledged;
96 bool rwyVacatedReported;
97 bool rwyVacatedAcknowledged;
98 bool instructedToGoAround; // set true if told by tower to go around
99 bool onRwy; // is physically on the runway
100 bool nextOnRwy; // currently projected by tower to be the next on the runway
102 bool vfrArrivalReported;
103 bool vfrArrivalAcknowledged;
105 // Type of operation the plane is doing
106 tower_traffic_type opType;
108 // Whereabouts in circuit if doing circuits
111 LandingType landingType;
113 bool isUser; // true if this plane is the user
117 typedef list < TowerPlaneRec* > tower_plane_rec_list_type;
118 typedef tower_plane_rec_list_type::iterator tower_plane_rec_list_iterator;
119 typedef tower_plane_rec_list_type::const_iterator tower_plane_rec_list_const_iterator;
122 class FGTower : public FGATC {
131 void Update(double dt);
133 void ReceiveUserCallback(int code);
135 // Contact tower for VFR approach
136 // eg "Cessna Charlie Foxtrot Golf Foxtrot Sierra eight miles South of the airport for full stop with Bravo"
137 // This function probably only called via user interaction - AI planes will have an overloaded function taking a planerec.
138 void VFRArrivalContact(string ID, LandingType opt = AIP_LT_UNKNOWN);
139 // For the AI planes...
140 void VFRArrivalContact(PlaneRec plane, FGAIPlane* requestee, LandingType lt = AIP_LT_UNKNOWN);
142 void RequestDepartureClearance(string ID);
143 void ReportFinal(string ID);
144 void ReportLongFinal(string ID);
145 void ReportOuterMarker(string ID);
146 void ReportMiddleMarker(string ID);
147 void ReportInnerMarker(string ID);
148 void ReportGoingAround(string ID);
149 void ReportRunwayVacated(string ID);
150 void ReportReadyForDeparture(string ID);
151 void ReportDownwind(string ID);
153 // Contact tower when at a hold short for departure - for now we'll assume plane - maybe vehicles might want to cross runway eventually?
154 void ContactAtHoldShort(PlaneRec plane, FGAIPlane* requestee, tower_traffic_type operation);
156 // Register the presence of an AI plane at a point where contact would already have been made in real life
157 // CAUTION - currently it is assumed that this plane's callsign is unique - it is up to AIMgr to generate unique callsigns.
158 void RegisterAIPlane(PlaneRec plane, FGAIPlane* ai, tower_traffic_type op, PatternLeg lg = LEG_UNKNOWN);
160 // Deregister and remove an AI plane.
161 void DeregisterAIPlane(string id);
163 // Public interface to the active runway - this will get more complex
164 // in the future and consider multi-runway use, airplane weight etc.
165 inline string GetActiveRunway() { return activeRwy; }
166 inline RunwayDetails GetActiveRunwayDetails() { return rwy; }
167 // Get the pattern direction of the active rwy.
168 inline int GetPatternDirection() { return rwy.patternDirection; }
170 inline void SetDisplay() { display = true; }
171 inline void SetNoDisplay() { display = false; }
173 inline string get_trans_ident() { return trans_ident; }
175 inline FGGround* GetGroundPtr() { return ground; }
177 // Returns true if positions of crosswind/downwind/base leg turns should be constrained by previous traffic
178 // plus the constraint position as a rwy orientated orthopos (meters)
179 bool GetCrosswindConstraint(double& cpos);
180 bool GetDownwindConstraint(double& dpos);
181 bool GetBaseConstraint(double& bpos);
183 string GenText(const string& m, int c);
187 // This is purely for synactic convienience to avoid writing globals->get_ATC_mgr()-> all through the code!
189 // Respond to a transmission
192 void ProcessRunwayVacatedReport(TowerPlaneRec* t);
194 // Remove all options from the user dialog choice
195 void RemoveAllUserDialogOptions();
197 // Periodic checks on the various traffic.
198 void CheckHoldList(double dt);
199 void CheckCircuitList(double dt);
200 void CheckRunwayList(double dt);
201 void CheckApproachList(double dt);
202 void CheckDepartureList(double dt);
204 // Currently this assumes we *are* next on the runway and doesn't check for planes about to land -
205 // this should be done prior to calling this function.
206 void ClearHoldingPlane(TowerPlaneRec* t);
208 // Find a pointer to plane of callsign ID within the internal data structures
209 TowerPlaneRec* FindPlane(string ID);
211 // Remove and delete all instances of a plane with a given ID
212 void RemovePlane(string ID);
214 // Figure out if a given position lies on the active runway
215 // Might have to change when we consider more than one active rwy.
216 bool OnActiveRunway(Point3D pt);
218 // Figure out if a given position lies on a runway or not
219 bool OnAnyRunway(Point3D pt);
221 // Calculate the eta of a plane to the threshold.
222 // For ground traffic this is the fastest they can get there.
223 // For air traffic this is the middle approximation.
224 void CalcETA(TowerPlaneRec* tpr, bool printout = false);
226 // Iterate through all the lists and call CalcETA for all the planes.
227 void doThresholdETACalc();
229 // Order the list of traffic as per expected threshold use and flag any conflicts
230 bool doThresholdUseOrder();
232 // Calculate the crow-flys distance of a plane to the threshold in meters
233 double CalcDistOutM(TowerPlaneRec* tpr);
235 // Calculate the crow-flys distance of a plane to the threshold in miles
236 double CalcDistOutMiles(TowerPlaneRec* tpr);
239 void doCommunication();
242 void IssueLandingClearance(TowerPlaneRec* tpr);
243 void IssueGoAround(TowerPlaneRec* tpr);
244 void IssueDepartureClearance(TowerPlaneRec* tpr);
246 unsigned int update_count; // Convienince counter for speading computational load over several updates
247 unsigned int update_count_max; // ditto.
249 bool display; // Flag to indicate whether we should be outputting to the ATC display.
250 bool displaying; // Flag to indicate whether we are outputting to the ATC display.
252 double timeSinceLastDeparture; // Time in seconds since last departure from active rwy.
253 bool departed; // set true when the above needs incrementing with time, false when it doesn't.
255 // environment - need to make sure we're getting the surface winds and not winds aloft.
256 SGPropertyNode* wind_from_hdg; //degrees
257 SGPropertyNode* wind_speed_knots; //knots
259 double aptElev; // Airport elevation
260 string activeRwy; // Active runway number - For now we'll disregard multiple / alternate runway operation.
261 RunwayDetails rwy; // Assumed to be the active one for now.
262 bool rwyOccupied; // Active runway occupied flag. For now we'll disregard land-and-hold-short operations.
263 FGATCAlignedProjection ortho; // Orthogonal mapping of the local area with the active runway threshold at the origin
265 // Figure out which runways are active.
266 // For now we'll just be simple and do one active runway - eventually this will get much more complex
267 // This is a private function - public interface to the results of this is through GetActiveRunway
270 // Need a data structure to hold details of the various active planes
271 // or possibly another data structure with the positions of the inactive planes.
272 // Need a data structure to hold outstanding communications from aircraft.
274 // Linked-list of planes on approach to active rwy ordered with nearest first (timewise).
275 // Includes planes that have landed but not yet vacated runway.
276 // Somewhat analagous to the paper strips used (used to be used?) in real life.
277 // Doesn't include planes in circuit until they turn onto base/final?
278 // TODO - may need to alter this for operation to more than one active rwy.
279 tower_plane_rec_list_type appList;
280 tower_plane_rec_list_iterator appListItr;
282 // What should we do with planes approaching the airport to join the circuit somewhere
283 // but not on straight-in though? - put them in here for now.
284 tower_plane_rec_list_type circuitAppList;
285 tower_plane_rec_list_iterator circuitAppListItr;
287 // List of departed planes (planes doing circuits go into circuitList not depList after departure)
288 tower_plane_rec_list_type depList;
289 tower_plane_rec_list_iterator depListItr;
291 // List of planes in the circuit (ordered by nearest to landing first)
292 tower_plane_rec_list_type circuitList;
293 tower_plane_rec_list_iterator circuitListItr;
295 // List of planes waiting to depart
296 tower_plane_rec_list_type holdList;
297 tower_plane_rec_list_iterator holdListItr;
299 // List of planes on rwy
300 tower_plane_rec_list_type rwyList;
301 tower_plane_rec_list_iterator rwyListItr;
303 // List of all planes due to use a given rwy arranged in projected order of rwy use
304 tower_plane_rec_list_type trafficList; // TODO - needs to be expandable to more than one rwy
305 tower_plane_rec_list_iterator trafficListItr;
307 // List of planes that have vacated the runway inbound but not yet handed off to ground
308 tower_plane_rec_list_type vacatedList;
309 tower_plane_rec_list_iterator vacatedListItr;
311 // Returns true if successful
312 bool RemoveFromTrafficList(string id);
313 bool RemoveFromAppList(string id);
314 bool RemoveFromRwyList(string id);
316 // Return the ETA of plane no. list_pos (1-based) in the traffic list.
317 // i.e. list_pos = 1 implies next to use runway.
318 double GetTrafficETA(unsigned int list_pos, bool printout = false);
320 // Add a tower plane rec with ETA to the traffic list in the correct position ETA-wise.
321 // Returns true if this could cause a threshold ETA conflict with other traffic, false otherwise.
322 bool AddToTrafficList(TowerPlaneRec* t, bool holding = false);
324 bool AddToCircuitList(TowerPlaneRec* t);
326 // Ground can be separate or handled by tower in real life.
327 // In the program we will always use a separate FGGround class, but we need to know
328 // whether it is supposed to be separate or not to give the correct instructions.
329 bool separateGround; // true if ground control is separate
330 FGGround* ground; // The ground control associated with this airport.
332 bool _departureControlled; // true if we need to hand off departing traffic to departure control
333 //FGDeparture* _departure; // The relevant departure control (once we've actually written it!)
335 // for failure modeling
336 string trans_ident; // transmitted ident
337 bool tower_failed; // tower failed?
339 // Pointers to current users position and orientation
340 SGPropertyNode* user_lon_node;
341 SGPropertyNode* user_lat_node;
342 SGPropertyNode* user_elev_node;
343 SGPropertyNode* user_hdg_node;
345 // Details of the general traffic flow etc in the circuit
346 double crosswind_leg_pos; // Distance from threshold crosswind leg is being turned to in meters (actual operation - *not* ideal circuit)
347 double downwind_leg_pos; // Actual offset distance in meters from the runway that planes are flying the downwind leg
348 // Currently not sure whether the above should be always +ve or just take the natural orthopos sign (+ve for RH circuit, -ve for LH).
349 double base_leg_pos; // Actual offset distance from the threshold (-ve) that planes are turning to base leg.
351 double nominal_crosswind_leg_pos;
352 double nominal_downwind_leg_pos;
353 double nominal_base_leg_pos;
355 friend istream& operator>> ( istream&, FGTower& );
358 #endif //_FG_TOWER_HXX