1 // // // FGAIAircraft - FGAIBase-derived class creates an AI airplane
3 // Written by David Culp, started October 2003.
5 // Copyright (C) 2003 David P. Culp - davidculp2@comcast.net
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.
25 #include <simgear/math/point3d.hxx>
26 #include <simgear/route/waypoint.hxx>
27 #include <Main/fg_props.hxx>
28 #include <Main/globals.hxx>
29 #include <Main/viewer.hxx>
30 #include <Scenery/scenery.hxx>
31 #include <Scenery/tilemgr.hxx>
38 # define finite _finite
39 #elif defined(__sun) || defined(sgi)
45 #include "AIAircraft.hxx"
46 #include "performancedata.hxx"
47 #include "performancedb.hxx"
49 //#include <Airports/trafficcontroller.hxx>
51 static string tempReg;
53 class AI_OutOfSight{};
56 FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) {
59 groundOffset = trafficRef->getGroundOffset();
60 setCallSign(trafficRef->getCallSign());
75 groundTargetSpeed = 0;
77 // set heading and altitude locks
81 headingChangeRate = 0.0;
85 _performance = 0; //TODO initialize to JET_TRANSPORT from PerformanceDB
90 FGAIAircraft::~FGAIAircraft() {
93 controller->signOff(getID());
97 void FGAIAircraft::readFromScenario(SGPropertyNode* scFileNode) {
101 FGAIBase::readFromScenario(scFileNode);
103 setPerformance(scFileNode->getStringValue("class", "jet_transport"));
104 setFlightPlan(scFileNode->getStringValue("flightplan"),
105 scFileNode->getBoolValue("repeat", false));
106 setCallSign(scFileNode->getStringValue("callsign"));
110 void FGAIAircraft::bind() {
113 props->tie("controls/gear/gear-down",
114 SGRawValueMethods<FGAIAircraft,bool>(*this,
115 &FGAIAircraft::_getGearDown));
119 void FGAIAircraft::unbind() {
122 props->untie("controls/gear/gear-down");
126 void FGAIAircraft::update(double dt) {
127 FGAIBase::update(dt);
132 void FGAIAircraft::setPerformance(const std::string& acclass) {
133 static PerformanceDB perfdb; //TODO make it a global service
134 setPerformance(perfdb.getDataFor(acclass));
138 void FGAIAircraft::setPerformance(PerformanceData *ps) {
143 void FGAIAircraft::Run(double dt) {
144 FGAIAircraft::dt = dt;
147 updatePrimaryTargetValues(); // target hdg, alt, speed
149 catch (AI_OutOfSight) {
152 catch (FP_Inactive) {
156 handleATCRequests(); // ATC also has a word to say
157 updateSecondaryTargetValues(); // target roll, vertical speed, pitch
159 UpdateRadar(manager);
164 void FGAIAircraft::AccelTo(double speed) {
169 void FGAIAircraft::PitchTo(double angle) {
175 void FGAIAircraft::RollTo(double angle) {
181 void FGAIAircraft::YawTo(double angle) {
186 void FGAIAircraft::ClimbTo(double alt_ft ) {
187 tgt_altitude_ft = alt_ft;
192 void FGAIAircraft::TurnTo(double heading) {
193 tgt_heading = heading;
198 double FGAIAircraft::sign(double x) {
206 void FGAIAircraft::setFlightPlan(const std::string& flightplan, bool repeat) {
207 if (!flightplan.empty()) {
208 FGAIFlightPlan* fp = new FGAIFlightPlan(flightplan);
209 fp->setRepeat(repeat);
215 void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f) {
221 void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) {
223 // the one behind you
224 FGAIFlightPlan::waypoint* prev = 0;
226 FGAIFlightPlan::waypoint* curr = 0;
228 FGAIFlightPlan::waypoint* next = 0;
230 prev = fp->getPreviousWaypoint();
231 curr = fp->getCurrentWaypoint();
232 next = fp->getNextWaypoint();
236 ///////////////////////////////////////////////////////////////////////////
237 // Initialize the flightplan
238 //////////////////////////////////////////////////////////////////////////
240 handleFirstWaypoint();
242 } // end of initialization
243 if (! fpExecutable(now))
247 if (! leadPointReached(curr)) {
248 controlHeading(curr);
249 controlSpeed(curr, next);
251 if (curr->finished) //end of the flight plan
261 //TODO more intelligent method in AIFlightPlan, no need to send data it already has :-)
262 tgt_heading = fp->getBearing(curr, next);
266 //TODO let the fp handle this (loading of next leg)
267 fp->IncrementWaypoint((bool) trafficRef);
268 if (!(fp->getNextWaypoint()) && trafficRef)
271 prev = fp->getPreviousWaypoint();
272 curr = fp->getCurrentWaypoint();
273 next = fp->getNextWaypoint();
275 // Now that we have incremented the waypoints, excute some traffic manager specific code
277 //TODO isn't this best executed right at the beginning?
278 if (! aiTrafficVisible()) {
283 if (! handleAirportEndPoints(prev, now)) {
288 announcePositionToController();
293 fp->setLeadDistance(tgt_speed, tgt_heading, curr, next);
296 if (!(prev->on_ground)) // only update the tgt altitude from flightplan if not on the ground
298 tgt_altitude_ft = prev->altitude;
299 if (curr->crossat > -1000.0) {
301 tgt_vs = (curr->crossat - altitude_ft) / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)
302 / 6076.0 / speed*60.0);
303 tgt_altitude_ft = curr->crossat;
308 tgt_speed = prev->speed;
309 hdg_lock = alt_lock = true;
310 no_roll = prev->on_ground;
315 void FGAIAircraft::initializeFlightPlan() {
319 bool FGAIAircraft::_getGearDown() const {
320 return _performance->gearExtensible(this);
324 void FGAIAircraft::loadNextLeg() {
327 if ((leg = fp->getLeg()) == 10) {
329 setCallSign(trafficRef->getCallSign());
330 //props->setStringValue("callsign", callsign.c_str());
335 FGAirport *dep = trafficRef->getDepartureAirport();
336 FGAirport *arr = trafficRef->getArrivalAirport();
341 double cruiseAlt = trafficRef->getCruiseAlt() * 100;
346 cruiseAlt, //(trafficRef->getCruiseAlt() * 100), // convert from FL to feet
347 trafficRef->getSpeed(),
351 trafficRef->getRadius(),
352 trafficRef->getFlightType(),
359 // Note: This code is copied from David Luff's AILocalTraffic
360 // Warning - ground elev determination is CPU intensive
361 // Either this function or the logic of how often it is called
362 // will almost certainly change.
364 void FGAIAircraft::getGroundElev(double dt) {
367 // Update minimally every three secs, but add some randomness
368 // to prevent all IA objects doing this in synchrony
369 if (dt_elev_count < (3.0) + (rand() % 10))
374 // Only do the proper hitlist stuff if we are within visible range of the viewer.
376 double visibility_meters = fgGetDouble("/environment/visibility-m");
378 FGViewer* vw = globals->get_current_view();
379 double course, distance;
381 SGWayPoint current(pos.getLongitudeDeg(), pos.getLatitudeDeg(), 0);
382 SGWayPoint view (vw->getLongitude_deg(), vw->getLatitude_deg(), 0);
383 view.CourseAndDistance(current, &course, &distance);
384 if (distance > visibility_meters) {
385 //aip.getSGLocation()->set_cur_elev_m(aptElev);
389 // FIXME: make sure the pos.lat/pos.lon values are in degrees ...
390 double range = 500.0;
391 if (!globals->get_tile_mgr()->scenery_available(pos.getLatitudeDeg(), pos.getLongitudeDeg(), range)) {
392 // Try to shedule tiles for that position.
393 globals->get_tile_mgr()->update( aip.getSGLocation(), range );
396 // FIXME: make sure the pos.lat/pos.lon values are in degrees ...
398 if (globals->get_scenery()->get_elevation_m(pos.getLatitudeDeg(), pos.getLongitudeDeg(), 20000.0, alt, 0))
399 tgt_altitude_ft = alt * SG_METER_TO_FEET;
404 void FGAIAircraft::doGroundAltitude() {
405 if (fabs(altitude_ft - (tgt_altitude_ft+groundOffset)) > 1000.0)
406 altitude_ft = (tgt_altitude_ft + groundOffset);
408 altitude_ft += 0.1 * ((tgt_altitude_ft+groundOffset) - altitude_ft);
412 void FGAIAircraft::announcePositionToController() {
414 int leg = fp->getLeg();
416 // Note that leg was been incremented after creating the current leg, so we should use
417 // leg numbers here that are one higher than the number that is used to create the leg
420 case 3: // Taxiing to runway
421 if (trafficRef->getDepartureAirport()->getDynamics()->getGroundNetwork()->exists())
422 controller = trafficRef->getDepartureAirport()->getDynamics()->getGroundNetwork();
424 case 4: //Take off tower controller
425 if (trafficRef->getDepartureAirport()->getDynamics()) {
426 controller = trafficRef->getDepartureAirport()->getDynamics()->getTowerController();
427 //if (trafficRef->getDepartureAirport()->getId() == "EHAM") {
428 //cerr << trafficRef->getCallSign() << " at runway " << fp->getRunway() << "Ready for departure "
429 // << trafficRef->getFlightType() << " to " << trafficRef->getArrivalAirport()->getId() << endl;
430 // if (controller == 0) {
431 //cerr << "Error in assigning controller at " << trafficRef->getDepartureAirport()->getId() << endl;
435 cerr << "Error: Could not find Dynamics at airport : " << trafficRef->getDepartureAirport()->getId() << endl;
438 case 9: // Taxiing for parking
439 if (trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork()->exists())
440 controller = trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork();
447 if ((controller != prevController) && (prevController != 0)) {
448 prevController->signOff(getID());
449 string callsign = trafficRef->getCallSign();
450 if ( trafficRef->getHeavy())
454 //cerr << callsign << " ready to taxi to runway " << fp->getRunway() << endl;
457 //cerr << callsign << " at runway " << fp->getRunway() << "Ready for take-off. "
458 // << trafficRef->getFlightRules() << " to " << trafficRef->getArrivalAirport()->getId()
459 // << "(" << trafficRef->getArrivalAirport()->getName() << ")."<< endl;
463 prevController = controller;
465 controller->announcePosition(getID(), fp, fp->getCurrentWaypoint()->routeIndex,
466 _getLatitude(), _getLongitude(), hdg, speed, altitude_ft,
467 trafficRef->getRadius(), leg, trafficRef->getCallSign());
473 void FGAIAircraft::processATC(FGATCInstruction instruction) {
474 if (instruction.getCheckForCircularWait()) {
475 // This is not exactly an elegant solution,
476 // but at least it gives me a chance to check
477 // if circular waits are resolved.
478 // For now, just take the offending aircraft
481 // a more proper way should be - of course - to
482 // let an offending aircraft take an evasive action
483 // for instance taxi back a little bit.
485 //cerr << "Processing ATC instruction (not Implimented yet)" << endl;
486 if (instruction.getHoldPattern ()) {}
489 if (instruction.getHoldPosition ()) {
497 // cerr << trafficRef->getCallSign() << " Resuming Taxi " << endl;
500 // Change speed Instruction. This can only be excecuted when there is no
501 // Hold position instruction.
502 if (instruction.getChangeSpeed ()) {
504 //cerr << trafficRef->getCallSign() << " Changing Speed " << endl;
505 AccelTo(instruction.getSpeed());
507 if (fp) AccelTo(fp->getPreviousWaypoint()->speed);
510 if (instruction.getChangeHeading ()) {
512 TurnTo(instruction.getHeading());
518 if (instruction.getChangeAltitude()) {}
523 void FGAIAircraft::handleFirstWaypoint() {
524 bool eraseWaypoints; //TODO YAGNI
527 eraseWaypoints = true;
529 eraseWaypoints = false;
532 FGAIFlightPlan::waypoint* prev = 0; // the one behind you
533 FGAIFlightPlan::waypoint* curr = 0; // the one ahead
534 FGAIFlightPlan::waypoint* next = 0;// the next plus 1
539 //TODO fp should handle this
540 fp->IncrementWaypoint(eraseWaypoints);
541 if (!(fp->getNextWaypoint()) && trafficRef)
544 prev = fp->getPreviousWaypoint(); //first waypoint
545 curr = fp->getCurrentWaypoint(); //second waypoint
546 next = fp->getNextWaypoint(); //third waypoint (might not exist!)
548 setLatitude(prev->latitude);
549 setLongitude(prev->longitude);
550 setSpeed(prev->speed);
551 setAltitude(prev->altitude);
553 if (prev->speed > 0.0)
554 setHeading(fp->getBearing(prev->latitude, prev->longitude, curr));
556 setHeading(fp->getBearing(curr->latitude, curr->longitude, prev));
558 // If next doesn't exist, as in incrementally created flightplans for
559 // AI/Trafficmanager created plans,
560 // Make sure lead distance is initialized otherwise
562 fp->setLeadDistance(speed, hdg, curr, next);
564 if (curr->crossat > -1000.0) //use a calculated descent/climb rate
567 tgt_vs = (curr->crossat - prev->altitude)
568 / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)
569 / 6076.0 / prev->speed*60.0);
570 tgt_altitude_ft = curr->crossat;
573 tgt_altitude_ft = prev->altitude;
575 alt_lock = hdg_lock = true;
576 no_roll = prev->on_ground;
578 Transform(); // make sure aip is initialized.
579 getGroundElev(60.1); // make sure it's executed first time around, so force a large dt value
582 // Make sure to announce the aircraft's position
583 announcePositionToController();
589 * Check Execution time (currently once every 100 ms)
590 * Add a bit of randomization to prevent the execution of all flight plans
591 * in synchrony, which can add significant periodic framerate flutter.
596 bool FGAIAircraft::fpExecutable(time_t now) {
597 double rand_exec_time = (rand() % 100) / 100;
598 return (dt_count > (0.1+rand_exec_time)) && (fp->isActive(now));
603 * Check to see if we've reached the lead point for our next turn
608 bool FGAIAircraft::leadPointReached(FGAIFlightPlan::waypoint* curr) {
609 double dist_to_go = fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr);
611 //cerr << "2" << endl;
612 double lead_dist = fp->getLeadDistance();
613 // experimental: Use fabs, because speed can be negative (I hope) during push_back.
615 if (lead_dist < fabs(2*speed)) {
616 //don't skip over the waypoint
617 lead_dist = fabs(2*speed);
618 //cerr << "Extending lead distance to " << lead_dist << endl;
621 //prev_dist_to_go = dist_to_go;
622 //if (dist_to_go < lead_dist)
623 // cerr << trafficRef->getCallSign() << " Distance : " << dist_to_go << ": Lead distance " << lead_dist << " " << curr->name << endl;
624 return dist_to_go < lead_dist;
628 bool FGAIAircraft::aiTrafficVisible() {
629 double userLatitude = fgGetDouble("/position/latitude-deg");
630 double userLongitude = fgGetDouble("/position/longitude-deg");
631 double course, distance;
633 SGWayPoint current(pos.getLongitudeDeg(), pos.getLatitudeDeg(), 0);
634 SGWayPoint user (userLongitude, userLatitude, 0);
636 user.CourseAndDistance(current, &course, &distance);
638 return ((distance * SG_METER_TO_NM) <= TRAFFICTOAIDIST);
643 * Handle release of parking gate, once were taxiing. Also ensure service time at the gate
644 * in the case of an arrival.
650 //TODO the trafficRef is the right place for the method
651 bool FGAIAircraft::handleAirportEndPoints(FGAIFlightPlan::waypoint* prev, time_t now) {
652 // prepare routing from one airport to another
653 FGAirport * dep = trafficRef->getDepartureAirport();
654 FGAirport * arr = trafficRef->getArrivalAirport();
659 // This waypoint marks the fact that the aircraft has passed the initial taxi
660 // departure waypoint, so it can release the parking.
661 if (prev->name == "PushBackPoint") {
662 dep->getDynamics()->releaseParking(fp->getGate());
663 time_t holdUntil = now + 120;
664 fp->setTime(holdUntil);
665 //cerr << _getCallsign() << "Holding at pushback point" << endl;
668 // This is the last taxi waypoint, and marks the the end of the flight plan
669 // so, the schedule should update and wait for the next departure time.
670 if (prev->name == "END") {
671 time_t nextDeparture = trafficRef->getDepartureTime();
672 // make sure to wait at least 20 minutes at parking to prevent "nervous" taxi behavior
673 if (nextDeparture < (now+1200)) {
674 nextDeparture = now + 1200;
676 fp->setTime(nextDeparture); // should be "next departure"
684 * Check difference between target bearing and current heading and correct if necessary.
688 void FGAIAircraft::controlHeading(FGAIFlightPlan::waypoint* curr) {
689 double calc_bearing = fp->getBearing(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr);
690 //cerr << "Bearing = " << calc_bearing << endl;
693 if (calc_bearing > 360)
697 if (finite(calc_bearing)) {
698 double hdg_error = calc_bearing - tgt_heading;
699 if (fabs(hdg_error) > 0.01) {
700 TurnTo( calc_bearing );
704 cerr << "calc_bearing is not a finite number : "
706 << "pos : " << pos.getLatitudeDeg() << ", " << pos.getLongitudeDeg()
707 << "waypoint " << curr->latitude << ", " << curr->longitude << endl;
708 cerr << "waypoint name " << curr->name;
715 * Update the lead distance calculation if speed has changed sufficiently
716 * to prevent spinning (hopefully);
721 void FGAIAircraft::controlSpeed(FGAIFlightPlan::waypoint* curr, FGAIFlightPlan::waypoint* next) {
722 double speed_diff = speed - prevSpeed;
724 if (fabs(speed_diff) > 10) {
727 fp->setLeadDistance(speed, tgt_heading, curr, next);
734 * Update target values (heading, alt, speed) depending on flight plan or control properties
736 void FGAIAircraft::updatePrimaryTargetValues() {
737 if (fp) // AI object has a flightplan
739 //TODO make this a function of AIBase
740 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
741 //cerr << "UpateTArgetValues() " << endl;
742 ProcessFlightPlan(dt, now);
744 // Do execute Ground elev for inactive aircraft, so they
745 // Are repositioned to the correct ground altitude when the user flies within visibility range.
746 // In addition, check whether we are out of user range, so this aircraft
749 Transform(); // make sure aip is initialized.
753 pos.setElevationFt(altitude_ft);
756 //cerr << trafficRef->getRegistration() << " Setting altitude to " << altitude_ft;
757 if (! aiTrafficVisible()) {
759 //cerr << trafficRef->getRegistration() << " is set to die " << endl;
760 throw AI_OutOfSight();
763 if (! fp->isActive(now)) {
767 // no flight plan, update target heading, speed, and altitude
768 // from control properties. These default to the initial
769 // settings in the config file, but can be changed "on the
771 string lat_mode = props->getStringValue("controls/flight/lateral-mode");
772 if ( lat_mode == "roll" ) {
774 = props->getDoubleValue("controls/flight/target-roll" );
778 = props->getDoubleValue("controls/flight/target-hdg" );
783 = props->getStringValue("controls/flight/longitude-mode");
784 if ( lon_mode == "alt" ) {
785 double alt = props->getDoubleValue("controls/flight/target-alt" );
789 = props->getDoubleValue("controls/flight/target-pitch" );
793 AccelTo( props->getDoubleValue("controls/flight/target-spd" ) );
797 void FGAIAircraft::updatePosition() {
798 // convert speed to degrees per second
799 double speed_north_deg_sec = cos( hdg * SGD_DEGREES_TO_RADIANS )
800 * speed * 1.686 / ft_per_deg_lat;
801 double speed_east_deg_sec = sin( hdg * SGD_DEGREES_TO_RADIANS )
802 * speed * 1.686 / ft_per_deg_lon;
805 pos.setLatitudeDeg( pos.getLatitudeDeg() + speed_north_deg_sec * dt);
806 pos.setLongitudeDeg( pos.getLongitudeDeg() + speed_east_deg_sec * dt);
810 void FGAIAircraft::updateHeading() {
811 // adjust heading based on current bank angle
816 // double turnConstant;
818 // turnConstant = 0.0088362;
820 // turnConstant = 0.088362;
821 // If on ground, calculate heading change directly
823 double headingDiff = fabs(hdg-tgt_heading);
824 double bank_sense = 0.0;
826 double diff = fabs(hdg - tgt_heading);
828 diff = fabs(diff - 360);
830 double sum = hdg + diff;
833 if (fabs(sum - tgt_heading) < 1.0) {
834 bank_sense = 1.0; // right turn
836 bank_sense = -1.0; // left turn
838 if (headingDiff > 180)
839 headingDiff = fabs(headingDiff - 360);
840 double sum = hdg + headingDiff;
843 if (fabs(sum - tgt_heading) > 0.0001) {
849 //cerr << trafficRef->getCallSign() << " Heading "
850 // << hdg << ". Target " << tgt_heading << ". Diff " << fabs(sum - tgt_heading) << ". Speed " << speed << endl;
851 //if (headingDiff > 60) {
852 groundTargetSpeed = tgt_speed; // * cos(headingDiff * SG_DEGREES_TO_RADIANS);
853 //groundTargetSpeed = tgt_speed - tgt_speed * (headingDiff/180);
855 // groundTargetSpeed = tgt_speed;
857 if (sign(groundTargetSpeed) != sign(tgt_speed))
858 groundTargetSpeed = 0.21 * sign(tgt_speed); // to prevent speed getting stuck in 'negative' mode
860 if (headingDiff > 30.0) {
861 // invert if pushed backward
862 headingChangeRate += 10.0 * dt * sign(roll);
864 // Clamp the maximum steering rate to 30 degrees per second,
865 // But only do this when the heading error is decreasing.
866 if ((headingDiff < headingError)) {
867 if (headingChangeRate > 30)
868 headingChangeRate = 30;
869 else if (headingChangeRate < -30)
870 headingChangeRate = -30;
873 if (fabs(headingChangeRate) > headingDiff)
874 headingChangeRate = headingDiff*sign(roll);
876 headingChangeRate += dt * sign(roll);
878 hdg += headingChangeRate * dt;
879 headingError = headingDiff;
881 if (fabs(speed) > 1.0) {
882 turn_radius_ft = 0.088362 * speed * speed
883 / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
885 // Check if turn_radius_ft == 0; this might lead to a division by 0.
886 turn_radius_ft = 1.0;
888 double turn_circum_ft = SGD_2PI * turn_radius_ft;
889 double dist_covered_ft = speed * 1.686 * dt;
890 double alpha = dist_covered_ft / turn_circum_ft * 360.0;
891 hdg += alpha * sign(roll);
893 while ( hdg > 360.0 ) {
905 void FGAIAircraft::updateBankAngleTarget() {
906 // adjust target bank angle if heading lock engaged
908 double bank_sense = 0.0;
909 double diff = fabs(hdg - tgt_heading);
911 diff = fabs(diff - 360);
913 double sum = hdg + diff;
916 if (fabs(sum - tgt_heading) < 1.0) {
917 bank_sense = 1.0; // right turn
919 bank_sense = -1.0; // left turn
921 if (diff < _performance->maximumBankAngle()) {
922 tgt_roll = diff * bank_sense;
924 tgt_roll = _performance->maximumBankAngle() * bank_sense;
926 if ((fabs((double) spinCounter) > 1) && (diff > _performance->maximumBankAngle())) {
927 tgt_speed *= 0.999; // Ugly hack: If aircraft get stuck, they will continually spin around.
928 // The only way to resolve this is to make them slow down.
934 void FGAIAircraft::updateVerticalSpeedTarget() {
935 // adjust target Altitude, based on ground elevation when on ground
939 } else if (alt_lock) {
940 // find target vertical speed
942 if (altitude_ft < tgt_altitude_ft) {
943 tgt_vs = tgt_altitude_ft - altitude_ft;
944 if (tgt_vs > _performance->climbRate())
945 tgt_vs = _performance->climbRate();
947 tgt_vs = tgt_altitude_ft - altitude_ft;
948 if (tgt_vs < (-_performance->descentRate()))
949 tgt_vs = -_performance->descentRate();
952 double max_vs = 4*(tgt_altitude_ft - altitude_ft);
954 if (tgt_altitude_ft < altitude_ft)
956 if ((fabs(tgt_altitude_ft - altitude_ft) < 1500.0)
957 && (fabs(max_vs) < fabs(tgt_vs)))
960 if (fabs(tgt_vs) < fabs(min_vs))
967 void FGAIAircraft::updatePitchAngleTarget() {
968 // if on ground and above vRotate -> initial rotation
969 if (onGround() && (speed > _performance->vRotate()))
970 tgt_pitch = 8.0; // some rough B737 value
972 //TODO pitch angle on approach and landing
974 // match pitch angle to vertical speed
975 else if (tgt_vs > 0) {
976 tgt_pitch = tgt_vs * 0.005;
978 tgt_pitch = tgt_vs * 0.002;
982 void FGAIAircraft::handleATCRequests() {
983 //TODO implement NullController for having no ATC to save the conditionals
985 controller->update(getID(),
986 pos.getLatitudeDeg(),
987 pos.getLongitudeDeg(),
991 processATC(controller->getInstruction(getID()));
995 void FGAIAircraft::updateActualState() {
996 //update current state
997 //TODO have a single tgt_speed and check speed limit on ground on setting tgt_speed
1001 speed = _performance->actualSpeed(this, groundTargetSpeed, dt);
1003 speed = _performance->actualSpeed(this, tgt_speed, dt);
1006 roll = _performance->actualBankAngle(this, tgt_roll, dt);
1008 // adjust altitude (meters) based on current vertical speed (fpm)
1009 altitude_ft += vs / 60.0 * dt;
1010 pos.setElevationFt(altitude_ft);
1012 vs = _performance->actualVerticalSpeed(this, tgt_vs, dt);
1013 pitch = _performance->actualPitch(this, tgt_pitch, dt);
1016 void FGAIAircraft::updateSecondaryTargetValues() {
1017 // derived target state values
1018 updateBankAngleTarget();
1019 updateVerticalSpeedTarget();
1020 updatePitchAngleTarget();
1022 //TODO calculate wind correction angle (tgt_yaw)