]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/newauto.cxx
95324fb387830157ed3680f5145a44fdd8a4cfdc
[flightgear.git] / src / Autopilot / newauto.cxx
1 // newauto.cxx -- autopilot defines and prototypes (very alpha)
2 // 
3 // Started April 1998  Copyright (C) 1998
4 //
5 // Contributions by Jeff Goeke-Smith <jgoeke@voyager.net>
6 //                  Norman Vine <nhv@cape.com>
7 //                  Curtis Olson <curt@flightgear.org>
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 //
23 // $Id$
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <stdio.h>              // sprintf()
31 #include <string.h>             // strcmp()
32
33 #include <simgear/constants.h>
34 #include <simgear/sg_inlines.h>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/math/sg_geodesy.hxx>
37 #include <simgear/math/sg_random.h>
38 #include <simgear/route/route.hxx>
39
40 #include <Cockpit/steam.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>
46
47 #include "newauto.hxx"
48
49
50 /// These statics will eventually go into the class
51 /// they are just here while I am experimenting -- NHV :-)
52 // AutoPilot Gain Adjuster members
53 static double MaxRollAdjust;        // MaxRollAdjust       = 2 * APData->MaxRoll;
54 static double RollOutAdjust;        // RollOutAdjust       = 2 * APData->RollOut;
55 static double MaxAileronAdjust;     // MaxAileronAdjust    = 2 * APData->MaxAileron;
56 static double RollOutSmoothAdjust;  // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
57
58 static char NewTgtAirportId[16];
59 // static char NewTgtAirportLabel[] = "Enter New TgtAirport ID"; 
60
61 extern char *coord_format_lat(float);
62 extern char *coord_format_lon(float);
63                         
64
65 // constructor
66 FGAutopilot::FGAutopilot()
67 {
68 }
69
70 // destructor
71 FGAutopilot::~FGAutopilot() {}
72
73
74 void FGAutopilot::MakeTargetLatLonStr( double lat, double lon ) {
75     sprintf( TargetLatitudeStr , "%s", coord_format_lat(get_TargetLatitude()));
76     sprintf( TargetLongitudeStr, "%s", coord_format_lon(get_TargetLongitude()));
77     sprintf( TargetLatLonStr, "%s  %s", TargetLatitudeStr, TargetLongitudeStr );
78 }
79
80
81 void FGAutopilot::MakeTargetAltitudeStr( double altitude ) {
82     if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
83         sprintf( TargetAltitudeStr, "APAltitude  %6.0f+", altitude );
84     } else {
85         sprintf( TargetAltitudeStr, "APAltitude  %6.0f", altitude );
86     }
87 }
88
89
90 void FGAutopilot::MakeTargetHeadingStr( double bearing ) {
91     if ( bearing < 0. ) {
92         bearing += 360.;
93     } else if (bearing > 360. ) {
94         bearing -= 360.;
95     }
96     sprintf( TargetHeadingStr, "APHeading  %6.1f", bearing );
97 }
98
99
100 static inline double get_speed( void ) {
101     return( cur_fdm_state->get_V_equiv_kts() );
102 }
103
104 static inline double get_ground_speed() {
105     // starts in ft/s so we convert to kts
106     static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
107
108     double ft_s = cur_fdm_state->get_V_ground_speed() 
109         * speedup_node->getIntValue();
110     double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
111
112     return kts;
113 }
114
115
116 void FGAutopilot::MakeTargetWPStr( double distance ) {
117     static time_t last_time = 0;
118     time_t current_time = time(NULL);
119     if ( last_time == current_time ) {
120         return;
121     }
122
123     last_time = current_time;
124
125     double accum = 0.0;
126
127     int size = globals->get_route()->size();
128
129     // start by wiping the strings
130     TargetWP1Str[0] = 0;
131     TargetWP2Str[0] = 0;
132     TargetWP3Str[0] = 0;
133
134     // current route
135     if ( size > 0 ) {
136         SGWayPoint wp1 = globals->get_route()->get_waypoint( 0 );
137         accum += distance;
138         double eta = accum * SG_METER_TO_NM / get_ground_speed();
139         if ( eta >= 100.0 ) { eta = 99.999; }
140         int major, minor;
141         if ( eta < (1.0/6.0) ) {
142             // within 10 minutes, bump up to min/secs
143             eta *= 60.0;
144         }
145         major = (int)eta;
146         minor = (int)((eta - (int)eta) * 60.0);
147         sprintf( TargetWP1Str, "%s %.2f NM  ETA %d:%02d",
148                  wp1.get_id().c_str(),
149                  accum*SG_METER_TO_NM, major, minor );
150     }
151
152     // next route
153     if ( size > 1 ) {
154         SGWayPoint wp2 = globals->get_route()->get_waypoint( 1 );
155         accum += wp2.get_distance();
156         
157         double eta = accum * SG_METER_TO_NM / get_ground_speed();
158         if ( eta >= 100.0 ) { eta = 99.999; }
159         int major, minor;
160         if ( eta < (1.0/6.0) ) {
161             // within 10 minutes, bump up to min/secs
162             eta *= 60.0;
163         }
164         major = (int)eta;
165         minor = (int)((eta - (int)eta) * 60.0);
166         sprintf( TargetWP2Str, "%s %.2f NM  ETA %d:%02d",
167                  wp2.get_id().c_str(),
168                  accum*SG_METER_TO_NM, major, minor );
169     }
170
171     // next route
172     if ( size > 2 ) {
173         for ( int i = 2; i < size; ++i ) {
174             accum += globals->get_route()->get_waypoint( i ).get_distance();
175         }
176         
177         SGWayPoint wpn = globals->get_route()->get_waypoint( size - 1 );
178
179         double eta = accum * SG_METER_TO_NM / get_ground_speed();
180         if ( eta >= 100.0 ) { eta = 99.999; }
181         int major, minor;
182         if ( eta < (1.0/6.0) ) {
183             // within 10 minutes, bump up to min/secs
184             eta *= 60.0;
185         }
186         major = (int)eta;
187         minor = (int)((eta - (int)eta) * 60.0);
188         sprintf( TargetWP3Str, "%s %.2f NM  ETA %d:%02d",
189                  wpn.get_id().c_str(),
190                  accum*SG_METER_TO_NM, major, minor );
191     }
192 }
193
194
195 void FGAutopilot::update_old_control_values() {
196     old_aileron = globals->get_controls()->get_aileron();
197     old_elevator = globals->get_controls()->get_elevator();
198     old_elevator_trim = globals->get_controls()->get_elevator_trim();
199     old_rudder = globals->get_controls()->get_rudder();
200 }
201
202
203 // Initialize autopilot subsystem
204
205 void FGAutopilot::init ()
206 {
207     SG_LOG( SG_AUTOPILOT, SG_INFO, "Init AutoPilot Subsystem" );
208
209     // bind data input property nodes...
210     latitude_node = fgGetNode("/position/latitude-deg", true);
211     longitude_node = fgGetNode("/position/longitude-deg", true);
212     altitude_node = fgGetNode("/position/altitude-ft", true);
213     altitude_agl_node = fgGetNode("/position/altitude-agl-ft", true);
214     vertical_speed_node = fgGetNode("/velocities/vertical-speed-fps", true);
215     heading_node = fgGetNode("/orientation/heading-deg", true);
216     roll_node = fgGetNode("/orientation/roll-deg", true);
217     pitch_node = fgGetNode("/orientation/pitch-deg", true);
218
219     // bind config property nodes...
220     TargetClimbRate
221         = fgGetNode("/autopilot/config/target-climb-rate-fpm", true);
222     TargetDescentRate
223         = fgGetNode("/autopilot/config/target-descent-rate-fpm", true);
224     min_climb = fgGetNode("/autopilot/config/min-climb-speed-kt", true);
225     best_climb = fgGetNode("/autopilot/config/best-climb-speed-kt", true);
226     elevator_adj_factor
227         = fgGetNode("/autopilot/config/elevator-adj-factor", true);
228     integral_contrib
229         = fgGetNode("/autopilot/config/integral-contribution", true);
230     zero_pitch_throttle
231         = fgGetNode("/autopilot/config/zero-pitch-throttle", true);
232     zero_pitch_trim_full_throttle
233         = fgGetNode("/autopilot/config/zero-pitch-trim-full-throttle", true);
234     current_throttle = fgGetNode("/controls/throttle");
235
236     // initialize config properties with defaults (in case config isn't there)
237     if ( TargetClimbRate->getFloatValue() < 1 )
238         fgSetFloat( "/autopilot/config/target-climb-rate-fpm", 500);
239     if ( TargetDescentRate->getFloatValue() < 1 )
240         fgSetFloat( "/autopilot/config/target-descent-rate-fpm", 1000 );
241     if ( min_climb->getFloatValue() < 1)
242         fgSetFloat( "/autopilot/config/min-climb-speed-kt", 70 );
243     if (best_climb->getFloatValue() < 1)
244         fgSetFloat( "/autopilot/config/best-climb-speed-kt", 120 );
245     if (elevator_adj_factor->getFloatValue() < 1)
246         fgSetFloat( "/autopilot/config/elevator-adj-factor", 5000 );
247     if ( integral_contrib->getFloatValue() < 0.0000001 )
248         fgSetFloat( "/autopilot/config/integral-contribution", 0.01 );
249     if ( zero_pitch_throttle->getFloatValue() < 0.0000001 )
250         fgSetFloat( "/autopilot/config/zero-pitch-throttle", 0.60 );
251     if ( zero_pitch_trim_full_throttle->getFloatValue() < 0.0000001 )
252         fgSetFloat( "/autopilot/config/zero-pitch-trim-full-throttle", 0.15 );
253
254     /* set defaults */
255     heading_hold = false ;      // turn the heading hold off
256     altitude_hold = false ;     // turn the altitude hold off
257     auto_throttle = false ;     // turn the auto throttle off
258     heading_mode = DEFAULT_AP_HEADING_LOCK;
259
260     DGTargetHeading = fgGetDouble("/autopilot/settings/heading-bug-deg");
261     TargetHeading = fgGetDouble("/autopilot/settings/heading-bug-deg");
262     TargetAltitude = fgGetDouble("/autopilot/settings/altitude-ft") * SG_FEET_TO_METER;
263
264     // Initialize target location to startup location
265     old_lat = latitude_node->getDoubleValue();
266     old_lon = longitude_node->getDoubleValue();
267     // set_WayPoint( old_lon, old_lat, 0.0, "default" );
268
269     MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
270         
271     alt_error_accum = 0.0;
272     climb_error_accum = 0.0;
273
274     MakeTargetAltitudeStr( TargetAltitude );
275     MakeTargetHeadingStr( TargetHeading );
276         
277     // These eventually need to be read from current_aircaft somehow.
278
279     // the maximum roll, in Deg
280     MaxRoll = 20;
281
282     // the deg from heading to start rolling out at, in Deg
283     RollOut = 20;
284
285     // how far can I move the aleron from center.
286     MaxAileron = .2;
287
288     // Smoothing distance for alerion control
289     RollOutSmooth = 10;
290
291     // Hardwired for now should be in options
292     // 25% max control variablilty  0.5 / 2.0
293     disengage_threshold = 1.0;
294
295 #if !defined( USING_SLIDER_CLASS )
296     MaxRollAdjust = 2 * MaxRoll;
297     RollOutAdjust = 2 * RollOut;
298     MaxAileronAdjust = 2 * MaxAileron;
299     RollOutSmoothAdjust = 2 * RollOutSmooth;
300 #endif  // !defined( USING_SLIDER_CLASS )
301
302     update_old_control_values();
303 };
304
305 void
306 FGAutopilot::bind ()
307 {
308     // Autopilot control property get/set bindings
309     fgTie("/autopilot/locks/altitude", this,
310           &FGAutopilot::getAPAltitudeLock, &FGAutopilot::setAPAltitudeLock);
311     fgSetArchivable("/autopilot/locks/altitude");
312     fgTie("/autopilot/settings/altitude-ft", this,
313           &FGAutopilot::getAPAltitude, &FGAutopilot::setAPAltitude);
314     fgSetArchivable("/autopilot/settings/altitude-ft");
315     fgTie("/autopilot/locks/glide-slope", this,
316           &FGAutopilot::getAPGSLock, &FGAutopilot::setAPGSLock);
317     fgSetArchivable("/autopilot/locks/glide-slope");
318     fgSetDouble("/autopilot/settings/altitude-ft", 3000.0f);
319     fgTie("/autopilot/locks/terrain", this,
320           &FGAutopilot::getAPTerrainLock, &FGAutopilot::setAPTerrainLock);
321     fgSetArchivable("/autopilot/locks/terrain");
322     fgTie("/autopilot/settings/climb-rate-fpm", this,
323           &FGAutopilot::getAPClimb, &FGAutopilot::setAPClimb, false);
324     fgSetArchivable("/autopilot/settings/climb-rate-fpm");
325     fgTie("/autopilot/locks/heading", this,
326           &FGAutopilot::getAPHeadingLock, &FGAutopilot::setAPHeadingLock);
327     fgSetArchivable("/autopilot/locks/heading");
328     fgTie("/autopilot/settings/heading-bug-deg", this,
329           &FGAutopilot::getAPHeadingBug, &FGAutopilot::setAPHeadingBug);
330     fgSetArchivable("/autopilot/settings/heading-bug-deg");
331     fgSetDouble("/autopilot/settings/heading-bug-deg", 0.0f);
332     fgTie("/autopilot/locks/wing-leveler", this,
333           &FGAutopilot::getAPWingLeveler, &FGAutopilot::setAPWingLeveler);
334     fgSetArchivable("/autopilot/locks/wing-leveler");
335     fgTie("/autopilot/locks/nav[0]", this,
336           &FGAutopilot::getAPNAV1Lock, &FGAutopilot::setAPNAV1Lock);
337     fgSetArchivable("/autopilot/locks/nav[0]");
338     fgTie("/autopilot/locks/auto-throttle", this,
339           &FGAutopilot::getAPAutoThrottleLock,
340           &FGAutopilot::setAPAutoThrottleLock);
341     fgSetArchivable("/autopilot/locks/auto-throttle");
342     fgTie("/autopilot/control-overrides/rudder", this,
343           &FGAutopilot::getAPRudderControl,
344           &FGAutopilot::setAPRudderControl);
345     fgSetArchivable("/autopilot/control-overrides/rudder");
346     fgTie("/autopilot/control-overrides/elevator", this,
347           &FGAutopilot::getAPElevatorControl,
348           &FGAutopilot::setAPElevatorControl);
349     fgSetArchivable("/autopilot/control-overrides/elevator");
350     fgTie("/autopilot/control-overrides/throttle", this,
351           &FGAutopilot::getAPThrottleControl,
352           &FGAutopilot::setAPThrottleControl);
353     fgSetArchivable("/autopilot/control-overrides/throttle");
354 }
355
356 void
357 FGAutopilot::unbind ()
358 {
359 }
360
361 // Reset the autopilot system
362 void FGAutopilot::reset() {
363
364     heading_hold = false ;      // turn the heading hold off
365     altitude_hold = false ;     // turn the altitude hold off
366     auto_throttle = false ;     // turn the auto throttle off
367     heading_mode = DEFAULT_AP_HEADING_LOCK;
368
369     // TargetHeading = 0.0;     // default direction, due north
370     MakeTargetHeadingStr( TargetHeading );                      
371         
372     // TargetAltitude = 3000;   // default altitude in meters
373     MakeTargetAltitudeStr( TargetAltitude );
374         
375     alt_error_accum = 0.0;
376     climb_error_accum = 0.0;
377         
378     update_old_control_values();
379
380     sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id") );
381         
382     MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
383 }
384
385
386 static double NormalizeDegrees( double Input ) {
387     // normalize the input to the range (-180,180]
388     // Input should not be greater than -360 to 360.
389     // Current rules send the output to an undefined state.
390     while ( Input > 180.0 ) { Input -= 360.0; }
391     while ( Input <= -180.0 ) { Input += 360.0; }
392
393     return Input;
394 };
395
396 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
397     // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
398     //assert(x1 != x2); // Divide by zero error.  Cold abort for now
399
400         // Could be
401         // static double y = 0.0;
402         // double dx = x2 -x1;
403         // if( (dx < -SG_EPSILON ) || ( dx > SG_EPSILON ) )
404         // {
405
406     double m, b, y;          // the constants to find in y=mx+b
407     // double m, b;
408
409     m = ( y2 - y1 ) / ( x2 - x1 );   // calculate the m
410
411     b = y1 - m * x1;       // calculate the b
412
413     y = m * x + b;       // the final calculation
414
415     // }
416
417     return ( y );
418
419 };
420
421
422 void
423 FGAutopilot::update (int dt)
424 {
425     // Remove the following lines when the calling funcitons start
426     // passing in the data pointer
427
428     // get control settings 
429         
430     double lat = latitude_node->getDoubleValue();
431     double lon = longitude_node->getDoubleValue();
432     double alt = altitude_node->getDoubleValue() * SG_FEET_TO_METER;
433
434     SG_LOG( SG_ALL, SG_DEBUG, "FGAutopilot::run()  lat = " << lat <<
435             "  lon = " << lon << "  alt = " << alt );
436         
437 #ifdef FG_FORCE_AUTO_DISENGAGE
438     // see if somebody else has changed them
439     if( fabs(aileron - old_aileron) > disengage_threshold ||
440         fabs(elevator - old_elevator) > disengage_threshold ||
441         fabs(elevator_trim - old_elevator_trim) > 
442         disengage_threshold ||          
443         fabs(rudder - old_rudder) > disengage_threshold )
444     {
445         // if controls changed externally turn autopilot off
446         waypoint_hold = false ;   // turn the target hold off
447         heading_hold = false ;    // turn the heading hold off
448         altitude_hold = false ;   // turn the altitude hold off
449         terrain_follow = false;   // turn the terrain_follow hold off
450         // auto_throttle = false; // turn the auto_throttle off
451
452         // stash this runs control settings
453         old_aileron = aileron;
454         old_elevator = elevator;
455         old_elevator_trim = elevator_trim;
456         old_rudder = rudder;
457         
458         return 0;
459     }
460 #endif
461         
462     // heading hold
463     if ( heading_hold == true ) {
464         if ( heading_mode == FG_DG_HEADING_LOCK ) {
465             TargetHeading = DGTargetHeading + FGSteam::get_DG_err();
466             while ( TargetHeading <   0.0 ) { TargetHeading += 360.0; }
467             while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
468             MakeTargetHeadingStr( TargetHeading );
469         } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
470             // we don't set a specific target heading in
471             // TC_HEADING_LOCK mode, we instead try to keep the turn
472             // coordinator zero'd
473         } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
474             // leave "true" target heading as is
475             while ( TargetHeading <   0.0 ) { TargetHeading += 360.0; }
476             while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
477             MakeTargetHeadingStr( TargetHeading );
478         } else if ( heading_mode == FG_HEADING_NAV1 ) {
479             // track the NAV1 heading needle deflection
480
481             // determine our current radial position relative to the
482             // navaid in "true" heading.
483             double cur_radial = current_radiostack->get_nav1_heading();
484             if ( current_radiostack->get_nav1_loc() ) {
485                 // ILS localizers radials are already "true" in our
486                 // database
487             } else {
488                 cur_radial += current_radiostack->get_nav1_magvar();
489             }
490             if ( current_radiostack->get_nav1_from_flag() ) {
491                 cur_radial += 180.0;
492                 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
493             }
494
495             // determine the target radial in "true" heading
496             double tgt_radial = current_radiostack->get_nav1_radial();
497             if ( current_radiostack->get_nav1_loc() ) {
498                 // ILS localizers radials are already "true" in our
499                 // database
500             } else {
501                 // VOR radials need to have that vor's offset added in
502                 tgt_radial += current_radiostack->get_nav1_magvar();
503             }
504
505             // determine the heading adjustment needed.
506             double adjustment = 
507                 current_radiostack->get_nav1_heading_needle_deflection()
508                 * (current_radiostack->get_nav1_loc_dist() * SG_METER_TO_NM);
509             SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
510
511             // determine the target heading to fly to intercept the
512             // tgt_radial
513             TargetHeading = tgt_radial + adjustment; 
514             while ( TargetHeading <   0.0 ) { TargetHeading += 360.0; }
515             while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
516
517             MakeTargetHeadingStr( TargetHeading );
518         } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
519             // update target heading to waypoint
520
521             double wp_course, wp_distance;
522
523 #ifdef DO_fgAP_CORRECTED_COURSE
524             // compute course made good
525             // this needs lots of special casing before use
526             double course, reverse, distance, corrected_course;
527             // need to test for iter
528             geo_inverse_wgs_84( 0, //fgAPget_altitude(),
529                                 old_lat,
530                                 old_lon,
531                                 lat,
532                                 lon,
533                                 &course,
534                                 &reverse,
535                                 &distance );
536 #endif // DO_fgAP_CORRECTED_COURSE
537
538             // compute course to way_point
539             // need to test for iter
540             SGWayPoint wp = globals->get_route()->get_first();
541             wp.CourseAndDistance( lon, lat, alt, 
542                                   &wp_course, &wp_distance );
543
544 #ifdef DO_fgAP_CORRECTED_COURSE
545             corrected_course = course - wp_course;
546             if( fabs(corrected_course) > 0.1 )
547                 printf("fgAP: course %f  wp_course %f  %f  %f\n",
548                        course, wp_course, fabs(corrected_course),
549                        distance );
550 #endif // DO_fgAP_CORRECTED_COURSE
551                 
552             if ( wp_distance > 100 ) {
553                 // corrected_course = course - wp_course;
554                 TargetHeading = NormalizeDegrees(wp_course);
555             } else {
556                 // pop off this waypoint from the list
557                 if ( globals->get_route()->size() ) {
558                     globals->get_route()->delete_first();
559                 }
560
561                 // see if there are more waypoints on the list
562                 if ( globals->get_route()->size() ) {
563                     // more waypoints
564                     set_HeadingMode( FG_HEADING_WAYPOINT );
565                 } else {
566                     // end of the line
567                     heading_mode = FG_TRUE_HEADING_LOCK;
568                     // use current heading
569                     TargetHeading = heading_node->getDoubleValue();
570                 }
571             }
572             MakeTargetHeadingStr( TargetHeading );
573             // Force this just in case
574             TargetDistance = wp_distance;
575             MakeTargetWPStr( wp_distance );
576         }
577
578         if ( heading_mode == FG_TC_HEADING_LOCK ) {
579             // drive the turn coordinator to zero
580             double turn = FGSteam::get_TC_std();
581             double AileronSet = -turn / 2.0;
582             SG_CLAMP_RANGE( AileronSet, -1.0, 1.0 );
583             globals->get_controls()->set_aileron( AileronSet );
584             globals->get_controls()->set_rudder( AileronSet / 4.0 );
585         } else {
586             // steer towards the target heading
587
588             double RelHeading;
589             double TargetRoll;
590             double RelRoll;
591             double AileronSet;
592
593             RelHeading
594                 = NormalizeDegrees( TargetHeading
595                                     - heading_node->getDoubleValue() );
596             // figure out how far off we are from desired heading
597
598             // Now it is time to deterime how far we should be rolled.
599             SG_LOG( SG_AUTOPILOT, SG_DEBUG, "RelHeading: " << RelHeading );
600
601
602             // Check if we are further from heading than the roll out point
603             if ( fabs( RelHeading ) > RollOut ) {
604                 // set Target Roll to Max in desired direction
605                 if ( RelHeading < 0 ) {
606                     TargetRoll = 0 - MaxRoll;
607                 } else {
608                     TargetRoll = MaxRoll;
609                 }
610             } else {
611                 // We have to calculate the Target roll
612
613                 // This calculation engine thinks that the Target roll
614                 // should be a line from (RollOut,MaxRoll) to (-RollOut,
615                 // -MaxRoll) I hope this works well.  If I get ambitious
616                 // some day this might become a fancier curve or
617                 // something.
618
619                 TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
620                                                 -MaxRoll, RollOut,
621                                                 MaxRoll );
622             }
623
624             // Target Roll has now been Found.
625
626             // Compare Target roll to Current Roll, Generate Rel Roll
627
628             SG_LOG( SG_COCKPIT, SG_BULK, "TargetRoll: " << TargetRoll );
629
630             RelRoll = NormalizeDegrees( TargetRoll 
631                                         - roll_node->getDoubleValue() );
632
633             // Check if we are further from heading than the roll out
634             // smooth point
635             if ( fabs( RelRoll ) > RollOutSmooth ) {
636                 // set Target Roll to Max in desired direction
637                 if ( RelRoll < 0 ) {
638                 AileronSet = 0 - MaxAileron;
639                 } else {
640                     AileronSet = MaxAileron;
641                 }
642             } else {
643                 AileronSet = LinearExtrapolate( RelRoll, -RollOutSmooth,
644                                                 -MaxAileron,
645                                                 RollOutSmooth,
646                                                 MaxAileron );
647             }
648
649             globals->get_controls()->set_aileron( AileronSet );
650             globals->get_controls()->set_rudder( AileronSet / 4.0 );
651             // controls.set_rudder( 0.0 );
652         }
653     }
654
655     // altitude hold
656     if ( altitude_hold ) {
657         double climb_rate;
658         double speed, max_climb, error;
659         double prop_error, int_error;
660         double prop_adj, int_adj, total_adj;
661
662         if ( altitude_mode == FG_ALTITUDE_LOCK ) {
663             climb_rate =
664                 ( TargetAltitude - FGSteam::get_ALT_ft() * SG_FEET_TO_METER ) * 8.0;
665         } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
666             double x = current_radiostack->get_nav1_gs_dist();
667             double y = (altitude_node->getDoubleValue()
668                         - current_radiostack->get_nav1_elev()) * SG_FEET_TO_METER;
669             double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
670
671             double target_angle = current_radiostack->get_nav1_target_gs();
672
673             double gs_diff = target_angle - current_angle;
674
675             // convert desired vertical path angle into a climb rate
676             double des_angle = current_angle - 10 * gs_diff;
677
678             // convert to meter/min
679             double horiz_vel = cur_fdm_state->get_V_ground_speed()
680                 * SG_FEET_TO_METER * 60.0;
681             climb_rate = -sin( des_angle * SGD_DEGREES_TO_RADIANS ) * horiz_vel;
682             /* climb_error_accum += gs_diff * 2.0; */
683             /* climb_rate = gs_diff * 200.0 + climb_error_accum; */
684         } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
685             // brain dead ground hugging with no look ahead
686             climb_rate =
687                 ( TargetAGL - altitude_agl_node->getDoubleValue()
688                   * SG_FEET_TO_METER ) * 16.0;
689         } else {
690             // just try to zero out rate of climb ...
691             climb_rate = 0.0;
692         }
693
694         speed = get_speed();
695
696         if ( speed < min_climb->getFloatValue() ) {
697             max_climb = 0.0;
698         } else if ( speed < best_climb->getFloatValue() ) {
699             max_climb = ((best_climb->getFloatValue()
700                           - min_climb->getFloatValue())
701                          - (best_climb->getFloatValue() - speed)) 
702                 * fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER) 
703                 / (best_climb->getFloatValue() - min_climb->getFloatValue());
704         } else {                        
705             max_climb = ( speed - best_climb->getFloatValue() ) * 10.0
706                 + fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER);
707         }
708
709         // this first one could be optional if we wanted to allow
710         // better climb performance assuming we have the airspeed to
711         // support it.
712         if ( climb_rate >
713              fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER) ) {
714             climb_rate
715                 = fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER);
716         }
717
718         if ( climb_rate > max_climb ) {
719             climb_rate = max_climb;
720         }
721
722         if ( climb_rate <
723              -fabs(TargetDescentRate->getFloatValue() * SG_FEET_TO_METER) ) {
724             climb_rate
725                 = -fabs(TargetDescentRate->getFloatValue() * SG_FEET_TO_METER);
726         }
727
728         error = vertical_speed_node->getDoubleValue() * 60
729             - climb_rate * SG_METER_TO_FEET;
730
731         // accumulate the error under the curve ... this really should
732         // be *= delta t
733         alt_error_accum += error;
734
735         // calculate integral error, and adjustment amount
736         int_error = alt_error_accum;
737         // printf("error = %.2f  int_error = %.2f\n", error, int_error);
738         int_adj = int_error / elevator_adj_factor->getFloatValue();
739
740         // caclulate proportional error
741         prop_error = error;
742         prop_adj = prop_error / elevator_adj_factor->getDoubleValue();
743
744         total_adj = ((double) 1.0 - (double) integral_contrib->getFloatValue()) * prop_adj
745             + (double) integral_contrib->getFloatValue() * int_adj;
746
747         // stop on autopilot trim at 30% +/-
748 //      if ( total_adj > 0.3 ) {
749 //           total_adj = 0.3;
750 //       } else if ( total_adj < -0.3 ) {
751 //           total_adj = -0.3;
752 //       }
753
754         // adjust for throttle pitch gain
755         total_adj += ((current_throttle->getFloatValue() - zero_pitch_throttle->getFloatValue())
756                      / (1 - zero_pitch_throttle->getFloatValue()))
757                      * zero_pitch_trim_full_throttle->getFloatValue();
758
759         globals->get_controls()->set_elevator_trim( total_adj );
760     }
761
762     // auto throttle
763     if ( auto_throttle ) {
764         double error;
765         double prop_error, int_error;
766         double prop_adj, int_adj, total_adj;
767
768         error = TargetSpeed - get_speed();
769
770         // accumulate the error under the curve ... this really should
771         // be *= delta t
772         speed_error_accum += error;
773         if ( speed_error_accum > 2000.0 ) {
774             speed_error_accum = 2000.0;
775         }
776         else if ( speed_error_accum < -2000.0 ) {
777             speed_error_accum = -2000.0;
778         }
779
780         // calculate integral error, and adjustment amount
781         int_error = speed_error_accum;
782
783         // printf("error = %.2f  int_error = %.2f\n", error, int_error);
784         int_adj = int_error / 200.0;
785
786         // caclulate proportional error
787         prop_error = error;
788         prop_adj = 0.5 + prop_error / 50.0;
789
790         total_adj = 0.9 * prop_adj + 0.1 * int_adj;
791         if ( total_adj > 1.0 ) {
792             total_adj = 1.0;
793         }
794         else if ( total_adj < 0.0 ) {
795             total_adj = 0.0;
796         }
797
798         globals->get_controls()->set_throttle( FGControls::ALL_ENGINES,
799                                                total_adj );
800     }
801
802 #ifdef THIS_CODE_IS_NOT_USED
803     if (Mode == 2) // Glide slope hold
804         {
805             double RelSlope;
806             double RelElevator;
807
808             // First, calculate Relative slope and normalize it
809             RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
810
811             // Now calculate the elevator offset from current angle
812             if ( abs(RelSlope) > SlopeSmooth )
813                 {
814                     if ( RelSlope < 0 )     //  set RelElevator to max in the correct direction
815                         RelElevator = -MaxElevator;
816                     else
817                         RelElevator = MaxElevator;
818                 }
819
820             else
821                 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
822
823             // set the elevator
824             fgElevMove(RelElevator);
825
826         }
827 #endif // THIS_CODE_IS_NOT_USED
828
829     // stash this runs control settings
830     //  update_old_control_values();
831     old_aileron = globals->get_controls()->get_aileron();
832     old_elevator = globals->get_controls()->get_elevator();
833     old_elevator_trim = globals->get_controls()->get_elevator_trim();
834     old_rudder = globals->get_controls()->get_rudder();
835
836     // for cross track error
837     old_lat = lat;
838     old_lon = lon;
839         
840     // Ok, we are done
841     SG_LOG( SG_ALL, SG_DEBUG, "FGAutopilot::run( returns )" );
842 }
843
844
845 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
846     heading_mode = mode;
847
848     if ( heading_mode == FG_DG_HEADING_LOCK ) {
849         // set heading hold to current heading (as read from DG)
850         // ... no, leave target heading along ... just use the current
851         // heading bug value
852         //  DGTargetHeading = FGSteam::get_DG_deg();
853     } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
854         // set autopilot to hold a zero turn (as reported by the TC)
855     } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
856         // set heading hold to current heading
857         TargetHeading = heading_node->getDoubleValue();
858     } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
859         if ( globals->get_route()->size() ) {
860             double course, distance;
861
862             old_lat = latitude_node->getDoubleValue();
863             old_lon = longitude_node->getDoubleValue();
864
865             waypoint = globals->get_route()->get_first();
866             waypoint.CourseAndDistance( longitude_node->getDoubleValue(),
867                                         latitude_node->getDoubleValue(),
868                                         altitude_node->getDoubleValue()
869                                         * SG_FEET_TO_METER,
870                                         &course, &distance );
871             TargetHeading = course;
872             TargetDistance = distance;
873             MakeTargetLatLonStr( waypoint.get_target_lat(),
874                                  waypoint.get_target_lon() );
875             MakeTargetWPStr( distance );
876
877             if ( waypoint.get_target_alt() > 0.0 ) {
878                 TargetAltitude = waypoint.get_target_alt();
879                 altitude_mode = FG_ALTITUDE_LOCK;
880                 set_AltitudeEnabled( true );
881                 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
882             }
883
884             SG_LOG( SG_COCKPIT, SG_INFO, " set_HeadingMode: ( "
885                     << get_TargetLatitude()  << " "
886                     << get_TargetLongitude() << " ) "
887                     );
888         } else {
889             // no more way points, default to heading lock.
890             heading_mode = FG_TC_HEADING_LOCK;
891         }
892     }
893
894     MakeTargetHeadingStr( TargetHeading );                      
895     update_old_control_values();
896 }
897
898
899 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
900     altitude_mode = mode;
901
902     alt_error_accum = 0.0;
903
904
905     if ( altitude_mode == FG_ALTITUDE_LOCK ) {
906         if ( TargetAltitude < altitude_agl_node->getDoubleValue()
907              * SG_FEET_TO_METER ) {
908         }
909
910         if ( !strcmp("/sim/startup/units", "feet") ) {
911             MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
912         } else {
913             MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
914         }
915     } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
916         climb_error_accum = 0.0;
917
918     } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
919         TargetAGL = altitude_agl_node->getDoubleValue() * SG_FEET_TO_METER;
920
921         if ( !strcmp("/sim/startup/units", "feet") ) {
922             MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
923         } else {
924             MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
925         }
926     }
927     
928     update_old_control_values();
929     SG_LOG( SG_COCKPIT, SG_INFO, " set_AltitudeMode():" );
930 }
931
932
933 void FGAutopilot::AltitudeSet( double new_altitude ) {
934     double target_alt = new_altitude;
935
936     if ( !strcmp("/sim/startup/units", "feet") ) {
937         target_alt = new_altitude * SG_FEET_TO_METER;
938     }
939
940     if( target_alt < scenery.get_cur_elev() ) {
941         target_alt = scenery.get_cur_elev();
942     }
943
944     TargetAltitude = target_alt;
945     altitude_mode = FG_ALTITUDE_LOCK;
946
947     if ( !strcmp("/sim/startup/units", "feet") ) {
948         target_alt *= SG_METER_TO_FEET;
949     }
950     // ApAltitudeDialogInput->setValue((float)target_alt);
951     MakeTargetAltitudeStr( target_alt );
952         
953     update_old_control_values();
954 }
955
956
957 void FGAutopilot::AltitudeAdjust( double inc )
958 {
959     double target_alt, target_agl;
960
961     if ( !strcmp("/sim/startup/units", "feet") ) {
962         target_alt = TargetAltitude * SG_METER_TO_FEET;
963         target_agl = TargetAGL * SG_METER_TO_FEET;
964     } else {
965         target_alt = TargetAltitude;
966         target_agl = TargetAGL;
967     }
968
969     if ( fabs((int)(target_alt / inc) * inc - target_alt) < SG_EPSILON ) {
970         target_alt += inc;
971     } else {
972         target_alt = ( int ) ( target_alt / inc ) * inc + inc;
973     }
974
975     if ( fabs((int)(target_agl / inc) * inc - target_agl) < SG_EPSILON ) {
976         target_agl += inc;
977     } else {
978         target_agl = ( int ) ( target_agl / inc ) * inc + inc;
979     }
980
981     if ( !strcmp("/sim/startup/units", "feet") ) {
982         target_alt *= SG_FEET_TO_METER;
983         target_agl *= SG_FEET_TO_METER;
984     }
985
986     TargetAltitude = target_alt;
987     TargetAGL = target_agl;
988         
989     if ( !strcmp("/sim/startup/units", "feet") )
990         target_alt *= SG_METER_TO_FEET;
991     if ( !strcmp("/sim/startup/units", "feet") )
992         target_agl *= SG_METER_TO_FEET;
993
994     if ( altitude_mode == FG_ALTITUDE_LOCK ) {
995         MakeTargetAltitudeStr( target_alt );
996     } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
997         MakeTargetAltitudeStr( target_agl );
998     }
999
1000     update_old_control_values();
1001 }
1002
1003
1004 void FGAutopilot::HeadingAdjust( double inc ) {
1005     if ( heading_mode != FG_DG_HEADING_LOCK
1006          && heading_mode != FG_TRUE_HEADING_LOCK )
1007     {
1008         heading_mode = FG_DG_HEADING_LOCK;
1009     }
1010
1011     if ( heading_mode == FG_DG_HEADING_LOCK ) {
1012         double target = ( int ) ( DGTargetHeading / inc ) * inc + inc;
1013         DGTargetHeading = NormalizeDegrees( target );
1014     } else {
1015         double target = ( int ) ( TargetHeading / inc ) * inc + inc;
1016         TargetHeading = NormalizeDegrees( target );
1017     }
1018
1019     update_old_control_values();
1020 }
1021
1022
1023 void FGAutopilot::HeadingSet( double new_heading ) {
1024     if( heading_mode == FG_TRUE_HEADING_LOCK ) {
1025         new_heading = NormalizeDegrees( new_heading );
1026         TargetHeading = new_heading;
1027         MakeTargetHeadingStr( TargetHeading );
1028     } else {
1029         heading_mode = FG_DG_HEADING_LOCK;
1030
1031         new_heading = NormalizeDegrees( new_heading );
1032         DGTargetHeading = new_heading;
1033         // following cast needed ambiguous plib
1034         // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1035         MakeTargetHeadingStr( DGTargetHeading );
1036     }
1037     update_old_control_values();
1038 }
1039
1040 void FGAutopilot::AutoThrottleAdjust( double inc ) {
1041     double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
1042
1043     TargetSpeed = target;
1044 }
1045
1046
1047 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
1048     auto_throttle = value;
1049
1050     if ( auto_throttle == true ) {
1051         TargetSpeed = fgGetDouble("/velocities/airspeed-kt");
1052         speed_error_accum = 0.0;
1053     }
1054
1055     update_old_control_values();
1056     SG_LOG( SG_COCKPIT, SG_INFO, " fgAPSetAutoThrottle: ("
1057             << auto_throttle << ") " << TargetSpeed );
1058 }
1059
1060
1061
1062 \f
1063 ////////////////////////////////////////////////////////////////////////
1064 // Kludged methods for tying to properties.
1065 //
1066 // These should change eventually; they all used to be static
1067 // functions.
1068 ////////////////////////////////////////////////////////////////////////
1069
1070 /**
1071  * Get the autopilot altitude lock (true=on).
1072  */
1073 bool
1074 FGAutopilot::getAPAltitudeLock () const
1075 {
1076     return (get_AltitudeEnabled() &&
1077             get_AltitudeMode()
1078             == FGAutopilot::FG_ALTITUDE_LOCK);
1079 }
1080
1081
1082 /**
1083  * Set the autopilot altitude lock (true=on).
1084  */
1085 void
1086 FGAutopilot::setAPAltitudeLock (bool lock)
1087 {
1088   if (lock)
1089     set_AltitudeMode(FGAutopilot::FG_ALTITUDE_LOCK);
1090   if (get_AltitudeMode() == FGAutopilot::FG_ALTITUDE_LOCK)
1091     set_AltitudeEnabled(lock);
1092 }
1093
1094
1095 /**
1096  * Get the autopilot target altitude in feet.
1097  */
1098 double
1099 FGAutopilot::getAPAltitude () const
1100 {
1101   return get_TargetAltitude() * SG_METER_TO_FEET;
1102 }
1103
1104
1105 /**
1106  * Set the autopilot target altitude in feet.
1107  */
1108 void
1109 FGAutopilot::setAPAltitude (double altitude)
1110 {
1111   set_TargetAltitude( altitude * SG_FEET_TO_METER );
1112 }
1113
1114 /**
1115  * Get the autopilot altitude lock (true=on).
1116  */
1117 bool
1118 FGAutopilot::getAPGSLock () const
1119 {
1120     return (get_AltitudeEnabled() &&
1121             (get_AltitudeMode()
1122              == FGAutopilot::FG_ALTITUDE_GS1));
1123 }
1124
1125
1126 /**
1127  * Set the autopilot altitude lock (true=on).
1128  */
1129 void
1130 FGAutopilot::setAPGSLock (bool lock)
1131 {
1132   if (lock)
1133     set_AltitudeMode(FGAutopilot::FG_ALTITUDE_GS1);
1134   if (get_AltitudeMode() == FGAutopilot::FG_ALTITUDE_GS1)
1135     set_AltitudeEnabled(lock);
1136 }
1137
1138
1139 /**
1140  * Get the autopilot terrain lock (true=on).
1141  */
1142 bool
1143 FGAutopilot::getAPTerrainLock () const
1144 {
1145     return (get_AltitudeEnabled() &&
1146             (get_AltitudeMode()
1147              == FGAutopilot::FG_ALTITUDE_TERRAIN));
1148 }
1149
1150
1151 /**
1152  * Set the autopilot terrain lock (true=on).
1153  */
1154 void
1155 FGAutopilot::setAPTerrainLock (bool lock)
1156 {
1157   if (lock) {
1158     set_AltitudeMode(FGAutopilot::FG_ALTITUDE_TERRAIN);
1159     set_TargetAGL(fgGetFloat("/position/altitude-agl-ft") *
1160                   SG_FEET_TO_METER);
1161   }
1162   if (get_AltitudeMode() == FGAutopilot::FG_ALTITUDE_TERRAIN)
1163     set_AltitudeEnabled(lock);
1164 }
1165
1166
1167 /**
1168  * Get the autopilot target altitude in feet.
1169  */
1170 double
1171 FGAutopilot::getAPClimb () const
1172 {
1173   return get_TargetClimbRate() * SG_METER_TO_FEET;
1174 }
1175
1176
1177 /**
1178  * Set the autopilot target altitude in feet.
1179  */
1180 void
1181 FGAutopilot::setAPClimb (double rate)
1182 {
1183     set_TargetClimbRate( rate * SG_FEET_TO_METER );
1184 }
1185
1186
1187 /**
1188  * Get the autopilot heading lock (true=on).
1189  */
1190 bool
1191 FGAutopilot::getAPHeadingLock () const
1192 {
1193     return
1194       (get_HeadingEnabled() &&
1195        get_HeadingMode() == DEFAULT_AP_HEADING_LOCK);
1196 }
1197
1198
1199 /**
1200  * Set the autopilot heading lock (true=on).
1201  */
1202 void
1203 FGAutopilot::setAPHeadingLock (bool lock)
1204 {
1205   if (lock)
1206     set_HeadingMode(DEFAULT_AP_HEADING_LOCK);
1207   if (get_HeadingMode() == DEFAULT_AP_HEADING_LOCK)
1208     set_HeadingEnabled(lock);
1209 }
1210
1211
1212 /**
1213  * Get the autopilot heading bug in degrees.
1214  */
1215 double
1216 FGAutopilot::getAPHeadingBug () const
1217 {
1218   return get_DGTargetHeading();
1219 }
1220
1221
1222 /**
1223  * Set the autopilot heading bug in degrees.
1224  */
1225 void
1226 FGAutopilot::setAPHeadingBug (double heading)
1227 {
1228   set_DGTargetHeading( heading );
1229 }
1230
1231
1232 /**
1233  * Get the autopilot wing leveler lock (true=on).
1234  */
1235 bool
1236 FGAutopilot::getAPWingLeveler () const
1237 {
1238     return
1239       (get_HeadingEnabled() &&
1240        get_HeadingMode() == FGAutopilot::FG_TC_HEADING_LOCK);
1241 }
1242
1243
1244 /**
1245  * Set the autopilot wing leveler lock (true=on).
1246  */
1247 void
1248 FGAutopilot::setAPWingLeveler (bool lock)
1249 {
1250     if (lock)
1251         set_HeadingMode(FGAutopilot::FG_TC_HEADING_LOCK);
1252     if (get_HeadingMode() == FGAutopilot::FG_TC_HEADING_LOCK)
1253       set_HeadingEnabled(lock);
1254 }
1255
1256 /**
1257  * Return true if the autopilot is locked to NAV1.
1258  */
1259 bool
1260 FGAutopilot::getAPNAV1Lock () const
1261 {
1262   return
1263     (get_HeadingEnabled() &&
1264      get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1);
1265 }
1266
1267
1268 /**
1269  * Set the autopilot NAV1 lock.
1270  */
1271 void
1272 FGAutopilot::setAPNAV1Lock (bool lock)
1273 {
1274   if (lock)
1275     set_HeadingMode(FGAutopilot::FG_HEADING_NAV1);
1276   if (get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1)
1277     set_HeadingEnabled(lock);
1278 }
1279
1280 /**
1281  * Get the autopilot autothrottle lock.
1282  */
1283 bool
1284 FGAutopilot::getAPAutoThrottleLock () const
1285 {
1286   return get_AutoThrottleEnabled();
1287 }
1288
1289
1290 /**
1291  * Set the autothrottle lock.
1292  */
1293 void
1294 FGAutopilot::setAPAutoThrottleLock (bool lock)
1295 {
1296   set_AutoThrottleEnabled(lock);
1297 }
1298
1299
1300 // kludge
1301 double
1302 FGAutopilot::getAPRudderControl () const
1303 {
1304     if (getAPHeadingLock())
1305         return get_TargetHeading();
1306     else
1307         return globals->get_controls()->get_rudder();
1308 }
1309
1310 // kludge
1311 void
1312 FGAutopilot::setAPRudderControl (double value)
1313 {
1314     if (getAPHeadingLock()) {
1315         SG_LOG(SG_GENERAL, SG_DEBUG, "setAPRudderControl " << value );
1316         value -= get_TargetHeading();
1317         HeadingAdjust(value < 0.0 ? -1.0 : 1.0);
1318     } else {
1319         globals->get_controls()->set_rudder(value);
1320     }
1321 }
1322
1323 // kludge
1324 double
1325 FGAutopilot::getAPElevatorControl () const
1326 {
1327   if (getAPAltitudeLock())
1328       return get_TargetAltitude();
1329   else
1330     return globals->get_controls()->get_elevator();
1331 }
1332
1333 // kludge
1334 void
1335 FGAutopilot::setAPElevatorControl (double value)
1336 {
1337     if (value != 0 && getAPAltitudeLock()) {
1338         SG_LOG(SG_GENERAL, SG_DEBUG, "setAPElevatorControl " << value );
1339         value -= get_TargetAltitude();
1340         AltitudeAdjust(value < 0.0 ? 100.0 : -100.0);
1341     } else {
1342         globals->get_controls()->set_elevator(value);
1343     }
1344 }
1345
1346 // kludge
1347 double
1348 FGAutopilot::getAPThrottleControl () const
1349 {
1350   if (getAPAutoThrottleLock())
1351     return 0.0;                 // always resets
1352   else
1353     return globals->get_controls()->get_throttle(0);
1354 }
1355
1356 // kludge
1357 void
1358 FGAutopilot::setAPThrottleControl (double value)
1359 {
1360   if (getAPAutoThrottleLock())
1361     AutoThrottleAdjust(value < 0.0 ? -0.01 : 0.01);
1362   else
1363     globals->get_controls()->set_throttle(FGControls::ALL_ENGINES, value);
1364 }
1365
1366 // end of newauto.cxx