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 FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) {
57 groundOffset = trafficRef->getGroundOffset();
58 setCallSign(trafficRef->getCallSign());
73 groundTargetSpeed = 0;
75 // set heading and altitude locks
79 headingChangeRate = 0.0;
84 _performance = 0; //TODO initialize to JET_TRANSPORT from PerformanceDB
89 FGAIAircraft::~FGAIAircraft() {
92 controller->signOff(getID());
96 void FGAIAircraft::readFromScenario(SGPropertyNode* scFileNode) {
100 FGAIBase::readFromScenario(scFileNode);
102 setPerformance(scFileNode->getStringValue("class", "jet_transport"));
103 setFlightPlan(scFileNode->getStringValue("flightplan"),
104 scFileNode->getBoolValue("repeat", false));
105 setCallSign(scFileNode->getStringValue("callsign"));
109 void FGAIAircraft::bind() {
112 props->tie("controls/gear/gear-down",
113 SGRawValueMethods<FGAIAircraft,bool>(*this,
114 &FGAIAircraft::_getGearDown));
115 props->tie("transponder-id",
116 SGRawValueMethods<FGAIAircraft,const char*>(*this,
117 &FGAIAircraft::_getTransponderCode));
121 void FGAIAircraft::unbind() {
124 props->untie("controls/gear/gear-down");
125 props->untie("transponder-id");
129 void FGAIAircraft::update(double dt) {
130 FGAIBase::update(dt);
135 void FGAIAircraft::setPerformance(const std::string& acclass) {
136 static PerformanceDB perfdb; //TODO make it a global service
137 setPerformance(perfdb.getDataFor(acclass));
141 void FGAIAircraft::setPerformance(PerformanceData *ps) {
146 void FGAIAircraft::Run(double dt) {
147 FGAIAircraft::dt = dt;
149 bool outOfSight = false,
150 flightplanActive = true;
151 updatePrimaryTargetValues(flightplanActive, outOfSight); // target hdg, alt, speed
156 if (!flightplanActive) {
157 groundTargetSpeed = 0;
160 handleATCRequests(); // ATC also has a word to say
161 updateSecondaryTargetValues(); // target roll, vertical speed, pitch
163 UpdateRadar(manager);
167 void FGAIAircraft::checkVisibility()
169 double visibility_meters = fgGetDouble("/environment/visibility-m");
170 FGViewer* vw = globals->get_current_view();
171 invisible = (SGGeodesy::distanceM(vw->getPosition(), pos) > visibility_meters);
176 void FGAIAircraft::AccelTo(double speed) {
181 void FGAIAircraft::PitchTo(double angle) {
187 void FGAIAircraft::RollTo(double angle) {
193 void FGAIAircraft::YawTo(double angle) {
198 void FGAIAircraft::ClimbTo(double alt_ft ) {
199 tgt_altitude_ft = alt_ft;
204 void FGAIAircraft::TurnTo(double heading) {
205 tgt_heading = heading;
210 double FGAIAircraft::sign(double x) {
218 void FGAIAircraft::setFlightPlan(const std::string& flightplan, bool repeat) {
219 if (!flightplan.empty()) {
220 FGAIFlightPlan* fp = new FGAIFlightPlan(flightplan);
221 fp->setRepeat(repeat);
227 void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f) {
233 void FGAIAircraft::ProcessFlightPlan( double dt, time_t now ) {
235 // the one behind you
236 FGAIFlightPlan::waypoint* prev = 0;
238 FGAIFlightPlan::waypoint* curr = 0;
240 FGAIFlightPlan::waypoint* next = 0;
242 prev = fp->getPreviousWaypoint();
243 curr = fp->getCurrentWaypoint();
244 next = fp->getNextWaypoint();
248 ///////////////////////////////////////////////////////////////////////////
249 // Initialize the flightplan
250 //////////////////////////////////////////////////////////////////////////
252 handleFirstWaypoint();
254 } // end of initialization
255 if (! fpExecutable(now))
259 if (! leadPointReached(curr)) {
260 controlHeading(curr);
261 controlSpeed(curr, next);
263 if (curr->finished) //end of the flight plan
273 //TODO more intelligent method in AIFlightPlan, no need to send data it already has :-)
274 tgt_heading = fp->getBearing(curr, next);
278 //TODO let the fp handle this (loading of next leg)
279 fp->IncrementWaypoint( trafficRef != 0 );
280 if (!(fp->getNextWaypoint()) && trafficRef != 0)
281 if (!loadNextLeg()) {
286 prev = fp->getPreviousWaypoint();
287 curr = fp->getCurrentWaypoint();
288 next = fp->getNextWaypoint();
290 // Now that we have incremented the waypoints, excute some traffic manager specific code
292 //TODO isn't this best executed right at the beginning?
293 if (! aiTrafficVisible()) {
298 if (! handleAirportEndPoints(prev, now)) {
303 announcePositionToController();
308 fp->setLeadDistance(tgt_speed, tgt_heading, curr, next);
311 if (!(prev->on_ground)) // only update the tgt altitude from flightplan if not on the ground
313 tgt_altitude_ft = prev->altitude;
314 if (curr->crossat > -1000.0) {
316 tgt_vs = (curr->crossat - altitude_ft) / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)
317 / 6076.0 / speed*60.0);
318 tgt_altitude_ft = curr->crossat;
323 tgt_speed = prev->speed;
324 hdg_lock = alt_lock = true;
325 no_roll = prev->on_ground;
330 void FGAIAircraft::initializeFlightPlan() {
334 bool FGAIAircraft::_getGearDown() const {
335 return _performance->gearExtensible(this);
339 const char * FGAIAircraft::_getTransponderCode() const {
340 return transponderCode.c_str();
344 bool FGAIAircraft::loadNextLeg() {
347 if ((leg = fp->getLeg()) == 10) {
348 if (!trafficRef->next()) {
351 setCallSign(trafficRef->getCallSign());
356 FGAirport *dep = trafficRef->getDepartureAirport();
357 FGAirport *arr = trafficRef->getArrivalAirport();
362 double cruiseAlt = trafficRef->getCruiseAlt() * 100;
369 trafficRef->getSpeed(),
373 trafficRef->getRadius(),
374 trafficRef->getFlightType(),
377 //cerr << "created leg " << leg << " for " << trafficRef->getCallSign() << endl;
383 // Note: This code is copied from David Luff's AILocalTraffic
384 // Warning - ground elev determination is CPU intensive
385 // Either this function or the logic of how often it is called
386 // will almost certainly change.
388 void FGAIAircraft::getGroundElev(double dt) {
391 // Update minimally every three secs, but add some randomness
392 // to prevent all AI objects doing this in synchrony
393 if (dt_elev_count < (3.0) + (rand() % 10))
398 // Only do the proper hitlist stuff if we are within visible range of the viewer.
400 double visibility_meters = fgGetDouble("/environment/visibility-m");
401 FGViewer* vw = globals->get_current_view();
403 if (SGGeodesy::distanceM(vw->getPosition(), pos) > visibility_meters) {
407 double range = 500.0;
408 if (!globals->get_tile_mgr()->scenery_available(pos, range)) {
409 // Try to shedule tiles for that position.
410 globals->get_tile_mgr()->update( pos, range );
414 if (getGroundElevationM(SGGeod::fromGeodM(pos, 20000), alt, 0))
415 tgt_altitude_ft = alt * SG_METER_TO_FEET;
420 void FGAIAircraft::doGroundAltitude() {
421 if (fabs(altitude_ft - (tgt_altitude_ft+groundOffset)) > 1000.0)
422 altitude_ft = (tgt_altitude_ft + groundOffset);
424 altitude_ft += 0.1 * ((tgt_altitude_ft+groundOffset) - altitude_ft);
428 void FGAIAircraft::announcePositionToController() {
430 int leg = fp->getLeg();
432 // Note that leg has been incremented after creating the current leg, so we should use
433 // leg numbers here that are one higher than the number that is used to create the leg
436 case 2: // Startup and Push back
437 if (trafficRef->getDepartureAirport()->getDynamics())
438 controller = trafficRef->getDepartureAirport()->getDynamics()->getStartupController();
440 case 3: // Taxiing to runway
441 if (trafficRef->getDepartureAirport()->getDynamics()->getGroundNetwork()->exists())
442 controller = trafficRef->getDepartureAirport()->getDynamics()->getGroundNetwork();
444 case 4: //Take off tower controller
445 if (trafficRef->getDepartureAirport()->getDynamics()) {
446 controller = trafficRef->getDepartureAirport()->getDynamics()->getTowerController();
448 cerr << "Error: Could not find Dynamics at airport : " << trafficRef->getDepartureAirport()->getId() << endl;
451 case 9: // Taxiing for parking
452 if (trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork()->exists())
453 controller = trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork();
460 if ((controller != prevController) && (prevController != 0)) {
461 prevController->signOff(getID());
463 prevController = controller;
465 controller->announcePosition(getID(), fp, fp->getCurrentWaypoint()->routeIndex,
466 _getLatitude(), _getLongitude(), hdg, speed, altitude_ft,
467 trafficRef->getRadius(), leg, this);
472 // Process ATC instructions and report back
474 void FGAIAircraft::processATC(FGATCInstruction instruction) {
475 if (instruction.getCheckForCircularWait()) {
476 // This is not exactly an elegant solution,
477 // but at least it gives me a chance to check
478 // if circular waits are resolved.
479 // For now, just take the offending aircraft
482 // a more proper way should be - of course - to
483 // let an offending aircraft take an evasive action
484 // for instance taxi back a little bit.
486 //cerr << "Processing ATC instruction (not Implimented yet)" << endl;
487 if (instruction.getHoldPattern ()) {}
490 if (instruction.getHoldPosition ()) {
498 // cerr << trafficRef->getCallSign() << " Resuming Taxi." << endl;
501 // Change speed Instruction. This can only be excecuted when there is no
502 // Hold position instruction.
503 if (instruction.getChangeSpeed ()) {
505 //cerr << trafficRef->getCallSign() << " Changing Speed " << endl;
506 AccelTo(instruction.getSpeed());
508 if (fp) AccelTo(fp->getPreviousWaypoint()->speed);
511 if (instruction.getChangeHeading ()) {
513 TurnTo(instruction.getHeading());
519 if (instruction.getChangeAltitude()) {}
524 void FGAIAircraft::handleFirstWaypoint() {
525 bool eraseWaypoints; //TODO YAGNI
528 eraseWaypoints = true;
530 eraseWaypoints = false;
533 FGAIFlightPlan::waypoint* prev = 0; // the one behind you
534 FGAIFlightPlan::waypoint* curr = 0; // the one ahead
535 FGAIFlightPlan::waypoint* next = 0;// the next plus 1
540 //TODO fp should handle this
541 fp->IncrementWaypoint(eraseWaypoints);
542 if (!(fp->getNextWaypoint()) && trafficRef)
543 if (!loadNextLeg()) {
548 prev = fp->getPreviousWaypoint(); //first waypoint
549 curr = fp->getCurrentWaypoint(); //second waypoint
550 next = fp->getNextWaypoint(); //third waypoint (might not exist!)
552 setLatitude(prev->latitude);
553 setLongitude(prev->longitude);
554 setSpeed(prev->speed);
555 setAltitude(prev->altitude);
557 if (prev->speed > 0.0)
558 setHeading(fp->getBearing(prev->latitude, prev->longitude, curr));
560 setHeading(fp->getBearing(curr->latitude, curr->longitude, prev));
562 // If next doesn't exist, as in incrementally created flightplans for
563 // AI/Trafficmanager created plans,
564 // Make sure lead distance is initialized otherwise
566 fp->setLeadDistance(speed, hdg, curr, next);
568 if (curr->crossat > -1000.0) //use a calculated descent/climb rate
571 tgt_vs = (curr->crossat - prev->altitude)
572 / (fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr)
573 / 6076.0 / prev->speed*60.0);
574 tgt_altitude_ft = curr->crossat;
577 tgt_altitude_ft = prev->altitude;
579 alt_lock = hdg_lock = true;
580 no_roll = prev->on_ground;
582 Transform(); // make sure aip is initialized.
583 getGroundElev(60.1); // make sure it's executed first time around, so force a large dt value
586 // Make sure to announce the aircraft's position
587 announcePositionToController();
593 * Check Execution time (currently once every 100 ms)
594 * Add a bit of randomization to prevent the execution of all flight plans
595 * in synchrony, which can add significant periodic framerate flutter.
600 bool FGAIAircraft::fpExecutable(time_t now) {
601 double rand_exec_time = (rand() % 100) / 100;
602 return (dt_count > (0.1+rand_exec_time)) && (fp->isActive(now));
607 * Check to see if we've reached the lead point for our next turn
612 bool FGAIAircraft::leadPointReached(FGAIFlightPlan::waypoint* curr) {
613 double dist_to_go = fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr);
615 //cerr << "2" << endl;
616 double lead_dist = fp->getLeadDistance();
617 // experimental: Use fabs, because speed can be negative (I hope) during push_back.
619 if (lead_dist < fabs(2*speed)) {
620 //don't skip over the waypoint
621 lead_dist = fabs(2*speed);
622 //cerr << "Extending lead distance to " << lead_dist << endl;
625 //prev_dist_to_go = dist_to_go;
626 //if (dist_to_go < lead_dist)
627 // cerr << trafficRef->getCallSign() << " Distance : "
628 // << dist_to_go << ": Lead distance "
629 // << lead_dist << " " << curr->name
630 // << " Ground target speed " << groundTargetSpeed << endl;
632 return dist_to_go < lead_dist;
636 bool FGAIAircraft::aiTrafficVisible() {
637 SGGeod userPos(SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"),
638 fgGetDouble("/position/latitude-deg")));
640 return (SGGeodesy::distanceNm(userPos, pos) <= TRAFFICTOAIDISTTODIE);
645 * Handle release of parking gate, once were taxiing. Also ensure service time at the gate
646 * in the case of an arrival.
652 //TODO the trafficRef is the right place for the method
653 bool FGAIAircraft::handleAirportEndPoints(FGAIFlightPlan::waypoint* prev, time_t now) {
654 // prepare routing from one airport to another
655 FGAirport * dep = trafficRef->getDepartureAirport();
656 FGAirport * arr = trafficRef->getArrivalAirport();
661 // This waypoint marks the fact that the aircraft has passed the initial taxi
662 // departure waypoint, so it can release the parking.
663 //cerr << trafficRef->getCallSign() << " has passed waypoint " << prev->name << " at speed " << speed << endl;
664 if (prev->name == "PushBackPoint") {
665 dep->getDynamics()->releaseParking(fp->getGate());
666 time_t holdUntil = now + 120;
667 fp->setTime(holdUntil);
668 //cerr << _getCallsign() << "Holding at pushback point" << endl;
671 // This is the last taxi waypoint, and marks the the end of the flight plan
672 // so, the schedule should update and wait for the next departure time.
673 if (prev->name == "END") {
674 time_t nextDeparture = trafficRef->getDepartureTime();
675 // make sure to wait at least 20 minutes at parking to prevent "nervous" taxi behavior
676 if (nextDeparture < (now+1200)) {
677 nextDeparture = now + 1200;
679 fp->setTime(nextDeparture); // should be "next departure"
687 * Check difference between target bearing and current heading and correct if necessary.
691 void FGAIAircraft::controlHeading(FGAIFlightPlan::waypoint* curr) {
692 double calc_bearing = fp->getBearing(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr);
693 //cerr << "Bearing = " << calc_bearing << endl;
696 if (calc_bearing > 360)
700 if (finite(calc_bearing)) {
701 double hdg_error = calc_bearing - tgt_heading;
702 if (fabs(hdg_error) > 0.01) {
703 TurnTo( calc_bearing );
707 cerr << "calc_bearing is not a finite number : "
709 << "pos : " << pos.getLatitudeDeg() << ", " << pos.getLongitudeDeg()
710 << "waypoint " << curr->latitude << ", " << curr->longitude << endl;
711 cerr << "waypoint name " << curr->name;
718 * Update the lead distance calculation if speed has changed sufficiently
719 * to prevent spinning (hopefully);
724 void FGAIAircraft::controlSpeed(FGAIFlightPlan::waypoint* curr, FGAIFlightPlan::waypoint* next) {
725 double speed_diff = speed - prevSpeed;
727 if (fabs(speed_diff) > 10) {
730 fp->setLeadDistance(speed, tgt_heading, curr, next);
737 * Update target values (heading, alt, speed) depending on flight plan or control properties
739 void FGAIAircraft::updatePrimaryTargetValues(bool& flightplanActive, bool& aiOutOfSight) {
740 if (fp) // AI object has a flightplan
742 //TODO make this a function of AIBase
743 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
744 //cerr << "UpateTArgetValues() " << endl;
745 ProcessFlightPlan(dt, now);
747 // Do execute Ground elev for inactive aircraft, so they
748 // Are repositioned to the correct ground altitude when the user flies within visibility range.
749 // In addition, check whether we are out of user range, so this aircraft
752 Transform(); // make sure aip is initialized.
756 pos.setElevationFt(altitude_ft);
759 //cerr << trafficRef->getRegistration() << " Setting altitude to " << altitude_ft;
760 aiOutOfSight = !aiTrafficVisible();
763 //cerr << trafficRef->getRegistration() << " is set to die " << endl;
768 timeElapsed = now - fp->getStartTime();
769 flightplanActive = fp->isActive(now);
771 // no flight plan, update target heading, speed, and altitude
772 // from control properties. These default to the initial
773 // settings in the config file, but can be changed "on the
775 string lat_mode = props->getStringValue("controls/flight/lateral-mode");
776 if ( lat_mode == "roll" ) {
778 = props->getDoubleValue("controls/flight/target-roll" );
782 = props->getDoubleValue("controls/flight/target-hdg" );
787 = props->getStringValue("controls/flight/longitude-mode");
788 if ( lon_mode == "alt" ) {
789 double alt = props->getDoubleValue("controls/flight/target-alt" );
793 = props->getDoubleValue("controls/flight/target-pitch" );
797 AccelTo( props->getDoubleValue("controls/flight/target-spd" ) );
801 void FGAIAircraft::updatePosition() {
802 // convert speed to degrees per second
803 double speed_north_deg_sec = cos( hdg * SGD_DEGREES_TO_RADIANS )
804 * speed * 1.686 / ft_per_deg_lat;
805 double speed_east_deg_sec = sin( hdg * SGD_DEGREES_TO_RADIANS )
806 * speed * 1.686 / ft_per_deg_lon;
809 pos.setLatitudeDeg( pos.getLatitudeDeg() + speed_north_deg_sec * dt);
810 pos.setLongitudeDeg( pos.getLongitudeDeg() + speed_east_deg_sec * dt);
814 void FGAIAircraft::updateHeading() {
815 // adjust heading based on current bank angle
820 // double turnConstant;
822 // turnConstant = 0.0088362;
824 // turnConstant = 0.088362;
825 // If on ground, calculate heading change directly
827 double headingDiff = fabs(hdg-tgt_heading);
828 double bank_sense = 0.0;
830 double diff = fabs(hdg - tgt_heading);
832 diff = fabs(diff - 360);
834 double sum = hdg + diff;
837 if (fabs(sum - tgt_heading) < 1.0) {
838 bank_sense = 1.0; // right turn
840 bank_sense = -1.0; // left turn
842 if (headingDiff > 180)
843 headingDiff = fabs(headingDiff - 360);
844 double sum = hdg + headingDiff;
847 if (fabs(sum - tgt_heading) > 0.0001) {
853 //cerr << trafficRef->getCallSign() << " Heading "
854 // << hdg << ". Target " << tgt_heading << ". Diff " << fabs(sum - tgt_heading) << ". Speed " << speed << endl;
855 //if (headingDiff > 60) {
856 groundTargetSpeed = tgt_speed; // * cos(headingDiff * SG_DEGREES_TO_RADIANS);
857 //groundTargetSpeed = tgt_speed - tgt_speed * (headingDiff/180);
859 // groundTargetSpeed = tgt_speed;
861 if (sign(groundTargetSpeed) != sign(tgt_speed))
862 groundTargetSpeed = 0.21 * sign(tgt_speed); // to prevent speed getting stuck in 'negative' mode
864 if (headingDiff > 30.0) {
865 // invert if pushed backward
866 headingChangeRate += 10.0 * dt * sign(roll);
868 // Clamp the maximum steering rate to 30 degrees per second,
869 // But only do this when the heading error is decreasing.
870 if ((headingDiff < headingError)) {
871 if (headingChangeRate > 30)
872 headingChangeRate = 30;
873 else if (headingChangeRate < -30)
874 headingChangeRate = -30;
877 if (fabs(headingChangeRate) > headingDiff)
878 headingChangeRate = headingDiff*sign(roll);
880 headingChangeRate += dt * sign(roll);
883 hdg += headingChangeRate * dt * (fabs(speed) / 15);
884 headingError = headingDiff;
886 if (fabs(speed) > 1.0) {
887 turn_radius_ft = 0.088362 * speed * speed
888 / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
890 // Check if turn_radius_ft == 0; this might lead to a division by 0.
891 turn_radius_ft = 1.0;
893 double turn_circum_ft = SGD_2PI * turn_radius_ft;
894 double dist_covered_ft = speed * 1.686 * dt;
895 double alpha = dist_covered_ft / turn_circum_ft * 360.0;
896 hdg += alpha * sign(roll);
898 while ( hdg > 360.0 ) {
910 void FGAIAircraft::updateBankAngleTarget() {
911 // adjust target bank angle if heading lock engaged
913 double bank_sense = 0.0;
914 double diff = fabs(hdg - tgt_heading);
916 diff = fabs(diff - 360);
918 double sum = hdg + diff;
921 if (fabs(sum - tgt_heading) < 1.0) {
922 bank_sense = 1.0; // right turn
924 bank_sense = -1.0; // left turn
926 if (diff < _performance->maximumBankAngle()) {
927 tgt_roll = diff * bank_sense;
929 tgt_roll = _performance->maximumBankAngle() * bank_sense;
931 if ((fabs((double) spinCounter) > 1) && (diff > _performance->maximumBankAngle())) {
932 tgt_speed *= 0.999; // Ugly hack: If aircraft get stuck, they will continually spin around.
933 // The only way to resolve this is to make them slow down.
939 void FGAIAircraft::updateVerticalSpeedTarget() {
940 // adjust target Altitude, based on ground elevation when on ground
944 } else if (alt_lock) {
945 // find target vertical speed
947 if (altitude_ft < tgt_altitude_ft) {
948 tgt_vs = tgt_altitude_ft - altitude_ft;
949 if (tgt_vs > _performance->climbRate())
950 tgt_vs = _performance->climbRate();
952 tgt_vs = tgt_altitude_ft - altitude_ft;
953 if (tgt_vs < (-_performance->descentRate()))
954 tgt_vs = -_performance->descentRate();
957 double max_vs = 4*(tgt_altitude_ft - altitude_ft);
959 if (tgt_altitude_ft < altitude_ft)
961 if ((fabs(tgt_altitude_ft - altitude_ft) < 1500.0)
962 && (fabs(max_vs) < fabs(tgt_vs)))
965 if (fabs(tgt_vs) < fabs(min_vs))
972 void FGAIAircraft::updatePitchAngleTarget() {
973 // if on ground and above vRotate -> initial rotation
974 if (onGround() && (speed > _performance->vRotate()))
975 tgt_pitch = 8.0; // some rough B737 value
977 //TODO pitch angle on approach and landing
979 // match pitch angle to vertical speed
980 else if (tgt_vs > 0) {
981 tgt_pitch = tgt_vs * 0.005;
983 tgt_pitch = tgt_vs * 0.002;
987 string FGAIAircraft::atGate() {
989 if (fp->getLeg() < 3) {
991 if (fp->getGate() > 0) {
993 trafficRef->getDepartureAirport()->getDynamics()->getParking(fp->getGate());
994 tmp = park->getName();
1001 void FGAIAircraft::handleATCRequests() {
1002 //TODO implement NullController for having no ATC to save the conditionals
1004 controller->update(getID(),
1005 pos.getLatitudeDeg(),
1006 pos.getLongitudeDeg(),
1010 processATC(controller->getInstruction(getID()));
1014 void FGAIAircraft::updateActualState() {
1015 //update current state
1016 //TODO have a single tgt_speed and check speed limit on ground on setting tgt_speed
1020 speed = _performance->actualSpeed(this, groundTargetSpeed, dt);
1022 speed = _performance->actualSpeed(this, tgt_speed, dt);
1025 roll = _performance->actualBankAngle(this, tgt_roll, dt);
1027 // adjust altitude (meters) based on current vertical speed (fpm)
1028 altitude_ft += vs / 60.0 * dt;
1029 pos.setElevationFt(altitude_ft);
1031 vs = _performance->actualVerticalSpeed(this, tgt_vs, dt);
1032 pitch = _performance->actualPitch(this, tgt_pitch, dt);
1035 void FGAIAircraft::updateSecondaryTargetValues() {
1036 // derived target state values
1037 updateBankAngleTarget();
1038 updateVerticalSpeedTarget();
1039 updatePitchAngleTarget();
1041 //TODO calculate wind correction angle (tgt_yaw)