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