X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FAIModel%2FAIAircraft.cxx;h=a525d7ef1bd427449b8562c9ac25b10fbdd07a53;hb=9cd3adb3a5487bbb51d42770d1df6112717f3f0d;hp=eb8469fdece8dd419a721b5c7262caf3ddc9a6af;hpb=fa7d9ed549a747deb36cfed0c6128840e47f5963;p=flightgear.git diff --git a/src/AIModel/AIAircraft.cxx b/src/AIModel/AIAircraft.cxx index eb8469fde..a525d7ef1 100644 --- a/src/AIModel/AIAircraft.cxx +++ b/src/AIModel/AIAircraft.cxx @@ -1,4 +1,4 @@ -// // // FGAIAircraft - FGAIBase-derived class creates an AI airplane +// FGAIAircraft - FGAIBase-derived class creates an AI airplane // // Written by David Culp, started October 2003. // @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -51,9 +52,6 @@ using std::string; static string tempReg; -class AI_OutOfSight{}; -class FP_Inactive{}; - FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) { trafficRef = ref; if (trafficRef) { @@ -81,8 +79,11 @@ FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) { roll = 0; headingChangeRate = 0.0; headingError = 0; + minBearing = 360; + speedFraction =1.0; holdPos = false; + needsTaxiClearance = false; _performance = 0; //TODO initialize to JET_TRANSPORT from PerformanceDB dt = 0; @@ -148,16 +149,16 @@ void FGAIAircraft::setPerformance(const std::string& acclass) { void FGAIAircraft::Run(double dt) { FGAIAircraft::dt = dt; - - try { - updatePrimaryTargetValues(); // target hdg, alt, speed - } - catch (AI_OutOfSight) { - return; + + bool outOfSight = false, + flightplanActive = true; + updatePrimaryTargetValues(flightplanActive, outOfSight); // target hdg, alt, speed + if (outOfSight) { + return; } - catch (FP_Inactive) { - //return; - groundTargetSpeed = 0; + + if (!flightplanActive) { + groundTargetSpeed = 0; } handleATCRequests(); // ATC also has a word to say @@ -259,6 +260,16 @@ void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) { return; dt_count = 0; + double distanceToDescent; + if(reachedEndOfCruise(distanceToDescent)) { + if (!loadNextLeg(distanceToDescent)) { + setDie(true); + return; + } + prev = fp->getPreviousWaypoint(); + curr = fp->getCurrentWaypoint(); + next = fp->getNextWaypoint(); + } if (! leadPointReached(curr)) { controlHeading(curr); controlSpeed(curr, next); @@ -280,7 +291,7 @@ void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) { //TODO let the fp handle this (loading of next leg) fp->IncrementWaypoint( trafficRef != 0 ); - if (!(fp->getNextWaypoint()) && trafficRef != 0) + if ( ((!(fp->getNextWaypoint()))) && (trafficRef != 0) ) if (!loadNextLeg()) { setDie(true); return; @@ -344,7 +355,7 @@ const char * FGAIAircraft::_getTransponderCode() const { } -bool FGAIAircraft::loadNextLeg() { +bool FGAIAircraft::loadNextLeg(double distance) { int leg; if ((leg = fp->getLeg()) == 10) { @@ -376,7 +387,8 @@ bool FGAIAircraft::loadNextLeg() { trafficRef->getRadius(), trafficRef->getFlightType(), acType, - company); + company, + distance); //cerr << "created leg " << leg << " for " << trafficRef->getCallSign() << endl; } return true; @@ -410,7 +422,7 @@ void FGAIAircraft::getGroundElev(double dt) { double range = 500.0; if (!globals->get_tile_mgr()->scenery_available(pos, range)) { // Try to shedule tiles for that position. - globals->get_tile_mgr()->update( pos, range ); + globals->get_tile_mgr()->schedule_tiles_at( pos, range ); } double alt; @@ -425,6 +437,7 @@ void FGAIAircraft::doGroundAltitude() { altitude_ft = (tgt_altitude_ft + groundOffset); else altitude_ft += 0.1 * ((tgt_altitude_ft+groundOffset) - altitude_ft); + tgt_vs = 0; } @@ -451,6 +464,11 @@ void FGAIAircraft::announcePositionToController() { cerr << "Error: Could not find Dynamics at airport : " << trafficRef->getDepartureAirport()->getId() << endl; } break; + case 7: + if (trafficRef->getDepartureAirport()->getDynamics()) { + controller = trafficRef->getArrivalAirport()->getDynamics()->getApproachController(); + } + break; case 9: // Taxiing for parking if (trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork()->exists()) controller = trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork(); @@ -631,8 +649,34 @@ bool FGAIAircraft::leadPointReached(FGAIFlightPlan::waypoint* curr) { // << dist_to_go << ": Lead distance " // << lead_dist << " " << curr->name // << " Ground target speed " << groundTargetSpeed << endl; - - return dist_to_go < lead_dist; + double bearing = 0; + if (speed > 50) { // don't do bearing calculations for ground traffic + bearing = getBearing(fp->getBearing(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)); + if (bearing < minBearing) { + minBearing = bearing; + if (minBearing < 10) { + minBearing = 10; + } + if ((minBearing < 360.0) && (minBearing > 10.0)) { + speedFraction = cos(minBearing *SG_DEGREES_TO_RADIANS); + } else { + speedFraction = 1.0; + } + } + } + if (trafficRef) { + //cerr << "Tracking callsign : \"" << fgGetString("/ai/track-callsign") << "\"" << endl; +/* if (trafficRef->getCallSign() == fgGetString("/ai/track-callsign")) { + cerr << trafficRef->getCallSign() << " " << tgt_altitude_ft << " " << _getSpeed() << " " + << _getAltitude() << " "<< _getLatitude() << " " << _getLongitude() << " " << dist_to_go << " " << lead_dist << " " << curr->name << " " << vs << " " << tgt_vs << " " << bearing << " " << minBearing << " " << speedFraction << endl; + }*/ + } + if ((dist_to_go < lead_dist) || (bearing > (minBearing * 1.1))) { + minBearing = 360; + return true; + } else { + return false; + } } @@ -666,9 +710,8 @@ bool FGAIAircraft::handleAirportEndPoints(FGAIFlightPlan::waypoint* prev, time_t //cerr << trafficRef->getCallSign() << " has passed waypoint " << prev->name << " at speed " << speed << endl; if (prev->name == "PushBackPoint") { dep->getDynamics()->releaseParking(fp->getGate()); - time_t holdUntil = now + 120; - fp->setTime(holdUntil); - //cerr << _getCallsign() << "Holding at pushback point" << endl; + AccelTo(0.0); + setTaxiClearanceRequest(true); } // This is the last taxi waypoint, and marks the the end of the flight plan @@ -739,7 +782,7 @@ void FGAIAircraft::controlSpeed(FGAIFlightPlan::waypoint* curr, FGAIFlightPlan:: /** * Update target values (heading, alt, speed) depending on flight plan or control properties */ -void FGAIAircraft::updatePrimaryTargetValues() { +void FGAIAircraft::updatePrimaryTargetValues(bool& flightplanActive, bool& aiOutOfSight) { if (fp) // AI object has a flightplan { //TODO make this a function of AIBase @@ -760,16 +803,16 @@ void FGAIAircraft::updatePrimaryTargetValues() { } if (trafficRef) { //cerr << trafficRef->getRegistration() << " Setting altitude to " << altitude_ft; - if (! aiTrafficVisible()) { + aiOutOfSight = !aiTrafficVisible(); + if (aiOutOfSight) { setDie(true); //cerr << trafficRef->getRegistration() << " is set to die " << endl; - throw AI_OutOfSight(); + aiOutOfSight = true; + return; } } timeElapsed = now - fp->getStartTime(); - if (! fp->isActive(now)) { - throw FP_Inactive(); - } + flightplanActive = fp->isActive(now); } else { // no flight plan, update target heading, speed, and altitude // from control properties. These default to the initial @@ -1022,7 +1065,7 @@ void FGAIAircraft::updateActualState() { if (onGround()) speed = _performance->actualSpeed(this, groundTargetSpeed, dt); else - speed = _performance->actualSpeed(this, tgt_speed, dt); + speed = _performance->actualSpeed(this, (tgt_speed *speedFraction), dt); updateHeading(); roll = _performance->actualBankAngle(this, tgt_roll, dt); @@ -1043,3 +1086,89 @@ void FGAIAircraft::updateSecondaryTargetValues() { //TODO calculate wind correction angle (tgt_yaw) } + + +bool FGAIAircraft::reachedEndOfCruise(double &distance) { + FGAIFlightPlan::waypoint* curr = fp->getCurrentWaypoint(); + if (curr->name == "BOD") { + double dist = fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr); + double descentSpeed = (getPerformance()->vDescent() * SG_NM_TO_METER) / 3600.0; // convert from kts to meter/s + double descentRate = (getPerformance()->descentRate() * SG_FEET_TO_METER) / 60.0; // convert from feet/min to meter/s + + double verticalDistance = ((altitude_ft - 2000.0) - trafficRef->getArrivalAirport()->getElevation()) *SG_FEET_TO_METER; + double descentTimeNeeded = verticalDistance / descentRate; + double distanceCovered = descentSpeed * descentTimeNeeded; + + //cerr << "Tracking : " << fgGetString("/ai/track-callsign"); + if (trafficRef->getCallSign() == fgGetString("/ai/track-callsign")) { + cerr << "Checking for end of cruise stage for :" << trafficRef->getCallSign() << endl; + cerr << "Descent rate : " << descentRate << endl; + cerr << "Descent speed : " << descentSpeed << endl; + cerr << "VerticalDistance : " << verticalDistance << ". Altitude : " << altitude_ft << ". Elevation " << trafficRef->getArrivalAirport()->getElevation() << endl; + cerr << "DecentTimeNeeded : " << descentTimeNeeded << endl; + cerr << "DistanceCovered : " << distanceCovered << endl; + } + //cerr << "Distance = " << distance << endl; + distance = distanceCovered; + if (dist < distanceCovered) { + if (trafficRef->getCallSign() == fgGetString("/ai/track-callsign")) { + //exit(1); + } + return true; + } else { + return false; + } + } else { + return false; + } +} + +void FGAIAircraft::resetPositionFromFlightPlan() +{ + // the one behind you + FGAIFlightPlan::waypoint* prev = 0; + // the one ahead + FGAIFlightPlan::waypoint* curr = 0; + // the next plus 1 + FGAIFlightPlan::waypoint* next = 0; + + prev = fp->getPreviousWaypoint(); + curr = fp->getCurrentWaypoint(); + next = fp->getNextWaypoint(); + + setLatitude(prev->latitude); + setLongitude(prev->longitude); + double tgt_heading = fp->getBearing(curr, next); + setHeading(tgt_heading); + setAltitude(prev->altitude); + setSpeed(prev->speed); +} + +double FGAIAircraft::getBearing(double crse) +{ + double hdgDiff = fabs(hdg-crse); + if (hdgDiff > 180) + hdgDiff = fabs(hdgDiff - 360); + return hdgDiff; +} + +time_t FGAIAircraft::checkForArrivalTime(string wptName) { + FGAIFlightPlan::waypoint* curr = 0; + curr = fp->getCurrentWaypoint(); + + double tracklength = fp->checkTrackLength(wptName); + if (tracklength > 0.1) { + tracklength += fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr); + } else { + return 0; + } + time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + time_t arrivalTime = fp->getArrivalTime(); + + time_t ete = tracklength / ((speed * SG_NM_TO_METER) / 3600.0); + time_t secondsToGo = arrivalTime - now; + if (trafficRef->getCallSign() == fgGetString("/ai/track-callsign")) { + cerr << "Checking arrival time: ete " << ete << ". Time to go : " << secondsToGo << ". Track length = " << tracklength << endl; + } + return (ete - secondsToGo); // Positive when we're too slow... +} \ No newline at end of file