]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/newauto.cxx
Default to glider again when model is not present.
[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     altitude_mode = DEFAULT_AP_ALTITUDE_LOCK;
260
261     DGTargetHeading = fgGetDouble("/autopilot/settings/heading-bug-deg");
262     TargetHeading = fgGetDouble("/autopilot/settings/heading-bug-deg");
263     TargetAltitude = fgGetDouble("/autopilot/settings/altitude-ft") * SG_FEET_TO_METER;
264
265     // Initialize target location to startup location
266     old_lat = latitude_node->getDoubleValue();
267     old_lon = longitude_node->getDoubleValue();
268     // set_WayPoint( old_lon, old_lat, 0.0, "default" );
269
270     MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
271         
272     alt_error_accum = 0.0;
273     climb_error_accum = 0.0;
274
275     MakeTargetAltitudeStr( TargetAltitude );
276     MakeTargetHeadingStr( TargetHeading );
277         
278     // These eventually need to be read from current_aircaft somehow.
279
280     // the maximum roll, in Deg
281     MaxRoll = 20;
282
283     // the deg from heading to start rolling out at, in Deg
284     RollOut = 20;
285
286     // how far can I move the aleron from center.
287     MaxAileron = .2;
288
289     // Smoothing distance for alerion control
290     RollOutSmooth = 10;
291
292     // Hardwired for now should be in options
293     // 25% max control variablilty  0.5 / 2.0
294     disengage_threshold = 1.0;
295
296 #if !defined( USING_SLIDER_CLASS )
297     MaxRollAdjust = 2 * MaxRoll;
298     RollOutAdjust = 2 * RollOut;
299     MaxAileronAdjust = 2 * MaxAileron;
300     RollOutSmoothAdjust = 2 * RollOutSmooth;
301 #endif  // !defined( USING_SLIDER_CLASS )
302
303     update_old_control_values();
304 };
305
306 void
307 FGAutopilot::bind ()
308 {
309     // Autopilot control property get/set bindings
310     fgTie("/autopilot/locks/altitude", this,
311           &FGAutopilot::getAPAltitudeLock, &FGAutopilot::setAPAltitudeLock);
312     fgSetArchivable("/autopilot/locks/altitude");
313     fgTie("/autopilot/settings/altitude-ft", this,
314           &FGAutopilot::getAPAltitude, &FGAutopilot::setAPAltitude);
315     fgSetArchivable("/autopilot/settings/altitude-ft");
316     fgTie("/autopilot/locks/glide-slope", this,
317           &FGAutopilot::getAPGSLock, &FGAutopilot::setAPGSLock);
318     fgSetArchivable("/autopilot/locks/glide-slope");
319     fgSetDouble("/autopilot/settings/altitude-ft", 3000.0f);
320     fgTie("/autopilot/locks/terrain", this,
321           &FGAutopilot::getAPTerrainLock, &FGAutopilot::setAPTerrainLock);
322     fgSetArchivable("/autopilot/locks/terrain");
323     fgTie("/autopilot/settings/climb-rate-fpm", this,
324           &FGAutopilot::getAPClimb, &FGAutopilot::setAPClimb, false);
325     fgSetArchivable("/autopilot/settings/climb-rate-fpm");
326     fgTie("/autopilot/locks/heading", this,
327           &FGAutopilot::getAPHeadingLock, &FGAutopilot::setAPHeadingLock);
328     fgSetArchivable("/autopilot/locks/heading");
329     fgTie("/autopilot/settings/heading-bug-deg", this,
330           &FGAutopilot::getAPHeadingBug, &FGAutopilot::setAPHeadingBug);
331     fgSetArchivable("/autopilot/settings/heading-bug-deg");
332     fgSetDouble("/autopilot/settings/heading-bug-deg", 0.0f);
333     fgTie("/autopilot/locks/wing-leveler", this,
334           &FGAutopilot::getAPWingLeveler, &FGAutopilot::setAPWingLeveler);
335     fgSetArchivable("/autopilot/locks/wing-leveler");
336     fgTie("/autopilot/locks/nav[0]", this,
337           &FGAutopilot::getAPNAV1Lock, &FGAutopilot::setAPNAV1Lock);
338     fgSetArchivable("/autopilot/locks/nav[0]");
339     fgTie("/autopilot/locks/auto-throttle", this,
340           &FGAutopilot::getAPAutoThrottleLock,
341           &FGAutopilot::setAPAutoThrottleLock);
342     fgSetArchivable("/autopilot/locks/auto-throttle");
343     fgTie("/autopilot/control-overrides/rudder", this,
344           &FGAutopilot::getAPRudderControl,
345           &FGAutopilot::setAPRudderControl);
346     fgSetArchivable("/autopilot/control-overrides/rudder");
347     fgTie("/autopilot/control-overrides/elevator", this,
348           &FGAutopilot::getAPElevatorControl,
349           &FGAutopilot::setAPElevatorControl);
350     fgSetArchivable("/autopilot/control-overrides/elevator");
351     fgTie("/autopilot/control-overrides/throttle", this,
352           &FGAutopilot::getAPThrottleControl,
353           &FGAutopilot::setAPThrottleControl);
354     fgSetArchivable("/autopilot/control-overrides/throttle");
355 }
356
357 void
358 FGAutopilot::unbind ()
359 {
360 }
361
362 // Reset the autopilot system
363 void FGAutopilot::reset() {
364
365     heading_hold = false ;      // turn the heading hold off
366     altitude_hold = false ;     // turn the altitude hold off
367     auto_throttle = false ;     // turn the auto throttle off
368     heading_mode = DEFAULT_AP_HEADING_LOCK;
369
370     // TargetHeading = 0.0;     // default direction, due north
371     MakeTargetHeadingStr( TargetHeading );                      
372         
373     // TargetAltitude = 3000;   // default altitude in meters
374     MakeTargetAltitudeStr( TargetAltitude );
375         
376     alt_error_accum = 0.0;
377     climb_error_accum = 0.0;
378         
379     update_old_control_values();
380
381     sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id") );
382         
383     MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
384 }
385
386
387 static double NormalizeDegrees( double Input ) {
388     // normalize the input to the range (-180,180]
389     // Input should not be greater than -360 to 360.
390     // Current rules send the output to an undefined state.
391     while ( Input > 180.0 ) { Input -= 360.0; }
392     while ( Input <= -180.0 ) { Input += 360.0; }
393
394     return Input;
395 };
396
397 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
398     // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
399     //assert(x1 != x2); // Divide by zero error.  Cold abort for now
400
401         // Could be
402         // static double y = 0.0;
403         // double dx = x2 -x1;
404         // if( (dx < -SG_EPSILON ) || ( dx > SG_EPSILON ) )
405         // {
406
407     double m, b, y;          // the constants to find in y=mx+b
408     // double m, b;
409
410     m = ( y2 - y1 ) / ( x2 - x1 );   // calculate the m
411
412     b = y1 - m * x1;       // calculate the b
413
414     y = m * x + b;       // the final calculation
415
416     // }
417
418     return ( y );
419
420 };
421
422
423 void
424 FGAutopilot::update (double dt)
425 {
426     // Remove the following lines when the calling funcitons start
427     // passing in the data pointer
428
429     // get control settings 
430         
431     double lat = latitude_node->getDoubleValue();
432     double lon = longitude_node->getDoubleValue();
433     double alt = altitude_node->getDoubleValue() * SG_FEET_TO_METER;
434
435     SG_LOG( SG_ALL, SG_DEBUG, "FGAutopilot::run()  lat = " << lat <<
436             "  lon = " << lon << "  alt = " << alt );
437         
438 #ifdef FG_FORCE_AUTO_DISENGAGE
439     // see if somebody else has changed them
440     if( fabs(aileron - old_aileron) > disengage_threshold ||
441         fabs(elevator - old_elevator) > disengage_threshold ||
442         fabs(elevator_trim - old_elevator_trim) > 
443         disengage_threshold ||          
444         fabs(rudder - old_rudder) > disengage_threshold )
445     {
446         // if controls changed externally turn autopilot off
447         waypoint_hold = false ;   // turn the target hold off
448         heading_hold = false ;    // turn the heading hold off
449         altitude_hold = false ;   // turn the altitude hold off
450         terrain_follow = false;   // turn the terrain_follow hold off
451         // auto_throttle = false; // turn the auto_throttle off
452
453         // stash this runs control settings
454         old_aileron = aileron;
455         old_elevator = elevator;
456         old_elevator_trim = elevator_trim;
457         old_rudder = rudder;
458         
459         return 0;
460     }
461 #endif
462         
463     // heading hold
464     if ( heading_hold == true ) {
465         if ( heading_mode == FG_DG_HEADING_LOCK ) {
466             TargetHeading = DGTargetHeading + FGSteam::get_DG_err();
467             while ( TargetHeading <   0.0 ) { TargetHeading += 360.0; }
468             while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
469             MakeTargetHeadingStr( TargetHeading );
470         } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
471             // we don't set a specific target heading in
472             // TC_HEADING_LOCK mode, we instead try to keep the turn
473             // coordinator zero'd
474         } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
475             // leave "true" target heading as is
476             while ( TargetHeading <   0.0 ) { TargetHeading += 360.0; }
477             while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
478             MakeTargetHeadingStr( TargetHeading );
479         } else if ( heading_mode == FG_HEADING_NAV1 ) {
480             // track the NAV1 heading needle deflection
481
482             // determine our current radial position relative to the
483             // navaid in "true" heading.
484             double cur_radial = current_radiostack->get_nav1_heading();
485             if ( current_radiostack->get_nav1_loc() ) {
486                 // ILS localizers radials are already "true" in our
487                 // database
488             } else {
489                 cur_radial += current_radiostack->get_nav1_magvar();
490             }
491             if ( current_radiostack->get_nav1_from_flag() ) {
492                 cur_radial += 180.0;
493                 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
494             }
495
496             // determine the target radial in "true" heading
497             double tgt_radial = current_radiostack->get_nav1_radial();
498             if ( current_radiostack->get_nav1_loc() ) {
499                 // ILS localizers radials are already "true" in our
500                 // database
501             } else {
502                 // VOR radials need to have that vor's offset added in
503                 tgt_radial += current_radiostack->get_nav1_magvar();
504             }
505
506             // determine the heading adjustment needed.
507             double adjustment = 
508                 current_radiostack->get_nav1_heading_needle_deflection()
509                 * (current_radiostack->get_nav1_loc_dist() * SG_METER_TO_NM);
510             SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
511
512             // determine the target heading to fly to intercept the
513             // tgt_radial
514             TargetHeading = tgt_radial + adjustment; 
515             while ( TargetHeading <   0.0 ) { TargetHeading += 360.0; }
516             while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
517
518             MakeTargetHeadingStr( TargetHeading );
519         } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
520             // update target heading to waypoint
521
522             double wp_course, wp_distance;
523
524 #ifdef DO_fgAP_CORRECTED_COURSE
525             // compute course made good
526             // this needs lots of special casing before use
527             double course, reverse, distance, corrected_course;
528             // need to test for iter
529             geo_inverse_wgs_84( 0, //fgAPget_altitude(),
530                                 old_lat,
531                                 old_lon,
532                                 lat,
533                                 lon,
534                                 &course,
535                                 &reverse,
536                                 &distance );
537 #endif // DO_fgAP_CORRECTED_COURSE
538
539             // compute course to way_point
540             // need to test for iter
541             SGWayPoint wp = globals->get_route()->get_first();
542             wp.CourseAndDistance( lon, lat, alt, 
543                                   &wp_course, &wp_distance );
544
545 #ifdef DO_fgAP_CORRECTED_COURSE
546             corrected_course = course - wp_course;
547             if( fabs(corrected_course) > 0.1 )
548                 printf("fgAP: course %f  wp_course %f  %f  %f\n",
549                        course, wp_course, fabs(corrected_course),
550                        distance );
551 #endif // DO_fgAP_CORRECTED_COURSE
552                 
553             if ( wp_distance > 100 ) {
554                 // corrected_course = course - wp_course;
555                 TargetHeading = NormalizeDegrees(wp_course);
556             } else {
557                 // pop off this waypoint from the list
558                 if ( globals->get_route()->size() ) {
559                     globals->get_route()->delete_first();
560                 }
561
562                 // see if there are more waypoints on the list
563                 if ( globals->get_route()->size() ) {
564                     // more waypoints
565                     set_HeadingMode( FG_HEADING_WAYPOINT );
566                 } else {
567                     // end of the line
568                     heading_mode = FG_TRUE_HEADING_LOCK;
569                     // use current heading
570                     TargetHeading = heading_node->getDoubleValue();
571                 }
572             }
573             MakeTargetHeadingStr( TargetHeading );
574             // Force this just in case
575             TargetDistance = wp_distance;
576             MakeTargetWPStr( wp_distance );
577         }
578
579         if ( heading_mode == FG_TC_HEADING_LOCK ) {
580             // drive the turn coordinator to zero
581             double turn = FGSteam::get_TC_std();
582             double AileronSet = -turn / 2.0;
583             SG_CLAMP_RANGE( AileronSet, -1.0, 1.0 );
584             globals->get_controls()->set_aileron( AileronSet );
585             globals->get_controls()->set_rudder( AileronSet / 4.0 );
586         } else {
587             // steer towards the target heading
588
589             double RelHeading;
590             double TargetRoll;
591             double RelRoll;
592             double AileronSet;
593
594             RelHeading
595                 = NormalizeDegrees( TargetHeading
596                                     - heading_node->getDoubleValue() );
597             // figure out how far off we are from desired heading
598
599             // Now it is time to deterime how far we should be rolled.
600             SG_LOG( SG_AUTOPILOT, SG_DEBUG,
601                     "Heading = " << heading_node->getDoubleValue() <<
602                     " TargetHeading = " << TargetHeading <<
603                     " RelHeading = " << RelHeading );
604
605             // Check if we are further from heading than the roll out point
606             if ( fabs( RelHeading ) > RollOut ) {
607                 // set Target Roll to Max in desired direction
608                 if ( RelHeading < 0 ) {
609                     TargetRoll = 0 - MaxRoll;
610                 } else {
611                     TargetRoll = MaxRoll;
612                 }
613             } else {
614                 // We have to calculate the Target roll
615
616                 // This calculation engine thinks that the Target roll
617                 // should be a line from (RollOut,MaxRoll) to (-RollOut,
618                 // -MaxRoll) I hope this works well.  If I get ambitious
619                 // some day this might become a fancier curve or
620                 // something.
621
622                 TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
623                                                 -MaxRoll, RollOut,
624                                                 MaxRoll );
625             }
626
627             // Target Roll has now been Found.
628
629             // Compare Target roll to Current Roll, Generate Rel Roll
630
631             SG_LOG( SG_COCKPIT, SG_BULK, "TargetRoll: " << TargetRoll );
632
633             RelRoll = NormalizeDegrees( TargetRoll 
634                                         - roll_node->getDoubleValue() );
635
636             // Check if we are further from heading than the roll out
637             // smooth point
638             if ( fabs( RelRoll ) > RollOutSmooth ) {
639                 // set Target Roll to Max in desired direction
640                 if ( RelRoll < 0 ) {
641                 AileronSet = 0 - MaxAileron;
642                 } else {
643                     AileronSet = MaxAileron;
644                 }
645             } else {
646                 AileronSet = LinearExtrapolate( RelRoll, -RollOutSmooth,
647                                                 -MaxAileron,
648                                                 RollOutSmooth,
649                                                 MaxAileron );
650             }
651
652             globals->get_controls()->set_aileron( AileronSet );
653             globals->get_controls()->set_rudder( AileronSet / 4.0 );
654             // controls.set_rudder( 0.0 );
655         }
656     }
657
658     // altitude hold
659     if ( altitude_hold ) {
660         double climb_rate;
661         double speed, max_climb, error;
662         double prop_error, int_error;
663         double prop_adj, int_adj, total_adj;
664
665         if ( altitude_mode == FG_ALTITUDE_LOCK ) {
666             climb_rate =
667                 ( TargetAltitude - FGSteam::get_ALT_ft() * SG_FEET_TO_METER ) * 8.0;
668         } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
669             double x = current_radiostack->get_nav1_gs_dist();
670             double y = (altitude_node->getDoubleValue()
671                         - current_radiostack->get_nav1_elev()) * SG_FEET_TO_METER;
672             double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
673
674             double target_angle = current_radiostack->get_nav1_target_gs();
675
676             double gs_diff = target_angle - current_angle;
677
678             // convert desired vertical path angle into a climb rate
679             double des_angle = current_angle - 10 * gs_diff;
680
681             // convert to meter/min
682             double horiz_vel = cur_fdm_state->get_V_ground_speed()
683                 * SG_FEET_TO_METER * 60.0;
684             climb_rate = -sin( des_angle * SGD_DEGREES_TO_RADIANS ) * horiz_vel;
685             /* climb_error_accum += gs_diff * 2.0; */
686             /* climb_rate = gs_diff * 200.0 + climb_error_accum; */
687         } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
688             // brain dead ground hugging with no look ahead
689             climb_rate =
690                 ( TargetAGL - altitude_agl_node->getDoubleValue()
691                   * SG_FEET_TO_METER ) * 16.0;
692         } else {
693             // just try to zero out rate of climb ...
694             climb_rate = 0.0;
695         }
696
697         speed = get_speed();
698
699         if ( speed < min_climb->getFloatValue() ) {
700             max_climb = 0.0;
701         } else if ( speed < best_climb->getFloatValue() ) {
702             max_climb = ((best_climb->getFloatValue()
703                           - min_climb->getFloatValue())
704                          - (best_climb->getFloatValue() - speed)) 
705                 * fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER) 
706                 / (best_climb->getFloatValue() - min_climb->getFloatValue());
707         } else {                        
708             max_climb = ( speed - best_climb->getFloatValue() ) * 10.0
709                 + fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER);
710         }
711
712         // this first one could be optional if we wanted to allow
713         // better climb performance assuming we have the airspeed to
714         // support it.
715         if ( climb_rate >
716              fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER) ) {
717             climb_rate
718                 = fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER);
719         }
720
721         if ( climb_rate > max_climb ) {
722             climb_rate = max_climb;
723         }
724
725         if ( climb_rate <
726              -fabs(TargetDescentRate->getFloatValue() * SG_FEET_TO_METER) ) {
727             climb_rate
728                 = -fabs(TargetDescentRate->getFloatValue() * SG_FEET_TO_METER);
729         }
730
731         error = vertical_speed_node->getDoubleValue() * 60
732             - climb_rate * SG_METER_TO_FEET;
733
734         // accumulate the error under the curve ... this really should
735         // be *= delta t
736         alt_error_accum += error;
737
738         // calculate integral error, and adjustment amount
739         int_error = alt_error_accum;
740         // printf("error = %.2f  int_error = %.2f\n", error, int_error);
741         int_adj = int_error / elevator_adj_factor->getFloatValue();
742
743         // caclulate proportional error
744         prop_error = error;
745         prop_adj = prop_error / elevator_adj_factor->getDoubleValue();
746
747         total_adj = ((double) 1.0 - (double) integral_contrib->getFloatValue()) * prop_adj
748             + (double) integral_contrib->getFloatValue() * int_adj;
749
750         // stop on autopilot trim at 30% +/-
751 //      if ( total_adj > 0.3 ) {
752 //           total_adj = 0.3;
753 //       } else if ( total_adj < -0.3 ) {
754 //           total_adj = -0.3;
755 //       }
756
757         // adjust for throttle pitch gain
758         total_adj += ((current_throttle->getFloatValue() - zero_pitch_throttle->getFloatValue())
759                      / (1 - zero_pitch_throttle->getFloatValue()))
760                      * zero_pitch_trim_full_throttle->getFloatValue();
761
762         globals->get_controls()->set_elevator_trim( total_adj );
763     }
764
765     // auto throttle
766     if ( auto_throttle ) {
767         double error;
768         double prop_error, int_error;
769         double prop_adj, int_adj, total_adj;
770
771         error = TargetSpeed - get_speed();
772
773         // accumulate the error under the curve ... this really should
774         // be *= delta t
775         speed_error_accum += error;
776         if ( speed_error_accum > 2000.0 ) {
777             speed_error_accum = 2000.0;
778         }
779         else if ( speed_error_accum < -2000.0 ) {
780             speed_error_accum = -2000.0;
781         }
782
783         // calculate integral error, and adjustment amount
784         int_error = speed_error_accum;
785
786         // printf("error = %.2f  int_error = %.2f\n", error, int_error);
787         int_adj = int_error / 200.0;
788
789         // caclulate proportional error
790         prop_error = error;
791         prop_adj = 0.5 + prop_error / 50.0;
792
793         total_adj = 0.9 * prop_adj + 0.1 * int_adj;
794         if ( total_adj > 1.0 ) {
795             total_adj = 1.0;
796         }
797         else if ( total_adj < 0.0 ) {
798             total_adj = 0.0;
799         }
800
801         globals->get_controls()->set_throttle( FGControls::ALL_ENGINES,
802                                                total_adj );
803     }
804
805 #ifdef THIS_CODE_IS_NOT_USED
806     if (Mode == 2) // Glide slope hold
807         {
808             double RelSlope;
809             double RelElevator;
810
811             // First, calculate Relative slope and normalize it
812             RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
813
814             // Now calculate the elevator offset from current angle
815             if ( abs(RelSlope) > SlopeSmooth )
816                 {
817                     if ( RelSlope < 0 )     //  set RelElevator to max in the correct direction
818                         RelElevator = -MaxElevator;
819                     else
820                         RelElevator = MaxElevator;
821                 }
822
823             else
824                 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
825
826             // set the elevator
827             fgElevMove(RelElevator);
828
829         }
830 #endif // THIS_CODE_IS_NOT_USED
831
832     // stash this runs control settings
833     //  update_old_control_values();
834     old_aileron = globals->get_controls()->get_aileron();
835     old_elevator = globals->get_controls()->get_elevator();
836     old_elevator_trim = globals->get_controls()->get_elevator_trim();
837     old_rudder = globals->get_controls()->get_rudder();
838
839     // for cross track error
840     old_lat = lat;
841     old_lon = lon;
842         
843     // Ok, we are done
844     SG_LOG( SG_ALL, SG_DEBUG, "FGAutopilot::run( returns )" );
845 }
846
847
848 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
849     heading_mode = mode;
850
851     if ( heading_mode == FG_DG_HEADING_LOCK ) {
852         // set heading hold to current heading (as read from DG)
853         // ... no, leave target heading along ... just use the current
854         // heading bug value
855         //  DGTargetHeading = FGSteam::get_DG_deg();
856     } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
857         // set autopilot to hold a zero turn (as reported by the TC)
858     } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
859         // set heading hold to current heading
860         TargetHeading = heading_node->getDoubleValue();
861     } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
862         if ( globals->get_route()->size() ) {
863             double course, distance;
864
865             old_lat = latitude_node->getDoubleValue();
866             old_lon = longitude_node->getDoubleValue();
867
868             waypoint = globals->get_route()->get_first();
869             waypoint.CourseAndDistance( longitude_node->getDoubleValue(),
870                                         latitude_node->getDoubleValue(),
871                                         altitude_node->getDoubleValue()
872                                         * SG_FEET_TO_METER,
873                                         &course, &distance );
874             TargetHeading = course;
875             TargetDistance = distance;
876             MakeTargetLatLonStr( waypoint.get_target_lat(),
877                                  waypoint.get_target_lon() );
878             MakeTargetWPStr( distance );
879
880             if ( waypoint.get_target_alt() > 0.0 ) {
881                 TargetAltitude = waypoint.get_target_alt();
882                 altitude_mode = FG_ALTITUDE_LOCK;
883                 set_AltitudeEnabled( true );
884                 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
885             }
886
887             SG_LOG( SG_COCKPIT, SG_INFO, " set_HeadingMode: ( "
888                     << get_TargetLatitude()  << " "
889                     << get_TargetLongitude() << " ) "
890                     );
891         } else {
892             // no more way points, default to heading lock.
893             heading_mode = FG_TC_HEADING_LOCK;
894         }
895     }
896
897     MakeTargetHeadingStr( TargetHeading );                      
898     update_old_control_values();
899 }
900
901
902 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
903     altitude_mode = mode;
904
905     alt_error_accum = 0.0;
906
907
908     if ( altitude_mode == FG_ALTITUDE_LOCK ) {
909         if ( TargetAltitude < altitude_agl_node->getDoubleValue()
910              * SG_FEET_TO_METER ) {
911         }
912
913         if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
914             MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
915         } else {
916             MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
917         }
918     } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
919         climb_error_accum = 0.0;
920
921     } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
922         TargetAGL = altitude_agl_node->getDoubleValue() * SG_FEET_TO_METER;
923
924         if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
925             MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
926         } else {
927             MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
928         }
929     }
930     
931     update_old_control_values();
932     SG_LOG( SG_COCKPIT, SG_INFO, " set_AltitudeMode():" );
933 }
934
935
936 void FGAutopilot::AltitudeSet( double new_altitude ) {
937     double target_alt = new_altitude;
938
939     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
940         target_alt = new_altitude * SG_FEET_TO_METER;
941     }
942
943     if( target_alt < globals->get_scenery()->get_cur_elev() ) {
944         target_alt = globals->get_scenery()->get_cur_elev();
945     }
946
947     TargetAltitude = target_alt;
948     altitude_mode = FG_ALTITUDE_LOCK;
949
950     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
951         target_alt *= SG_METER_TO_FEET;
952     }
953     // ApAltitudeDialogInput->setValue((float)target_alt);
954     MakeTargetAltitudeStr( target_alt );
955         
956     update_old_control_values();
957 }
958
959
960 void FGAutopilot::AltitudeAdjust( double inc )
961 {
962     double target_alt, target_agl;
963
964     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
965         target_alt = TargetAltitude * SG_METER_TO_FEET;
966         target_agl = TargetAGL * SG_METER_TO_FEET;
967     } else {
968         target_alt = TargetAltitude;
969         target_agl = TargetAGL;
970     }
971
972     if ( fabs((int)(target_alt / inc) * inc - target_alt) < SG_EPSILON ) {
973         target_alt += inc;
974     } else {
975         target_alt = ( int ) ( target_alt / inc ) * inc + inc;
976     }
977
978     if ( fabs((int)(target_agl / inc) * inc - target_agl) < SG_EPSILON ) {
979         target_agl += inc;
980     } else {
981         target_agl = ( int ) ( target_agl / inc ) * inc + inc;
982     }
983
984     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") ) {
985         target_alt *= SG_FEET_TO_METER;
986         target_agl *= SG_FEET_TO_METER;
987     }
988
989     TargetAltitude = target_alt;
990     TargetAGL = target_agl;
991         
992     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
993         target_alt *= SG_METER_TO_FEET;
994     if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
995         target_agl *= SG_METER_TO_FEET;
996
997     if ( altitude_mode == FG_ALTITUDE_LOCK ) {
998         MakeTargetAltitudeStr( target_alt );
999     } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
1000         MakeTargetAltitudeStr( target_agl );
1001     }
1002
1003     update_old_control_values();
1004 }
1005
1006
1007 void FGAutopilot::HeadingAdjust( double inc ) {
1008     if ( heading_mode != FG_DG_HEADING_LOCK
1009          && heading_mode != FG_TRUE_HEADING_LOCK )
1010     {
1011         heading_mode = FG_DG_HEADING_LOCK;
1012     }
1013
1014     if ( heading_mode == FG_DG_HEADING_LOCK ) {
1015         double target = ( int ) ( DGTargetHeading / inc ) * inc + inc;
1016         DGTargetHeading = NormalizeDegrees( target );
1017     } else {
1018         double target = ( int ) ( TargetHeading / inc ) * inc + inc;
1019         TargetHeading = NormalizeDegrees( target );
1020     }
1021
1022     update_old_control_values();
1023 }
1024
1025
1026 void FGAutopilot::HeadingSet( double new_heading ) {
1027     if( heading_mode == FG_TRUE_HEADING_LOCK ) {
1028         new_heading = NormalizeDegrees( new_heading );
1029         TargetHeading = new_heading;
1030         MakeTargetHeadingStr( TargetHeading );
1031     } else {
1032         heading_mode = FG_DG_HEADING_LOCK;
1033
1034         new_heading = NormalizeDegrees( new_heading );
1035         DGTargetHeading = new_heading;
1036         // following cast needed ambiguous plib
1037         // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1038         MakeTargetHeadingStr( DGTargetHeading );
1039     }
1040     update_old_control_values();
1041 }
1042
1043 void FGAutopilot::AutoThrottleAdjust( double inc ) {
1044     double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
1045
1046     TargetSpeed = target;
1047 }
1048
1049
1050 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
1051     auto_throttle = value;
1052
1053     if ( auto_throttle == true ) {
1054         TargetSpeed = fgGetDouble("/velocities/airspeed-kt");
1055         speed_error_accum = 0.0;
1056     }
1057
1058     update_old_control_values();
1059     SG_LOG( SG_COCKPIT, SG_INFO, " fgAPSetAutoThrottle: ("
1060             << auto_throttle << ") " << TargetSpeed );
1061 }
1062
1063
1064
1065 \f
1066 ////////////////////////////////////////////////////////////////////////
1067 // Kludged methods for tying to properties.
1068 //
1069 // These should change eventually; they all used to be static
1070 // functions.
1071 ////////////////////////////////////////////////////////////////////////
1072
1073 /**
1074  * Get the autopilot altitude lock (true=on).
1075  */
1076 bool
1077 FGAutopilot::getAPAltitudeLock () const
1078 {
1079     return (get_AltitudeEnabled() &&
1080             get_AltitudeMode()
1081             == FGAutopilot::FG_ALTITUDE_LOCK);
1082 }
1083
1084
1085 /**
1086  * Set the autopilot altitude lock (true=on).
1087  */
1088 void
1089 FGAutopilot::setAPAltitudeLock (bool lock)
1090 {
1091   if (lock)
1092     set_AltitudeMode(FGAutopilot::FG_ALTITUDE_LOCK);
1093   if (get_AltitudeMode() == FGAutopilot::FG_ALTITUDE_LOCK)
1094     set_AltitudeEnabled(lock);
1095 }
1096
1097
1098 /**
1099  * Get the autopilot target altitude in feet.
1100  */
1101 double
1102 FGAutopilot::getAPAltitude () const
1103 {
1104   return get_TargetAltitude() * SG_METER_TO_FEET;
1105 }
1106
1107
1108 /**
1109  * Set the autopilot target altitude in feet.
1110  */
1111 void
1112 FGAutopilot::setAPAltitude (double altitude)
1113 {
1114   set_TargetAltitude( altitude * SG_FEET_TO_METER );
1115 }
1116
1117 /**
1118  * Get the autopilot altitude lock (true=on).
1119  */
1120 bool
1121 FGAutopilot::getAPGSLock () const
1122 {
1123     return (get_AltitudeEnabled() &&
1124             (get_AltitudeMode()
1125              == FGAutopilot::FG_ALTITUDE_GS1));
1126 }
1127
1128
1129 /**
1130  * Set the autopilot altitude lock (true=on).
1131  */
1132 void
1133 FGAutopilot::setAPGSLock (bool lock)
1134 {
1135   if (lock)
1136     set_AltitudeMode(FGAutopilot::FG_ALTITUDE_GS1);
1137   if (get_AltitudeMode() == FGAutopilot::FG_ALTITUDE_GS1)
1138     set_AltitudeEnabled(lock);
1139 }
1140
1141
1142 /**
1143  * Get the autopilot terrain lock (true=on).
1144  */
1145 bool
1146 FGAutopilot::getAPTerrainLock () const
1147 {
1148     return (get_AltitudeEnabled() &&
1149             (get_AltitudeMode()
1150              == FGAutopilot::FG_ALTITUDE_TERRAIN));
1151 }
1152
1153
1154 /**
1155  * Set the autopilot terrain lock (true=on).
1156  */
1157 void
1158 FGAutopilot::setAPTerrainLock (bool lock)
1159 {
1160   if (lock) {
1161     set_AltitudeMode(FGAutopilot::FG_ALTITUDE_TERRAIN);
1162     set_TargetAGL(fgGetFloat("/position/altitude-agl-ft") *
1163                   SG_FEET_TO_METER);
1164   }
1165   if (get_AltitudeMode() == FGAutopilot::FG_ALTITUDE_TERRAIN)
1166     set_AltitudeEnabled(lock);
1167 }
1168
1169
1170 /**
1171  * Get the autopilot target altitude in feet.
1172  */
1173 double
1174 FGAutopilot::getAPClimb () const
1175 {
1176   return get_TargetClimbRate() * SG_METER_TO_FEET;
1177 }
1178
1179
1180 /**
1181  * Set the autopilot target altitude in feet.
1182  */
1183 void
1184 FGAutopilot::setAPClimb (double rate)
1185 {
1186     set_TargetClimbRate( rate * SG_FEET_TO_METER );
1187 }
1188
1189
1190 /**
1191  * Get the autopilot heading lock (true=on).
1192  */
1193 bool
1194 FGAutopilot::getAPHeadingLock () const
1195 {
1196     return
1197       (get_HeadingEnabled() &&
1198        get_HeadingMode() == DEFAULT_AP_HEADING_LOCK);
1199 }
1200
1201
1202 /**
1203  * Set the autopilot heading lock (true=on).
1204  */
1205 void
1206 FGAutopilot::setAPHeadingLock (bool lock)
1207 {
1208   if (lock)
1209     set_HeadingMode(DEFAULT_AP_HEADING_LOCK);
1210   if (get_HeadingMode() == DEFAULT_AP_HEADING_LOCK)
1211     set_HeadingEnabled(lock);
1212 }
1213
1214
1215 /**
1216  * Get the autopilot heading bug in degrees.
1217  */
1218 double
1219 FGAutopilot::getAPHeadingBug () const
1220 {
1221   return get_DGTargetHeading();
1222 }
1223
1224
1225 /**
1226  * Set the autopilot heading bug in degrees.
1227  */
1228 void
1229 FGAutopilot::setAPHeadingBug (double heading)
1230 {
1231   set_DGTargetHeading( heading );
1232 }
1233
1234
1235 /**
1236  * Get the autopilot wing leveler lock (true=on).
1237  */
1238 bool
1239 FGAutopilot::getAPWingLeveler () const
1240 {
1241     return
1242       (get_HeadingEnabled() &&
1243        get_HeadingMode() == FGAutopilot::FG_TC_HEADING_LOCK);
1244 }
1245
1246
1247 /**
1248  * Set the autopilot wing leveler lock (true=on).
1249  */
1250 void
1251 FGAutopilot::setAPWingLeveler (bool lock)
1252 {
1253     if (lock)
1254         set_HeadingMode(FGAutopilot::FG_TC_HEADING_LOCK);
1255     if (get_HeadingMode() == FGAutopilot::FG_TC_HEADING_LOCK)
1256       set_HeadingEnabled(lock);
1257 }
1258
1259 /**
1260  * Return true if the autopilot is locked to NAV1.
1261  */
1262 bool
1263 FGAutopilot::getAPNAV1Lock () const
1264 {
1265   return
1266     (get_HeadingEnabled() &&
1267      get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1);
1268 }
1269
1270
1271 /**
1272  * Set the autopilot NAV1 lock.
1273  */
1274 void
1275 FGAutopilot::setAPNAV1Lock (bool lock)
1276 {
1277   if (lock)
1278     set_HeadingMode(FGAutopilot::FG_HEADING_NAV1);
1279   if (get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1)
1280     set_HeadingEnabled(lock);
1281 }
1282
1283 /**
1284  * Get the autopilot autothrottle lock.
1285  */
1286 bool
1287 FGAutopilot::getAPAutoThrottleLock () const
1288 {
1289   return get_AutoThrottleEnabled();
1290 }
1291
1292
1293 /**
1294  * Set the autothrottle lock.
1295  */
1296 void
1297 FGAutopilot::setAPAutoThrottleLock (bool lock)
1298 {
1299   set_AutoThrottleEnabled(lock);
1300 }
1301
1302
1303 // kludge
1304 double
1305 FGAutopilot::getAPRudderControl () const
1306 {
1307     if (getAPHeadingLock())
1308         return get_TargetHeading();
1309     else
1310         return globals->get_controls()->get_rudder();
1311 }
1312
1313 // kludge
1314 void
1315 FGAutopilot::setAPRudderControl (double value)
1316 {
1317     if (getAPHeadingLock()) {
1318         SG_LOG(SG_GENERAL, SG_DEBUG, "setAPRudderControl " << value );
1319         value -= get_TargetHeading();
1320         HeadingAdjust(value < 0.0 ? -1.0 : 1.0);
1321     } else {
1322         globals->get_controls()->set_rudder(value);
1323     }
1324 }
1325
1326 // kludge
1327 double
1328 FGAutopilot::getAPElevatorControl () const
1329 {
1330   if (getAPAltitudeLock())
1331       return get_TargetAltitude();
1332   else
1333     return globals->get_controls()->get_elevator();
1334 }
1335
1336 // kludge
1337 void
1338 FGAutopilot::setAPElevatorControl (double value)
1339 {
1340     if (value != 0 && getAPAltitudeLock()) {
1341         SG_LOG(SG_GENERAL, SG_DEBUG, "setAPElevatorControl " << value );
1342         value -= get_TargetAltitude();
1343         AltitudeAdjust(value < 0.0 ? 100.0 : -100.0);
1344     } else {
1345         globals->get_controls()->set_elevator(value);
1346     }
1347 }
1348
1349 // kludge
1350 double
1351 FGAutopilot::getAPThrottleControl () const
1352 {
1353   if (getAPAutoThrottleLock())
1354     return 0.0;                 // always resets
1355   else
1356     return globals->get_controls()->get_throttle(0);
1357 }
1358
1359 // kludge
1360 void
1361 FGAutopilot::setAPThrottleControl (double value)
1362 {
1363   if (getAPAutoThrottleLock())
1364     AutoThrottleAdjust(value < 0.0 ? -0.01 : 0.01);
1365   else
1366     globals->get_controls()->set_throttle(FGControls::ALL_ENGINES, value);
1367 }
1368
1369 // end of newauto.cxx