#include STL_FSTREAM
#include "ground.hxx"
+#include "ATCmgr.hxx"
#include "ATCutils.hxx"
+#include "ATCdisplay.hxx"
+#include "AILocalTraffic.hxx"
SG_USING_STD(ifstream);
SG_USING_STD(cout);
}
FGGround::FGGround() {
+ ATCmgr = globals->get_ATC_mgr();
display = false;
networkLoadOK = false;
+ ground_traffic.erase(ground_traffic.begin(), ground_traffic.end());
+ ground_traffic_itr = ground_traffic.begin();
+
+ // Init the property nodes - TODO - need to make sure we're getting surface winds.
+ wind_from_hdg = fgGetNode("/environment/wind-from-heading-deg", true);
+ wind_speed_knots = fgGetNode("/environment/wind-speed-kt", true);
+
+ // TODO - get the actual airport elevation
+ aptElev = 0.0;
+}
+
+FGGround::FGGround(string id) {
+ ATCmgr = globals->get_ATC_mgr();
+ display = false;
+ networkLoadOK = false;
+ ground_traffic.erase(ground_traffic.begin(), ground_traffic.end());
+ ground_traffic_itr = ground_traffic.begin();
+ ident = id;
+
+ // Init the property nodes - TODO - need to make sure we're getting surface winds.
+ wind_from_hdg = fgGetNode("/environment/wind-from-heading-deg", true);
+ wind_speed_knots = fgGetNode("/environment/wind-speed-kt", true);
+
+ // TODO - get the actual airport elevation
+ aptElev = 0.0;
}
FGGround::~FGGround() {
string taxiPath = "ATC/KEMT.taxi"; // FIXME - HARDWIRED FOR TESTING
path.append(taxiPath);
- SG_LOG(SG_GENERAL, SG_INFO, "Trying to read taxiway data for " << ident << "...");
+ SG_LOG(SG_ATC, SG_INFO, "Trying to read taxiway data for " << ident << "...");
//cout << "Trying to read taxiway data for " << ident << "..." << endl;
fin.open(path.c_str(), ios::in);
if(!fin) {
- SG_LOG(SG_GENERAL, SG_ALERT, "Unable to open taxiway data input file " << path.c_str());
+ SG_LOG(SG_ATC, SG_ALERT, "Unable to open taxiway data input file " << path.c_str());
//cout << "Unable to open taxiway data input file " << path.c_str() << endl;
return(false);
}
// Node, arc, or [End]?
//cout << "Read in ground network element type = " << buf << endl;
if(!strcmp(buf, "[End]")) { // TODO - maybe make this more robust to spelling errors by just looking for '['
- SG_LOG(SG_GENERAL, SG_INFO, "Done reading " << path.c_str() << endl);
+ SG_LOG(SG_ATC, SG_INFO, "Done reading " << path.c_str() << endl);
break;
} else if(!strcmp(buf, "N")) {
// Node
} else if(!strcmp(buf, "H")) {
np->type = HOLD;
} else {
- cout << "**** ERROR ***** Unknown node type in taxi network...\n";
+ SG_LOG(SG_ATC, SG_ALERT, "**** ERROR ***** Unknown node type in taxi network...\n");
delete np;
return(false);
}
fin >> buf; // rwy exit information - gets parsed later - FRAGILE - will break if buf is reused.
// Now the name
+ fin >> ch; // strip the leading " off
np->name = "";
while(1) {
fin.unsetf(ios::skipws);
fin >> ch;
- np->name += ch;
if((ch == '"') || (ch == 0x0A)) {
break;
} // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
+ np->name += ch;
}
fin.setf(ios::skipws);
network.push_back(np);
// FIXME - fragile - replies on buf not getting modified from exits read to here
// see if we also need to push it onto the runway exit list
- cout << "strlen(buf) = " << strlen(buf) << endl;
+ //cout << "strlen(buf) = " << strlen(buf) << endl;
if(strlen(buf) > 2) {
- cout << "Calling ParseRwyExits for " << buf << endl;
+ //cout << "Calling ParseRwyExits for " << buf << endl;
ParseRwyExits(np, buf);
}
} else if(!strcmp(buf, "A")) {
} else if(!strcmp(buf, "T")) {
ap->type = TAXIWAY;
} else {
- cout << "**** ERROR ***** Unknown arc type in taxi network...\n";
+ SG_LOG(SG_ATC, SG_ALERT, "**** ERROR ***** Unknown arc type in taxi network...\n");
delete ap;
return(false);
}
} else if(!strcmp(buf, "N")) {
ap->directed = false;
} else {
- cout << "**** ERROR ***** Unknown arc directed value in taxi network - should be Y/N !!!\n";
+ SG_LOG(SG_ATC, SG_ALERT, "**** ERROR ***** Unknown arc directed value in taxi network - should be Y/N !!!\n");
delete ap;
return(false);
}
gateCount++;
} else {
// Something has gone seriously pear-shaped
- cout << "********* ERROR - unknown ground network element type... aborting read of " << path.c_str() << '\n';
+ SG_LOG(SG_ATC, SG_ALERT, "********* ERROR - unknown ground network element type... aborting read of " << path.c_str() << '\n');
return(false);
}
void FGGround::Init() {
display = false;
+ untowered = false;
- // For now we'll hardwire the threshold end
- Point3D P010(-118.037483, 34.081358, 296 * SG_FEET_TO_METER);
- double hdg = 25.32;
- ortho.Init(P010, hdg);
-
+ // Figure out which is the active runway - TODO - it would be better to have ground call tower
+ // for runway operation details, but at the moment we can't guarantee that tower control at a given airport
+ // will be initialised before ground so we can't do that.
+ DoRwyDetails();
+ //cout << "In FGGround::Init, active rwy is " << activeRwy << '\n';
+ ortho.Init(rwy.threshold_pos, rwy.hdg);
+
networkLoadOK = LoadNetwork();
}
-void FGGround::Update() {
+void FGGround::Update(double dt) {
// 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.
// Lets take the example of a plane which has just contacted ground
// following landing - presumably requesting where to go?
// First we need to establish the position of the plane within the logical network.
- // Next we need to decide where its going.
+ // Next we need to decide where its going.
+
+ if(ground_traffic.size()) {
+ if(ground_traffic_itr == ground_traffic.end()) {
+ ground_traffic_itr = ground_traffic.begin();
+ }
+
+ //Process(*ground_traffic_itr);
+ GroundRec* g = *ground_traffic_itr;
+ if(g->taxiRequestOutstanding) {
+ double responseTime = 10.0; // seconds - this should get more sophisticated at some point
+ if(g->clearanceCounter > responseTime) {
+ // DO CLEARANCE
+ // TODO - move the mechanics of making up the transmission out of the main Update(...) routine.
+ string trns = "";
+ trns += g->plane.callsign;
+ trns += " taxi holding point runway "; // TODO - add the holding point name
+ // eg " taxi holding point G2 runway "
+ trns += ConvertRwyNumToSpokenString(activeRwy);
+ if(display) {
+ globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
+ }
+ g->planePtr->RegisterTransmission(1); // cleared to taxi
+ g->clearanceCounter = 0.0;
+ g->taxiRequestOutstanding = false;
+ } else {
+ g->clearanceCounter += (dt * ground_traffic.size());
+ }
+ } else if(((FGAILocalTraffic*)(g->planePtr))->AtHoldShort()) { // That's a hack - eventually we should monitor actual position
+ // HACK ALERT - the automatic cast to AILocalTraffic has to go once we have other sorts working!!!!! FIXME TODO
+ // NOTE - we don't need to do the contact tower bit unless we have separate tower and ground
+ string trns = g->plane.callsign;
+ trns += " contact Tower ";
+ double f = globals->get_ATC_mgr()->GetFrequency(ident, TOWER);
+ char buf[10];
+ sprintf(buf, "%f", f);
+ trns += buf;
+ if(display) {
+ globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
+ }
+ g->planePtr->RegisterTransmission(2); // contact tower
+ delete *ground_traffic_itr;
+ ground_traffic.erase(ground_traffic_itr);
+ ground_traffic_itr = ground_traffic.begin();
+ }
+ ++ground_traffic_itr;
+ }
+}
+
+// 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
+// Copied from FGTower - TODO - it would be better to implement this just once, and have ground call tower
+// for runway operation details, but at the moment we can't guarantee that tower control at a given airport
+// will be initialised before ground so we can't do that.
+void FGGround::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;
+ bool rwyGood = globals->get_runways()->search(ident, int(hdg), &runway);
+ if(rwyGood) {
+ activeRwy = runway.rwy_no;
+ rwy.rwyID = runway.rwy_no;
+ SG_LOG(SG_ATC, SG_INFO, "In FGGround, 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;
+ 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";
+ }
}
// Return a random gate ID of an unused gate.
}
}
+
+// WARNING - This is hardwired to my prototype logical network format
+// and will almost certainly change when Bernie's stuff comes on-line.
+// Returns NULL if it can't find a valid node.
+node* FGGround::GetThresholdNode(string rwyID) {
+ // For now go through all the nodes and parse their names
+ // Maybe in the future we'll map threshold nodes by ID
+ //cout << "Size of network is " << network.size() << '\n';
+ for(unsigned int i=0; i<network.size(); ++i) {
+ //cout << "Name = " << network[i]->name << '\n';
+ if(network[i]->name.size()) {
+ string s = network[i]->name;
+ // Warning - the next bit is fragile and dependent on my current naming scheme
+ //cout << "substr = " << s.substr(0,3) << '\n';
+ //cout << "size of s = " << s.size() << '\n';
+ if(s.substr(0,3) == "rwy") {
+ //cout << "subsubstr = " << s.substr(4, s.size() - 4) << '\n';
+ if(s.substr(4, s.size() - 4) == rwyID) {
+ return network[i];
+ }
+ }
+ }
+ }
+ return NULL;
+}
+
+
// Get a path from a point on a runway to a gate
// TODO !!
return(GetShortestPath(A, B));
};
+// Get a path from a node to a runway threshold
+ground_network_path_type FGGround::GetPath(node* A, string rwyID) {
+ node* b = GetThresholdNode(rwyID);
+ if(b == NULL) {
+ SG_LOG(SG_ATC, SG_ALERT, "ERROR - unable to find path to runway theshold in ground.cxx\n");
+ ground_network_path_type emptyPath;
+ emptyPath.erase(emptyPath.begin(), emptyPath.end());
+ return(emptyPath);
+ }
+ return GetShortestPath(A, b);
+}
+
+// Get a path from a node to a runway hold short point
+// Bit of a hack this at the moment!
+ground_network_path_type FGGround::GetPathToHoldShort(node* A, string rwyID) {
+ ground_network_path_type path = GetPath(A, rwyID);
+ path.pop_back(); // That should be the threshold stripped of
+ path.pop_back(); // and that should be the arc from hold short to threshold
+ // This isn't robust though - TODO - implement properly!
+ return(path);
+}
// A shortest path algorithm from memory (ie. I can't find the bl&*dy book again!)
// I'm sure there must be enchancements that we can make to this, such as biasing the
//cout << "pathsCreated = " << pathsCreated << '\n';
if(pathsCreated > 0) {
- SG_LOG(SG_GENERAL, SG_ALERT, "WARNING - Possible memory leak in FGGround::GetShortestPath\n\
+ SG_LOG(SG_ATC, SG_ALERT, "WARNING - Possible memory leak in FGGround::GetShortestPath\n\
Please report to flightgear-devel@flightgear.org\n");
}
// Return a list of exits from a given runway
// It is up to the calling function to check for non-zero size of returned array before use
-node_array_type FGGround::GetExits(int rwyID) {
- return(runways[rwyID].exits);
+node_array_type FGGround::GetExits(string rwyID) {
+ // FIXME - get a 07L or similar in here and we're stuffed!!!
+ return(runways[atoi(rwyID.c_str())].exits);
+}
+
+void FGGround::RequestDeparture(PlaneRec plane, FGAIEntity* requestee) {
+ // For now we'll just automatically clear all planes to the runway hold.
+ // This communication needs to be delayed 20 sec or so from receiving the request.
+ // Even if display=false we still need to start the timer in case display=true when communication starts.
+ // We also need to bear in mind we also might have other outstanding communications, although for now we'll punt that issue!
+ // FIXME - sort the above!
+
+ // HACK - assume that anything requesting departure is new for now - FIXME LATER
+ GroundRec* g = new GroundRec;
+ g->plane = plane;
+ g->planePtr = requestee;
+ g->taxiRequestOutstanding = true;
+ g->clearanceCounter = 0;
+ g->cleared = false;
+ g->incoming = false;
+ // TODO - need to handle the next 3 as well
+ //Point3D current_pos;
+ //node* destination;
+ //node* last_clearance;
+
+ ground_traffic.push_back(g);
}
#if 0