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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <simgear/compiler.h>
25 #include <simgear/math/SGMath.hxx>
26 #include <simgear/misc/sgstream.hxx>
33 #include "ATCProjection.hxx"
34 #include "AIPlane.hxx"
39 //DCL - a complete guess for now.
40 #define FG_TOWER_DEFAULT_RANGE 30
42 enum tower_traffic_type {
44 INBOUND, // CIRCUIT traffic gets changed to INBOUND when on final of the full-stop circuit.
46 TTT_UNKNOWN, // departure, but we don't know if for circuits or leaving properly
50 ostream& operator << (ostream& os, tower_traffic_type ttt);
52 enum tower_callback_type {
53 USER_REQUEST_VFR_DEPARTURE = 1,
54 USER_REQUEST_VFR_ARRIVAL = 2,
55 USER_REQUEST_VFR_ARRIVAL_FULL_STOP = 3,
56 USER_REQUEST_VFR_ARRIVAL_TOUCH_AND_GO = 4,
57 USER_REPORT_3_MILE_FINAL = 5,
58 USER_REPORT_DOWNWIND = 6,
59 USER_REPORT_RWY_VACATED = 7,
60 USER_REPORT_GOING_AROUND = 8,
61 USER_REQUEST_TAKE_OFF = 9
64 // TODO - need some differentiation of IFR and VFR traffic in order to give the former priority.
66 // Structure for holding details of a plane under tower control.
67 // Not fixed yet - may include more stuff later.
73 TowerPlaneRec(const PlaneRec& p);
74 TowerPlaneRec(const SGGeod& pt);
75 TowerPlaneRec(const PlaneRec& p, const SGGeod& pt);
77 FGAIPlane* planePtr; // This might move to the planeRec eventually
81 double eta; // seconds
82 double dist_out; // meters from theshold
85 bool clearedToTakeOff;
86 // ought to add time cleared to depart so we can nag if necessary
87 bool holdShortReported;
89 bool downwindReported;
90 bool longFinalReported;
91 bool longFinalAcknowledged;
93 bool finalAcknowledged;
94 bool rwyVacatedReported;
95 bool rwyVacatedAcknowledged;
96 bool goAroundReported; // set true if plane informs tower that it's going around.
97 bool instructedToGoAround; // set true if plane told by tower to go around.
98 bool onRwy; // is physically on the runway
99 bool nextOnRwy; // currently projected by tower to be the next on the runway
100 bool gearWasUp; // Tell to ATC about gear
101 bool gearUpReported; // Tell to pilot about landing gear
103 bool vfrArrivalReported;
104 bool vfrArrivalAcknowledged;
106 // Type of operation the plane is doing
107 tower_traffic_type opType;
109 // Whereabouts in circuit if doing circuits
112 LandingType landingType;
113 bool isUser; // true if this plane is the user
117 typedef std::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(const std::string& ID, const LandingType& opt = AIP_LT_UNKNOWN);
139 // For the AI planes...
140 void VFRArrivalContact(const PlaneRec& plane, FGAIPlane* requestee, const LandingType& lt = AIP_LT_UNKNOWN);
142 void RequestDepartureClearance(const std::string& ID);
143 void RequestTakeOffClearance(const std::string& ID);
144 void ReportFinal(const std::string& ID);
145 void ReportLongFinal(const std::string& ID);
146 void ReportOuterMarker(const std::string& ID);
147 void ReportMiddleMarker(const std::string& ID);
148 void ReportInnerMarker(const std::string& ID);
149 void ReportRunwayVacated(const std::string& ID);
150 void ReportReadyForDeparture(const std::string& ID);
151 void ReportDownwind(const std::string& ID);
152 void ReportGoingAround(const std::string& ID);
154 // Contact tower when at a hold short for departure - for now we'll assume plane - maybe vehicles might want to cross runway eventually?
155 void ContactAtHoldShort(const PlaneRec& plane, FGAIPlane* requestee, tower_traffic_type operation);
157 // Register the presence of an AI plane at a point where contact would already have been made in real life
158 // CAUTION - currently it is assumed that this plane's callsign is unique - it is up to AIMgr to generate unique callsigns.
159 void RegisterAIPlane(const PlaneRec& plane, FGAIPlane* ai, const tower_traffic_type& op, const PatternLeg& lg = LEG_UNKNOWN);
161 // Deregister and remove an AI plane.
162 void DeregisterAIPlane(const std::string& id);
164 // Public interface to the active runway - this will get more complex
165 // in the future and consider multi-runway use, airplane weight etc.
166 inline const std::string& GetActiveRunway() const { return activeRwy; }
167 inline const RunwayDetails& GetActiveRunwayDetails() const { return rwy; }
168 // Get the pattern direction of the active rwy.
169 inline int GetPatternDirection() const { return rwy.patternDirection; }
171 inline const std::string& get_trans_ident() const { return trans_ident; }
173 inline FGGround* const GetGroundPtr() const { return ground; }
175 // Returns true if positions of crosswind/downwind/base leg turns should be constrained by previous traffic
176 // plus the constraint position as a rwy orientated orthopos (meters)
177 bool GetCrosswindConstraint(double& cpos);
178 bool GetDownwindConstraint(double& dpos);
179 bool GetBaseConstraint(double& bpos);
181 std::string GenText(const std::string& m, int c);
182 std::string GetWeather();
183 std::string GetATISID();
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);
193 void ProcessDownwindReport(TowerPlaneRec* t);
195 // Remove all options from the user dialog choice
196 void RemoveAllUserDialogOptions();
198 // Periodic checks on the various traffic.
199 void CheckHoldList(double dt);
200 void CheckCircuitList(double dt);
201 void CheckRunwayList(double dt);
202 void CheckApproachList(double dt);
203 void CheckDepartureList(double dt);
205 // Currently this assumes we *are* next on the runway and doesn't check for planes about to land -
206 // this should be done prior to calling this function.
207 void ClearHoldingPlane(TowerPlaneRec* t);
209 // Find a pointer to plane of callsign ID within the internal data structures
210 TowerPlaneRec* FindPlane(const std::string& ID);
212 // Remove and delete all instances of a plane with a given ID
213 void RemovePlane(const std::string& ID);
215 // Figure out if a given position lies on the active runway
216 // Might have to change when we consider more than one active rwy.
217 bool OnActiveRunway(const SGGeod& pt);
219 // Figure out if a given position lies on a runway or not
220 bool OnAnyRunway(const SGGeod& pt, bool onGround);
222 // Calculate the eta of a plane to the threshold.
223 // For ground traffic this is the fastest they can get there.
224 // For air traffic this is the middle approximation.
225 void CalcETA(TowerPlaneRec* tpr, bool printout = false);
227 // Iterate through all the std::lists and call CalcETA for all the planes.
228 void doThresholdETACalc();
230 // Order the std::list of traffic as per expected threshold use and flag any conflicts
231 bool doThresholdUseOrder();
233 // Calculate the crow-flys distance of a plane to the threshold in meters
234 double CalcDistOutM(TowerPlaneRec* tpr);
236 // Calculate the crow-flys distance of a plane to the threshold in miles
237 double CalcDistOutMiles(TowerPlaneRec* tpr);
240 void doCommunication();
243 void IssueLandingClearance(TowerPlaneRec* tpr);
244 void IssueGoAround(TowerPlaneRec* tpr);
245 void IssueDepartureClearance(TowerPlaneRec* tpr);
247 unsigned int update_count; // Convienince counter for speading computational load over several updates
248 unsigned int update_count_max; // ditto.
250 double timeSinceLastDeparture; // Time in seconds since last departure from active rwy.
251 bool departed; // set true when the above needs incrementing with time, false when it doesn't.
253 // environment - need to make sure we're getting the surface winds and not winds aloft.
254 SGPropertyNode_ptr wind_from_hdg; //degrees
255 SGPropertyNode_ptr wind_speed_knots; //knots
257 double aptElev; // Airport elevation
258 std::string activeRwy; // Active runway number - For now we'll disregard multiple / alternate runway operation.
259 RunwayDetails rwy; // Assumed to be the active one for now.
260 bool rwyOccupied; // Active runway occupied flag. For now we'll disregard land-and-hold-short operations.
261 FGATCAlignedProjection ortho; // Orthogonal mapping of the local area with the active runway threshold at the origin
262 FGATCAlignedProjection ortho_temp; // Ortho for any runway (needed to get plane position in airport)
264 // Figure out which runways are active.
265 // For now we'll just be simple and do one active runway - eventually this will get much more complex
266 // This is a private function - public interface to the results of this is through GetActiveRunway
269 // Need a data structure to hold details of the various active planes
270 // or possibly another data structure with the positions of the inactive planes.
271 // Need a data structure to hold outstanding communications from aircraft.
273 // Linked-list of planes on approach to active rwy ordered with nearest first (timewise).
274 // Includes planes that have landed but not yet vacated runway.
275 // Somewhat analagous to the paper strips used (used to be used?) in real life.
276 // Doesn't include planes in circuit until they turn onto base/final?
277 // TODO - may need to alter this for operation to more than one active rwy.
278 tower_plane_rec_list_type appList;
279 tower_plane_rec_list_iterator appListItr;
281 // What should we do with planes approaching the airport to join the circuit somewhere
282 // but not on straight-in though? - put them in here for now.
283 tower_plane_rec_list_type circuitAppList;
284 tower_plane_rec_list_iterator circuitAppListItr;
286 // List of departed planes (planes doing circuits go into circuitList not depList after departure)
287 tower_plane_rec_list_type depList;
288 tower_plane_rec_list_iterator depListItr;
290 // List of planes in the circuit (ordered by nearest to landing first)
291 tower_plane_rec_list_type circuitList;
292 tower_plane_rec_list_iterator circuitListItr;
294 // List of planes waiting to depart
295 tower_plane_rec_list_type holdList;
296 tower_plane_rec_list_iterator holdListItr;
298 // List of planes on rwy
299 tower_plane_rec_list_type rwyList;
300 tower_plane_rec_list_iterator rwyListItr;
302 // List of all planes due to use a given rwy arranged in projected order of rwy use
303 tower_plane_rec_list_type trafficList; // TODO - needs to be expandable to more than one rwy
304 tower_plane_rec_list_iterator trafficListItr;
306 // List of planes that have vacated the runway inbound but not yet handed off to ground
307 tower_plane_rec_list_type vacatedList;
308 tower_plane_rec_list_iterator vacatedListItr;
310 // Returns true if successful
311 bool RemoveFromTrafficList(const std::string& id);
312 bool RemoveFromAppList(const std::string& id);
313 bool RemoveFromRwyList(const std::string& id);
315 // Return the ETA of plane no. list_pos (1-based) in the traffic list.
316 // i.e. list_pos = 1 implies next to use runway.
317 double GetTrafficETA(unsigned int list_pos, bool printout = false);
319 // Add a tower plane rec with ETA to the traffic list in the correct position ETA-wise.
320 // Returns true if this could cause a threshold ETA conflict with other traffic, false otherwise.
321 bool AddToTrafficList(TowerPlaneRec* t, bool holding = false);
323 bool AddToCircuitList(TowerPlaneRec* t);
325 // Add to vacated list only if not already present
326 void AddToVacatedList(TowerPlaneRec* t);
328 void AddToHoldingList(TowerPlaneRec* t);
330 // Ground can be separate or handled by tower in real life.
331 // In the program we will always use a separate FGGround class, but we need to know
332 // whether it is supposed to be separate or not to give the correct instructions.
333 bool separateGround; // true if ground control is separate
334 FGGround* ground; // The ground control associated with this airport.
336 bool _departureControlled; // true if we need to hand off departing traffic to departure control
337 //FGDeparture* _departure; // The relevant departure control (once we've actually written it!)
339 // for failure modeling
340 std::string trans_ident; // transmitted ident
341 bool tower_failed; // tower failed?
343 // Pointers to current users position and orientation
344 SGPropertyNode_ptr user_lon_node;
345 SGPropertyNode_ptr user_lat_node;
346 SGPropertyNode_ptr user_elev_node;
347 SGPropertyNode_ptr user_hdg_node;
349 // Details of the general traffic flow etc in the circuit
350 double crosswind_leg_pos; // Distance from threshold crosswind leg is being turned to in meters (actual operation - *not* ideal circuit)
351 double downwind_leg_pos; // Actual offset distance in meters from the runway that planes are flying the downwind leg
352 // Currently not sure whether the above should be always +ve or just take the natural orthopos sign (+ve for RH circuit, -ve for LH).
353 double base_leg_pos; // Actual offset distance from the threshold (-ve) that planes are turning to base leg.
355 double nominal_crosswind_leg_pos;
356 double nominal_downwind_leg_pos;
357 double nominal_base_leg_pos;
359 friend std::istream& operator>> ( std::istream&, FGTower& );
362 #endif //_FG_TOWER_HXX