2002-04-03 23:54:44 +00:00
// FGTower - a class to provide tower control at towered airports.
//
// Written by David Luff, started March 2002.
//
// Copyright (C) 2002 David C. Luff - david.luff@nottingham.ac.uk
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# include <Main/globals.hxx>
2003-03-27 15:41:09 +00:00
# include <Airports/runways.hxx>
# include <simgear/math/sg_geodesy.hxx>
# include <simgear/debug/logstream.hxx>
2002-04-03 23:54:44 +00:00
# include "tower.hxx"
# include "ATCdisplay.hxx"
2002-12-04 20:06:20 +00:00
# include "ATCmgr.hxx"
2003-03-27 15:41:09 +00:00
# include "ATCutils.hxx"
# include "commlist.hxx"
2003-04-15 22:55:22 +00:00
# include "AILocalTraffic.hxx"
2002-12-04 20:06:20 +00:00
2003-03-11 17:15:23 +00:00
SG_USING_STD ( cout ) ;
2002-12-04 20:06:20 +00:00
// TowerPlaneRec
TowerPlaneRec : : TowerPlaneRec ( ) :
clearedToLand ( false ) ,
2003-03-27 15:41:09 +00:00
clearedToLineUp ( false ) ,
clearedToTakeOff ( false ) ,
holdShortReported ( false ) ,
2002-12-04 20:06:20 +00:00
longFinalReported ( false ) ,
longFinalAcknowledged ( false ) ,
finalReported ( false ) ,
2003-03-27 15:41:09 +00:00
finalAcknowledged ( false ) ,
2003-04-15 22:55:22 +00:00
opType ( TTT_UNKNOWN ) ,
2003-06-11 21:49:46 +00:00
leg ( LEG_UNKNOWN ) ,
2003-03-27 15:41:09 +00:00
isUser ( false )
2002-12-04 20:06:20 +00:00
{
2003-03-27 15:41:09 +00:00
plane . callsign = " UNKNOWN " ;
2002-12-04 20:06:20 +00:00
}
2003-03-27 15:41:09 +00:00
TowerPlaneRec : : TowerPlaneRec ( PlaneRec p ) :
2002-12-04 20:06:20 +00:00
clearedToLand ( false ) ,
2003-03-27 15:41:09 +00:00
clearedToLineUp ( false ) ,
clearedToTakeOff ( false ) ,
holdShortReported ( false ) ,
2002-12-04 20:06:20 +00:00
longFinalReported ( false ) ,
longFinalAcknowledged ( false ) ,
finalReported ( false ) ,
2003-03-27 15:41:09 +00:00
finalAcknowledged ( false ) ,
2003-04-15 22:55:22 +00:00
opType ( TTT_UNKNOWN ) ,
2003-06-11 21:49:46 +00:00
leg ( LEG_UNKNOWN ) ,
2003-03-27 15:41:09 +00:00
isUser ( false )
2002-12-04 20:06:20 +00:00
{
2003-03-27 15:41:09 +00:00
plane = p ;
2002-12-04 20:06:20 +00:00
}
TowerPlaneRec : : TowerPlaneRec ( Point3D pt ) :
clearedToLand ( false ) ,
2003-03-27 15:41:09 +00:00
clearedToLineUp ( false ) ,
clearedToTakeOff ( false ) ,
holdShortReported ( false ) ,
2002-12-04 20:06:20 +00:00
longFinalReported ( false ) ,
longFinalAcknowledged ( false ) ,
finalReported ( false ) ,
2003-03-27 15:41:09 +00:00
finalAcknowledged ( false ) ,
2003-04-15 22:55:22 +00:00
opType ( TTT_UNKNOWN ) ,
2003-06-11 21:49:46 +00:00
leg ( LEG_UNKNOWN ) ,
2003-03-27 15:41:09 +00:00
isUser ( false )
2002-12-04 20:06:20 +00:00
{
2003-03-27 15:41:09 +00:00
plane . callsign = " UNKNOWN " ;
2002-12-04 20:06:20 +00:00
pos = pt ;
}
2003-03-27 15:41:09 +00:00
TowerPlaneRec : : TowerPlaneRec ( PlaneRec p , Point3D pt ) :
2002-12-04 20:06:20 +00:00
clearedToLand ( false ) ,
2003-03-27 15:41:09 +00:00
clearedToLineUp ( false ) ,
clearedToTakeOff ( false ) ,
holdShortReported ( false ) ,
2002-12-04 20:06:20 +00:00
longFinalReported ( false ) ,
longFinalAcknowledged ( false ) ,
finalReported ( false ) ,
2003-03-27 15:41:09 +00:00
finalAcknowledged ( false ) ,
2003-04-15 22:55:22 +00:00
opType ( TTT_UNKNOWN ) ,
2003-06-11 21:49:46 +00:00
leg ( LEG_UNKNOWN ) ,
2003-03-27 15:41:09 +00:00
isUser ( false )
2002-12-04 20:06:20 +00:00
{
2003-03-27 15:41:09 +00:00
plane = p ;
2002-12-04 20:06:20 +00:00
pos = pt ;
}
// FGTower
2002-04-03 23:54:44 +00:00
FGTower : : FGTower ( ) {
2003-03-10 13:41:37 +00:00
ATCmgr = globals - > get_ATC_mgr ( ) ;
2003-03-27 15:41:09 +00:00
// Init the property nodes - TODO - need to make sure we're getting surface winds.
wind_from_hdg = fgGetNode ( " /environment/wind-from-heading-deg " , true ) ;
2003-07-31 01:41:46 +00:00
wind_speed_knots = fgGetNode ( " /environment/wind-speed-kt " , true ) ;
2003-03-27 15:41:09 +00:00
holdListItr = holdList . begin ( ) ;
appListItr = appList . begin ( ) ;
depListItr = depList . begin ( ) ;
rwyListItr = rwyList . begin ( ) ;
circuitListItr = circuitList . begin ( ) ;
trafficListItr = trafficList . begin ( ) ;
2002-04-03 23:54:44 +00:00
}
FGTower : : ~ FGTower ( ) {
2003-03-10 13:41:37 +00:00
if ( ! separateGround ) {
delete ground ;
}
2002-04-03 23:54:44 +00:00
}
void FGTower : : Init ( ) {
display = false ;
2003-03-10 13:41:37 +00:00
2003-03-27 15:41:09 +00:00
// Pointers to user's position
user_lon_node = fgGetNode ( " /position/longitude-deg " , true ) ;
user_lat_node = fgGetNode ( " /position/latitude-deg " , true ) ;
user_elev_node = fgGetNode ( " /position/altitude-ft " , true ) ;
2003-04-15 22:55:22 +00:00
user_hdg_node = fgGetNode ( " /orientation/heading-deg " , true ) ;
2003-03-27 15:41:09 +00:00
2003-03-10 13:41:37 +00:00
// Need some way to initialise rwyOccupied flag correctly if the user is on the runway and to know its the user.
// I'll punt the startup issue for now though!!!
rwyOccupied = false ;
// Setup the ground control at this airport
AirportATC a ;
2003-03-27 15:41:09 +00:00
//cout << "Tower ident = " << ident << '\n';
2003-03-10 13:41:37 +00:00
if ( ATCmgr - > GetAirportATCDetails ( ident , & a ) ) {
if ( a . ground_freq ) { // Ground control
ground = ( FGGround * ) ATCmgr - > GetATCPointer ( ident , GROUND ) ;
separateGround = true ;
if ( ground = = NULL ) {
// Something has gone wrong :-(
2003-03-27 15:41:09 +00:00
SG_LOG ( SG_ATC , SG_WARN , " ERROR - ground has frequency but can't get ground pointer :-( " ) ;
2003-03-10 13:41:37 +00:00
ground = new FGGround ( ident ) ;
separateGround = false ;
ground - > Init ( ) ;
if ( display ) {
ground - > SetDisplay ( ) ;
} else {
ground - > SetNoDisplay ( ) ;
}
}
} else {
// Initialise ground anyway to do the shortest path stuff!
// Note that we're now responsible for updating and deleting this - NOT the ATCMgr.
ground = new FGGround ( ident ) ;
separateGround = false ;
ground - > Init ( ) ;
if ( display ) {
ground - > SetDisplay ( ) ;
} else {
ground - > SetNoDisplay ( ) ;
}
}
} else {
2003-03-27 15:41:09 +00:00
SG_LOG ( SG_ATC , SG_ALERT , " Unable to find airport details for " < < ident < < " in FGTower::Init() " ) ;
2003-03-11 17:15:23 +00:00
// Initialise ground anyway to avoid segfault later
ground = new FGGround ( ident ) ;
separateGround = false ;
ground - > Init ( ) ;
if ( display ) {
ground - > SetDisplay ( ) ;
} else {
ground - > SetNoDisplay ( ) ;
}
2003-03-10 13:41:37 +00:00
}
2003-03-27 15:41:09 +00:00
// Get the airport elevation
aptElev = dclGetAirportElev ( ident . c_str ( ) ) * SG_FEET_TO_METER ;
DoRwyDetails ( ) ;
// FIXME - this currently assumes use of the active rwy by the user.
rwyOccupied = OnAnyRunway ( Point3D ( user_lon_node - > getDoubleValue ( ) , user_lat_node - > getDoubleValue ( ) , 0.0 ) ) ;
if ( rwyOccupied ) {
// Assume the user is started at the threshold ready to take-off
TowerPlaneRec * t = new TowerPlaneRec ;
t - > plane . callsign = " Charlie Foxtrot Sierra " ; // C-FGFS !!! - fixme - this is a bit hardwired
2003-04-15 22:55:22 +00:00
t - > opType = TTT_UNKNOWN ; // We don't know if the user wants to do circuits or a departure...
t - > leg = TAKEOFF_ROLL ;
2003-03-27 15:41:09 +00:00
t - > isUser = true ;
t - > planePtr = NULL ;
t - > clearedToTakeOff = true ;
rwyList . push_back ( t ) ;
}
2002-04-03 23:54:44 +00:00
}
2003-03-27 15:41:09 +00:00
void FGTower : : Update ( double dt ) {
2003-04-15 22:55:22 +00:00
static int ii = 0 ; // Counter for spreading the load
2003-05-07 00:07:20 +00:00
int ii_max = 15 ;
2003-03-27 15:41:09 +00:00
//cout << "T" << flush;
2003-04-15 22:55:22 +00:00
// Each time step, what do we need to do?
// We need to go through the list of outstanding requests and acknowedgements
// and process at least one of them.
// We need to go through the list of planes under our control and check if
// any need to be addressed.
// We need to check for planes not under our control coming within our
// control area and address if necessary.
2003-03-10 13:41:37 +00:00
// TODO - a lot of the below probably doesn't need to be called every frame and should be staggered.
// Sort the arriving planes
2003-04-15 22:55:22 +00:00
2003-03-10 13:41:37 +00:00
// Calculate the eta of each plane to the threshold.
// For ground traffic this is the fastest they can get there.
// For air traffic this is the middle approximation.
2003-04-15 22:55:22 +00:00
if ( ii = = 1 ) {
doThresholdETACalc ( ) ;
}
2003-03-10 13:41:37 +00:00
// Order the list of traffic as per expected threshold use and flag any conflicts
2003-04-15 22:55:22 +00:00
if ( ii = = 2 ) {
bool conflicts = doThresholdUseOrder ( ) ;
}
2003-03-10 13:41:37 +00:00
// sortConficts() !!!
2003-03-27 15:41:09 +00:00
// Do one plane from the hold list
2003-04-15 22:55:22 +00:00
if ( ii = = 4 ) {
if ( holdList . size ( ) ) {
//cout << "*holdListItr = " << *holdListItr << endl;
if ( holdListItr = = holdList . end ( ) ) {
holdListItr = holdList . begin ( ) ;
}
//cout << "*holdListItr = " << *holdListItr << endl;
//Process(*holdListItr);
TowerPlaneRec * t = * holdListItr ;
//cout << "t = " << t << endl;
if ( t - > holdShortReported ) {
double responseTime = 10.0 ; // seconds - this should get more sophisticated at some point
if ( t - > clearanceCounter > responseTime ) {
if ( t - > nextOnRwy ) {
if ( rwyOccupied ) {
// Do nothing for now - consider acknowloging hold short eventually
2003-03-27 15:41:09 +00:00
} else {
2003-04-15 22:55:22 +00:00
// Lets Roll !!!!
string trns = t - > plane . callsign ;
//if(departed plane < some threshold in time away) {
if ( 0 ) { // FIXME
trns + = " line up " ;
t - > clearedToLineUp = true ;
t - > planePtr - > RegisterTransmission ( 3 ) ; // cleared to line-up
t - > leg = TAKEOFF_ROLL ;
//} else if(arriving plane < some threshold away) {
} else if ( 0 ) { // FIXME
trns + = " cleared immediate take-off " ;
// TODO - add traffic is... ?
t - > clearedToTakeOff = true ;
t - > planePtr - > RegisterTransmission ( 4 ) ; // cleared to take-off - TODO differentiate between immediate and normal take-off
t - > leg = TAKEOFF_ROLL ;
} else {
trns + = " cleared for take-off " ;
// TODO - add traffic is... ?
t - > clearedToTakeOff = true ;
t - > planePtr - > RegisterTransmission ( 4 ) ; // cleared to take-off
t - > leg = TAKEOFF_ROLL ;
}
if ( display ) {
globals - > get_ATC_display ( ) - > RegisterSingleMessage ( trns , 0 ) ;
}
t - > holdShortReported = false ;
t - > clearanceCounter = 0 ;
rwyList . push_back ( t ) ;
rwyOccupied = true ;
holdList . erase ( holdListItr ) ;
holdListItr = holdList . begin ( ) ;
2003-03-27 15:41:09 +00:00
}
2003-04-15 22:55:22 +00:00
} else {
// possibly tell him to hold and what position he is?
2003-03-27 15:41:09 +00:00
}
} else {
2003-05-07 00:07:20 +00:00
t - > clearanceCounter + = ( dt * holdList . size ( ) * ii_max ) ;
2003-03-27 15:41:09 +00:00
}
2003-04-15 22:55:22 +00:00
}
+ + holdListItr ;
}
}
// Do the runway list - we'll do the whole runway list since it's important and there'll never be many planes on the rwy at once!!
if ( ii = = 5 ) {
if ( rwyOccupied ) {
if ( ! rwyList . size ( ) ) {
rwyOccupied = false ;
2003-03-27 15:41:09 +00:00
} else {
2003-04-15 22:55:22 +00:00
rwyListItr = rwyList . begin ( ) ;
TowerPlaneRec * t = * rwyListItr ;
if ( t - > isUser ) {
bool on_rwy = OnActiveRunway ( Point3D ( user_lon_node - > getDoubleValue ( ) , user_lat_node - > getDoubleValue ( ) , 0.0 ) ) ;
// TODO - how do we find the position when it's not the user?
if ( ! on_rwy ) {
if ( ( t - > opType = = INBOUND ) | | ( t - > opType = = STRAIGHT_IN ) ) {
rwyList . pop_front ( ) ;
delete t ;
// TODO - tell it to taxi / contact ground / don't delete it etc!
} else if ( t - > opType = = OUTBOUND ) {
depList . push_back ( t ) ;
rwyList . pop_front ( ) ;
} else if ( t - > opType = = CIRCUIT ) {
circuitList . push_back ( t ) ;
rwyList . pop_front ( ) ;
} else if ( t - > opType = = TTT_UNKNOWN ) {
depList . push_back ( t ) ;
circuitList . push_back ( t ) ;
rwyList . pop_front ( ) ;
} else {
// HELP - we shouldn't ever get here!!!
}
}
} // else TODO figure out what to do when it's not the user
2003-03-27 15:41:09 +00:00
}
2003-04-15 22:55:22 +00:00
}
2003-03-27 15:41:09 +00:00
}
2003-04-15 22:55:22 +00:00
// do the ciruit list
if ( ii = = 6 ) {
// Clear the constraints - we recalculate here.
base_leg_pos = 0.0 ;
downwind_leg_pos = 0.0 ;
crosswind_leg_pos = 0.0 ;
if ( circuitList . size ( ) ) {
circuitListItr = circuitList . begin ( ) ; // TODO - at the moment we're constraining plane 2 based on plane 1 - this won't work for 3 planes in the circuit!!
TowerPlaneRec * t = * circuitListItr ;
2003-06-11 21:49:46 +00:00
if ( t - > isUser ) {
t - > pos . setlon ( user_lon_node - > getDoubleValue ( ) ) ;
t - > pos . setlat ( user_lat_node - > getDoubleValue ( ) ) ;
t - > pos . setelev ( user_elev_node - > getDoubleValue ( ) ) ;
} else {
// TODO - set/update the position if it's an AI plane
}
2003-04-15 22:55:22 +00:00
Point3D tortho = ortho . ConvertToLocal ( t - > pos ) ;
2003-03-27 15:41:09 +00:00
if ( t - > isUser ) {
2003-04-15 22:55:22 +00:00
// Need to figure out which leg he's on
2003-06-11 21:49:46 +00:00
//cout << "rwy.hdg = " << rwy.hdg << " user hdg = " << user_hdg_node->getDoubleValue();
2003-04-15 22:55:22 +00:00
double ho = GetAngleDiff_deg ( user_hdg_node - > getDoubleValue ( ) , rwy . hdg ) ;
2003-06-11 21:49:46 +00:00
//cout << " ho = " << ho << '\n';
2003-04-15 22:55:22 +00:00
// TODO FIXME - get the wind and convert this to track, or otherwise use track somehow!!!
// If it's gusty might need to filter the value, although we are leaving 30 degrees each way leeway!
if ( abs ( ho ) < 30 ) {
// could be either takeoff, climbout or landing - check orthopos.y
2003-06-11 21:49:46 +00:00
//cout << "tortho.y = " << tortho.y() << '\n';
if ( ( tortho . y ( ) < 0 ) | | ( t - > leg = = TURN4 ) | | ( t - > leg = = FINAL ) ) {
t - > leg = FINAL ;
//cout << "Final\n";
2003-04-15 22:55:22 +00:00
} else {
t - > leg = CLIMBOUT ; // TODO - check elev wrt. apt elev to differentiate takeoff roll and climbout
//cout << "Climbout\n";
2003-06-11 21:49:46 +00:00
// If it's the user we may be unsure of his/her intentions.
// (Hopefully the AI planes won't try confusing the sim!!!)
if ( t - > opType = = TTT_UNKNOWN ) {
if ( tortho . y ( ) > 5000 ) {
// 5 km out from threshold - assume it's a departure
t - > opType = OUTBOUND ;
// Since we are unknown operation we should be in depList already.
circuitList . erase ( circuitListItr ) ;
circuitListItr = circuitList . begin ( ) ;
}
} else if ( t - > opType = = CIRCUIT ) {
if ( tortho . y ( ) > 10000 ) {
// 10 km out - assume the user has abandoned the circuit!!
t - > opType = OUTBOUND ;
depList . push_back ( t ) ;
circuitList . erase ( circuitListItr ) ;
circuitListItr = circuitList . begin ( ) ;
}
}
2003-04-15 22:55:22 +00:00
}
} else if ( abs ( ho ) < 60 ) {
// turn1 or turn 4
// TODO - either fix or doublecheck this hack by looking at heading and pattern direction
if ( ( t - > leg = = CLIMBOUT ) | | ( t - > leg = = TURN1 ) ) {
t - > leg = TURN1 ;
//cout << "Turn1\n";
} else {
t - > leg = TURN4 ;
//cout << "Turn4\n";
}
} else if ( abs ( ho ) < 120 ) {
// crosswind or base
// TODO - either fix or doublecheck this hack by looking at heading and pattern direction
if ( ( t - > leg = = TURN1 ) | | ( t - > leg = = CROSSWIND ) ) {
t - > leg = CROSSWIND ;
//cout << "Crosswind\n";
} else {
t - > leg = BASE ;
//cout << "Base\n";
}
} else if ( abs ( ho ) < 150 ) {
// turn2 or turn 3
// TODO - either fix or doublecheck this hack by looking at heading and pattern direction
if ( ( t - > leg = = CROSSWIND ) | | ( t - > leg = = TURN2 ) ) {
t - > leg = TURN2 ;
//cout << "Turn2\n";
} else {
t - > leg = TURN3 ;
//cout << "Turn3\n";
}
} else {
// downwind
t - > leg = DOWNWIND ;
//cout << "Downwind\n";
2003-03-27 15:41:09 +00:00
}
2003-04-15 22:55:22 +00:00
} else {
t - > leg = t - > planePtr - > GetLeg ( ) ;
}
switch ( t - > leg ) {
case FINAL :
// Base leg must be at least as far out as the plane is - actually possibly not necessary for separation, but we'll use that for now.
base_leg_pos = tortho . y ( ) ;
2003-06-11 21:49:46 +00:00
//cout << "base_leg_pos = " << base_leg_pos << '\n';
2003-04-15 22:55:22 +00:00
break ;
case TURN4 :
// Fall through to base
case BASE :
base_leg_pos = tortho . y ( ) ;
2003-06-11 21:49:46 +00:00
//cout << "base_leg_pos = " << base_leg_pos << '\n';
2003-04-15 22:55:22 +00:00
break ;
case TURN3 :
// Fall through to downwind
case DOWNWIND :
// Only have the downwind leg pos as turn-to-base constraint if more negative than we already have.
base_leg_pos = ( tortho . y ( ) < base_leg_pos ? tortho . y ( ) : base_leg_pos ) ;
2003-06-11 21:49:46 +00:00
//cout << "base_leg_pos = " << base_leg_pos;
2003-04-15 22:55:22 +00:00
downwind_leg_pos = tortho . x ( ) ; // Assume that a following plane can simply be constrained by the immediately in front downwind plane
2003-06-11 21:49:46 +00:00
//cout << " downwind_leg_pos = " << downwind_leg_pos << '\n';
2003-04-15 22:55:22 +00:00
break ;
case TURN2 :
// Fall through to crosswind
case CROSSWIND :
crosswind_leg_pos = tortho . y ( ) ;
2003-06-11 21:49:46 +00:00
//cout << "crosswind_leg_pos = " << crosswind_leg_pos << '\n';
2003-04-15 22:55:22 +00:00
break ;
case TURN1 :
// Fall through to climbout
case CLIMBOUT :
// Only use current by constraint as largest
2003-06-11 21:49:46 +00:00
crosswind_leg_pos = ( tortho . y ( ) > crosswind_leg_pos ? tortho . y ( ) : crosswind_leg_pos ) ;
//cout << "crosswind_leg_pos = " << crosswind_leg_pos << '\n';
2003-04-15 22:55:22 +00:00
break ;
case TAKEOFF_ROLL :
break ;
case LEG_UNKNOWN :
break ;
case LANDING_ROLL :
break ;
default :
break ;
}
2003-03-27 15:41:09 +00:00
}
}
2003-03-10 13:41:37 +00:00
doCommunication ( ) ;
if ( ! separateGround ) {
// The display stuff might have to get more clever than this when not separate
// since the tower and ground might try communicating simultaneously even though
// they're mean't to be the same contoller/frequency!!
if ( display ) {
ground - > SetDisplay ( ) ;
} else {
ground - > SetNoDisplay ( ) ;
}
2003-03-27 15:41:09 +00:00
ground - > Update ( dt ) ;
}
2003-04-15 22:55:22 +00:00
+ + ii ;
// How big should ii get - ie how long should the update cycle interval stretch?
2003-05-07 00:07:20 +00:00
if ( ii > = ii_max ) {
2003-04-15 22:55:22 +00:00
ii = 0 ;
}
2003-09-05 10:22:18 +00:00
if ( ident = = " KEMT " ) {
// For AI debugging convienience - may be removed
Point3D user_pos ;
user_pos . setlon ( user_lon_node - > getDoubleValue ( ) ) ;
user_pos . setlat ( user_lat_node - > getDoubleValue ( ) ) ;
user_pos . setelev ( user_elev_node - > getDoubleValue ( ) ) ;
Point3D user_ortho_pos = ortho . ConvertToLocal ( user_pos ) ;
fgSetDouble ( " /AI/user/ortho-x " , user_ortho_pos . x ( ) ) ;
fgSetDouble ( " /AI/user/ortho-y " , user_ortho_pos . y ( ) ) ;
fgSetDouble ( " /AI/user/elev " , user_elev_node - > getDoubleValue ( ) ) ;
}
2003-03-27 15:41:09 +00:00
}
2003-06-11 21:49:46 +00:00
// Returns true if positions of crosswind/downwind/base leg turns should be constrained by previous traffic
// plus the constraint position as a rwy orientated orthopos (meters)
bool FGTower : : GetCrosswindConstraint ( double & cpos ) {
if ( crosswind_leg_pos ! = 0.0 ) {
cpos = crosswind_leg_pos ;
return ( true ) ;
} else {
cpos = 0.0 ;
return ( false ) ;
}
}
bool FGTower : : GetDownwindConstraint ( double & dpos ) {
if ( downwind_leg_pos ! = 0.0 ) {
dpos = downwind_leg_pos ;
return ( true ) ;
} else {
dpos = 0.0 ;
return ( false ) ;
}
}
bool FGTower : : GetBaseConstraint ( double & bpos ) {
if ( base_leg_pos ! = 0.0 ) {
bpos = base_leg_pos ;
return ( true ) ;
} else {
bpos = 0.0 ;
return ( false ) ;
}
}
2003-03-27 15:41:09 +00:00
// Figure out which runways are active.
// For now we'll just be simple and do one active runway - eventually this will get much more complex
// This is a private function - public interface to the results of this is through GetActiveRunway
void FGTower : : DoRwyDetails ( ) {
//cout << "GetRwyDetails called" << endl;
// Based on the airport-id and wind get the active runway
//wind
double hdg = wind_from_hdg - > getDoubleValue ( ) ;
double speed = wind_speed_knots - > getDoubleValue ( ) ;
hdg = ( speed = = 0.0 ? 270.0 : hdg ) ;
//cout << "Heading = " << hdg << '\n';
FGRunway runway ;
2003-08-29 04:11:23 +00:00
bool rwyGood = globals - > get_runways ( ) - > search ( ident , int ( hdg ) , & runway ) ;
2003-03-27 15:41:09 +00:00
if ( rwyGood ) {
activeRwy = runway . rwy_no ;
2003-03-28 15:24:02 +00:00
rwy . rwyID = runway . rwy_no ;
2003-03-27 15:41:09 +00:00
SG_LOG ( SG_ATC , SG_INFO , " Active runway for airport " < < ident < < " is " < < activeRwy ) ;
// Get the threshold position
double other_way = runway . heading - 180.0 ;
while ( other_way < = 0.0 ) {
other_way + = 360.0 ;
}
// move to the +l end/center of the runway
//cout << "Runway center is at " << runway.lon << ", " << runway.lat << '\n';
Point3D origin = Point3D ( runway . lon , runway . lat , aptElev ) ;
Point3D ref = origin ;
double tshlon , tshlat , tshr ;
double tolon , tolat , tor ;
rwy . length = runway . length * SG_FEET_TO_METER ;
2003-03-31 16:24:41 +00:00
rwy . width = runway . width * SG_FEET_TO_METER ;
2003-03-27 15:41:09 +00:00
geo_direct_wgs_84 ( aptElev , ref . lat ( ) , ref . lon ( ) , other_way ,
rwy . length / 2.0 - 25.0 , & tshlat , & tshlon , & tshr ) ;
geo_direct_wgs_84 ( aptElev , ref . lat ( ) , ref . lon ( ) , runway . heading ,
rwy . length / 2.0 - 25.0 , & tolat , & tolon , & tor ) ;
// Note - 25 meters in from the runway end is a bit of a hack to put the plane ahead of the user.
// now copy what we need out of runway into rwy
rwy . threshold_pos = Point3D ( tshlon , tshlat , aptElev ) ;
Point3D takeoff_end = Point3D ( tolon , tolat , aptElev ) ;
//cout << "Threshold position = " << tshlon << ", " << tshlat << ", " << aptElev << '\n';
//cout << "Takeoff position = " << tolon << ", " << tolat << ", " << aptElev << '\n';
rwy . hdg = runway . heading ;
// Set the projection for the local area based on this active runway
ortho . Init ( rwy . threshold_pos , rwy . hdg ) ;
rwy . end1ortho = ortho . ConvertToLocal ( rwy . threshold_pos ) ; // should come out as zero
rwy . end2ortho = ortho . ConvertToLocal ( takeoff_end ) ;
} else {
SG_LOG ( SG_ATC , SG_ALERT , " Help - can't get good runway in FGTower!! " ) ;
activeRwy = " NN " ;
}
}
// Figure out if a given position lies on the active runway
// Might have to change when we consider more than one active rwy.
bool FGTower : : OnActiveRunway ( Point3D pt ) {
2003-03-31 16:24:41 +00:00
// TODO - check that the centre calculation below isn't confused by displaced thesholds etc.
Point3D xyc ( ( rwy . end1ortho . x ( ) + rwy . end2ortho . x ( ) ) / 2.0 , ( rwy . end1ortho . y ( ) + rwy . end2ortho . y ( ) ) / 2.0 , 0.0 ) ;
Point3D xyp = ortho . ConvertToLocal ( pt ) ;
2003-03-27 15:41:09 +00:00
2003-03-31 16:24:41 +00:00
//cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
//cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
double rlen = rwy . length / 2.0 + 5.0 ;
double rwidth = rwy . width / 2.0 ;
double ldiff = fabs ( xyp . y ( ) - xyc . y ( ) ) ;
double wdiff = fabs ( xyp . x ( ) - xyc . x ( ) ) ;
return ( ( ldiff < rlen ) & & ( wdiff < rwidth ) ) ;
2003-03-27 15:41:09 +00:00
}
// Figure out if a given position lies on any runway or not
2003-03-31 16:24:41 +00:00
// Only call this at startup - reading the runways database is expensive and needs to be fixed!
2003-03-27 15:41:09 +00:00
bool FGTower : : OnAnyRunway ( Point3D pt ) {
ATCData ad ;
double dist = current_commlist - > FindClosest ( lon , lat , elev , ad , TOWER , 10.0 ) ;
if ( dist < 0.0 ) {
return ( false ) ;
2003-03-10 13:41:37 +00:00
}
2003-03-27 15:41:09 +00:00
// Based on the airport-id, go through all the runways and check for a point in them
// TODO - do we actually need to search for the airport - surely we already know our ident and
// can just search runways of our airport???
//cout << "Airport ident is " << ad.ident << '\n';
FGRunway runway ;
2003-08-29 04:11:23 +00:00
bool rwyGood = globals - > get_runways ( ) - > search ( ad . ident , & runway ) ;
2003-03-27 15:41:09 +00:00
if ( ! rwyGood ) {
SG_LOG ( SG_ATC , SG_WARN , " Unable to find any runways for airport ID " < < ad . ident < < " in FGTower " ) ;
}
bool on = false ;
while ( runway . id = = ad . ident ) {
2003-03-31 16:24:41 +00:00
on = OnRunway ( pt , runway ) ;
2003-03-27 15:41:09 +00:00
//cout << "Runway " << runway.rwy_no << ": On = " << (on ? "true\n" : "false\n");
if ( on ) return ( true ) ;
2003-08-29 04:11:23 +00:00
globals - > get_runways ( ) - > next ( & runway ) ;
2003-03-27 15:41:09 +00:00
}
return ( on ) ;
2003-03-10 13:41:37 +00:00
}
2003-03-27 15:41:09 +00:00
2003-03-10 13:41:37 +00:00
// Calculate the eta of each plane to the threshold.
// For ground traffic this is the fastest they can get there.
// For air traffic this is the middle approximation.
void FGTower : : doThresholdETACalc ( ) {
// For now we'll be very crude and hardwire expected speeds to C172-like values
2003-03-27 15:41:09 +00:00
// The speeds below are specified in knots IAS and then converted to m/s
double app_ias = 100.0 * 0.514444 ; // Speed during straight-in approach
double circuit_ias = 80.0 * 0.514444 ; // Speed around circuit
double final_ias = 70.0 * 0.514444 ; // Speed during final approach
2003-03-10 13:41:37 +00:00
tower_plane_rec_list_iterator twrItr ;
2003-03-27 15:41:09 +00:00
// Sign convention - dist_out is -ve for approaching planes and +ve for departing planes
// dist_across is +ve in the pattern direction - ie a plane correctly on downwind will have a +ve dist_across
2003-03-10 13:41:37 +00:00
for ( twrItr = trafficList . begin ( ) ; twrItr ! = trafficList . end ( ) ; twrItr + + ) {
2003-03-27 15:41:09 +00:00
TowerPlaneRec * tpr = * twrItr ;
Point3D op = ortho . ConvertToLocal ( tpr - > pos ) ;
double dist_out_m = op . y ( ) ;
double dist_across_m = fabs ( op . x ( ) ) ; // FIXME = the fabs is a hack to cope with the fact that we don't know the circuit direction yet
//cout << "Doing ETA calc for " << tpr->plane.callsign << '\n';
if ( tpr - > opType = = CIRCUIT ) {
// It's complicated - depends on if base leg is delayed or not
2003-04-15 22:55:22 +00:00
if ( tpr - > leg = = LANDING_ROLL ) {
2003-03-27 15:41:09 +00:00
tpr - > eta = 0 ;
2003-04-15 22:55:22 +00:00
} else if ( ( tpr - > leg = = FINAL ) | | ( tpr - > leg = = TURN4 ) ) {
2003-03-27 15:41:09 +00:00
tpr - > eta = fabs ( dist_out_m ) / final_ias ;
2003-04-15 22:55:22 +00:00
} else if ( ( tpr - > leg = = BASE ) | | ( tpr - > leg = = TURN3 ) ) {
2003-03-27 15:41:09 +00:00
tpr - > eta = ( fabs ( dist_out_m ) / final_ias ) + ( dist_across_m / circuit_ias ) ;
} else {
// Need to calculate where base leg is likely to be
// FIXME - for now I'll hardwire it to 1000m which is what AILocalTraffic uses!!!
// TODO - as a matter of design - AILocalTraffic should get the nominal no-traffic base turn distance from Tower, since in real life the published pattern might differ from airport to airport
double nominal_base_dist_out_m = - 1000 ;
double current_base_dist_out_m = nominal_base_dist_out_m ;
double nominal_dist_across_m = 1000 ; // Hardwired value from AILocalTraffic
double nominal_cross_dist_out_m = 1000 ; // Bit of a guess - AI plane turns to crosswind at 600ft agl.
tpr - > eta = fabs ( current_base_dist_out_m ) / final_ias ; // final
2003-04-15 22:55:22 +00:00
if ( ( tpr - > leg = = DOWNWIND ) | | ( tpr - > leg = = TURN2 ) ) {
2003-03-27 15:41:09 +00:00
tpr - > eta + = dist_across_m / circuit_ias ;
tpr - > eta + = fabs ( current_base_dist_out_m - dist_out_m ) / circuit_ias ;
2003-04-15 22:55:22 +00:00
} else if ( ( tpr - > leg = = CROSSWIND ) | | ( tpr - > leg = = TURN1 ) ) {
2003-03-27 15:41:09 +00:00
tpr - > eta + = nominal_dist_across_m / circuit_ias ; // should we use the dist across of the previous plane if there is previous still on downwind?
tpr - > eta + = fabs ( current_base_dist_out_m - nominal_cross_dist_out_m ) / circuit_ias ;
tpr - > eta + = ( nominal_dist_across_m - dist_across_m ) / circuit_ias ;
} else {
// We've only just started - why not use a generic estimate?
}
}
} else if ( ( tpr - > opType = = INBOUND ) | | ( tpr - > opType = = STRAIGHT_IN ) ) {
// It's simpler!
} else {
// Must be outbound - ignore it!
}
//cout << "ETA = " << tpr->eta << '\n';
2003-03-10 13:41:37 +00:00
}
}
2003-03-27 15:41:09 +00:00
2003-03-10 13:41:37 +00:00
bool FGTower : : doThresholdUseOrder ( ) {
return ( true ) ;
}
void FGTower : : doCommunication ( ) {
2002-04-03 23:54:44 +00:00
}
2003-03-27 15:41:09 +00:00
2003-04-15 22:55:22 +00:00
void FGTower : : ContactAtHoldShort ( PlaneRec plane , FGAIPlane * requestee , tower_traffic_type operation ) {
2003-03-27 15:41:09 +00:00
// HACK - assume that anything contacting at hold short is new for now - FIXME LATER
TowerPlaneRec * t = new TowerPlaneRec ;
t - > plane = plane ;
t - > planePtr = requestee ;
t - > holdShortReported = true ;
t - > clearanceCounter = 0 ;
t - > clearedToLineUp = false ;
t - > clearedToTakeOff = false ;
t - > opType = operation ;
// HACK ALERT - THIS IS HARDWIRED FOR TESTING - FIXME TODO ETC
t - > nextOnRwy = true ;
//cout << "t = " << t << '\n';
holdList . push_back ( t ) ;
}
2002-12-17 11:36:14 +00:00
void FGTower : : RequestLandingClearance ( string ID ) {
2003-03-27 15:41:09 +00:00
//cout << "Request Landing Clearance called...\n";
2002-12-17 11:36:14 +00:00
}
void FGTower : : RequestDepartureClearance ( string ID ) {
2003-03-27 15:41:09 +00:00
//cout << "Request Departure Clearance called...\n";
2002-12-17 11:36:14 +00:00
}
//void FGTower::ReportFinal(string ID);
//void FGTower::ReportLongFinal(string ID);
//void FGTower::ReportOuterMarker(string ID);
//void FGTower::ReportMiddleMarker(string ID);
//void FGTower::ReportInnerMarker(string ID);
//void FGTower::ReportGoingAround(string ID);
void FGTower : : ReportRunwayVacated ( string ID ) {
2003-03-27 15:41:09 +00:00
//cout << "Report Runway Vacated Called...\n";
2002-12-17 11:36:14 +00:00
}