1 // newauto.cxx -- autopilot defines and prototypes (very alpha)
3 // Started April 1998 Copyright (C) 1998
5 // Contributions by Jeff Goeke-Smith <jgoeke@voyager.net>
6 // Norman Vine <nhv@cape.com>
7 // Curtis Olson <curt@flightgear.org>
8 // Wendell Turner <wendell@adsi-m4.com>
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <stdio.h> // sprintf()
32 #include <string.h> // strcmp()
34 #include <simgear/constants.h>
35 #include <simgear/sg_inlines.h>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
38 #include <simgear/math/sg_random.h>
39 #include <simgear/route/route.hxx>
41 #include <Cockpit/radiostack.hxx>
42 #include <Controls/controls.hxx>
43 #include <FDM/flight.hxx>
44 #include <Main/globals.hxx>
45 #include <Scenery/scenery.hxx>
47 #include "newauto.hxx"
48 #include "auto_gui.hxx"
51 /// These statics will eventually go into the class
52 /// they are just here while I am experimenting -- NHV :-)
53 // AutoPilot Gain Adjuster members
54 static double MaxRollAdjust; // MaxRollAdjust = 2 * APData->MaxRoll;
55 static double RollOutAdjust; // RollOutAdjust = 2 * APData->RollOut;
56 static double MaxAileronAdjust; // MaxAileronAdjust = 2 * APData->MaxAileron;
57 static double RollOutSmoothAdjust; // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
59 static char NewTgtAirportId[16];
60 // static char NewTgtAirportLabel[] = "Enter New TgtAirport ID";
62 extern char *coord_format_lat(float);
63 extern char *coord_format_lon(float);
67 FGAutopilot::FGAutopilot()
72 FGAutopilot::~FGAutopilot() {}
75 void FGAutopilot::MakeTargetLatLonStr( double lat, double lon ) {
76 sprintf( TargetLatitudeStr , "%s", coord_format_lat(get_TargetLatitude()));
77 sprintf( TargetLongitudeStr, "%s", coord_format_lon(get_TargetLongitude()));
78 sprintf( TargetLatLonStr, "%s %s", TargetLatitudeStr, TargetLongitudeStr );
82 void FGAutopilot::MakeTargetAltitudeStr( double altitude ) {
83 if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
84 sprintf( TargetAltitudeStr, "APAltitude %6.0f+", altitude );
86 sprintf( TargetAltitudeStr, "APAltitude %6.0f", altitude );
91 void FGAutopilot::MakeTargetHeadingStr( double bearing ) {
94 } else if (bearing > 360. ) {
97 sprintf( TargetHeadingStr, "APHeading %6.1f", bearing );
101 static inline double get_ground_speed() {
102 // starts in ft/s so we convert to kts
103 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
105 double ft_s = cur_fdm_state->get_V_ground_speed()
106 * speedup_node->getIntValue();
107 double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
113 void FGAutopilot::MakeTargetWPStr( double distance ) {
114 static time_t last_time = 0;
115 time_t current_time = time(NULL);
116 if ( last_time == current_time ) {
120 last_time = current_time;
124 int size = globals->get_route()->size();
126 // start by wiping the strings
133 SGWayPoint wp1 = globals->get_route()->get_waypoint( 0 );
135 double eta = accum * SG_METER_TO_NM / get_ground_speed();
136 if ( eta >= 100.0 ) { eta = 99.999; }
138 if ( eta < (1.0/6.0) ) {
139 // within 10 minutes, bump up to min/secs
143 minor = (int)((eta - (int)eta) * 60.0);
144 sprintf( TargetWP1Str, "%s %.2f NM ETA %d:%02d",
145 wp1.get_id().c_str(),
146 accum*SG_METER_TO_NM, major, minor );
151 SGWayPoint wp2 = globals->get_route()->get_waypoint( 1 );
152 accum += wp2.get_distance();
154 double eta = accum * SG_METER_TO_NM / get_ground_speed();
155 if ( eta >= 100.0 ) { eta = 99.999; }
157 if ( eta < (1.0/6.0) ) {
158 // within 10 minutes, bump up to min/secs
162 minor = (int)((eta - (int)eta) * 60.0);
163 sprintf( TargetWP2Str, "%s %.2f NM ETA %d:%02d",
164 wp2.get_id().c_str(),
165 accum*SG_METER_TO_NM, major, minor );
170 for ( int i = 2; i < size; ++i ) {
171 accum += globals->get_route()->get_waypoint( i ).get_distance();
174 SGWayPoint wpn = globals->get_route()->get_waypoint( size - 1 );
176 double eta = accum * SG_METER_TO_NM / get_ground_speed();
177 if ( eta >= 100.0 ) { eta = 99.999; }
179 if ( eta < (1.0/6.0) ) {
180 // within 10 minutes, bump up to min/secs
184 minor = (int)((eta - (int)eta) * 60.0);
185 sprintf( TargetWP3Str, "%s %.2f NM ETA %d:%02d",
186 wpn.get_id().c_str(),
187 accum*SG_METER_TO_NM, major, minor );
192 void FGAutopilot::update_old_control_values() {
193 old_aileron = globals->get_controls()->get_aileron();
194 old_elevator = globals->get_controls()->get_elevator();
195 old_elevator_trim = globals->get_controls()->get_elevator_trim();
196 old_rudder = globals->get_controls()->get_rudder();
200 // Initialize autopilot subsystem
202 void FGAutopilot::init ()
204 SG_LOG( SG_AUTOPILOT, SG_INFO, "Init AutoPilot Subsystem" );
206 // bind data input property nodes...
207 latitude_node = fgGetNode("/position/latitude-deg", true);
208 longitude_node = fgGetNode("/position/longitude-deg", true);
209 altitude_node = fgGetNode("/position/altitude-ft", true);
210 altitude_agl_node = fgGetNode("/position/altitude-agl-ft", true);
211 vertical_speed_node = fgGetNode("/velocities/vertical-speed-fps", true);
212 airspeed_node = fgGetNode("/velocities/airspeed-kt", true);
213 heading_node = fgGetNode("/orientation/heading-deg", true);
215 // support non-dg systems that indicate magnetic heading (e.g. 747-400)
216 if (fgGetBool("autopilot/config/indicated-heading-magnetic")) {
217 // use magnetic heading indicated
218 indicated_heading_node
219 = fgGetNode("/orientation/heading-magnetic-deg",
222 // use dg heading indicated
223 indicated_heading_node
224 = fgGetNode("/instrumentation/heading-indicator/indicated-heading-deg",
228 roll_node = fgGetNode("/orientation/roll-deg", true);
229 pitch_node = fgGetNode("/orientation/pitch-deg", true);
233 // bind config property nodes...
235 = fgGetNode("/autopilot/config/target-climb-rate-fpm", true);
237 = fgGetNode("/autopilot/config/target-descent-rate-fpm", true);
238 min_climb = fgGetNode("/autopilot/config/min-climb-speed-kt", true);
239 best_climb = fgGetNode("/autopilot/config/best-climb-speed-kt", true);
241 = fgGetNode("/autopilot/config/elevator-adj-factor", true);
243 = fgGetNode("/autopilot/config/integral-contribution", true);
245 = fgGetNode("/autopilot/config/zero-pitch-throttle", true);
246 zero_pitch_trim_full_throttle
247 = fgGetNode("/autopilot/config/zero-pitch-trim-full-throttle", true);
248 max_aileron_node = fgGetNode("/autopilot/config/max-aileron", true);
249 max_roll_node = fgGetNode("/autopilot/config/max-roll-deg", true);
250 roll_out_node = fgGetNode("/autopilot/config/roll-out-deg", true);
251 roll_out_smooth_node = fgGetNode("/autopilot/config/roll-out-smooth-deg", true);
253 = fgGetNode("/autopilot/config/throttle-adj-factor", true);
255 = fgGetNode("/autopilot/config/throttle-integral", true);
257 = fgGetNode("/autopilot/output/speed_change_anticipated_kt", true);
259 terrain_follow_factor = fgGetNode("/autopilot/config/terrain-follow-factor", true);
261 current_throttle = fgGetNode("/controls/engines/engine/throttle");
263 // initialize config properties with defaults (in case config isn't there)
264 if ( TargetClimbRate->getFloatValue() < 1 )
265 fgSetFloat( "/autopilot/config/target-climb-rate-fpm", 500);
266 if ( TargetDescentRate->getFloatValue() < 1 )
267 fgSetFloat( "/autopilot/config/target-descent-rate-fpm", 1000 );
268 if ( min_climb->getFloatValue() < 1)
269 fgSetFloat( "/autopilot/config/min-climb-speed-kt", 70 );
270 if (best_climb->getFloatValue() < 1)
271 fgSetFloat( "/autopilot/config/best-climb-speed-kt", 120 );
272 if (elevator_adj_factor->getFloatValue() < 1)
273 fgSetFloat( "/autopilot/config/elevator-adj-factor", 5000 );
274 if ( integral_contrib->getFloatValue() < 0.0000001 )
275 fgSetFloat( "/autopilot/config/integral-contribution", 0.01 );
276 if ( zero_pitch_throttle->getFloatValue() < 0.0000001 )
277 fgSetFloat( "/autopilot/config/zero-pitch-throttle", 0.60 );
278 if ( zero_pitch_trim_full_throttle->getFloatValue() < 0.0000001 )
279 fgSetFloat( "/autopilot/config/zero-pitch-trim-full-throttle", 0.15 );
280 if ( max_aileron_node->getFloatValue() < 0.0000001 )
281 fgSetFloat( "/autopilot/config/max-aileron", 0.2 );
282 if ( max_roll_node->getFloatValue() < 0.0000001 )
283 fgSetFloat( "/autopilot/config/max-roll-deg", 20 );
284 if ( roll_out_node->getFloatValue() < 0.0000001 )
285 fgSetFloat( "/autopilot/config/roll-out-deg", 20 );
286 if ( roll_out_smooth_node->getFloatValue() < 0.0000001 )
287 fgSetFloat( "/autopilot/config/roll-out-smooth-deg", 10 );
288 if (throttle_adj_factor->getFloatValue() < 1)
289 fgSetFloat( "/autopilot/config/throttle-adj-factor", 5000 );
290 if ( throttle_integral->getFloatValue() < 0.0000001 )
291 fgSetFloat( "/autopilot/config/throttle-integral", 0.001 );
292 if (terrain_follow_factor->getFloatValue() < 1)
293 fgSetFloat( "/autopilot/config/terrain-follow-factor", 16 );
296 heading_hold = false ; // turn the heading hold off
297 altitude_hold = false ; // turn the altitude hold off
298 auto_throttle = false ; // turn the auto throttle off
299 heading_mode = DEFAULT_AP_HEADING_LOCK;
300 altitude_mode = DEFAULT_AP_ALTITUDE_LOCK;
302 DGTargetHeading = fgGetDouble("/autopilot/settings/heading-bug-deg");
303 TargetHeading = fgGetDouble("/autopilot/settings/heading-bug-deg");
304 TargetAltitude = fgGetDouble("/autopilot/settings/altitude-ft") * SG_FEET_TO_METER;
305 TargetSpeed = fgGetDouble("/autopilot/settings/speed-kt");
307 // Initialize target location to startup location
308 old_lat = latitude_node->getDoubleValue();
309 old_lon = longitude_node->getDoubleValue();
310 // set_WayPoint( old_lon, old_lat, 0.0, "default" );
312 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
314 alt_error_accum = 0.0;
315 climb_error_accum = 0.0;
317 MakeTargetAltitudeStr( TargetAltitude );
318 MakeTargetHeadingStr( TargetHeading );
320 // These eventually need to be read from current_aircaft somehow.
322 // the maximum roll, in Deg
325 // the deg from heading to start rolling out at, in Deg
328 // Smoothing distance for alerion control
331 // Hardwired for now should be in options
332 // 25% max control variablilty 0.5 / 2.0
333 disengage_threshold = 1.0;
335 // set default aileron max deflection
338 // used to calculate acceleration
341 #if !defined( USING_SLIDER_CLASS )
342 MaxRollAdjust = 2 * MaxRoll;
343 RollOutAdjust = 2 * RollOut;
344 //MaxAileronAdjust = 2 * MaxAileron;
345 RollOutSmoothAdjust = 2 * RollOutSmooth;
346 #endif // !defined( USING_SLIDER_CLASS )
348 update_old_control_values();
354 // Autopilot control property get/set bindings
355 fgTie("/autopilot/locks/altitude", this,
356 &FGAutopilot::getAPAltitudeLock, &FGAutopilot::setAPAltitudeLock);
357 fgSetArchivable("/autopilot/locks/altitude");
358 fgTie("/autopilot/settings/altitude-ft", this,
359 &FGAutopilot::getAPAltitude, &FGAutopilot::setAPAltitude);
360 fgSetArchivable("/autopilot/settings/altitude-ft");
361 fgTie("/autopilot/locks/glide-slope", this,
362 &FGAutopilot::getAPGSLock, &FGAutopilot::setAPGSLock);
363 fgSetArchivable("/autopilot/locks/glide-slope");
364 fgSetDouble("/autopilot/settings/altitude-ft", 3000.0f);
365 fgTie("/autopilot/locks/terrain", this,
366 &FGAutopilot::getAPTerrainLock, &FGAutopilot::setAPTerrainLock);
367 fgSetArchivable("/autopilot/locks/terrain");
368 fgTie("/autopilot/settings/climb-rate-fpm", this,
369 &FGAutopilot::getAPClimb, &FGAutopilot::setAPClimb, false);
370 fgSetArchivable("/autopilot/settings/climb-rate-fpm");
371 fgTie("/autopilot/locks/heading", this,
372 &FGAutopilot::getAPHeadingLock, &FGAutopilot::setAPHeadingLock);
373 fgSetArchivable("/autopilot/locks/heading");
375 fgTie("/autopilot/locks/vert-speed", this,
376 &FGAutopilot::getAPVertSpeedLock, &FGAutopilot::setAPVertSpeedLock);
377 fgSetArchivable("/autopilot/locks/vert-speed");
380 fgTie("/autopilot/settings/heading-bug-deg", this,
381 &FGAutopilot::getAPHeadingBug, &FGAutopilot::setAPHeadingBug);
382 fgSetArchivable("/autopilot/settings/heading-bug-deg");
383 fgSetDouble("/autopilot/settings/heading-bug-deg", 0.0f);
385 fgTie("/autopilot/settings/waypoint", this,
386 &FGAutopilot::getAPwaypoint, &FGAutopilot::setAPwaypoint);
387 fgSetArchivable("/autopilot/settings/waypoint");
388 fgSetString("/autopilot/settings/waypoint", "");
390 fgTie("/autopilot/locks/wing-leveler", this,
391 &FGAutopilot::getAPWingLeveler, &FGAutopilot::setAPWingLeveler);
392 fgSetArchivable("/autopilot/locks/wing-leveler");
393 fgTie("/autopilot/locks/nav[0]", this,
394 &FGAutopilot::getAPNAV1Lock, &FGAutopilot::setAPNAV1Lock);
395 fgSetArchivable("/autopilot/locks/nav[0]");
396 fgTie("/autopilot/locks/auto-throttle", this,
397 &FGAutopilot::getAPAutoThrottleLock,
398 &FGAutopilot::setAPAutoThrottleLock);
399 fgSetArchivable("/autopilot/locks/auto-throttle");
400 fgTie("/autopilot/settings/speed-kt", this,
401 &FGAutopilot::getAPAutoThrottle, &FGAutopilot::setAPAutoThrottle);
402 fgSetArchivable("/autopilot/settings/altitude-ft");
403 fgTie("/autopilot/control-overrides/rudder", this,
404 &FGAutopilot::getAPRudderControl,
405 &FGAutopilot::setAPRudderControl);
406 fgSetArchivable("/autopilot/control-overrides/rudder");
407 fgTie("/autopilot/control-overrides/elevator", this,
408 &FGAutopilot::getAPElevatorControl,
409 &FGAutopilot::setAPElevatorControl);
410 fgSetArchivable("/autopilot/control-overrides/elevator");
411 fgTie("/autopilot/control-overrides/throttle", this,
412 &FGAutopilot::getAPThrottleControl,
413 &FGAutopilot::setAPThrottleControl);
414 fgSetArchivable("/autopilot/control-overrides/throttle");
416 fgTie("/autopilot/settings/vertical-speed-fpm", this,
417 &FGAutopilot::getAPVertSpeed, &FGAutopilot::setAPVertSpeed);
418 fgSetArchivable("/autopilot/settings/vertical-speed-fpm");
419 fgSetDouble("/autopilot/settings/vertical-speed-fpm", 0.0f);
424 FGAutopilot::unbind ()
428 // Reset the autopilot system
429 void FGAutopilot::reset() {
431 heading_hold = false ; // turn the heading hold off
432 altitude_hold = false ; // turn the altitude hold off
433 auto_throttle = false ; // turn the auto throttle off
434 heading_mode = DEFAULT_AP_HEADING_LOCK;
436 // TargetHeading = 0.0; // default direction, due north
437 MakeTargetHeadingStr( TargetHeading );
439 // TargetAltitude = 3000; // default altitude in meters
440 MakeTargetAltitudeStr( TargetAltitude );
442 alt_error_accum = 0.0;
443 climb_error_accum = 0.0;
445 update_old_control_values();
447 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/presets/airport-id") );
449 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
453 static double NormalizeDegrees( double Input ) {
454 // normalize the input to the range (-180,180]
455 // Input should not be greater than -360 to 360.
456 // Current rules send the output to an undefined state.
457 while ( Input > 180.0 ) { Input -= 360.0; }
458 while ( Input <= -180.0 ) { Input += 360.0; }
463 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
464 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
465 //assert(x1 != x2); // Divide by zero error. Cold abort for now
468 // static double y = 0.0;
469 // double dx = x2 -x1;
470 // if( (dx < -SG_EPSILON ) || ( dx > SG_EPSILON ) )
473 double m, b, y; // the constants to find in y=mx+b
476 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
478 b = y1 - m * x1; // calculate the b
480 y = m * x + b; // the final calculation
490 FGAutopilot::update (double dt)
493 // get control settings
494 double lat = latitude_node->getDoubleValue();
495 double lon = longitude_node->getDoubleValue();
496 double alt = altitude_node->getDoubleValue() * SG_FEET_TO_METER;
498 // get config settings
499 MaxAileron = max_aileron_node->getDoubleValue();
500 MaxRoll = max_roll_node->getDoubleValue();
501 RollOut = roll_out_node->getDoubleValue();
502 RollOutSmooth = roll_out_smooth_node->getDoubleValue();
504 SG_LOG( SG_ALL, SG_DEBUG, "FGAutopilot::run() lat = " << lat <<
505 " lon = " << lon << " alt = " << alt );
507 #ifdef FG_FORCE_AUTO_DISENGAGE
508 // see if somebody else has changed them
509 if( fabs(aileron - old_aileron) > disengage_threshold ||
510 fabs(elevator - old_elevator) > disengage_threshold ||
511 fabs(elevator_trim - old_elevator_trim) >
512 disengage_threshold ||
513 fabs(rudder - old_rudder) > disengage_threshold )
515 // if controls changed externally turn autopilot off
516 waypoint_hold = false ; // turn the target hold off
517 heading_hold = false ; // turn the heading hold off
518 altitude_hold = false ; // turn the altitude hold off
519 terrain_follow = false; // turn the terrain_follow hold off
520 // auto_throttle = false; // turn the auto_throttle off
522 // stash this runs control settings
523 old_aileron = aileron;
524 old_elevator = elevator;
525 old_elevator_trim = elevator_trim;
533 if ( heading_hold == true ) {
534 if ( heading_mode == FG_DG_HEADING_LOCK ) {
535 double dg_error = heading_node->getDoubleValue()
536 - indicated_heading_node->getDoubleValue();
537 TargetHeading = DGTargetHeading + dg_error;
538 // cout << "dg_error = " << dg_error << endl;
539 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
540 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
541 MakeTargetHeadingStr( TargetHeading );
542 } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
543 // we don't set a specific target heading in
544 // TC_HEADING_LOCK mode, we instead try to keep the turn
545 // coordinator zero'd
546 } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
547 // leave "true" target heading as is
548 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
549 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
550 MakeTargetHeadingStr( TargetHeading );
551 } else if ( heading_mode == FG_HEADING_NAV1 ) {
552 // track the NAV1 heading needle deflection
554 // determine our current radial position relative to the
555 // navaid in "true" heading.
556 double cur_radial = current_radiostack->get_navcom1()->get_nav_reciprocal_radial();
557 if ( current_radiostack->get_navcom1()->get_nav_loc() ) {
558 // ILS localizers radials are already "true" in our
561 cur_radial += current_radiostack->get_navcom1()->get_nav_twist();
563 if ( current_radiostack->get_navcom1()->get_nav_from_flag() ) {
565 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
568 // determine the target radial in "true" heading
570 = current_radiostack->get_navcom1()->get_nav_target_radial();
571 if ( current_radiostack->get_navcom1()->get_nav_loc() ) {
572 // ILS localizers radials are already "true" in our
575 // VOR radials need to have that vor's offset added in
576 tgt_radial += current_radiostack->get_navcom1()->get_nav_twist();
579 // determine the heading adjustment needed.
581 current_radiostack->get_navcom1()->get_nav_cdi_deflection()
582 * (current_radiostack->get_navcom1()->get_nav_loc_dist() * SG_METER_TO_NM);
583 SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
585 // clamp closer when inside cone when beyond 5km...
586 if (current_radiostack->get_navcom1()->get_nav_loc_dist() > 5000) {
587 double clamp_angle = fabs(current_radiostack->get_navcom1()->get_nav_cdi_deflection()) * 3;
588 if (clamp_angle < 30)
589 SG_CLAMP_RANGE( adjustment, -clamp_angle, clamp_angle);
592 // determine the target heading to fly to intercept the
594 TargetHeading = tgt_radial + adjustment;
595 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
596 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
598 MakeTargetHeadingStr( TargetHeading );
599 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
600 // update target heading to waypoint
602 double wp_course, wp_distance;
604 #ifdef DO_fgAP_CORRECTED_COURSE
605 // compute course made good
606 // this needs lots of special casing before use
607 double course, reverse, distance, corrected_course;
608 // need to test for iter
609 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
617 #endif // DO_fgAP_CORRECTED_COURSE
619 // compute course to way_point
620 // need to test for iter
621 SGWayPoint wp = globals->get_route()->get_first();
622 wp.CourseAndDistance( lon, lat, alt,
623 &wp_course, &wp_distance );
625 #ifdef DO_fgAP_CORRECTED_COURSE
626 corrected_course = course - wp_course;
627 if( fabs(corrected_course) > 0.1 )
628 printf("fgAP: course %f wp_course %f %f %f\n",
629 course, wp_course, fabs(corrected_course),
631 #endif // DO_fgAP_CORRECTED_COURSE
633 if ( wp_distance > 100 ) {
634 // corrected_course = course - wp_course;
635 TargetHeading = NormalizeDegrees(wp_course);
637 // pop off this waypoint from the list
638 if ( globals->get_route()->size() ) {
639 globals->get_route()->delete_first();
642 // see if there are more waypoints on the list
643 if ( globals->get_route()->size() ) {
645 set_HeadingMode( FG_HEADING_WAYPOINT );
648 heading_mode = DEFAULT_AP_HEADING_LOCK;
649 // use current heading
650 TargetHeading = heading_node->getDoubleValue();
653 MakeTargetHeadingStr( TargetHeading );
654 // Force this just in case
655 TargetDistance = wp_distance;
656 MakeTargetWPStr( wp_distance );
659 if ( heading_mode == FG_TC_HEADING_LOCK ) {
660 // drive the turn coordinator to zero
662 fgGetDouble("/instrumentation/turn-indicator/indicated-turn-rate");
663 double AileronSet = -turn / 2.0;
664 SG_CLAMP_RANGE( AileronSet, -1.0, 1.0 );
665 globals->get_controls()->set_aileron( AileronSet );
666 globals->get_controls()->set_rudder( AileronSet / 4.0 );
668 // steer towards the target heading
676 = NormalizeDegrees( TargetHeading
677 - heading_node->getDoubleValue() );
678 // figure out how far off we are from desired heading
680 // Now it is time to deterime how far we should be rolled.
681 SG_LOG( SG_AUTOPILOT, SG_DEBUG,
682 "Heading = " << heading_node->getDoubleValue() <<
683 " TargetHeading = " << TargetHeading <<
684 " RelHeading = " << RelHeading );
686 // Check if we are further from heading than the roll out point
687 if ( fabs( RelHeading ) > RollOut ) {
688 // set Target Roll to Max in desired direction
689 if ( RelHeading < 0 ) {
690 TargetRoll = 0 - MaxRoll;
692 TargetRoll = MaxRoll;
695 // We have to calculate the Target roll
697 // This calculation engine thinks that the Target roll
698 // should be a line from (RollOut,MaxRoll) to (-RollOut,
699 // -MaxRoll) I hope this works well. If I get ambitious
700 // some day this might become a fancier curve or
703 TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
708 // Target Roll has now been Found.
710 // Compare Target roll to Current Roll, Generate Rel Roll
712 SG_LOG( SG_COCKPIT, SG_BULK, "TargetRoll: " << TargetRoll );
714 RelRoll = NormalizeDegrees( TargetRoll
715 - roll_node->getDoubleValue() );
717 // Check if we are further from heading than the roll out
719 if ( fabs( RelRoll ) > RollOutSmooth ) {
720 // set Target Roll to Max in desired direction
722 AileronSet = 0 - MaxAileron;
724 AileronSet = MaxAileron;
727 AileronSet = LinearExtrapolate( RelRoll, -RollOutSmooth,
733 globals->get_controls()->set_aileron( AileronSet );
734 globals->get_controls()->set_rudder( AileronSet / 4.0 );
735 // controls.set_rudder( 0.0 );
740 if ( altitude_hold ) {
742 double speed, max_climb, error;
743 double prop_error, int_error;
744 double prop_adj, int_adj, total_adj;
746 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
749 fgGetDouble("/instrumentation/altimeter/indicated-altitude-ft") * SG_FEET_TO_METER ) * 8.0;
750 } else if ( altitude_mode == FG_TRUE_ALTITUDE_LOCK ) {
751 climb_rate = ( TargetAltitude - alt ) * 8.0;
752 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
753 double x = current_radiostack->get_navcom1()->get_nav_gs_dist();
754 double y = (altitude_node->getDoubleValue()
755 - current_radiostack->get_navcom1()->get_nav_elev()) * SG_FEET_TO_METER;
756 double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
758 double target_angle = current_radiostack->get_navcom1()->get_nav_target_gs();
759 double gs_diff = target_angle - current_angle;
761 // convert desired vertical path angle into a climb rate
762 double des_angle = current_angle - 10 * gs_diff;
764 // estimate horizontal speed towards ILS in meters per minute
765 static double horiz_vel = 0.0;
766 static double last_x = 0.0;
767 double dist = last_x - x;
769 double new_vel = ( dist / dt ) * 60.0;
770 horiz_vel = 0.75 * horiz_vel + 0.25 * new_vel;
771 // double horiz_vel = cur_fdm_state->get_V_ground_speed()
772 // * SG_FEET_TO_METER * 60.0;
773 // double horiz_vel = airspeed_node->getFloatValue()
774 // * SG_FEET_TO_METER * 60.0;
776 climb_rate = -sin( des_angle * SGD_DEGREES_TO_RADIANS ) * horiz_vel;
777 /* climb_error_accum += gs_diff * 2.0; */
778 /* climb_rate = gs_diff * 200.0 + climb_error_accum; */
779 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
780 // brain dead ground hugging with no look ahead
782 ( TargetAGL - altitude_agl_node->getDoubleValue()
784 * terrain_follow_factor->getFloatValue();
785 } else if ( altitude_mode == FG_VERT_SPEED ) {
786 climb_rate = TargetVertSpeed * SG_FEET_TO_METER;
787 // switch to altitude hold, if set, within 500ft of target
788 if (fabs(TargetAltitude * SG_METER_TO_FEET - altitude_node->getDoubleValue()) < 500) {
789 set_AltitudeMode( FG_ALTITUDE_LOCK );
792 // just try to zero out rate of climb ...
796 speed = airspeed_node->getFloatValue();
798 if ( speed < min_climb->getFloatValue() ) {
800 } else if ( speed < best_climb->getFloatValue() ) {
801 max_climb = ((best_climb->getFloatValue()
802 - min_climb->getFloatValue())
803 - (best_climb->getFloatValue() - speed))
804 * fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER)
805 / (best_climb->getFloatValue() - min_climb->getFloatValue());
807 max_climb = ( speed - best_climb->getFloatValue() ) * 10.0
808 + fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER);
811 if (altitude_mode != FG_VERT_SPEED) {
813 // this first one could be optional if we wanted to allow
814 // better climb performance assuming we have the airspeed to
817 fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER) ) {
819 = fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER);
822 if ( climb_rate > max_climb ) {
823 climb_rate = max_climb;
827 -fabs(TargetDescentRate->getFloatValue() * SG_FEET_TO_METER) ) {
829 = -fabs(TargetDescentRate->getFloatValue() * SG_FEET_TO_METER);
834 error = vertical_speed_node->getDoubleValue() * 60
835 - climb_rate * SG_METER_TO_FEET;
837 // accumulate the error under the curve ... this really should
839 alt_error_accum += error;
841 // calculate integral error, and adjustment amount
842 int_error = alt_error_accum;
843 // printf("error = %.2f int_error = %.2f\n", error, int_error);
845 // scale elev_adj_factor by speed of aircraft in relation to min climb
846 double elev_adj_factor = elevator_adj_factor->getFloatValue();
848 pow(float(speed / min_climb->getFloatValue()), 3.0f);
850 int_adj = int_error / elev_adj_factor;
852 // caclulate proportional error
854 prop_adj = prop_error / elev_adj_factor;
856 total_adj = ((double) 1.0 - (double) integral_contrib->getFloatValue()) * prop_adj
857 + (double) integral_contrib->getFloatValue() * int_adj;
859 // stop on autopilot trim at 30% +/-
860 // if ( total_adj > 0.3 ) {
862 // } else if ( total_adj < -0.3 ) {
866 // adjust for throttle pitch gain
867 total_adj += ((current_throttle->getFloatValue() - zero_pitch_throttle->getFloatValue())
868 / (1 - zero_pitch_throttle->getFloatValue()))
869 * zero_pitch_trim_full_throttle->getFloatValue();
871 globals->get_controls()->set_elevator_trim( total_adj );
875 if ( auto_throttle ) {
877 double prop_error, int_error;
878 double prop_adj, int_adj, total_adj;
881 // estimate speed in 10 seconds
882 lookahead = airspeed_node->getFloatValue() + ( airspeed_node->getFloatValue() - previous_speed) * (10/(dt + 0.000001));
883 previous_speed = airspeed_node->getFloatValue();
885 // compare targetspeed to anticipated airspeed
886 error = TargetSpeed - lookahead;
888 // output anticipated speed change...
889 speed_change_node->setDoubleValue(lookahead - airspeed_node->getFloatValue());
891 // accumulate the error under the curve ... this really should
893 speed_error_accum += error;
894 if ( speed_error_accum > 2000.0 ) {
895 speed_error_accum = 2000.0;
897 else if ( speed_error_accum < -2000.0 ) {
898 speed_error_accum = -2000.0;
901 // calculate integral error, and adjustment amount
902 int_error = speed_error_accum;
904 // printf("error = %.2f int_error = %.2f\n", error, int_error);
905 int_adj = int_error / throttle_adj_factor->getFloatValue();
907 // caclulate proportional error
909 prop_adj = prop_error / throttle_adj_factor->getFloatValue();
911 total_adj = (1.0 - throttle_integral->getFloatValue()) * prop_adj +
912 throttle_integral->getFloatValue() * int_adj;
914 current_ap_throttle = current_ap_throttle + total_adj;
916 if ( current_ap_throttle > 1.0 ) {
917 current_ap_throttle = 1.0;
919 else if ( current_ap_throttle < 0.0 ) {
920 current_ap_throttle = 0.0;
923 globals->get_controls()->set_throttle( FGControls::ALL_ENGINES,
924 current_ap_throttle );
927 #ifdef THIS_CODE_IS_NOT_USED
928 if (Mode == 2) // Glide slope hold
933 // First, calculate Relative slope and normalize it
934 RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
936 // Now calculate the elevator offset from current angle
937 if ( abs(RelSlope) > SlopeSmooth )
939 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
940 RelElevator = -MaxElevator;
942 RelElevator = MaxElevator;
946 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
949 fgElevMove(RelElevator);
952 #endif // THIS_CODE_IS_NOT_USED
954 // stash this runs control settings
955 // update_old_control_values();
956 old_aileron = globals->get_controls()->get_aileron();
957 old_elevator = globals->get_controls()->get_elevator();
958 old_elevator_trim = globals->get_controls()->get_elevator_trim();
959 old_rudder = globals->get_controls()->get_rudder();
961 // for cross track error
966 SG_LOG( SG_ALL, SG_DEBUG, "FGAutopilot::run( returns )" );
970 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
973 if ( heading_mode == FG_DG_HEADING_LOCK ) {
974 // use current heading bug value
975 } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
976 // set autopilot to hold a zero turn (as reported by the TC)
977 } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
978 // set heading hold to current heading
979 TargetHeading = heading_node->getDoubleValue();
980 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
981 if ( globals->get_route()->size() ) {
982 double course, distance;
984 old_lat = latitude_node->getDoubleValue();
985 old_lon = longitude_node->getDoubleValue();
987 waypoint = globals->get_route()->get_first();
988 waypoint.CourseAndDistance( longitude_node->getDoubleValue(),
989 latitude_node->getDoubleValue(),
990 altitude_node->getDoubleValue()
992 &course, &distance );
993 TargetHeading = course;
994 TargetDistance = distance;
995 MakeTargetLatLonStr( waypoint.get_target_lat(),
996 waypoint.get_target_lon() );
997 MakeTargetWPStr( distance );
999 if ( waypoint.get_target_alt() > 0.0 ) {
1000 TargetAltitude = waypoint.get_target_alt();
1001 altitude_mode = DEFAULT_AP_ALTITUDE_LOCK;
1002 set_AltitudeEnabled( true );
1003 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
1006 SG_LOG( SG_COCKPIT, SG_INFO, " set_HeadingMode: ( "
1007 << get_TargetLatitude() << " "
1008 << get_TargetLongitude() << " ) "
1011 // no more way points, default to heading lock.
1012 heading_mode = FG_TC_HEADING_LOCK;
1016 MakeTargetHeadingStr( TargetHeading );
1017 update_old_control_values();
1021 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
1022 altitude_mode = mode;
1024 // only reset accum error if not in altitude mode for smooth transitions
1025 // from one altitude mode to another until pitch control damping added.
1026 if (!altitude_hold) {
1027 alt_error_accum = 0.0;
1030 if ( altitude_mode == DEFAULT_AP_ALTITUDE_LOCK ) {
1031 if ( TargetAltitude < altitude_agl_node->getDoubleValue()
1032 * SG_FEET_TO_METER ) {
1035 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
1036 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
1038 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
1040 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
1041 climb_error_accum = 0.0;
1043 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
1044 TargetAGL = altitude_agl_node->getDoubleValue() * SG_FEET_TO_METER;
1046 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
1047 MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
1049 MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
1053 update_old_control_values();
1054 SG_LOG( SG_COCKPIT, SG_INFO, " set_AltitudeMode():" );
1058 void FGAutopilot::AltitudeSet( double new_altitude ) {
1059 double target_alt = new_altitude;
1060 altitude_mode = DEFAULT_AP_ALTITUDE_LOCK;
1062 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
1063 target_alt = new_altitude * SG_FEET_TO_METER;
1066 if( target_alt < globals->get_scenery()->get_cur_elev() ) {
1067 target_alt = globals->get_scenery()->get_cur_elev();
1070 TargetAltitude = target_alt;
1072 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
1073 target_alt *= SG_METER_TO_FEET;
1075 // ApAltitudeDialogInput->setValue((float)target_alt);
1076 MakeTargetAltitudeStr( target_alt );
1078 update_old_control_values();
1082 void FGAutopilot::AltitudeAdjust( double inc )
1084 double target_alt, target_agl;
1086 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
1087 target_alt = TargetAltitude * SG_METER_TO_FEET;
1088 target_agl = TargetAGL * SG_METER_TO_FEET;
1090 target_alt = TargetAltitude;
1091 target_agl = TargetAGL;
1094 if ( fabs((int)(target_alt / inc) * inc - target_alt) < SG_EPSILON ) {
1097 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
1100 if ( fabs((int)(target_agl / inc) * inc - target_agl) < SG_EPSILON ) {
1103 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
1106 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
1107 target_alt *= SG_FEET_TO_METER;
1108 target_agl *= SG_FEET_TO_METER;
1111 TargetAltitude = target_alt;
1112 TargetAGL = target_agl;
1114 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
1115 target_alt *= SG_METER_TO_FEET;
1116 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
1117 target_agl *= SG_METER_TO_FEET;
1119 if ( altitude_mode == DEFAULT_AP_ALTITUDE_LOCK ) {
1120 MakeTargetAltitudeStr( target_alt );
1121 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
1122 MakeTargetAltitudeStr( target_agl );
1125 update_old_control_values();
1129 void FGAutopilot::HeadingAdjust( double inc ) {
1130 if ( heading_mode != FG_DG_HEADING_LOCK
1131 && heading_mode != FG_TRUE_HEADING_LOCK )
1133 heading_mode = DEFAULT_AP_HEADING_LOCK;
1136 if ( heading_mode == FG_DG_HEADING_LOCK ) {
1137 double target = ( int ) ( DGTargetHeading / inc ) * inc + inc;
1138 DGTargetHeading = NormalizeDegrees( target );
1140 double target = ( int ) ( TargetHeading / inc ) * inc + inc;
1141 TargetHeading = NormalizeDegrees( target );
1144 update_old_control_values();
1148 void FGAutopilot::HeadingSet( double new_heading ) {
1149 heading_mode = DEFAULT_AP_HEADING_LOCK;
1150 if( heading_mode == FG_TRUE_HEADING_LOCK ) {
1151 new_heading = NormalizeDegrees( new_heading );
1152 TargetHeading = new_heading;
1153 MakeTargetHeadingStr( TargetHeading );
1155 heading_mode = FG_DG_HEADING_LOCK;
1157 new_heading = NormalizeDegrees( new_heading );
1158 DGTargetHeading = new_heading;
1159 // following cast needed ambiguous plib
1160 // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1161 MakeTargetHeadingStr( DGTargetHeading );
1163 update_old_control_values();
1166 void FGAutopilot::AutoThrottleAdjust( double inc ) {
1167 double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
1169 set_TargetSpeed( target );
1173 * Set the autothrottle speed
1176 FGAutopilot::setAPAutoThrottle (double speed_kt)
1178 set_TargetSpeed( speed_kt );
1182 * Get the autothrottle speed
1185 FGAutopilot::getAPAutoThrottle () const
1187 return get_TargetSpeed();
1190 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
1191 auto_throttle = value;
1193 if ( auto_throttle == true ) {
1194 if (TargetSpeed < 0.0001) {
1195 TargetSpeed = fgGetDouble("/velocities/airspeed-kt");
1197 speed_error_accum = 0.0;
1198 // initialize autothrottle at current control setting;
1199 current_ap_throttle = current_throttle->getFloatValue();
1202 update_old_control_values();
1203 SG_LOG( SG_COCKPIT, SG_INFO, " fgAPSetAutoThrottle: ("
1204 << auto_throttle << ") " << TargetSpeed );
1210 ////////////////////////////////////////////////////////////////////////
1211 // Kludged methods for tying to properties.
1213 // These should change eventually; they all used to be static
1215 ////////////////////////////////////////////////////////////////////////
1218 * Get the autopilot altitude lock (true=on).
1221 FGAutopilot::getAPAltitudeLock () const
1223 return (get_AltitudeEnabled() &&
1225 == DEFAULT_AP_ALTITUDE_LOCK);
1230 * Set the autopilot altitude lock (true=on).
1233 FGAutopilot::setAPAltitudeLock (bool lock)
1236 set_AltitudeMode(DEFAULT_AP_ALTITUDE_LOCK);
1237 if (get_AltitudeMode() == DEFAULT_AP_ALTITUDE_LOCK)
1238 set_AltitudeEnabled(lock);
1243 * Get the autopilot target altitude in feet.
1246 FGAutopilot::getAPAltitude () const
1248 return get_TargetAltitude() * SG_METER_TO_FEET;
1253 * Set the autopilot target altitude in feet.
1256 FGAutopilot::setAPAltitude (double altitude)
1258 set_TargetAltitude( altitude * SG_FEET_TO_METER );
1262 * Get the autopilot altitude lock (true=on).
1265 FGAutopilot::getAPGSLock () const
1267 return (get_AltitudeEnabled() &&
1269 == FGAutopilot::FG_ALTITUDE_GS1));
1274 * Set the autopilot altitude lock (true=on).
1277 FGAutopilot::setAPGSLock (bool lock)
1280 set_AltitudeMode(FGAutopilot::FG_ALTITUDE_GS1);
1281 if (get_AltitudeMode() == FGAutopilot::FG_ALTITUDE_GS1)
1282 set_AltitudeEnabled(lock);
1287 * Get the autopilot terrain lock (true=on).
1290 FGAutopilot::getAPTerrainLock () const
1292 return (get_AltitudeEnabled() &&
1294 == FGAutopilot::FG_ALTITUDE_TERRAIN));
1299 * Set the autopilot terrain lock (true=on).
1302 FGAutopilot::setAPTerrainLock (bool lock)
1305 set_AltitudeMode(FGAutopilot::FG_ALTITUDE_TERRAIN);
1306 set_TargetAGL(fgGetFloat("/position/altitude-agl-ft") *
1309 if (get_AltitudeMode() == FGAutopilot::FG_ALTITUDE_TERRAIN)
1310 set_AltitudeEnabled(lock);
1314 * Get the autopilot vertical speed lock (true=on).
1317 FGAutopilot::getAPVertSpeedLock () const
1319 return (get_AltitudeEnabled() &&
1321 == FGAutopilot::FG_VERT_SPEED));
1326 * Set the autopilot vert speed lock (true=on).
1329 FGAutopilot::setAPVertSpeedLock (bool lock)
1332 set_AltitudeMode(FGAutopilot::FG_VERT_SPEED);
1333 if (get_AltitudeMode() == FGAutopilot::FG_VERT_SPEED)
1334 set_AltitudeEnabled(lock);
1339 * Get the autopilot target altitude in feet.
1342 FGAutopilot::getAPClimb () const
1344 return get_TargetClimbRate() * SG_METER_TO_FEET;
1349 * Set the autopilot target altitude in feet.
1352 FGAutopilot::setAPClimb (double rate)
1354 set_TargetClimbRate( rate * SG_FEET_TO_METER );
1359 * Get the autopilot heading lock (true=on).
1362 FGAutopilot::getAPHeadingLock () const
1365 (get_HeadingEnabled() &&
1366 get_HeadingMode() == DEFAULT_AP_HEADING_LOCK);
1371 * Set the autopilot heading lock (true=on).
1374 FGAutopilot::setAPHeadingLock (bool lock)
1377 set_HeadingMode(DEFAULT_AP_HEADING_LOCK);
1378 if (get_HeadingMode() == DEFAULT_AP_HEADING_LOCK)
1379 set_HeadingEnabled(lock);
1384 * Get the autopilot heading bug in degrees.
1387 FGAutopilot::getAPHeadingBug () const
1389 return get_DGTargetHeading();
1394 * Set the autopilot heading bug in degrees.
1397 FGAutopilot::setAPHeadingBug (double heading)
1399 set_DGTargetHeading( heading );
1404 * return blank-separated string of waypoints
1407 FGAutopilot::getAPwaypoint () const
1409 static char wplist[500];
1413 // FIXME: This can cause a possible buffer overflow, EMH
1414 if ( globals->get_route()->size() > 0 ) {
1415 WPListsize = globals->get_route()->size();
1417 for (i = 0; i < globals->get_route()->size(); i++ ) {
1418 p += sprintf(p, "%5s ",
1419 globals->get_route()->get_waypoint(i).get_id().c_str() );
1424 return "none specified";
1430 * set next waypoint (if str='0', then delete next(first) waypoint)
1433 FGAutopilot::setAPwaypoint (const char * apt)
1435 if (strcmp(apt, "0")==0)
1437 SG_LOG( SG_AUTOPILOT, SG_INFO, "delete of first wp" );
1438 if ( globals->get_route()->size() )
1439 globals->get_route()->delete_first();
1443 if ( NewWaypoint( apt ) == 0)
1444 SG_LOG( SG_AUTOPILOT, SG_INFO, "Waypoint " << apt << "not in d.b." );
1447 /* SG_LOG( SG_AUTOPILOT, SG_INFO, "GOOD!" ); */
1452 * Get the autopilot wing leveler lock (true=on).
1455 FGAutopilot::getAPWingLeveler () const
1458 (get_HeadingEnabled() &&
1459 get_HeadingMode() == FGAutopilot::FG_TC_HEADING_LOCK);
1464 * Set the autopilot wing leveler lock (true=on).
1467 FGAutopilot::setAPWingLeveler (bool lock)
1470 set_HeadingMode(FGAutopilot::FG_TC_HEADING_LOCK);
1471 if (get_HeadingMode() == FGAutopilot::FG_TC_HEADING_LOCK)
1472 set_HeadingEnabled(lock);
1476 * Return true if the autopilot is locked to NAV1.
1479 FGAutopilot::getAPNAV1Lock () const
1482 (get_HeadingEnabled() &&
1483 get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1);
1488 * Set the autopilot NAV1 lock.
1491 FGAutopilot::setAPNAV1Lock (bool lock)
1494 set_HeadingMode(FGAutopilot::FG_HEADING_NAV1);
1495 if (get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1)
1496 set_HeadingEnabled(lock);
1500 * Get the autopilot autothrottle lock.
1503 FGAutopilot::getAPAutoThrottleLock () const
1505 return get_AutoThrottleEnabled();
1510 * Set the autothrottle lock.
1513 FGAutopilot::setAPAutoThrottleLock (bool lock)
1515 set_AutoThrottleEnabled(lock);
1521 FGAutopilot::getAPRudderControl () const
1523 if (getAPHeadingLock())
1524 return get_TargetHeading();
1526 return globals->get_controls()->get_rudder();
1531 FGAutopilot::setAPRudderControl (double value)
1533 if (getAPHeadingLock()) {
1534 SG_LOG(SG_GENERAL, SG_DEBUG, "setAPRudderControl " << value );
1535 value -= get_TargetHeading();
1536 HeadingAdjust(value < 0.0 ? -1.0 : 1.0);
1538 globals->get_controls()->set_rudder(value);
1544 FGAutopilot::getAPElevatorControl () const
1546 if (getAPAltitudeLock())
1547 return get_TargetAltitude();
1549 return globals->get_controls()->get_elevator();
1554 FGAutopilot::setAPElevatorControl (double value)
1556 if (value != 0 && getAPAltitudeLock()) {
1557 SG_LOG(SG_GENERAL, SG_DEBUG, "setAPElevatorControl " << value );
1558 value -= get_TargetAltitude();
1559 AltitudeAdjust(value < 0.0 ? 100.0 : -100.0);
1561 globals->get_controls()->set_elevator(value);
1567 FGAutopilot::getAPThrottleControl () const
1569 if (getAPAutoThrottleLock())
1570 return 0.0; // always resets
1572 return globals->get_controls()->get_throttle(0);
1577 FGAutopilot::setAPThrottleControl (double value)
1579 if (getAPAutoThrottleLock())
1580 AutoThrottleAdjust(value < 0.0 ? -0.01 : 0.01);
1582 globals->get_controls()->set_throttle(FGControls::ALL_ENGINES, value);
1586 * Get the vertical speed selected
1589 FGAutopilot::getAPVertSpeed () const
1591 return TargetVertSpeed;
1596 * Set the selected vertical speed
1599 FGAutopilot::setAPVertSpeed (double speed)
1601 TargetVertSpeed = speed;
1604 // end of newauto.cxx