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/route/waypoint.hxx>
26 #include <Main/fg_props.hxx>
27 #include <Main/globals.hxx>
28 #include <Main/viewer.hxx>
29 #include <Scenery/scenery.hxx>
30 #include <Scenery/tilemgr.hxx>
31 #include <Airports/dynamics.hxx>
39 # define finite _finite
40 #elif defined(__sun) || defined(sgi)
46 #include "AIAircraft.hxx"
47 #include "performancedata.hxx"
48 #include "performancedb.hxx"
50 //#include <Airports/trafficcontroller.hxx>
52 static string tempReg;
54 class AI_OutOfSight{};
57 FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) {
60 groundOffset = trafficRef->getGroundOffset();
61 setCallSign(trafficRef->getCallSign());
76 groundTargetSpeed = 0;
78 // set heading and altitude locks
82 headingChangeRate = 0.0;
87 _performance = 0; //TODO initialize to JET_TRANSPORT from PerformanceDB
92 FGAIAircraft::~FGAIAircraft() {
95 controller->signOff(getID());
99 void FGAIAircraft::readFromScenario(SGPropertyNode* scFileNode) {
103 FGAIBase::readFromScenario(scFileNode);
105 setPerformance(scFileNode->getStringValue("class", "jet_transport"));
106 setFlightPlan(scFileNode->getStringValue("flightplan"),
107 scFileNode->getBoolValue("repeat", false));
108 setCallSign(scFileNode->getStringValue("callsign"));
112 void FGAIAircraft::bind() {
115 props->tie("controls/gear/gear-down",
116 SGRawValueMethods<FGAIAircraft,bool>(*this,
117 &FGAIAircraft::_getGearDown));
118 props->tie("transponder-id",
119 SGRawValueMethods<FGAIAircraft,const char*>(*this,
120 &FGAIAircraft::_getTransponderCode));
124 void FGAIAircraft::unbind() {
127 props->untie("controls/gear/gear-down");
128 props->untie("transponder-id");
132 void FGAIAircraft::update(double dt) {
133 FGAIBase::update(dt);
138 void FGAIAircraft::setPerformance(const std::string& acclass) {
139 static PerformanceDB perfdb; //TODO make it a global service
140 setPerformance(perfdb.getDataFor(acclass));
144 void FGAIAircraft::setPerformance(PerformanceData *ps) {
149 void FGAIAircraft::Run(double dt) {
150 FGAIAircraft::dt = dt;
153 updatePrimaryTargetValues(); // target hdg, alt, speed
155 catch (AI_OutOfSight) {
158 catch (FP_Inactive) {
160 groundTargetSpeed = 0;
163 handleATCRequests(); // ATC also has a word to say
164 updateSecondaryTargetValues(); // target roll, vertical speed, pitch
166 UpdateRadar(manager);
170 void FGAIAircraft::checkVisibility()
172 double visibility_meters = fgGetDouble("/environment/visibility-m");
173 FGViewer* vw = globals->get_current_view();
174 invisible = (SGGeodesy::distanceM(vw->getPosition(), pos) > visibility_meters);
179 void FGAIAircraft::AccelTo(double speed) {
184 void FGAIAircraft::PitchTo(double angle) {
190 void FGAIAircraft::RollTo(double angle) {
196 void FGAIAircraft::YawTo(double angle) {
201 void FGAIAircraft::ClimbTo(double alt_ft ) {
202 tgt_altitude_ft = alt_ft;
207 void FGAIAircraft::TurnTo(double heading) {
208 tgt_heading = heading;
213 double FGAIAircraft::sign(double x) {
221 void FGAIAircraft::setFlightPlan(const std::string& flightplan, bool repeat) {
222 if (!flightplan.empty()) {
223 FGAIFlightPlan* fp = new FGAIFlightPlan(flightplan);
224 fp->setRepeat(repeat);
230 void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f) {
236 void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) {
238 // the one behind you
239 FGAIFlightPlan::waypoint* prev = 0;
241 FGAIFlightPlan::waypoint* curr = 0;
243 FGAIFlightPlan::waypoint* next = 0;
245 prev = fp->getPreviousWaypoint();
246 curr = fp->getCurrentWaypoint();
247 next = fp->getNextWaypoint();
251 ///////////////////////////////////////////////////////////////////////////
252 // Initialize the flightplan
253 //////////////////////////////////////////////////////////////////////////
255 handleFirstWaypoint();
257 } // end of initialization
258 if (! fpExecutable(now))
262 if (! leadPointReached(curr)) {
263 controlHeading(curr);
264 controlSpeed(curr, next);
266 if (curr->finished) //end of the flight plan
276 //TODO more intelligent method in AIFlightPlan, no need to send data it already has :-)
277 tgt_heading = fp->getBearing(curr, next);
281 //TODO let the fp handle this (loading of next leg)
282 fp->IncrementWaypoint( trafficRef != 0 );
283 if (!(fp->getNextWaypoint()) && trafficRef != 0)
284 if (!loadNextLeg()) {
289 prev = fp->getPreviousWaypoint();
290 curr = fp->getCurrentWaypoint();
291 next = fp->getNextWaypoint();
293 // Now that we have incremented the waypoints, excute some traffic manager specific code
295 //TODO isn't this best executed right at the beginning?
296 if (! aiTrafficVisible()) {
301 if (! handleAirportEndPoints(prev, now)) {
306 announcePositionToController();
311 fp->setLeadDistance(tgt_speed, tgt_heading, curr, next);
314 if (!(prev->on_ground)) // only update the tgt altitude from flightplan if not on the ground
316 tgt_altitude_ft = prev->altitude;
317 if (curr->crossat > -1000.0) {
319 tgt_vs = (curr->crossat - altitude_ft) / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)
320 / 6076.0 / speed*60.0);
321 tgt_altitude_ft = curr->crossat;
326 tgt_speed = prev->speed;
327 hdg_lock = alt_lock = true;
328 no_roll = prev->on_ground;
333 void FGAIAircraft::initializeFlightPlan() {
337 bool FGAIAircraft::_getGearDown() const {
338 return _performance->gearExtensible(this);
342 const char * FGAIAircraft::_getTransponderCode() const {
343 return transponderCode.c_str();
347 bool FGAIAircraft::loadNextLeg() {
350 if ((leg = fp->getLeg()) == 10) {
351 if (!trafficRef->next()) {
354 setCallSign(trafficRef->getCallSign());
359 FGAirport *dep = trafficRef->getDepartureAirport();
360 FGAirport *arr = trafficRef->getArrivalAirport();
365 double cruiseAlt = trafficRef->getCruiseAlt() * 100;
372 trafficRef->getSpeed(),
376 trafficRef->getRadius(),
377 trafficRef->getFlightType(),
380 //cerr << "created leg " << leg << " for " << trafficRef->getCallSign() << endl;
386 // Note: This code is copied from David Luff's AILocalTraffic
387 // Warning - ground elev determination is CPU intensive
388 // Either this function or the logic of how often it is called
389 // will almost certainly change.
391 void FGAIAircraft::getGroundElev(double dt) {
394 // Update minimally every three secs, but add some randomness
395 // to prevent all AI objects doing this in synchrony
396 if (dt_elev_count < (3.0) + (rand() % 10))
401 // Only do the proper hitlist stuff if we are within visible range of the viewer.
403 double visibility_meters = fgGetDouble("/environment/visibility-m");
404 FGViewer* vw = globals->get_current_view();
406 if (SGGeodesy::distanceM(vw->getPosition(), pos) > visibility_meters) {
410 double range = 500.0;
411 if (!globals->get_tile_mgr()->scenery_available(pos, range)) {
412 // Try to shedule tiles for that position.
413 globals->get_tile_mgr()->update( pos, range );
417 if (getGroundElevationM(SGGeod::fromGeodM(pos, 20000), alt, 0))
418 tgt_altitude_ft = alt * SG_METER_TO_FEET;
423 void FGAIAircraft::doGroundAltitude() {
424 if (fabs(altitude_ft - (tgt_altitude_ft+groundOffset)) > 1000.0)
425 altitude_ft = (tgt_altitude_ft + groundOffset);
427 altitude_ft += 0.1 * ((tgt_altitude_ft+groundOffset) - altitude_ft);
431 void FGAIAircraft::announcePositionToController() {
433 int leg = fp->getLeg();
435 // Note that leg has been incremented after creating the current leg, so we should use
436 // leg numbers here that are one higher than the number that is used to create the leg
439 case 2: // Startup and Push back
440 if (trafficRef->getDepartureAirport()->getDynamics())
441 controller = trafficRef->getDepartureAirport()->getDynamics()->getStartupController();
443 case 3: // Taxiing to runway
444 if (trafficRef->getDepartureAirport()->getDynamics()->getGroundNetwork()->exists())
445 controller = trafficRef->getDepartureAirport()->getDynamics()->getGroundNetwork();
447 case 4: //Take off tower controller
448 if (trafficRef->getDepartureAirport()->getDynamics()) {
449 controller = trafficRef->getDepartureAirport()->getDynamics()->getTowerController();
451 cerr << "Error: Could not find Dynamics at airport : " << trafficRef->getDepartureAirport()->getId() << endl;
454 case 9: // Taxiing for parking
455 if (trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork()->exists())
456 controller = trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork();
463 if ((controller != prevController) && (prevController != 0)) {
464 prevController->signOff(getID());
466 prevController = controller;
468 controller->announcePosition(getID(), fp, fp->getCurrentWaypoint()->routeIndex,
469 _getLatitude(), _getLongitude(), hdg, speed, altitude_ft,
470 trafficRef->getRadius(), leg, this);
475 // Process ATC instructions and report back
477 void FGAIAircraft::processATC(FGATCInstruction instruction) {
478 if (instruction.getCheckForCircularWait()) {
479 // This is not exactly an elegant solution,
480 // but at least it gives me a chance to check
481 // if circular waits are resolved.
482 // For now, just take the offending aircraft
485 // a more proper way should be - of course - to
486 // let an offending aircraft take an evasive action
487 // for instance taxi back a little bit.
489 //cerr << "Processing ATC instruction (not Implimented yet)" << endl;
490 if (instruction.getHoldPattern ()) {}
493 if (instruction.getHoldPosition ()) {
501 // cerr << trafficRef->getCallSign() << " Resuming Taxi." << endl;
504 // Change speed Instruction. This can only be excecuted when there is no
505 // Hold position instruction.
506 if (instruction.getChangeSpeed ()) {
508 //cerr << trafficRef->getCallSign() << " Changing Speed " << endl;
509 AccelTo(instruction.getSpeed());
511 if (fp) AccelTo(fp->getPreviousWaypoint()->speed);
514 if (instruction.getChangeHeading ()) {
516 TurnTo(instruction.getHeading());
522 if (instruction.getChangeAltitude()) {}
527 void FGAIAircraft::handleFirstWaypoint() {
528 bool eraseWaypoints; //TODO YAGNI
531 eraseWaypoints = true;
533 eraseWaypoints = false;
536 FGAIFlightPlan::waypoint* prev = 0; // the one behind you
537 FGAIFlightPlan::waypoint* curr = 0; // the one ahead
538 FGAIFlightPlan::waypoint* next = 0;// the next plus 1
543 //TODO fp should handle this
544 fp->IncrementWaypoint(eraseWaypoints);
545 if (!(fp->getNextWaypoint()) && trafficRef)
546 if (!loadNextLeg()) {
551 prev = fp->getPreviousWaypoint(); //first waypoint
552 curr = fp->getCurrentWaypoint(); //second waypoint
553 next = fp->getNextWaypoint(); //third waypoint (might not exist!)
555 setLatitude(prev->latitude);
556 setLongitude(prev->longitude);
557 setSpeed(prev->speed);
558 setAltitude(prev->altitude);
560 if (prev->speed > 0.0)
561 setHeading(fp->getBearing(prev->latitude, prev->longitude, curr));
563 setHeading(fp->getBearing(curr->latitude, curr->longitude, prev));
565 // If next doesn't exist, as in incrementally created flightplans for
566 // AI/Trafficmanager created plans,
567 // Make sure lead distance is initialized otherwise
569 fp->setLeadDistance(speed, hdg, curr, next);
571 if (curr->crossat > -1000.0) //use a calculated descent/climb rate
574 tgt_vs = (curr->crossat - prev->altitude)
575 / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)
576 / 6076.0 / prev->speed*60.0);
577 tgt_altitude_ft = curr->crossat;
580 tgt_altitude_ft = prev->altitude;
582 alt_lock = hdg_lock = true;
583 no_roll = prev->on_ground;
585 Transform(); // make sure aip is initialized.
586 getGroundElev(60.1); // make sure it's executed first time around, so force a large dt value
589 // Make sure to announce the aircraft's position
590 announcePositionToController();
596 * Check Execution time (currently once every 100 ms)
597 * Add a bit of randomization to prevent the execution of all flight plans
598 * in synchrony, which can add significant periodic framerate flutter.
603 bool FGAIAircraft::fpExecutable(time_t now) {
604 double rand_exec_time = (rand() % 100) / 100;
605 return (dt_count > (0.1+rand_exec_time)) && (fp->isActive(now));
610 * Check to see if we've reached the lead point for our next turn
615 bool FGAIAircraft::leadPointReached(FGAIFlightPlan::waypoint* curr) {
616 double dist_to_go = fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr);
618 //cerr << "2" << endl;
619 double lead_dist = fp->getLeadDistance();
620 // experimental: Use fabs, because speed can be negative (I hope) during push_back.
622 if (lead_dist < fabs(2*speed)) {
623 //don't skip over the waypoint
624 lead_dist = fabs(2*speed);
625 //cerr << "Extending lead distance to " << lead_dist << endl;
628 //prev_dist_to_go = dist_to_go;
629 //if (dist_to_go < lead_dist)
630 // cerr << trafficRef->getCallSign() << " Distance : "
631 // << dist_to_go << ": Lead distance "
632 // << lead_dist << " " << curr->name
633 // << " Ground target speed " << groundTargetSpeed << endl;
635 return dist_to_go < lead_dist;
639 bool FGAIAircraft::aiTrafficVisible() {
640 SGGeod userPos(SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"),
641 fgGetDouble("/position/latitude-deg")));
643 return (SGGeodesy::distanceNm(userPos, pos) <= TRAFFICTOAIDISTTODIE);
648 * Handle release of parking gate, once were taxiing. Also ensure service time at the gate
649 * in the case of an arrival.
655 //TODO the trafficRef is the right place for the method
656 bool FGAIAircraft::handleAirportEndPoints(FGAIFlightPlan::waypoint* prev, time_t now) {
657 // prepare routing from one airport to another
658 FGAirport * dep = trafficRef->getDepartureAirport();
659 FGAirport * arr = trafficRef->getArrivalAirport();
664 // This waypoint marks the fact that the aircraft has passed the initial taxi
665 // departure waypoint, so it can release the parking.
666 //cerr << trafficRef->getCallSign() << " has passed waypoint " << prev->name << " at speed " << speed << endl;
667 if (prev->name == "PushBackPoint") {
668 dep->getDynamics()->releaseParking(fp->getGate());
669 time_t holdUntil = now + 120;
670 fp->setTime(holdUntil);
671 //cerr << _getCallsign() << "Holding at pushback point" << endl;
674 // This is the last taxi waypoint, and marks the the end of the flight plan
675 // so, the schedule should update and wait for the next departure time.
676 if (prev->name == "END") {
677 time_t nextDeparture = trafficRef->getDepartureTime();
678 // make sure to wait at least 20 minutes at parking to prevent "nervous" taxi behavior
679 if (nextDeparture < (now+1200)) {
680 nextDeparture = now + 1200;
682 fp->setTime(nextDeparture); // should be "next departure"
690 * Check difference between target bearing and current heading and correct if necessary.
694 void FGAIAircraft::controlHeading(FGAIFlightPlan::waypoint* curr) {
695 double calc_bearing = fp->getBearing(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr);
696 //cerr << "Bearing = " << calc_bearing << endl;
699 if (calc_bearing > 360)
703 if (finite(calc_bearing)) {
704 double hdg_error = calc_bearing - tgt_heading;
705 if (fabs(hdg_error) > 0.01) {
706 TurnTo( calc_bearing );
710 cerr << "calc_bearing is not a finite number : "
712 << "pos : " << pos.getLatitudeDeg() << ", " << pos.getLongitudeDeg()
713 << "waypoint " << curr->latitude << ", " << curr->longitude << endl;
714 cerr << "waypoint name " << curr->name;
721 * Update the lead distance calculation if speed has changed sufficiently
722 * to prevent spinning (hopefully);
727 void FGAIAircraft::controlSpeed(FGAIFlightPlan::waypoint* curr, FGAIFlightPlan::waypoint* next) {
728 double speed_diff = speed - prevSpeed;
730 if (fabs(speed_diff) > 10) {
733 fp->setLeadDistance(speed, tgt_heading, curr, next);
740 * Update target values (heading, alt, speed) depending on flight plan or control properties
742 void FGAIAircraft::updatePrimaryTargetValues() {
743 if (fp) // AI object has a flightplan
745 //TODO make this a function of AIBase
746 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
747 //cerr << "UpateTArgetValues() " << endl;
748 ProcessFlightPlan(dt, now);
750 // Do execute Ground elev for inactive aircraft, so they
751 // Are repositioned to the correct ground altitude when the user flies within visibility range.
752 // In addition, check whether we are out of user range, so this aircraft
755 Transform(); // make sure aip is initialized.
759 pos.setElevationFt(altitude_ft);
762 //cerr << trafficRef->getRegistration() << " Setting altitude to " << altitude_ft;
763 if (! aiTrafficVisible()) {
765 //cerr << trafficRef->getRegistration() << " is set to die " << endl;
766 throw AI_OutOfSight();
769 timeElapsed = now - fp->getStartTime();
770 if (! fp->isActive(now)) {
774 // no flight plan, update target heading, speed, and altitude
775 // from control properties. These default to the initial
776 // settings in the config file, but can be changed "on the
778 string lat_mode = props->getStringValue("controls/flight/lateral-mode");
779 if ( lat_mode == "roll" ) {
781 = props->getDoubleValue("controls/flight/target-roll" );
785 = props->getDoubleValue("controls/flight/target-hdg" );
790 = props->getStringValue("controls/flight/longitude-mode");
791 if ( lon_mode == "alt" ) {
792 double alt = props->getDoubleValue("controls/flight/target-alt" );
796 = props->getDoubleValue("controls/flight/target-pitch" );
800 AccelTo( props->getDoubleValue("controls/flight/target-spd" ) );
804 void FGAIAircraft::updatePosition() {
805 // convert speed to degrees per second
806 double speed_north_deg_sec = cos( hdg * SGD_DEGREES_TO_RADIANS )
807 * speed * 1.686 / ft_per_deg_lat;
808 double speed_east_deg_sec = sin( hdg * SGD_DEGREES_TO_RADIANS )
809 * speed * 1.686 / ft_per_deg_lon;
812 pos.setLatitudeDeg( pos.getLatitudeDeg() + speed_north_deg_sec * dt);
813 pos.setLongitudeDeg( pos.getLongitudeDeg() + speed_east_deg_sec * dt);
817 void FGAIAircraft::updateHeading() {
818 // adjust heading based on current bank angle
823 // double turnConstant;
825 // turnConstant = 0.0088362;
827 // turnConstant = 0.088362;
828 // If on ground, calculate heading change directly
830 double headingDiff = fabs(hdg-tgt_heading);
831 double bank_sense = 0.0;
833 double diff = fabs(hdg - tgt_heading);
835 diff = fabs(diff - 360);
837 double sum = hdg + diff;
840 if (fabs(sum - tgt_heading) < 1.0) {
841 bank_sense = 1.0; // right turn
843 bank_sense = -1.0; // left turn
845 if (headingDiff > 180)
846 headingDiff = fabs(headingDiff - 360);
847 double sum = hdg + headingDiff;
850 if (fabs(sum - tgt_heading) > 0.0001) {
856 //cerr << trafficRef->getCallSign() << " Heading "
857 // << hdg << ". Target " << tgt_heading << ". Diff " << fabs(sum - tgt_heading) << ". Speed " << speed << endl;
858 //if (headingDiff > 60) {
859 groundTargetSpeed = tgt_speed; // * cos(headingDiff * SG_DEGREES_TO_RADIANS);
860 //groundTargetSpeed = tgt_speed - tgt_speed * (headingDiff/180);
862 // groundTargetSpeed = tgt_speed;
864 if (sign(groundTargetSpeed) != sign(tgt_speed))
865 groundTargetSpeed = 0.21 * sign(tgt_speed); // to prevent speed getting stuck in 'negative' mode
867 if (headingDiff > 30.0) {
868 // invert if pushed backward
869 headingChangeRate += 10.0 * dt * sign(roll);
871 // Clamp the maximum steering rate to 30 degrees per second,
872 // But only do this when the heading error is decreasing.
873 if ((headingDiff < headingError)) {
874 if (headingChangeRate > 30)
875 headingChangeRate = 30;
876 else if (headingChangeRate < -30)
877 headingChangeRate = -30;
880 if (fabs(headingChangeRate) > headingDiff)
881 headingChangeRate = headingDiff*sign(roll);
883 headingChangeRate += dt * sign(roll);
886 hdg += headingChangeRate * dt * (fabs(speed) / 15);
887 headingError = headingDiff;
889 if (fabs(speed) > 1.0) {
890 turn_radius_ft = 0.088362 * speed * speed
891 / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
893 // Check if turn_radius_ft == 0; this might lead to a division by 0.
894 turn_radius_ft = 1.0;
896 double turn_circum_ft = SGD_2PI * turn_radius_ft;
897 double dist_covered_ft = speed * 1.686 * dt;
898 double alpha = dist_covered_ft / turn_circum_ft * 360.0;
899 hdg += alpha * sign(roll);
901 while ( hdg > 360.0 ) {
913 void FGAIAircraft::updateBankAngleTarget() {
914 // adjust target bank angle if heading lock engaged
916 double bank_sense = 0.0;
917 double diff = fabs(hdg - tgt_heading);
919 diff = fabs(diff - 360);
921 double sum = hdg + diff;
924 if (fabs(sum - tgt_heading) < 1.0) {
925 bank_sense = 1.0; // right turn
927 bank_sense = -1.0; // left turn
929 if (diff < _performance->maximumBankAngle()) {
930 tgt_roll = diff * bank_sense;
932 tgt_roll = _performance->maximumBankAngle() * bank_sense;
934 if ((fabs((double) spinCounter) > 1) && (diff > _performance->maximumBankAngle())) {
935 tgt_speed *= 0.999; // Ugly hack: If aircraft get stuck, they will continually spin around.
936 // The only way to resolve this is to make them slow down.
942 void FGAIAircraft::updateVerticalSpeedTarget() {
943 // adjust target Altitude, based on ground elevation when on ground
947 } else if (alt_lock) {
948 // find target vertical speed
950 if (altitude_ft < tgt_altitude_ft) {
951 tgt_vs = tgt_altitude_ft - altitude_ft;
952 if (tgt_vs > _performance->climbRate())
953 tgt_vs = _performance->climbRate();
955 tgt_vs = tgt_altitude_ft - altitude_ft;
956 if (tgt_vs < (-_performance->descentRate()))
957 tgt_vs = -_performance->descentRate();
960 double max_vs = 4*(tgt_altitude_ft - altitude_ft);
962 if (tgt_altitude_ft < altitude_ft)
964 if ((fabs(tgt_altitude_ft - altitude_ft) < 1500.0)
965 && (fabs(max_vs) < fabs(tgt_vs)))
968 if (fabs(tgt_vs) < fabs(min_vs))
975 void FGAIAircraft::updatePitchAngleTarget() {
976 // if on ground and above vRotate -> initial rotation
977 if (onGround() && (speed > _performance->vRotate()))
978 tgt_pitch = 8.0; // some rough B737 value
980 //TODO pitch angle on approach and landing
982 // match pitch angle to vertical speed
983 else if (tgt_vs > 0) {
984 tgt_pitch = tgt_vs * 0.005;
986 tgt_pitch = tgt_vs * 0.002;
990 string FGAIAircraft::atGate() {
992 if (fp->getLeg() < 3) {
994 if (fp->getGate() > 0) {
996 trafficRef->getDepartureAirport()->getDynamics()->getParking(fp->getGate());
997 tmp = park->getName();
1004 void FGAIAircraft::handleATCRequests() {
1005 //TODO implement NullController for having no ATC to save the conditionals
1007 controller->update(getID(),
1008 pos.getLatitudeDeg(),
1009 pos.getLongitudeDeg(),
1013 processATC(controller->getInstruction(getID()));
1017 void FGAIAircraft::updateActualState() {
1018 //update current state
1019 //TODO have a single tgt_speed and check speed limit on ground on setting tgt_speed
1023 speed = _performance->actualSpeed(this, groundTargetSpeed, dt);
1025 speed = _performance->actualSpeed(this, tgt_speed, dt);
1028 roll = _performance->actualBankAngle(this, tgt_roll, dt);
1030 // adjust altitude (meters) based on current vertical speed (fpm)
1031 altitude_ft += vs / 60.0 * dt;
1032 pos.setElevationFt(altitude_ft);
1034 vs = _performance->actualVerticalSpeed(this, tgt_vs, dt);
1035 pitch = _performance->actualPitch(this, tgt_pitch, dt);
1038 void FGAIAircraft::updateSecondaryTargetValues() {
1039 // derived target state values
1040 updateBankAngleTarget();
1041 updateVerticalSpeedTarget();
1042 updatePitchAngleTarget();
1044 //TODO calculate wind correction angle (tgt_yaw)