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) {
154 groundTargetSpeed = 0;
157 handleATCRequests(); // ATC also has a word to say
158 updateSecondaryTargetValues(); // target roll, vertical speed, pitch
160 UpdateRadar(manager);
165 void FGAIAircraft::AccelTo(double speed) {
170 void FGAIAircraft::PitchTo(double angle) {
176 void FGAIAircraft::RollTo(double angle) {
182 void FGAIAircraft::YawTo(double angle) {
187 void FGAIAircraft::ClimbTo(double alt_ft ) {
188 tgt_altitude_ft = alt_ft;
193 void FGAIAircraft::TurnTo(double heading) {
194 tgt_heading = heading;
199 double FGAIAircraft::sign(double x) {
207 void FGAIAircraft::setFlightPlan(const std::string& flightplan, bool repeat) {
208 if (!flightplan.empty()) {
209 FGAIFlightPlan* fp = new FGAIFlightPlan(flightplan);
210 fp->setRepeat(repeat);
216 void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f) {
222 void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) {
224 // the one behind you
225 FGAIFlightPlan::waypoint* prev = 0;
227 FGAIFlightPlan::waypoint* curr = 0;
229 FGAIFlightPlan::waypoint* next = 0;
231 prev = fp->getPreviousWaypoint();
232 curr = fp->getCurrentWaypoint();
233 next = fp->getNextWaypoint();
237 ///////////////////////////////////////////////////////////////////////////
238 // Initialize the flightplan
239 //////////////////////////////////////////////////////////////////////////
241 handleFirstWaypoint();
243 } // end of initialization
244 if (! fpExecutable(now))
248 if (! leadPointReached(curr)) {
249 controlHeading(curr);
250 controlSpeed(curr, next);
252 if (curr->finished) //end of the flight plan
262 //TODO more intelligent method in AIFlightPlan, no need to send data it already has :-)
263 tgt_heading = fp->getBearing(curr, next);
267 //TODO let the fp handle this (loading of next leg)
268 fp->IncrementWaypoint((bool) trafficRef);
269 if (!(fp->getNextWaypoint()) && trafficRef)
272 prev = fp->getPreviousWaypoint();
273 curr = fp->getCurrentWaypoint();
274 next = fp->getNextWaypoint();
276 // Now that we have incremented the waypoints, excute some traffic manager specific code
278 //TODO isn't this best executed right at the beginning?
279 if (! aiTrafficVisible()) {
284 if (! handleAirportEndPoints(prev, now)) {
289 announcePositionToController();
294 fp->setLeadDistance(tgt_speed, tgt_heading, curr, next);
297 if (!(prev->on_ground)) // only update the tgt altitude from flightplan if not on the ground
299 tgt_altitude_ft = prev->altitude;
300 if (curr->crossat > -1000.0) {
302 tgt_vs = (curr->crossat - altitude_ft) / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)
303 / 6076.0 / speed*60.0);
304 tgt_altitude_ft = curr->crossat;
309 tgt_speed = prev->speed;
310 hdg_lock = alt_lock = true;
311 no_roll = prev->on_ground;
316 void FGAIAircraft::initializeFlightPlan() {
320 bool FGAIAircraft::_getGearDown() const {
321 return _performance->gearExtensible(this);
325 void FGAIAircraft::loadNextLeg() {
328 if ((leg = fp->getLeg()) == 10) {
330 setCallSign(trafficRef->getCallSign());
331 //props->setStringValue("callsign", callsign.c_str());
336 FGAirport *dep = trafficRef->getDepartureAirport();
337 FGAirport *arr = trafficRef->getArrivalAirport();
342 double cruiseAlt = trafficRef->getCruiseAlt() * 100;
347 cruiseAlt, //(trafficRef->getCruiseAlt() * 100), // convert from FL to feet
348 trafficRef->getSpeed(),
352 trafficRef->getRadius(),
353 trafficRef->getFlightType(),
360 // Note: This code is copied from David Luff's AILocalTraffic
361 // Warning - ground elev determination is CPU intensive
362 // Either this function or the logic of how often it is called
363 // will almost certainly change.
365 void FGAIAircraft::getGroundElev(double dt) {
368 // Update minimally every three secs, but add some randomness
369 // to prevent all AI objects doing this in synchrony
370 if (dt_elev_count < (3.0) + (rand() % 10))
375 // Only do the proper hitlist stuff if we are within visible range of the viewer.
377 double visibility_meters = fgGetDouble("/environment/visibility-m");
379 FGViewer* vw = globals->get_current_view();
380 double course, distance;
382 SGWayPoint current(pos.getLongitudeDeg(), pos.getLatitudeDeg(), 0);
383 SGWayPoint view (vw->getLongitude_deg(), vw->getLatitude_deg(), 0);
384 view.CourseAndDistance(current, &course, &distance);
385 if (distance > visibility_meters) {
386 //aip.getSGLocation()->set_cur_elev_m(aptElev);
390 // FIXME: make sure the pos.lat/pos.lon values are in degrees ...
391 double range = 500.0;
392 if (!globals->get_tile_mgr()->scenery_available(pos.getLatitudeDeg(), pos.getLongitudeDeg(), range)) {
393 // Try to shedule tiles for that position.
394 globals->get_tile_mgr()->update( aip.getSGLocation(), range );
397 // FIXME: make sure the pos.lat/pos.lon values are in degrees ...
399 if (globals->get_scenery()->get_elevation_m(pos.getLatitudeDeg(), pos.getLongitudeDeg(), 20000.0, alt, 0))
400 tgt_altitude_ft = alt * SG_METER_TO_FEET;
405 void FGAIAircraft::doGroundAltitude() {
406 if (fabs(altitude_ft - (tgt_altitude_ft+groundOffset)) > 1000.0)
407 altitude_ft = (tgt_altitude_ft + groundOffset);
409 altitude_ft += 0.1 * ((tgt_altitude_ft+groundOffset) - altitude_ft);
413 void FGAIAircraft::announcePositionToController() {
415 int leg = fp->getLeg();
417 // Note that leg has been incremented after creating the current leg, so we should use
418 // leg numbers here that are one higher than the number that is used to create the leg
421 case 2: // Startup and Push back
422 if (trafficRef->getDepartureAirport()->getDynamics())
423 controller = trafficRef->getDepartureAirport()->getDynamics()->getStartupController();
425 case 3: // Taxiing to runway
426 if (trafficRef->getDepartureAirport()->getDynamics()->getGroundNetwork()->exists())
427 controller = trafficRef->getDepartureAirport()->getDynamics()->getGroundNetwork();
429 case 4: //Take off tower controller
430 if (trafficRef->getDepartureAirport()->getDynamics()) {
431 controller = trafficRef->getDepartureAirport()->getDynamics()->getTowerController();
432 //if (trafficRef->getDepartureAirport()->getId() == "EHAM") {
433 //string trns = trafficRef->getCallSign() + " at runway " + fp->getRunway() +
434 // ". Ready for departure. " + trafficRef->getFlightType() + " to " +
435 // trafficRef->getArrivalAirport()->getId();
436 //fgSetString("/sim/messages/atc", trns.c_str());
437 // if (controller == 0) {
438 //cerr << "Error in assigning controller at " << trafficRef->getDepartureAirport()->getId() << endl;
442 cerr << "Error: Could not find Dynamics at airport : " << trafficRef->getDepartureAirport()->getId() << endl;
445 case 9: // Taxiing for parking
446 if (trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork()->exists())
447 controller = trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork();
454 if ((controller != prevController) && (prevController != 0)) {
455 prevController->signOff(getID());
457 prevController = controller;
459 controller->announcePosition(getID(), fp, fp->getCurrentWaypoint()->routeIndex,
460 _getLatitude(), _getLongitude(), hdg, speed, altitude_ft,
461 trafficRef->getRadius(), leg, this);
466 // Process ATC instructions and report back
468 void FGAIAircraft::processATC(FGATCInstruction instruction) {
469 if (instruction.getCheckForCircularWait()) {
470 // This is not exactly an elegant solution,
471 // but at least it gives me a chance to check
472 // if circular waits are resolved.
473 // For now, just take the offending aircraft
476 // a more proper way should be - of course - to
477 // let an offending aircraft take an evasive action
478 // for instance taxi back a little bit.
480 //cerr << "Processing ATC instruction (not Implimented yet)" << endl;
481 if (instruction.getHoldPattern ()) {}
484 if (instruction.getHoldPosition ()) {
492 // cerr << trafficRef->getCallSign() << " Resuming Taxi." << endl;
495 // Change speed Instruction. This can only be excecuted when there is no
496 // Hold position instruction.
497 if (instruction.getChangeSpeed ()) {
499 //cerr << trafficRef->getCallSign() << " Changing Speed " << endl;
500 AccelTo(instruction.getSpeed());
502 if (fp) AccelTo(fp->getPreviousWaypoint()->speed);
505 if (instruction.getChangeHeading ()) {
507 TurnTo(instruction.getHeading());
513 if (instruction.getChangeAltitude()) {}
518 void FGAIAircraft::handleFirstWaypoint() {
519 bool eraseWaypoints; //TODO YAGNI
522 eraseWaypoints = true;
524 eraseWaypoints = false;
527 FGAIFlightPlan::waypoint* prev = 0; // the one behind you
528 FGAIFlightPlan::waypoint* curr = 0; // the one ahead
529 FGAIFlightPlan::waypoint* next = 0;// the next plus 1
534 //TODO fp should handle this
535 fp->IncrementWaypoint(eraseWaypoints);
536 if (!(fp->getNextWaypoint()) && trafficRef)
539 prev = fp->getPreviousWaypoint(); //first waypoint
540 curr = fp->getCurrentWaypoint(); //second waypoint
541 next = fp->getNextWaypoint(); //third waypoint (might not exist!)
543 setLatitude(prev->latitude);
544 setLongitude(prev->longitude);
545 setSpeed(prev->speed);
546 setAltitude(prev->altitude);
548 if (prev->speed > 0.0)
549 setHeading(fp->getBearing(prev->latitude, prev->longitude, curr));
551 setHeading(fp->getBearing(curr->latitude, curr->longitude, prev));
553 // If next doesn't exist, as in incrementally created flightplans for
554 // AI/Trafficmanager created plans,
555 // Make sure lead distance is initialized otherwise
557 fp->setLeadDistance(speed, hdg, curr, next);
559 if (curr->crossat > -1000.0) //use a calculated descent/climb rate
562 tgt_vs = (curr->crossat - prev->altitude)
563 / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)
564 / 6076.0 / prev->speed*60.0);
565 tgt_altitude_ft = curr->crossat;
568 tgt_altitude_ft = prev->altitude;
570 alt_lock = hdg_lock = true;
571 no_roll = prev->on_ground;
573 Transform(); // make sure aip is initialized.
574 getGroundElev(60.1); // make sure it's executed first time around, so force a large dt value
577 // Make sure to announce the aircraft's position
578 announcePositionToController();
584 * Check Execution time (currently once every 100 ms)
585 * Add a bit of randomization to prevent the execution of all flight plans
586 * in synchrony, which can add significant periodic framerate flutter.
591 bool FGAIAircraft::fpExecutable(time_t now) {
592 double rand_exec_time = (rand() % 100) / 100;
593 return (dt_count > (0.1+rand_exec_time)) && (fp->isActive(now));
598 * Check to see if we've reached the lead point for our next turn
603 bool FGAIAircraft::leadPointReached(FGAIFlightPlan::waypoint* curr) {
604 double dist_to_go = fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr);
606 //cerr << "2" << endl;
607 double lead_dist = fp->getLeadDistance();
608 // experimental: Use fabs, because speed can be negative (I hope) during push_back.
610 if (lead_dist < fabs(2*speed)) {
611 //don't skip over the waypoint
612 lead_dist = fabs(2*speed);
613 //cerr << "Extending lead distance to " << lead_dist << endl;
616 //prev_dist_to_go = dist_to_go;
617 //if (dist_to_go < lead_dist)
618 // cerr << trafficRef->getCallSign() << " Distance : " << dist_to_go << ": Lead distance " << lead_dist << " " << curr->name << endl;
619 return dist_to_go < lead_dist;
623 bool FGAIAircraft::aiTrafficVisible() {
624 double userLatitude = fgGetDouble("/position/latitude-deg");
625 double userLongitude = fgGetDouble("/position/longitude-deg");
626 double course, distance;
628 SGWayPoint current(pos.getLongitudeDeg(), pos.getLatitudeDeg(), 0);
629 SGWayPoint user (userLongitude, userLatitude, 0);
631 user.CourseAndDistance(current, &course, &distance);
633 return ((distance * SG_METER_TO_NM) <= TRAFFICTOAIDIST);
638 * Handle release of parking gate, once were taxiing. Also ensure service time at the gate
639 * in the case of an arrival.
645 //TODO the trafficRef is the right place for the method
646 bool FGAIAircraft::handleAirportEndPoints(FGAIFlightPlan::waypoint* prev, time_t now) {
647 // prepare routing from one airport to another
648 FGAirport * dep = trafficRef->getDepartureAirport();
649 FGAirport * arr = trafficRef->getArrivalAirport();
654 // This waypoint marks the fact that the aircraft has passed the initial taxi
655 // departure waypoint, so it can release the parking.
656 if (prev->name == "PushBackPoint") {
657 dep->getDynamics()->releaseParking(fp->getGate());
658 time_t holdUntil = now + 120;
659 fp->setTime(holdUntil);
660 //cerr << _getCallsign() << "Holding at pushback point" << endl;
663 // This is the last taxi waypoint, and marks the the end of the flight plan
664 // so, the schedule should update and wait for the next departure time.
665 if (prev->name == "END") {
666 time_t nextDeparture = trafficRef->getDepartureTime();
667 // make sure to wait at least 20 minutes at parking to prevent "nervous" taxi behavior
668 if (nextDeparture < (now+1200)) {
669 nextDeparture = now + 1200;
671 fp->setTime(nextDeparture); // should be "next departure"
679 * Check difference between target bearing and current heading and correct if necessary.
683 void FGAIAircraft::controlHeading(FGAIFlightPlan::waypoint* curr) {
684 double calc_bearing = fp->getBearing(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr);
685 //cerr << "Bearing = " << calc_bearing << endl;
688 if (calc_bearing > 360)
692 if (finite(calc_bearing)) {
693 double hdg_error = calc_bearing - tgt_heading;
694 if (fabs(hdg_error) > 0.01) {
695 TurnTo( calc_bearing );
699 cerr << "calc_bearing is not a finite number : "
701 << "pos : " << pos.getLatitudeDeg() << ", " << pos.getLongitudeDeg()
702 << "waypoint " << curr->latitude << ", " << curr->longitude << endl;
703 cerr << "waypoint name " << curr->name;
710 * Update the lead distance calculation if speed has changed sufficiently
711 * to prevent spinning (hopefully);
716 void FGAIAircraft::controlSpeed(FGAIFlightPlan::waypoint* curr, FGAIFlightPlan::waypoint* next) {
717 double speed_diff = speed - prevSpeed;
719 if (fabs(speed_diff) > 10) {
722 fp->setLeadDistance(speed, tgt_heading, curr, next);
729 * Update target values (heading, alt, speed) depending on flight plan or control properties
731 void FGAIAircraft::updatePrimaryTargetValues() {
732 if (fp) // AI object has a flightplan
734 //TODO make this a function of AIBase
735 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
736 //cerr << "UpateTArgetValues() " << endl;
737 ProcessFlightPlan(dt, now);
739 // Do execute Ground elev for inactive aircraft, so they
740 // Are repositioned to the correct ground altitude when the user flies within visibility range.
741 // In addition, check whether we are out of user range, so this aircraft
744 Transform(); // make sure aip is initialized.
748 pos.setElevationFt(altitude_ft);
751 //cerr << trafficRef->getRegistration() << " Setting altitude to " << altitude_ft;
752 if (! aiTrafficVisible()) {
754 //cerr << trafficRef->getRegistration() << " is set to die " << endl;
755 throw AI_OutOfSight();
758 timeElapsed = now - fp->getStartTime();
759 if (! fp->isActive(now)) {
763 // no flight plan, update target heading, speed, and altitude
764 // from control properties. These default to the initial
765 // settings in the config file, but can be changed "on the
767 string lat_mode = props->getStringValue("controls/flight/lateral-mode");
768 if ( lat_mode == "roll" ) {
770 = props->getDoubleValue("controls/flight/target-roll" );
774 = props->getDoubleValue("controls/flight/target-hdg" );
779 = props->getStringValue("controls/flight/longitude-mode");
780 if ( lon_mode == "alt" ) {
781 double alt = props->getDoubleValue("controls/flight/target-alt" );
785 = props->getDoubleValue("controls/flight/target-pitch" );
789 AccelTo( props->getDoubleValue("controls/flight/target-spd" ) );
793 void FGAIAircraft::updatePosition() {
794 // convert speed to degrees per second
795 double speed_north_deg_sec = cos( hdg * SGD_DEGREES_TO_RADIANS )
796 * speed * 1.686 / ft_per_deg_lat;
797 double speed_east_deg_sec = sin( hdg * SGD_DEGREES_TO_RADIANS )
798 * speed * 1.686 / ft_per_deg_lon;
801 pos.setLatitudeDeg( pos.getLatitudeDeg() + speed_north_deg_sec * dt);
802 pos.setLongitudeDeg( pos.getLongitudeDeg() + speed_east_deg_sec * dt);
806 void FGAIAircraft::updateHeading() {
807 // adjust heading based on current bank angle
812 // double turnConstant;
814 // turnConstant = 0.0088362;
816 // turnConstant = 0.088362;
817 // If on ground, calculate heading change directly
819 double headingDiff = fabs(hdg-tgt_heading);
820 double bank_sense = 0.0;
822 double diff = fabs(hdg - tgt_heading);
824 diff = fabs(diff - 360);
826 double sum = hdg + diff;
829 if (fabs(sum - tgt_heading) < 1.0) {
830 bank_sense = 1.0; // right turn
832 bank_sense = -1.0; // left turn
834 if (headingDiff > 180)
835 headingDiff = fabs(headingDiff - 360);
836 double sum = hdg + headingDiff;
839 if (fabs(sum - tgt_heading) > 0.0001) {
845 //cerr << trafficRef->getCallSign() << " Heading "
846 // << hdg << ". Target " << tgt_heading << ". Diff " << fabs(sum - tgt_heading) << ". Speed " << speed << endl;
847 //if (headingDiff > 60) {
848 groundTargetSpeed = tgt_speed; // * cos(headingDiff * SG_DEGREES_TO_RADIANS);
849 //groundTargetSpeed = tgt_speed - tgt_speed * (headingDiff/180);
851 // groundTargetSpeed = tgt_speed;
853 if (sign(groundTargetSpeed) != sign(tgt_speed))
854 groundTargetSpeed = 0.21 * sign(tgt_speed); // to prevent speed getting stuck in 'negative' mode
856 if (headingDiff > 30.0) {
857 // invert if pushed backward
858 headingChangeRate += 10.0 * dt * sign(roll);
860 // Clamp the maximum steering rate to 30 degrees per second,
861 // But only do this when the heading error is decreasing.
862 if ((headingDiff < headingError)) {
863 if (headingChangeRate > 30)
864 headingChangeRate = 30;
865 else if (headingChangeRate < -30)
866 headingChangeRate = -30;
869 if (fabs(headingChangeRate) > headingDiff)
870 headingChangeRate = headingDiff*sign(roll);
872 headingChangeRate += dt * sign(roll);
875 hdg += headingChangeRate * dt * (fabs(speed) / 15);
876 headingError = headingDiff;
878 if (fabs(speed) > 1.0) {
879 turn_radius_ft = 0.088362 * speed * speed
880 / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
882 // Check if turn_radius_ft == 0; this might lead to a division by 0.
883 turn_radius_ft = 1.0;
885 double turn_circum_ft = SGD_2PI * turn_radius_ft;
886 double dist_covered_ft = speed * 1.686 * dt;
887 double alpha = dist_covered_ft / turn_circum_ft * 360.0;
888 hdg += alpha * sign(roll);
890 while ( hdg > 360.0 ) {
902 void FGAIAircraft::updateBankAngleTarget() {
903 // adjust target bank angle if heading lock engaged
905 double bank_sense = 0.0;
906 double diff = fabs(hdg - tgt_heading);
908 diff = fabs(diff - 360);
910 double sum = hdg + diff;
913 if (fabs(sum - tgt_heading) < 1.0) {
914 bank_sense = 1.0; // right turn
916 bank_sense = -1.0; // left turn
918 if (diff < _performance->maximumBankAngle()) {
919 tgt_roll = diff * bank_sense;
921 tgt_roll = _performance->maximumBankAngle() * bank_sense;
923 if ((fabs((double) spinCounter) > 1) && (diff > _performance->maximumBankAngle())) {
924 tgt_speed *= 0.999; // Ugly hack: If aircraft get stuck, they will continually spin around.
925 // The only way to resolve this is to make them slow down.
931 void FGAIAircraft::updateVerticalSpeedTarget() {
932 // adjust target Altitude, based on ground elevation when on ground
936 } else if (alt_lock) {
937 // find target vertical speed
939 if (altitude_ft < tgt_altitude_ft) {
940 tgt_vs = tgt_altitude_ft - altitude_ft;
941 if (tgt_vs > _performance->climbRate())
942 tgt_vs = _performance->climbRate();
944 tgt_vs = tgt_altitude_ft - altitude_ft;
945 if (tgt_vs < (-_performance->descentRate()))
946 tgt_vs = -_performance->descentRate();
949 double max_vs = 4*(tgt_altitude_ft - altitude_ft);
951 if (tgt_altitude_ft < altitude_ft)
953 if ((fabs(tgt_altitude_ft - altitude_ft) < 1500.0)
954 && (fabs(max_vs) < fabs(tgt_vs)))
957 if (fabs(tgt_vs) < fabs(min_vs))
964 void FGAIAircraft::updatePitchAngleTarget() {
965 // if on ground and above vRotate -> initial rotation
966 if (onGround() && (speed > _performance->vRotate()))
967 tgt_pitch = 8.0; // some rough B737 value
969 //TODO pitch angle on approach and landing
971 // match pitch angle to vertical speed
972 else if (tgt_vs > 0) {
973 tgt_pitch = tgt_vs * 0.005;
975 tgt_pitch = tgt_vs * 0.002;
979 string FGAIAircraft::atGate() {
981 if (fp->getLeg() < 3) {
983 if (fp->getGate() > 0) {
985 trafficRef->getDepartureAirport()->getDynamics()->getParking(fp->getGate());
986 tmp = park->getName();
993 void FGAIAircraft::handleATCRequests() {
994 //TODO implement NullController for having no ATC to save the conditionals
996 controller->update(getID(),
997 pos.getLatitudeDeg(),
998 pos.getLongitudeDeg(),
1002 processATC(controller->getInstruction(getID()));
1006 void FGAIAircraft::updateActualState() {
1007 //update current state
1008 //TODO have a single tgt_speed and check speed limit on ground on setting tgt_speed
1012 speed = _performance->actualSpeed(this, groundTargetSpeed, dt);
1014 speed = _performance->actualSpeed(this, tgt_speed, dt);
1017 roll = _performance->actualBankAngle(this, tgt_roll, dt);
1019 // adjust altitude (meters) based on current vertical speed (fpm)
1020 altitude_ft += vs / 60.0 * dt;
1021 pos.setElevationFt(altitude_ft);
1023 vs = _performance->actualVerticalSpeed(this, tgt_vs, dt);
1024 pitch = _performance->actualPitch(this, tgt_pitch, dt);
1027 void FGAIAircraft::updateSecondaryTargetValues() {
1028 // derived target state values
1029 updateBankAngleTarget();
1030 updateVerticalSpeedTarget();
1031 updatePitchAngleTarget();
1033 //TODO calculate wind correction angle (tgt_yaw)