1 // newauto.cxx -- autopilot defines and prototypes (very alpha)
3 // Started April 1998 Copyright (C) 1998
5 // Contributions by Jeff Goeke-Smith <jgoeke@voyager.net>
6 // Norman Vine <nhv@cape.com>
7 // Curtis Olson <curt@flightgear.org>
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.
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.
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.
30 #include <stdio.h> // sprintf()
32 #include <simgear/constants.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/math/sg_geodesy.hxx>
36 #include <Cockpit/radiostack.hxx>
37 #include <Controls/controls.hxx>
38 #include <FDM/flight.hxx>
39 #include <Main/bfi.hxx>
40 #include <Main/globals.hxx>
41 #include <Main/options.hxx>
42 #include <Scenery/scenery.hxx>
44 #include "newauto.hxx"
47 FGAutopilot *current_autopilot;
50 // Climb speed constants
51 const double min_climb = 70.0; // kts
52 const double best_climb = 75.0; // kts
53 const double ideal_climb_rate = 500.0; // fpm
55 /// These statics will eventually go into the class
56 /// they are just here while I am experimenting -- NHV :-)
57 // AutoPilot Gain Adjuster members
58 static double MaxRollAdjust; // MaxRollAdjust = 2 * APData->MaxRoll;
59 static double RollOutAdjust; // RollOutAdjust = 2 * APData->RollOut;
60 static double MaxAileronAdjust; // MaxAileronAdjust = 2 * APData->MaxAileron;
61 static double RollOutSmoothAdjust; // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
63 static char NewTgtAirportId[16];
64 // static char NewTgtAirportLabel[] = "Enter New TgtAirport ID";
66 extern char *coord_format_lat(float);
67 extern char *coord_format_lon(float);
70 void FGAutopilot::MakeTargetLatLonStr( double lat, double lon ) {
71 sprintf( TargetLatitudeStr , "%s", coord_format_lat(get_TargetLatitude()));
72 sprintf( TargetLongitudeStr, "%s", coord_format_lon(get_TargetLongitude()));
73 sprintf( TargetLatLonStr, "%s %s", TargetLatitudeStr, TargetLongitudeStr );
77 void FGAutopilot::MakeTargetAltitudeStr( double altitude ) {
78 if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
79 sprintf( TargetAltitudeStr, "APAltitude %6.0f+", altitude );
81 sprintf( TargetAltitudeStr, "APAltitude %6.0f", altitude );
86 void FGAutopilot::MakeTargetHeadingStr( double bearing ) {
89 } else if (bearing > 360. ) {
92 sprintf( TargetHeadingStr, "APHeading %6.1f", bearing );
96 static inline double get_speed( void ) {
97 return( cur_fdm_state->get_V_equiv_kts() );
100 static inline double get_ground_speed() {
101 // starts in ft/s so we convert to kts
102 double ft_s = cur_fdm_state->get_V_ground_speed()
103 * current_options.get_speed_up();;
104 double kts = ft_s * FEET_TO_METER * 3600 * METER_TO_NM;
110 void FGAutopilot::MakeTargetDistanceStr( double distance ) {
111 double eta = distance*METER_TO_NM / get_ground_speed();
112 if ( eta >= 100.0 ) { eta = 99.999; }
114 if ( eta < (1.0/6.0) ) {
115 // within 10 minutes, bump up to min/secs
119 minor = (int)((eta - (int)eta) * 60.0);
120 sprintf( TargetDistanceStr, "%s %.2f NM ETA %d:%02d",
121 waypoint.get_id().c_str(),
122 distance*METER_TO_NM, major, minor );
123 // cout << "distance = " << distance*METER_TO_NM
124 // << " gndsp = " << get_ground_speed()
125 // << " time = " << eta
126 // << " major = " << major
127 // << " minor = " << minor
132 void FGAutopilot::update_old_control_values() {
133 old_aileron = FGBFI::getAileron();
134 old_elevator = FGBFI::getElevator();
135 old_elevator_trim = FGBFI::getElevatorTrim();
136 old_rudder = FGBFI::getRudder();
140 // Initialize autopilot subsystem
141 void FGAutopilot::init() {
142 FG_LOG( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem" );
144 heading_hold = false ; // turn the heading hold off
145 altitude_hold = false ; // turn the altitude hold off
146 auto_throttle = false ; // turn the auto throttle off
148 // Initialize target location to startup location
149 old_lat = FGBFI::getLatitude();
150 old_lon = FGBFI::getLongitude();
151 // set_WayPoint( old_lon, old_lat, 0.0, "default" );
153 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
155 TargetHeading = 0.0; // default direction, due north
156 TargetAltitude = 3000; // default altitude in meters
157 alt_error_accum = 0.0;
158 climb_error_accum = 0.0;
160 MakeTargetAltitudeStr( 3000.0);
161 MakeTargetHeadingStr( 0.0 );
163 // These eventually need to be read from current_aircaft somehow.
165 // the maximum roll, in Deg
168 // the deg from heading to start rolling out at, in Deg
171 // how far can I move the aleron from center.
174 // Smoothing distance for alerion control
177 // Hardwired for now should be in options
178 // 25% max control variablilty 0.5 / 2.0
179 disengage_threshold = 1.0;
181 #if !defined( USING_SLIDER_CLASS )
182 MaxRollAdjust = 2 * MaxRoll;
183 RollOutAdjust = 2 * RollOut;
184 MaxAileronAdjust = 2 * MaxAileron;
185 RollOutSmoothAdjust = 2 * RollOutSmooth;
186 #endif // !defined( USING_SLIDER_CLASS )
188 update_old_control_values();
190 // Initialize GUI components of autopilot
191 // NewTgtAirportInit();
192 // fgAPAdjustInit() ;
194 // NewAltitudeInit();
198 // Reset the autopilot system
199 void FGAutopilot::reset() {
201 heading_hold = false ; // turn the heading hold off
202 altitude_hold = false ; // turn the altitude hold off
203 auto_throttle = false ; // turn the auto throttle off
205 TargetHeading = 0.0; // default direction, due north
206 MakeTargetHeadingStr( TargetHeading );
208 TargetAltitude = 3000; // default altitude in meters
209 MakeTargetAltitudeStr( TargetAltitude );
211 alt_error_accum = 0.0;
212 climb_error_accum = 0.0;
214 update_old_control_values();
216 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
218 // TargetLatitude = FGBFI::getLatitude();
219 // TargetLongitude = FGBFI::getLongitude();
220 // s<et_WayPoint( FGBFI::getLongitude(), FGBFI::getLatitude(), 0.0, "reset" );
222 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
226 static double NormalizeDegrees( double Input ) {
227 // normalize the input to the range (-180,180]
228 // Input should not be greater than -360 to 360.
229 // Current rules send the output to an undefined state.
233 else if ( Input <= -180 )
234 while ( Input <= -180 )
239 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
240 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
241 //assert(x1 != x2); // Divide by zero error. Cold abort for now
244 // static double y = 0.0;
245 // double dx = x2 -x1;
246 // if( (dx < -FG_EPSILON ) || ( dx > FG_EPSILON ) )
249 double m, b, y; // the constants to find in y=mx+b
252 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
254 b = y1 - m * x1; // calculate the b
256 y = m * x + b; // the final calculation
265 int FGAutopilot::run() {
266 // Remove the following lines when the calling funcitons start
267 // passing in the data pointer
269 // get control settings
270 // double aileron = FGBFI::getAileron();
271 // double elevator = FGBFI::getElevator();
272 // double elevator_trim = FGBFI::getElevatorTrim();
273 // double rudder = FGBFI::getRudder();
275 double lat = FGBFI::getLatitude();
276 double lon = FGBFI::getLongitude();
277 double alt = FGBFI::getAltitude() * FEET_TO_METER;
279 #ifdef FG_FORCE_AUTO_DISENGAGE
280 // see if somebody else has changed them
281 if( fabs(aileron - old_aileron) > disengage_threshold ||
282 fabs(elevator - old_elevator) > disengage_threshold ||
283 fabs(elevator_trim - old_elevator_trim) >
284 disengage_threshold ||
285 fabs(rudder - old_rudder) > disengage_threshold )
287 // if controls changed externally turn autopilot off
288 waypoint_hold = false ; // turn the target hold off
289 heading_hold = false ; // turn the heading hold off
290 altitude_hold = false ; // turn the altitude hold off
291 terrain_follow = false; // turn the terrain_follow hold off
292 // auto_throttle = false; // turn the auto_throttle off
294 // stash this runs control settings
295 old_aileron = aileron;
296 old_elevator = elevator;
297 old_elevator_trim = elevator_trim;
305 if ( heading_hold == true ) {
307 if ( heading_mode == FG_HEADING_LOCK ) {
308 // leave target heading alone
309 } else if ( heading_mode == FG_HEADING_NAV1 ) {
312 if ( current_radiostack->get_nav1_loc() ) {
313 // localizers radials are "true"
314 tgt_radial = current_radiostack->get_nav1_radial();
316 tgt_radial = current_radiostack->get_nav1_radial()
317 + FGBFI::getMagVar();
319 cur_radial = current_radiostack->get_nav1_heading();
320 // cout << "target rad (true) = " << tgt_radial
321 // << " current rad (true) = " << cur_radial
324 double diff = (tgt_radial - cur_radial);
325 while ( diff < -180.0 ) { diff += 360.0; }
326 while ( diff > 180.0 ) { diff -= 360.0; }
328 diff *= (current_radiostack->get_nav1_loc_dist() * METER_TO_NM);
329 if ( diff < -30.0 ) { diff = -30.0; }
330 if ( diff > 30.0 ) { diff = 30.0; }
332 TargetHeading = cur_radial - diff;
333 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
334 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
335 // cout << "target course (true) = " << TargetHeading << endl;
336 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
337 // update target heading to waypoint
339 double wp_course, wp_distance;
341 #ifdef DO_fgAP_CORRECTED_COURSE
342 // compute course made good
343 // this needs lots of special casing before use
344 double course, reverse, distance, corrected_course;
345 // need to test for iter
346 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
354 #endif // DO_fgAP_CORRECTED_COURSE
356 // compute course to way_point
357 // need to test for iter
358 waypoint.CourseAndDistance( lon, lat, alt,
359 &wp_course, &wp_distance );
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),
367 #endif // DO_fgAP_CORRECTED_COURSE
369 if ( wp_distance > 100 ) {
370 // corrected_course = course - wp_course;
371 TargetHeading = NormalizeDegrees(wp_course);
373 cout << "Reached waypoint within " << wp_distance << "meters"
376 // pop off this waypoint from the list
377 if ( globals->get_route()->size() ) {
378 globals->get_route()->delete_first();
381 // see if there are more waypoints on the list
382 if ( globals->get_route()->size() ) {
384 set_HeadingMode( FG_HEADING_WAYPOINT );
387 heading_mode = FG_HEADING_LOCK;
388 // use current heading
389 TargetHeading = FGBFI::getHeading();
392 MakeTargetHeadingStr( TargetHeading );
393 // Force this just in case
394 TargetDistance = wp_distance;
395 MakeTargetDistanceStr( wp_distance );
403 RelHeading = NormalizeDegrees( TargetHeading - FGBFI::getHeading() );
404 // figure out how far off we are from desired heading
406 // Now it is time to deterime how far we should be rolled.
407 FG_LOG( FG_AUTOPILOT, FG_DEBUG, "RelHeading: " << RelHeading );
410 // Check if we are further from heading than the roll out point
411 if ( fabs( RelHeading ) > RollOut ) {
412 // set Target Roll to Max in desired direction
413 if ( RelHeading < 0 ) {
414 TargetRoll = 0 - MaxRoll;
416 TargetRoll = MaxRoll;
419 // We have to calculate the Target roll
421 // This calculation engine thinks that the Target roll
422 // should be a line from (RollOut,MaxRoll) to (-RollOut,
423 // -MaxRoll) I hope this works well. If I get ambitious
424 // some day this might become a fancier curve or
427 TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
432 // Target Roll has now been Found.
434 // Compare Target roll to Current Roll, Generate Rel Roll
436 FG_LOG( FG_COCKPIT, FG_BULK, "TargetRoll: " << TargetRoll );
438 RelRoll = NormalizeDegrees( TargetRoll - FGBFI::getRoll() );
440 // Check if we are further from heading than the roll out smooth point
441 if ( fabs( RelRoll ) > RollOutSmooth ) {
442 // set Target Roll to Max in desired direction
444 AileronSet = 0 - MaxAileron;
446 AileronSet = MaxAileron;
449 AileronSet = LinearExtrapolate( RelRoll, -RollOutSmooth,
455 controls.set_aileron( AileronSet );
456 controls.set_rudder( AileronSet / 4.0 );
457 // controls.set_rudder( 0.0 );
461 if ( altitude_hold ) {
462 double speed, max_climb, error;
463 double prop_error, int_error;
464 double prop_adj, int_adj, total_adj;
466 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
467 // normal altitude hold
468 // cout << "TargetAltitude = " << TargetAltitude
469 // << "Altitude = " << FGBFI::getAltitude() * FEET_TO_METER
472 ( TargetAltitude - FGBFI::getAltitude() * FEET_TO_METER ) * 8.0;
473 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
474 double x = current_radiostack->get_nav1_gs_dist();
475 double y = (FGBFI::getAltitude()
476 - current_radiostack->get_nav1_elev()) * FEET_TO_METER;
477 double angle = atan2( y, x ) * RAD_TO_DEG;
478 double gs_diff = current_radiostack->get_nav1_target_gs() - angle;
479 climb_error_accum += gs_diff * 2.0;
480 TargetClimbRate = gs_diff * 200.0 + climb_error_accum;
481 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
482 // brain dead ground hugging with no look ahead
484 ( TargetAGL - FGBFI::getAGL()*FEET_TO_METER ) * 16.0;
485 // cout << "target agl = " << TargetAGL
486 // << " current agl = " << fgAPget_agl()
487 // << " target climb rate = " << TargetClimbRate
490 // just try to zero out rate of climb ...
491 TargetClimbRate = 0.0;
496 if ( speed < min_climb ) {
498 } else if ( speed < best_climb ) {
499 max_climb = ((best_climb - min_climb) - (best_climb - speed))
501 / (best_climb - min_climb);
503 max_climb = ( speed - best_climb ) * 10.0 + ideal_climb_rate;
506 // this first one could be optional if we wanted to allow
507 // better climb performance assuming we have the airspeed to
509 if ( TargetClimbRate > ideal_climb_rate ) {
510 TargetClimbRate = ideal_climb_rate;
513 if ( TargetClimbRate > max_climb ) {
514 TargetClimbRate = max_climb;
517 if ( TargetClimbRate < -ideal_climb_rate ) {
518 TargetClimbRate = -ideal_climb_rate;
521 error = FGBFI::getVerticalSpeed() * FEET_TO_METER - TargetClimbRate;
522 // cout << "climb rate = " << fgAPget_climb()
523 // << " error = " << error << endl;
525 // accumulate the error under the curve ... this really should
527 alt_error_accum += error;
529 // calculate integral error, and adjustment amount
530 int_error = alt_error_accum;
531 // printf("error = %.2f int_error = %.2f\n", error, int_error);
532 int_adj = int_error / 8000.0;
534 // caclulate proportional error
536 prop_adj = prop_error / 2000.0;
538 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
539 // if ( total_adj > 0.6 ) {
541 // } else if ( total_adj < -0.2 ) {
544 if ( total_adj > 1.0 ) {
546 } else if ( total_adj < -1.0 ) {
550 controls.set_elevator( total_adj );
554 if ( auto_throttle ) {
556 double prop_error, int_error;
557 double prop_adj, int_adj, total_adj;
559 error = TargetSpeed - get_speed();
561 // accumulate the error under the curve ... this really should
563 speed_error_accum += error;
564 if ( speed_error_accum > 2000.0 ) {
565 speed_error_accum = 2000.0;
567 else if ( speed_error_accum < -2000.0 ) {
568 speed_error_accum = -2000.0;
571 // calculate integral error, and adjustment amount
572 int_error = speed_error_accum;
574 // printf("error = %.2f int_error = %.2f\n", error, int_error);
575 int_adj = int_error / 200.0;
577 // caclulate proportional error
579 prop_adj = 0.5 + prop_error / 50.0;
581 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
582 if ( total_adj > 1.0 ) {
585 else if ( total_adj < 0.0 ) {
589 controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
592 #ifdef THIS_CODE_IS_NOT_USED
593 if (Mode == 2) // Glide slope hold
598 // First, calculate Relative slope and normalize it
599 RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
601 // Now calculate the elevator offset from current angle
602 if ( abs(RelSlope) > SlopeSmooth )
604 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
605 RelElevator = -MaxElevator;
607 RelElevator = MaxElevator;
611 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
614 fgElevMove(RelElevator);
617 #endif // THIS_CODE_IS_NOT_USED
619 // stash this runs control settings
620 // update_old_control_values();
621 old_aileron = controls.get_aileron();
622 old_elevator = controls.get_elevator();
623 old_elevator_trim = controls.get_elevator_trim();
624 old_rudder = controls.get_rudder();
626 // for cross track error
635 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
638 if ( heading_mode == FG_HEADING_LOCK ) {
639 // set heading hold to current heading
640 TargetHeading = FGBFI::getHeading();
641 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
642 if ( globals->get_route()->size() ) {
643 double course, distance;
645 old_lat = FGBFI::getLatitude();
646 old_lon = FGBFI::getLongitude();
648 waypoint = globals->get_route()->get_first();
649 waypoint.CourseAndDistance( FGBFI::getLongitude(),
650 FGBFI::getLatitude(),
651 FGBFI::getLatitude() * FEET_TO_METER,
652 &course, &distance );
653 TargetHeading = course;
654 TargetDistance = distance;
655 MakeTargetLatLonStr( waypoint.get_target_lat(),
656 waypoint.get_target_lon() );
657 MakeTargetDistanceStr( distance );
659 if ( waypoint.get_target_alt() > 0.0 ) {
660 TargetAltitude = waypoint.get_target_alt();
661 altitude_mode = FG_ALTITUDE_LOCK;
662 set_AltitudeEnabled( true );
663 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
666 FG_LOG( FG_COCKPIT, FG_INFO, " set_HeadingMode: ( "
667 << get_TargetLatitude() << " "
668 << get_TargetLongitude() << " ) "
671 // no more way points, default to heading lock.
672 heading_mode = FG_HEADING_LOCK;
673 TargetHeading = FGBFI::getHeading();
677 MakeTargetHeadingStr( TargetHeading );
678 update_old_control_values();
682 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
683 altitude_mode = mode;
685 alt_error_accum = 0.0;
687 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
688 // lock at current altitude
689 TargetAltitude = FGBFI::getAltitude() * FEET_TO_METER;
691 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
692 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
694 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
696 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
697 climb_error_accum = 0.0;
699 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
700 TargetAGL = FGBFI::getAGL() * FEET_TO_METER;
702 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
703 MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
705 MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
709 update_old_control_values();
710 FG_LOG( FG_COCKPIT, FG_INFO, " set_AltitudeMode():" );
715 static inline double get_aoa( void ) {
716 return( cur_fdm_state->get_Gamma_vert_rad() * RAD_TO_DEG );
719 static inline double fgAPget_latitude( void ) {
720 return( cur_fdm_state->get_Latitude() * RAD_TO_DEG );
723 static inline double fgAPget_longitude( void ) {
724 return( cur_fdm_state->get_Longitude() * RAD_TO_DEG );
727 static inline double fgAPget_roll( void ) {
728 return( cur_fdm_state->get_Phi() * RAD_TO_DEG );
731 static inline double get_pitch( void ) {
732 return( cur_fdm_state->get_Theta() );
735 double fgAPget_heading( void ) {
736 return( cur_fdm_state->get_Psi() * RAD_TO_DEG );
739 static inline double fgAPget_altitude( void ) {
740 return( cur_fdm_state->get_Altitude() * FEET_TO_METER );
743 static inline double fgAPget_climb( void ) {
744 // return in meters per minute
745 return( cur_fdm_state->get_Climb_Rate() * FEET_TO_METER * 60 );
748 static inline double get_sideslip( void ) {
749 return( cur_fdm_state->get_Beta() );
752 static inline double fgAPget_agl( void ) {
755 agl = cur_fdm_state->get_Altitude() * FEET_TO_METER
763 void FGAutopilot::AltitudeSet( double new_altitude ) {
764 double target_alt = new_altitude;
766 // cout << "new altitude = " << new_altitude << endl;
768 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
769 target_alt = new_altitude * FEET_TO_METER;
772 if( target_alt < scenery.cur_elev ) {
773 target_alt = scenery.cur_elev;
776 TargetAltitude = target_alt;
777 altitude_mode = FG_ALTITUDE_LOCK;
779 // cout << "TargetAltitude = " << TargetAltitude << endl;
781 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
782 target_alt *= METER_TO_FEET;
784 // ApAltitudeDialogInput->setValue((float)target_alt);
785 MakeTargetAltitudeStr( target_alt );
787 update_old_control_values();
791 void FGAutopilot::AltitudeAdjust( double inc )
793 double target_alt, target_agl;
795 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
796 target_alt = TargetAltitude * METER_TO_FEET;
797 target_agl = TargetAGL * METER_TO_FEET;
799 target_alt = TargetAltitude;
800 target_agl = TargetAGL;
803 // cout << "target_agl = " << target_agl << endl;
804 // cout << "target_agl / inc = " << target_agl / inc << endl;
805 // cout << "(int)(target_agl / inc) = " << (int)(target_agl / inc) << endl;
807 if ( fabs((int)(target_alt / inc) * inc - target_alt) < FG_EPSILON ) {
810 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
813 if ( fabs((int)(target_agl / inc) * inc - target_agl) < FG_EPSILON ) {
816 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
819 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
820 target_alt *= FEET_TO_METER;
821 target_agl *= FEET_TO_METER;
824 TargetAltitude = target_alt;
825 TargetAGL = target_agl;
827 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
828 target_alt *= METER_TO_FEET;
829 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
830 target_agl *= METER_TO_FEET;
832 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
833 MakeTargetAltitudeStr( target_alt );
834 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
835 MakeTargetAltitudeStr( target_agl );
838 update_old_control_values();
842 void FGAutopilot::HeadingAdjust( double inc ) {
843 heading_mode = FG_HEADING_LOCK;
845 double target = ( int ) ( TargetHeading / inc ) * inc + inc;
847 TargetHeading = NormalizeDegrees( target );
848 // following cast needed ambiguous plib
849 // ApHeadingDialogInput -> setValue ((float)TargetHeading );
850 MakeTargetHeadingStr( TargetHeading );
851 update_old_control_values();
855 void FGAutopilot::HeadingSet( double new_heading ) {
856 heading_mode = FG_HEADING_LOCK;
858 new_heading = NormalizeDegrees( new_heading );
859 TargetHeading = new_heading;
860 // following cast needed ambiguous plib
861 // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
862 MakeTargetHeadingStr( TargetHeading );
863 update_old_control_values();
866 void FGAutopilot::AutoThrottleAdjust( double inc ) {
867 double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
869 TargetSpeed = target;
873 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
874 auto_throttle = value;
876 if ( auto_throttle == true ) {
877 TargetSpeed = FGBFI::getAirspeed();
878 speed_error_accum = 0.0;
881 update_old_control_values();
882 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAutoThrottle: ("
883 << auto_throttle << ") " << TargetSpeed );