]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/newauto.cxx
Fixes to magnetic compass.
[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
32 #include <simgear/constants.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/math/fg_geodesy.hxx>
35
36 #include <Cockpit/radiostack.hxx>
37 #include <Controls/controls.hxx>
38 #include <FDM/flight.hxx>
39 #include <Main/bfi.hxx>
40 #include <Main/options.hxx>
41 #include <Scenery/scenery.hxx>
42
43 #include "newauto.hxx"
44
45
46 FGAutopilot *current_autopilot;
47
48
49 // Climb speed constants
50 const double min_climb = 70.0;  // kts
51 const double best_climb = 75.0; // kts
52 const double ideal_climb_rate = 500.0; // fpm
53
54 /// These statics will eventually go into the class
55 /// they are just here while I am experimenting -- NHV :-)
56 // AutoPilot Gain Adjuster members
57 static double MaxRollAdjust;        // MaxRollAdjust       = 2 * APData->MaxRoll;
58 static double RollOutAdjust;        // RollOutAdjust       = 2 * APData->RollOut;
59 static double MaxAileronAdjust;     // MaxAileronAdjust    = 2 * APData->MaxAileron;
60 static double RollOutSmoothAdjust;  // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
61
62 static char NewTgtAirportId[16];
63 // static char NewTgtAirportLabel[] = "Enter New TgtAirport ID"; 
64
65 extern char *coord_format_lat(float);
66 extern char *coord_format_lon(float);
67                         
68
69 void FGAutopilot::MakeTargetLatLonStr( double lat, double lon ) {
70     sprintf( TargetLatitudeStr , "%s", coord_format_lat(TargetLatitude) );
71     sprintf( TargetLongitudeStr, "%s", coord_format_lon(TargetLongitude) );
72     sprintf( TargetLatLonStr, "%s  %s", TargetLatitudeStr, TargetLongitudeStr );
73 }
74
75
76 void FGAutopilot::MakeTargetAltitudeStr( double altitude ) {
77     if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
78         sprintf( TargetAltitudeStr, "APAltitude  %6.0f+", altitude );
79     } else {
80         sprintf( TargetAltitudeStr, "APAltitude  %6.0f", altitude );
81     }
82 }
83
84
85 void FGAutopilot::MakeTargetHeadingStr( double bearing ) {
86     if( bearing < 0. ) {
87         bearing += 360.;
88     } else if (bearing > 360. ) {
89         bearing -= 360.;
90     }
91     sprintf( TargetHeadingStr, "APHeading  %6.1f", bearing );
92 }
93
94
95 static inline double get_speed( void ) {
96     return( cur_fdm_state->get_V_equiv_kts() );
97 }
98
99 static inline double get_ground_speed() {
100     // starts in ft/s so we convert to kts
101     double ft_s = cur_fdm_state->get_V_ground_speed() 
102         * current_options.get_speed_up();;
103     double kts = ft_s * FEET_TO_METER * 3600 * METER_TO_NM;
104
105     return kts;
106 }
107
108
109 void FGAutopilot::MakeTargetDistanceStr( double distance ) {
110     double eta = distance*METER_TO_NM / get_ground_speed();
111     if ( eta >= 100.0 ) { eta = 99.999; }
112     int major, minor;
113     if ( eta < (1.0/6.0) ) {
114         // within 10 minutes, bump up to min/secs
115         eta *= 60.0;
116     }
117     major = (int)eta;
118     minor = (int)((eta - (int)eta) * 60.0);
119     sprintf( TargetDistanceStr, "APDistance %.2f NM  ETA %d:%02d",
120              distance*METER_TO_NM, major, minor );
121     // cout << "distance = " << distance*METER_TO_NM
122     //      << "  gndsp = " << get_ground_speed() 
123     //      << "  time = " << eta
124     //      << "  major = " << major
125     //      << "  minor = " << minor
126     //      << endl;
127 }
128
129
130 void FGAutopilot::update_old_control_values() {
131     old_aileron = FGBFI::getAileron();
132     old_elevator = FGBFI::getElevator();
133     old_elevator_trim = FGBFI::getElevatorTrim();
134     old_rudder = FGBFI::getRudder();
135 }
136
137
138 // Initialize autopilot subsystem
139 void FGAutopilot::init() {
140     FG_LOG( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem" );
141
142     heading_hold = false ;      // turn the heading hold off
143     altitude_hold = false ;     // turn the altitude hold off
144     auto_throttle = false ;     // turn the auto throttle off
145
146     // Initialize target location to startup location
147     old_lat = TargetLatitude = FGBFI::getLatitude();
148     old_lon = TargetLongitude = FGBFI::getLongitude();
149
150     MakeTargetLatLonStr( TargetLatitude, TargetLongitude);
151         
152     TargetHeading = 0.0;        // default direction, due north
153     TargetAltitude = 3000;      // default altitude in meters
154     alt_error_accum = 0.0;
155     climb_error_accum = 0.0;
156
157     MakeTargetAltitudeStr( 3000.0);
158     MakeTargetHeadingStr( 0.0 );
159         
160     // These eventually need to be read from current_aircaft somehow.
161
162     // the maximum roll, in Deg
163     MaxRoll = 20;
164
165     // the deg from heading to start rolling out at, in Deg
166     RollOut = 20;
167
168     // how far can I move the aleron from center.
169     MaxAileron = .2;
170
171     // Smoothing distance for alerion control
172     RollOutSmooth = 10;
173
174     // Hardwired for now should be in options
175     // 25% max control variablilty  0.5 / 2.0
176     disengage_threshold = 1.0;
177
178 #if !defined( USING_SLIDER_CLASS )
179     MaxRollAdjust = 2 * MaxRoll;
180     RollOutAdjust = 2 * RollOut;
181     MaxAileronAdjust = 2 * MaxAileron;
182     RollOutSmoothAdjust = 2 * RollOutSmooth;
183 #endif  // !defined( USING_SLIDER_CLASS )
184
185     update_old_control_values();
186         
187     // Initialize GUI components of autopilot
188     // NewTgtAirportInit();
189     // fgAPAdjustInit() ;
190     // NewHeadingInit();
191     // NewAltitudeInit();
192 };
193
194
195 // Reset the autopilot system
196 void FGAutopilot::reset() {
197
198     heading_hold = false ;      // turn the heading hold off
199     altitude_hold = false ;     // turn the altitude hold off
200     auto_throttle = false ;     // turn the auto throttle off
201
202     TargetHeading = 0.0;        // default direction, due north
203     MakeTargetHeadingStr( TargetHeading );                      
204         
205     TargetAltitude = 3000;   // default altitude in meters
206     MakeTargetAltitudeStr( TargetAltitude );
207         
208     alt_error_accum = 0.0;
209     climb_error_accum = 0.0;
210         
211     update_old_control_values();
212
213     sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
214         
215     TargetLatitude = FGBFI::getLatitude();
216     TargetLongitude = FGBFI::getLongitude();
217     MakeTargetLatLonStr( TargetLatitude, TargetLongitude );
218 }
219
220
221 static double NormalizeDegrees( double Input ) {
222     // normalize the input to the range (-180,180]
223     // Input should not be greater than -360 to 360.
224     // Current rules send the output to an undefined state.
225     if ( Input > 180 )
226         while(Input > 180 )
227             Input -= 360;
228     else if ( Input <= -180 )
229         while ( Input <= -180 )
230             Input += 360;
231     return ( Input );
232 };
233
234 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
235     // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
236     //assert(x1 != x2); // Divide by zero error.  Cold abort for now
237
238         // Could be
239         // static double y = 0.0;
240         // double dx = x2 -x1;
241         // if( (dx < -FG_EPSILON ) || ( dx > FG_EPSILON ) )
242         // {
243
244     double m, b, y;          // the constants to find in y=mx+b
245     // double m, b;
246
247     m = ( y2 - y1 ) / ( x2 - x1 );   // calculate the m
248
249     b = y1 - m * x1;       // calculate the b
250
251     y = m * x + b;       // the final calculation
252
253     // }
254
255     return ( y );
256
257 };
258
259
260 int FGAutopilot::run() {
261     // Remove the following lines when the calling funcitons start
262     // passing in the data pointer
263
264     // get control settings 
265     // double aileron = FGBFI::getAileron();
266     // double elevator = FGBFI::getElevator();
267     // double elevator_trim = FGBFI::getElevatorTrim();
268     // double rudder = FGBFI::getRudder();
269         
270     double lat = FGBFI::getLatitude();
271     double lon = FGBFI::getLongitude();
272
273 #ifdef FG_FORCE_AUTO_DISENGAGE
274     // see if somebody else has changed them
275     if( fabs(aileron - old_aileron) > disengage_threshold ||
276         fabs(elevator - old_elevator) > disengage_threshold ||
277         fabs(elevator_trim - old_elevator_trim) > 
278         disengage_threshold ||          
279         fabs(rudder - old_rudder) > disengage_threshold )
280     {
281         // if controls changed externally turn autopilot off
282         waypoint_hold = false ;   // turn the target hold off
283         heading_hold = false ;    // turn the heading hold off
284         altitude_hold = false ;   // turn the altitude hold off
285         terrain_follow = false;   // turn the terrain_follow hold off
286         // auto_throttle = false; // turn the auto_throttle off
287
288         // stash this runs control settings
289         old_aileron = aileron;
290         old_elevator = elevator;
291         old_elevator_trim = elevator_trim;
292         old_rudder = rudder;
293         
294         return 0;
295     }
296 #endif
297         
298     // heading hold
299     if ( heading_hold == true ) {
300
301         if ( heading_mode == FG_HEADING_LOCK ) {
302             // leave target heading alone
303         } else if ( heading_mode == FG_HEADING_NAV1 ) {
304             double tgt_radial;
305             double cur_radial;
306             if ( current_radiostack->get_nav1_loc() ) {
307                 // localizers radials are "true"
308                 tgt_radial = current_radiostack->get_nav1_radial();
309             } else {
310                 tgt_radial = current_radiostack->get_nav1_radial() 
311                     + FGBFI::getMagVar();
312             }
313             cur_radial = current_radiostack->get_nav1_heading();
314             // cout << "target rad (true) = " << tgt_radial 
315             //      << "  current rad (true) = " << cur_radial
316             //      << endl;
317
318             double diff = (tgt_radial - cur_radial);
319             while ( diff < -180.0 ) { diff += 360.0; }
320             while ( diff > 180.0 ) { diff -= 360.0; }
321                 
322             diff *= (current_radiostack->get_nav1_loc_dist() * METER_TO_NM);
323             if ( diff < -30.0 ) { diff = -30.0; }
324             if ( diff >  30.0 ) { diff =  30.0; }
325
326             TargetHeading = cur_radial - diff;
327             while ( TargetHeading <   0.0 ) { TargetHeading += 360.0; }
328             while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
329             // cout << "target course (true) = " << TargetHeading << endl;
330         } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
331             // update target heading to waypoint
332
333             double wp_course, wp_reverse, wp_distance;
334
335 #ifdef DO_fgAP_CORRECTED_COURSE
336             // compute course made good
337             // this needs lots of special casing before use
338             double course, reverse, distance, corrected_course;
339             // need to test for iter
340             geo_inverse_wgs_84( 0, //fgAPget_altitude(),
341                                 old_lat,
342                                 old_lon,
343                                 lat,
344                                 lon,
345                                 &course,
346                                 &reverse,
347                                 &distance );
348 #endif // DO_fgAP_CORRECTED_COURSE
349
350             // compute course to way_point
351             // need to test for iter
352             if( ! geo_inverse_wgs_84( 0, //fgAPget_altitude(),
353                                       lat,
354                                       lon,
355                                       TargetLatitude,
356                                       TargetLongitude,
357                                       &wp_course,
358                                       &wp_reverse,
359                                       &wp_distance ) ) {
360                 
361 #ifdef DO_fgAP_CORRECTED_COURSE
362                 corrected_course = course - wp_course;
363                 if( fabs(corrected_course) > 0.1 )
364                     printf("fgAP: course %f  wp_course %f  %f  %f\n",
365                            course, wp_course, fabs(corrected_course),
366                            distance );
367 #endif // DO_fgAP_CORRECTED_COURSE
368                 
369                 if ( wp_distance > 100 ) {
370                     // corrected_course = course - wp_course;
371                     TargetHeading = NormalizeDegrees(wp_course);
372                 } else {
373                     printf("distance(%f) to close\n", wp_distance);
374                     // Real Close -- set heading hold to current heading
375                     // and Ring the arival bell !!
376                     heading_mode = FG_HEADING_LOCK;
377                     // use current heading
378                     TargetHeading = FGBFI::getHeading();
379                 }
380                 MakeTargetHeadingStr( TargetHeading );
381                 // Force this just in case
382                 TargetDistance = wp_distance;
383                 MakeTargetDistanceStr( wp_distance );
384             }
385         }
386
387         double RelHeading;
388         double TargetRoll;
389         double RelRoll;
390         double AileronSet;
391
392         RelHeading = NormalizeDegrees( TargetHeading - FGBFI::getHeading() );
393         // figure out how far off we are from desired heading
394
395         // Now it is time to deterime how far we should be rolled.
396         FG_LOG( FG_AUTOPILOT, FG_DEBUG, "RelHeading: " << RelHeading );
397
398
399         // Check if we are further from heading than the roll out point
400         if ( fabs( RelHeading ) > RollOut ) {
401             // set Target Roll to Max in desired direction
402             if ( RelHeading < 0 ) {
403                 TargetRoll = 0 - MaxRoll;
404             } else {
405                 TargetRoll = MaxRoll;
406             }
407         } else {
408             // We have to calculate the Target roll
409
410             // This calculation engine thinks that the Target roll
411             // should be a line from (RollOut,MaxRoll) to (-RollOut,
412             // -MaxRoll) I hope this works well.  If I get ambitious
413             // some day this might become a fancier curve or
414             // something.
415
416             TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
417                                             -MaxRoll, RollOut,
418                                             MaxRoll );
419         }
420
421         // Target Roll has now been Found.
422
423         // Compare Target roll to Current Roll, Generate Rel Roll
424
425         FG_LOG( FG_COCKPIT, FG_BULK, "TargetRoll: " << TargetRoll );
426
427         RelRoll = NormalizeDegrees( TargetRoll - FGBFI::getRoll() );
428
429         // Check if we are further from heading than the roll out smooth point
430         if ( fabs( RelRoll ) > RollOutSmooth ) {
431             // set Target Roll to Max in desired direction
432             if ( RelRoll < 0 ) {
433                 AileronSet = 0 - MaxAileron;
434             } else {
435                 AileronSet = MaxAileron;
436             }
437         } else {
438             AileronSet = LinearExtrapolate( RelRoll, -RollOutSmooth,
439                                             -MaxAileron,
440                                             RollOutSmooth,
441                                             MaxAileron );
442         }
443
444         controls.set_aileron( AileronSet );
445         controls.set_rudder( AileronSet / 4.0 );
446         // controls.set_rudder( 0.0 );
447     }
448
449     // altitude hold
450     if ( altitude_hold ) {
451         double speed, max_climb, error;
452         double prop_error, int_error;
453         double prop_adj, int_adj, total_adj;
454
455         if ( altitude_mode == FG_ALTITUDE_LOCK ) {
456             // normal altitude hold
457             // cout << "TargetAltitude = " << TargetAltitude
458             //      << "Altitude = " << FGBFI::getAltitude() * FEET_TO_METER
459             //      << endl;
460             TargetClimbRate =
461                 ( TargetAltitude - FGBFI::getAltitude() * FEET_TO_METER ) * 8.0;
462         } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
463             double x = current_radiostack->get_nav1_gs_dist();
464             double y = (FGBFI::getAltitude() 
465                         - current_radiostack->get_nav1_elev()) * FEET_TO_METER;
466             double angle = atan2( y, x ) * RAD_TO_DEG;
467             double gs_diff = current_radiostack->get_nav1_target_gs() - angle;
468             climb_error_accum += gs_diff * 2.0;
469             TargetClimbRate = gs_diff * 200.0 + climb_error_accum;
470         } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
471             // brain dead ground hugging with no look ahead
472             TargetClimbRate =
473                 ( TargetAGL - FGBFI::getAGL()*FEET_TO_METER ) * 16.0;
474             // cout << "target agl = " << TargetAGL 
475             //      << "  current agl = " << fgAPget_agl() 
476             //      << "  target climb rate = " << TargetClimbRate 
477             //      << endl;
478         } else {
479             // just try to zero out rate of climb ...
480             TargetClimbRate = 0.0;
481         }
482
483         speed = get_speed();
484
485         if ( speed < min_climb ) {
486             max_climb = 0.0;
487         } else if ( speed < best_climb ) {
488             max_climb = ((best_climb - min_climb) - (best_climb - speed)) 
489                 * ideal_climb_rate 
490                 / (best_climb - min_climb);
491         } else {                        
492             max_climb = ( speed - best_climb ) * 10.0 + ideal_climb_rate;
493         }
494
495         // this first one could be optional if we wanted to allow
496         // better climb performance assuming we have the airspeed to
497         // support it.
498         if ( TargetClimbRate > ideal_climb_rate ) {
499             TargetClimbRate = ideal_climb_rate;
500         }
501
502         if ( TargetClimbRate > max_climb ) {
503             TargetClimbRate = max_climb;
504         }
505
506         if ( TargetClimbRate < -ideal_climb_rate ) {
507             TargetClimbRate = -ideal_climb_rate;
508         }
509
510         error = FGBFI::getVerticalSpeed() * FEET_TO_METER - TargetClimbRate;
511         // cout << "climb rate = " << fgAPget_climb() 
512         //      << "  error = " << error << endl;
513
514         // accumulate the error under the curve ... this really should
515         // be *= delta t
516         alt_error_accum += error;
517
518         // calculate integral error, and adjustment amount
519         int_error = alt_error_accum;
520         // printf("error = %.2f  int_error = %.2f\n", error, int_error);
521         int_adj = int_error / 8000.0;
522
523         // caclulate proportional error
524         prop_error = error;
525         prop_adj = prop_error / 2000.0;
526
527         total_adj = 0.9 * prop_adj + 0.1 * int_adj;
528         // if ( total_adj > 0.6 ) {
529         //     total_adj = 0.6;
530         // } else if ( total_adj < -0.2 ) {
531         //     total_adj = -0.2;
532         // }
533         if ( total_adj > 1.0 ) {
534             total_adj = 1.0;
535         } else if ( total_adj < -1.0 ) {
536             total_adj = -1.0;
537         }
538
539         controls.set_elevator( total_adj );
540     }
541
542     // auto throttle
543     if ( auto_throttle ) {
544         double error;
545         double prop_error, int_error;
546         double prop_adj, int_adj, total_adj;
547
548         error = TargetSpeed - get_speed();
549
550         // accumulate the error under the curve ... this really should
551         // be *= delta t
552         speed_error_accum += error;
553         if ( speed_error_accum > 2000.0 ) {
554             speed_error_accum = 2000.0;
555         }
556         else if ( speed_error_accum < -2000.0 ) {
557             speed_error_accum = -2000.0;
558         }
559
560         // calculate integral error, and adjustment amount
561         int_error = speed_error_accum;
562
563         // printf("error = %.2f  int_error = %.2f\n", error, int_error);
564         int_adj = int_error / 200.0;
565
566         // caclulate proportional error
567         prop_error = error;
568         prop_adj = 0.5 + prop_error / 50.0;
569
570         total_adj = 0.9 * prop_adj + 0.1 * int_adj;
571         if ( total_adj > 1.0 ) {
572             total_adj = 1.0;
573         }
574         else if ( total_adj < 0.0 ) {
575             total_adj = 0.0;
576         }
577
578         controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
579     }
580
581 #ifdef THIS_CODE_IS_NOT_USED
582     if (Mode == 2) // Glide slope hold
583         {
584             double RelSlope;
585             double RelElevator;
586
587             // First, calculate Relative slope and normalize it
588             RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
589
590             // Now calculate the elevator offset from current angle
591             if ( abs(RelSlope) > SlopeSmooth )
592                 {
593                     if ( RelSlope < 0 )     //  set RelElevator to max in the correct direction
594                         RelElevator = -MaxElevator;
595                     else
596                         RelElevator = MaxElevator;
597                 }
598
599             else
600                 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
601
602             // set the elevator
603             fgElevMove(RelElevator);
604
605         }
606 #endif // THIS_CODE_IS_NOT_USED
607
608     // stash this runs control settings
609     //  update_old_control_values();
610     old_aileron = controls.get_aileron();
611     old_elevator = controls.get_elevator();
612     old_elevator_trim = controls.get_elevator_trim();
613     old_rudder = controls.get_rudder();
614
615     // for cross track error
616     old_lat = lat;
617     old_lon = lon;
618         
619         // Ok, we are done
620     return 0;
621 }
622
623
624 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
625     heading_mode = mode;
626
627     if ( heading_mode == FG_HEADING_LOCK ) {
628         // set heading hold to current heading
629         TargetHeading = FGBFI::getHeading();
630     } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
631         double course, reverse, distance;
632         // turn on location hold
633         // turn on heading hold
634         old_lat = FGBFI::getLatitude();
635         old_lon = FGBFI::getLongitude();
636                         
637         // need to test for iter
638         if( !geo_inverse_wgs_84( FGBFI::getAltitude() * FEET_TO_METER,
639                                  FGBFI::getLatitude(),
640                                  FGBFI::getLongitude(),
641                                  TargetLatitude,
642                                  TargetLongitude,
643                                  &course,
644                                  &reverse,
645                                  &distance ) ) {
646             TargetHeading = course;
647             TargetDistance = distance;
648             MakeTargetDistanceStr( distance );
649         }
650
651         FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetLocation: ( "
652                 << TargetLatitude  << " "
653                 << TargetLongitude << " ) "
654                 );
655     }
656         
657     MakeTargetHeadingStr( TargetHeading );                      
658     update_old_control_values();
659 }
660
661
662 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
663     altitude_mode = mode;
664
665     alt_error_accum = 0.0;
666
667     if ( altitude_mode == FG_ALTITUDE_LOCK ) {
668         // lock at current altitude
669         TargetAltitude = FGBFI::getAltitude() * FEET_TO_METER;
670
671         if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
672             MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
673         } else {
674             MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
675         }
676     } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
677         climb_error_accum = 0.0;
678
679     } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
680         TargetAGL = FGBFI::getAGL() * FEET_TO_METER;
681
682         if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
683             MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
684         } else {
685             MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
686         }
687     }
688     
689     update_old_control_values();
690     FG_LOG( FG_COCKPIT, FG_INFO, " set_AltitudeMode():" );
691 }
692
693
694 #if 0
695 static inline double get_aoa( void ) {
696     return( cur_fdm_state->get_Gamma_vert_rad() * RAD_TO_DEG );
697 }
698
699 static inline double fgAPget_latitude( void ) {
700     return( cur_fdm_state->get_Latitude() * RAD_TO_DEG );
701 }
702
703 static inline double fgAPget_longitude( void ) {
704     return( cur_fdm_state->get_Longitude() * RAD_TO_DEG );
705 }
706
707 static inline double fgAPget_roll( void ) {
708     return( cur_fdm_state->get_Phi() * RAD_TO_DEG );
709 }
710
711 static inline double get_pitch( void ) {
712     return( cur_fdm_state->get_Theta() );
713 }
714
715 double fgAPget_heading( void ) {
716     return( cur_fdm_state->get_Psi() * RAD_TO_DEG );
717 }
718
719 static inline double fgAPget_altitude( void ) {
720     return( cur_fdm_state->get_Altitude() * FEET_TO_METER );
721 }
722
723 static inline double fgAPget_climb( void ) {
724     // return in meters per minute
725     return( cur_fdm_state->get_Climb_Rate() * FEET_TO_METER * 60 );
726 }
727
728 static inline double get_sideslip( void ) {
729     return( cur_fdm_state->get_Beta() );
730 }
731
732 static inline double fgAPget_agl( void ) {
733     double agl;
734
735     agl = cur_fdm_state->get_Altitude() * FEET_TO_METER
736         - scenery.cur_elev;
737
738     return( agl );
739 }
740 #endif
741
742
743 void FGAutopilot::AltitudeSet( double new_altitude ) {
744     double target_alt = new_altitude;
745
746     // cout << "new altitude = " << new_altitude << endl;
747
748     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
749         target_alt = new_altitude * FEET_TO_METER;
750     }
751
752     if( target_alt < scenery.cur_elev ) {
753         target_alt = scenery.cur_elev;
754     }
755
756     TargetAltitude = target_alt;
757     altitude_mode = FG_ALTITUDE_LOCK;
758
759     // cout << "TargetAltitude = " << TargetAltitude << endl;
760
761     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
762         target_alt *= METER_TO_FEET;
763     }
764     // ApAltitudeDialogInput->setValue((float)target_alt);
765     MakeTargetAltitudeStr( target_alt );
766         
767     update_old_control_values();
768 }
769
770
771 void FGAutopilot::AltitudeAdjust( double inc )
772 {
773     double target_alt, target_agl;
774
775     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
776         target_alt = TargetAltitude * METER_TO_FEET;
777         target_agl = TargetAGL * METER_TO_FEET;
778     } else {
779         target_alt = TargetAltitude;
780         target_agl = TargetAGL;
781     }
782
783     target_alt = ( int ) ( target_alt / inc ) * inc + inc;
784     target_agl = ( int ) ( target_agl / inc ) * inc + inc;
785
786     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
787         target_alt *= FEET_TO_METER;
788         target_agl *= FEET_TO_METER;
789     }
790
791     TargetAltitude = target_alt;
792     TargetAGL = target_agl;
793         
794     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
795         target_alt *= METER_TO_FEET;
796     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
797         target_agl *= METER_TO_FEET;
798
799     if ( altitude_mode == FG_ALTITUDE_LOCK ) {
800         MakeTargetAltitudeStr( target_alt );
801     } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
802         MakeTargetAltitudeStr( target_agl );
803     }
804
805     update_old_control_values();
806 }
807
808
809 void FGAutopilot::HeadingAdjust( double inc ) {
810     heading_mode = FG_HEADING_LOCK;
811         
812     double target = ( int ) ( TargetHeading / inc ) * inc + inc;
813
814     TargetHeading = NormalizeDegrees( target );
815     // following cast needed ambiguous plib
816     // ApHeadingDialogInput -> setValue ((float)TargetHeading );
817     MakeTargetHeadingStr( TargetHeading );                      
818     update_old_control_values();
819 }
820
821
822 void FGAutopilot::HeadingSet( double new_heading ) {
823     heading_mode = FG_HEADING_LOCK;
824         
825     new_heading = NormalizeDegrees( new_heading );
826     TargetHeading = new_heading;
827     // following cast needed ambiguous plib
828     // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
829     MakeTargetHeadingStr( TargetHeading );                      
830     update_old_control_values();
831 }
832
833 void FGAutopilot::AutoThrottleAdjust( double inc ) {
834     double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
835
836     TargetSpeed = target;
837 }
838
839
840 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
841     auto_throttle = value;
842
843     if ( auto_throttle == true ) {
844         TargetSpeed = FGBFI::getAirspeed();
845         speed_error_accum = 0.0;
846     }
847
848     update_old_control_values();
849     FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAutoThrottle: ("
850             << auto_throttle << ") " << TargetSpeed );
851 }