X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FAIModel%2FAIAircraft.cxx;h=a153db7f7dc8fd6dcc13ac303207afb84415b201;hb=5228053f7f39eba5ecd0de57ddeafd96097205f8;hp=2bf71b3b18b96b35a66462bce9f0fe2c9e282e97;hpb=8f056da61884b14e37ec92e52c4e5823a50d30e5;p=flightgear.git diff --git a/src/AIModel/AIAircraft.cxx b/src/AIModel/AIAircraft.cxx index 2bf71b3b1..a153db7f7 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,11 +52,15 @@ using std::string; static string tempReg; -FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) { +FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : + /* HOT must be disabled for AI Aircraft, + * otherwise traffic detection isn't working as expected.*/ + FGAIBase(otAircraft, false) +{ trafficRef = ref; if (trafficRef) { groundOffset = trafficRef->getGroundOffset(); - setCallSign(trafficRef->getCallSign()); + setCallSign(trafficRef->getCallSign()); } else groundOffset = 0; @@ -78,8 +83,12 @@ FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) { roll = 0; headingChangeRate = 0.0; headingError = 0; + minBearing = 360; + speedFraction =1.0; holdPos = false; + needsTaxiClearance = false; + _needsGroundElevation = true; _performance = 0; //TODO initialize to JET_TRANSPORT from PerformanceDB dt = 0; @@ -160,7 +169,12 @@ void FGAIAircraft::setPerformance(const std::string& acclass) { handleATCRequests(); // ATC also has a word to say updateSecondaryTargetValues(); // target roll, vertical speed, pitch updateActualState(); - UpdateRadar(manager); + // We currently have one situation in which an AIAircraft object is used that is not attached to the + // AI manager. In this particular case, the AIAircraft is used to shadow the user's aircraft's behavior in the AI world. + // Since we perhaps don't want a radar entry of our own aircraft, the following conditional should probably be adequate + // enough + if (manager) + UpdateRadar(manager); checkVisibility(); } @@ -175,6 +189,8 @@ void FGAIAircraft::checkVisibility() void FGAIAircraft::AccelTo(double speed) { tgt_speed = speed; + if (!isStationary()) + _needsGroundElevation = true; } @@ -256,9 +272,32 @@ 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); + if (speed < 0) { + cerr << getCallSign() + << ": verifying lead distance to waypoint : " + << fp->getCurrentWaypoint()->name << " " + << fp->getLeadDistance() << ". Distance to go " + << (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)) + << ". Target speed = " + << tgt_speed + << ". Current speed = " + << speed + << ". Minimum Bearing " << minBearing + << endl; + } } else { if (curr->finished) //end of the flight plan { @@ -277,7 +316,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; @@ -315,17 +354,44 @@ void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) { use_perf_vs = false; tgt_vs = (curr->crossat - altitude_ft) / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr) / 6076.0 / speed*60.0); + checkTcas(); tgt_altitude_ft = curr->crossat; } else { use_perf_vs = true; } } - tgt_speed = prev->speed; + AccelTo(prev->speed); hdg_lock = alt_lock = true; no_roll = prev->on_ground; } } +void FGAIAircraft::checkTcas(void) +{ + if (props->getIntValue("tcas/threat-level",0)==3) + { + int RASense = props->getIntValue("tcas/ra-sense",0); + if ((RASense>0)&&(tgt_vs<4000)) + // upward RA: climb! + tgt_vs = 4000; + else + if (RASense<0) + { + // downward RA: descend! + if (altitude_ft < 1000) + { + // too low: level off + if (tgt_vs>0) + tgt_vs = 0; + } + else + { + if (tgt_vs >- 4000) + tgt_vs = -4000; + } + } + } +} void FGAIAircraft::initializeFlightPlan() { } @@ -341,7 +407,7 @@ const char * FGAIAircraft::_getTransponderCode() const { } -bool FGAIAircraft::loadNextLeg() { +bool FGAIAircraft::loadNextLeg(double distance) { int leg; if ((leg = fp->getLeg()) == 10) { @@ -373,7 +439,8 @@ bool FGAIAircraft::loadNextLeg() { trafficRef->getRadius(), trafficRef->getFlightType(), acType, - company); + company, + distance); //cerr << "created leg " << leg << " for " << trafficRef->getCallSign() << endl; } return true; @@ -388,6 +455,8 @@ bool FGAIAircraft::loadNextLeg() { void FGAIAircraft::getGroundElev(double dt) { dt_elev_count += dt; + if (!needGroundElevation()) + return; // Update minimally every three secs, but add some randomness // to prevent all AI objects doing this in synchrony if (dt_elev_count < (3.0) + (rand() % 10)) @@ -405,23 +474,30 @@ 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 ); + if (globals->get_tile_mgr()->schedule_scenery(pos, range, 5.0)) + { + double alt; + if (getGroundElevationM(SGGeod::fromGeodM(pos, 20000), alt, 0)) + { + tgt_altitude_ft = alt * SG_METER_TO_FEET; + if (isStationary()) + { + // aircraft is stationary and we obtained altitude for this spot - we're done. + _needsGroundElevation = false; + } + } } - - double alt; - if (getGroundElevationM(SGGeod::fromGeodM(pos, 20000), alt, 0)) - tgt_altitude_ft = alt * SG_METER_TO_FEET; } } void FGAIAircraft::doGroundAltitude() { - if (fabs(altitude_ft - (tgt_altitude_ft+groundOffset)) > 1000.0) + if ((fabs(altitude_ft - (tgt_altitude_ft+groundOffset)) > 1000.0)|| + (isStationary())) altitude_ft = (tgt_altitude_ft + groundOffset); else altitude_ft += 0.1 * ((tgt_altitude_ft+groundOffset) - altitude_ft); + tgt_vs = 0; } @@ -448,6 +524,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(); @@ -571,6 +652,7 @@ void FGAIAircraft::handleFirstWaypoint() { tgt_vs = (curr->crossat - prev->altitude) / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr) / 6076.0 / prev->speed*60.0); + checkTcas(); tgt_altitude_ft = curr->crossat; } else { use_perf_vs = true; @@ -582,6 +664,7 @@ void FGAIAircraft::handleFirstWaypoint() { Transform(); // make sure aip is initialized. getGroundElev(60.1); // make sure it's executed first time around, so force a large dt value doGroundAltitude(); + _needsGroundElevation = true; // check ground elevation again (maybe scenery wasn't available yet) } // Make sure to announce the aircraft's position announcePositionToController(); @@ -615,7 +698,15 @@ bool FGAIAircraft::leadPointReached(FGAIFlightPlan::waypoint* curr) { //cerr << "2" << endl; double lead_dist = fp->getLeadDistance(); // experimental: Use fabs, because speed can be negative (I hope) during push_back. - + if ((dist_to_go < fabs(10.0* speed)) && (speed < 0) && (tgt_speed < 0) && fp->getCurrentWaypoint()->name == string("PushBackPoint")) { + tgt_speed = -(dist_to_go / 10.0); + if (tgt_speed > -0.5) { + tgt_speed = -0.5; + } + if (fp->getPreviousWaypoint()->speed < tgt_speed) { + fp->getPreviousWaypoint()->speed = tgt_speed; + } + } if (lead_dist < fabs(2*speed)) { //don't skip over the waypoint lead_dist = fabs(2*speed); @@ -628,8 +719,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; + } } @@ -663,9 +780,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 @@ -850,8 +966,8 @@ void FGAIAircraft::updateHeading() { bank_sense = 1.0; } //if (trafficRef) - //cerr << trafficRef->getCallSign() << " Heading " - // << hdg << ". Target " << tgt_heading << ". Diff " << fabs(sum - tgt_heading) << ". Speed " << speed << endl; + // cerr << trafficRef->getCallSign() << " Heading " + // << hdg << ". Target " << tgt_heading << ". Diff " << fabs(sum - tgt_heading) << ". Speed " << speed << endl; //if (headingDiff > 60) { groundTargetSpeed = tgt_speed; // * cos(headingDiff * SG_DEGREES_TO_RADIANS); //groundTargetSpeed = tgt_speed - tgt_speed * (headingDiff/180); @@ -860,27 +976,34 @@ void FGAIAircraft::updateHeading() { //} if (sign(groundTargetSpeed) != sign(tgt_speed)) groundTargetSpeed = 0.21 * sign(tgt_speed); // to prevent speed getting stuck in 'negative' mode - - if (headingDiff > 30.0) { - // invert if pushed backward - headingChangeRate += 10.0 * dt * sign(roll); - - // Clamp the maximum steering rate to 30 degrees per second, - // But only do this when the heading error is decreasing. - if ((headingDiff < headingError)) { - if (headingChangeRate > 30) - headingChangeRate = 30; - else if (headingChangeRate < -30) - headingChangeRate = -30; + + // Only update the target values when we're not moving because otherwise we might introduce an enormous target change rate while waiting a the gate, or holding. + if (speed != 0) { + if (headingDiff > 30.0) { + // invert if pushed backward + headingChangeRate += 10.0 * dt * sign(roll); + + // Clamp the maximum steering rate to 30 degrees per second, + // But only do this when the heading error is decreasing. + if ((headingDiff < headingError)) { + if (headingChangeRate > 30) + headingChangeRate = 30; + else if (headingChangeRate < -30) + headingChangeRate = -30; + } + } else { + if (speed != 0) { + if (fabs(headingChangeRate) > headingDiff) + headingChangeRate = headingDiff*sign(roll); + else + headingChangeRate += dt * sign(roll); + } } - } else { - if (fabs(headingChangeRate) > headingDiff) - headingChangeRate = headingDiff*sign(roll); - else - headingChangeRate += dt * sign(roll); } - - hdg += headingChangeRate * dt * (fabs(speed) / 15); + if (trafficRef) + //cerr << trafficRef->getCallSign() << " Heading " + // << hdg << ". Target " << tgt_heading << ". Diff " << fabs(sum - tgt_heading) << ". Speed " << speed << "Heading change rate : " << headingChangeRate << " bacnk sence " << bank_sense << endl; + hdg += headingChangeRate * dt * sqrt(fabs(speed) / 15); headingError = headingDiff; } else { if (fabs(speed) > 1.0) { @@ -967,6 +1090,7 @@ void FGAIAircraft::updateVerticalSpeedTarget() { } } //else // tgt_vs = 0.0; + checkTcas(); } void FGAIAircraft::updatePitchAngleTarget() { @@ -1001,12 +1125,12 @@ string FGAIAircraft::atGate() { void FGAIAircraft::handleATCRequests() { //TODO implement NullController for having no ATC to save the conditionals if (controller) { - controller->update(getID(), - pos.getLatitudeDeg(), - pos.getLongitudeDeg(), - hdg, - speed, - altitude_ft, dt); + controller->updateAircraftInformation(getID(), + pos.getLatitudeDeg(), + pos.getLongitudeDeg(), + hdg, + speed, + altitude_ft, dt); processATC(controller->getInstruction(getID())); } } @@ -1019,7 +1143,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); @@ -1040,3 +1164,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... +}