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 * globals->get_options()->get_speed_up();;
104 double kts = ft_s * FEET_TO_METER * 3600 * METER_TO_NM;
110 void FGAutopilot::MakeTargetWPStr( double distance ) {
113 int size = globals->get_route()->size();
115 // start by wiping the strings
122 SGWayPoint wp1 = globals->get_route()->get_waypoint( 0 );
124 double eta = accum * METER_TO_NM / get_ground_speed();
125 if ( eta >= 100.0 ) { eta = 99.999; }
127 if ( eta < (1.0/6.0) ) {
128 // within 10 minutes, bump up to min/secs
132 minor = (int)((eta - (int)eta) * 60.0);
133 sprintf( TargetWP1Str, "%s %.2f NM ETA %d:%02d",
134 wp1.get_id().c_str(),
135 accum*METER_TO_NM, major, minor );
136 // cout << "distance = " << distance*METER_TO_NM
137 // << " gndsp = " << get_ground_speed()
138 // << " time = " << eta
139 // << " major = " << major
140 // << " minor = " << minor
146 SGWayPoint wp2 = globals->get_route()->get_waypoint( 1 );
147 accum += wp2.get_distance();
149 double eta = accum * METER_TO_NM / get_ground_speed();
150 if ( eta >= 100.0 ) { eta = 99.999; }
152 if ( eta < (1.0/6.0) ) {
153 // within 10 minutes, bump up to min/secs
157 minor = (int)((eta - (int)eta) * 60.0);
158 sprintf( TargetWP2Str, "%s %.2f NM ETA %d:%02d",
159 wp2.get_id().c_str(),
160 accum*METER_TO_NM, major, minor );
165 for ( int i = 2; i < size; ++i ) {
166 accum += globals->get_route()->get_waypoint( i ).get_distance();
169 SGWayPoint wpn = globals->get_route()->get_waypoint( size - 1 );
171 double eta = accum * METER_TO_NM / get_ground_speed();
172 if ( eta >= 100.0 ) { eta = 99.999; }
174 if ( eta < (1.0/6.0) ) {
175 // within 10 minutes, bump up to min/secs
179 minor = (int)((eta - (int)eta) * 60.0);
180 sprintf( TargetWP3Str, "%s %.2f NM ETA %d:%02d",
181 wpn.get_id().c_str(),
182 accum*METER_TO_NM, major, minor );
187 void FGAutopilot::update_old_control_values() {
188 old_aileron = FGBFI::getAileron();
189 old_elevator = FGBFI::getElevator();
190 old_elevator_trim = FGBFI::getElevatorTrim();
191 old_rudder = FGBFI::getRudder();
195 // Initialize autopilot subsystem
196 void FGAutopilot::init() {
197 FG_LOG( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem" );
199 heading_hold = false ; // turn the heading hold off
200 altitude_hold = false ; // turn the altitude hold off
201 auto_throttle = false ; // turn the auto throttle off
203 // Initialize target location to startup location
204 old_lat = FGBFI::getLatitude();
205 old_lon = FGBFI::getLongitude();
206 // set_WayPoint( old_lon, old_lat, 0.0, "default" );
208 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
210 TargetHeading = 0.0; // default direction, due north
211 TargetAltitude = 3000; // default altitude in meters
212 alt_error_accum = 0.0;
213 climb_error_accum = 0.0;
215 MakeTargetAltitudeStr( 3000.0);
216 MakeTargetHeadingStr( 0.0 );
218 // These eventually need to be read from current_aircaft somehow.
220 // the maximum roll, in Deg
223 // the deg from heading to start rolling out at, in Deg
226 // how far can I move the aleron from center.
229 // Smoothing distance for alerion control
232 // Hardwired for now should be in options
233 // 25% max control variablilty 0.5 / 2.0
234 disengage_threshold = 1.0;
236 #if !defined( USING_SLIDER_CLASS )
237 MaxRollAdjust = 2 * MaxRoll;
238 RollOutAdjust = 2 * RollOut;
239 MaxAileronAdjust = 2 * MaxAileron;
240 RollOutSmoothAdjust = 2 * RollOutSmooth;
241 #endif // !defined( USING_SLIDER_CLASS )
243 update_old_control_values();
245 // Initialize GUI components of autopilot
246 // NewTgtAirportInit();
247 // fgAPAdjustInit() ;
249 // NewAltitudeInit();
253 // Reset the autopilot system
254 void FGAutopilot::reset() {
256 heading_hold = false ; // turn the heading hold off
257 altitude_hold = false ; // turn the altitude hold off
258 auto_throttle = false ; // turn the auto throttle off
260 TargetHeading = 0.0; // default direction, due north
261 MakeTargetHeadingStr( TargetHeading );
263 TargetAltitude = 3000; // default altitude in meters
264 MakeTargetAltitudeStr( TargetAltitude );
266 alt_error_accum = 0.0;
267 climb_error_accum = 0.0;
269 update_old_control_values();
271 sprintf( NewTgtAirportId, "%s", globals->get_options()->get_airport_id().c_str() );
273 // TargetLatitude = FGBFI::getLatitude();
274 // TargetLongitude = FGBFI::getLongitude();
275 // set_WayPoint( FGBFI::getLongitude(), FGBFI::getLatitude(), 0.0, "reset" );
277 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
281 static double NormalizeDegrees( double Input ) {
282 // normalize the input to the range (-180,180]
283 // Input should not be greater than -360 to 360.
284 // Current rules send the output to an undefined state.
288 else if ( Input <= -180 )
289 while ( Input <= -180 )
294 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
295 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
296 //assert(x1 != x2); // Divide by zero error. Cold abort for now
299 // static double y = 0.0;
300 // double dx = x2 -x1;
301 // if( (dx < -FG_EPSILON ) || ( dx > FG_EPSILON ) )
304 double m, b, y; // the constants to find in y=mx+b
307 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
309 b = y1 - m * x1; // calculate the b
311 y = m * x + b; // the final calculation
320 int FGAutopilot::run() {
321 // Remove the following lines when the calling funcitons start
322 // passing in the data pointer
324 // get control settings
325 // double aileron = FGBFI::getAileron();
326 // double elevator = FGBFI::getElevator();
327 // double elevator_trim = FGBFI::getElevatorTrim();
328 // double rudder = FGBFI::getRudder();
330 double lat = FGBFI::getLatitude();
331 double lon = FGBFI::getLongitude();
332 double alt = FGBFI::getAltitude() * FEET_TO_METER;
334 #ifdef FG_FORCE_AUTO_DISENGAGE
335 // see if somebody else has changed them
336 if( fabs(aileron - old_aileron) > disengage_threshold ||
337 fabs(elevator - old_elevator) > disengage_threshold ||
338 fabs(elevator_trim - old_elevator_trim) >
339 disengage_threshold ||
340 fabs(rudder - old_rudder) > disengage_threshold )
342 // if controls changed externally turn autopilot off
343 waypoint_hold = false ; // turn the target hold off
344 heading_hold = false ; // turn the heading hold off
345 altitude_hold = false ; // turn the altitude hold off
346 terrain_follow = false; // turn the terrain_follow hold off
347 // auto_throttle = false; // turn the auto_throttle off
349 // stash this runs control settings
350 old_aileron = aileron;
351 old_elevator = elevator;
352 old_elevator_trim = elevator_trim;
360 if ( heading_hold == true ) {
362 if ( heading_mode == FG_HEADING_LOCK ) {
363 // leave target heading alone
364 } else if ( heading_mode == FG_HEADING_NAV1 ) {
367 if ( current_radiostack->get_nav1_loc() ) {
368 // localizers radials are "true"
369 tgt_radial = current_radiostack->get_nav1_radial();
371 tgt_radial = current_radiostack->get_nav1_radial()
372 + FGBFI::getMagVar();
374 cur_radial = current_radiostack->get_nav1_heading();
375 // cout << "target rad (true) = " << tgt_radial
376 // << " current rad (true) = " << cur_radial
379 double diff = (tgt_radial - cur_radial);
380 while ( diff < -180.0 ) { diff += 360.0; }
381 while ( diff > 180.0 ) { diff -= 360.0; }
383 diff *= (current_radiostack->get_nav1_loc_dist() * METER_TO_NM);
384 if ( diff < -30.0 ) { diff = -30.0; }
385 if ( diff > 30.0 ) { diff = 30.0; }
387 TargetHeading = cur_radial - diff;
388 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
389 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
390 // cout << "target course (true) = " << TargetHeading << endl;
391 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
392 // update target heading to waypoint
394 double wp_course, wp_distance;
396 #ifdef DO_fgAP_CORRECTED_COURSE
397 // compute course made good
398 // this needs lots of special casing before use
399 double course, reverse, distance, corrected_course;
400 // need to test for iter
401 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
409 #endif // DO_fgAP_CORRECTED_COURSE
411 // compute course to way_point
412 // need to test for iter
413 SGWayPoint wp = globals->get_route()->get_first();
414 wp.CourseAndDistance( lon, lat, alt,
415 &wp_course, &wp_distance );
417 #ifdef DO_fgAP_CORRECTED_COURSE
418 corrected_course = course - wp_course;
419 if( fabs(corrected_course) > 0.1 )
420 printf("fgAP: course %f wp_course %f %f %f\n",
421 course, wp_course, fabs(corrected_course),
423 #endif // DO_fgAP_CORRECTED_COURSE
425 if ( wp_distance > 100 ) {
426 // corrected_course = course - wp_course;
427 TargetHeading = NormalizeDegrees(wp_course);
429 cout << "Reached waypoint within " << wp_distance << "meters"
432 // pop off this waypoint from the list
433 if ( globals->get_route()->size() ) {
434 globals->get_route()->delete_first();
437 // see if there are more waypoints on the list
438 if ( globals->get_route()->size() ) {
440 set_HeadingMode( FG_HEADING_WAYPOINT );
443 heading_mode = FG_HEADING_LOCK;
444 // use current heading
445 TargetHeading = FGBFI::getHeading();
448 MakeTargetHeadingStr( TargetHeading );
449 // Force this just in case
450 TargetDistance = wp_distance;
451 MakeTargetWPStr( wp_distance );
459 RelHeading = NormalizeDegrees( TargetHeading - FGBFI::getHeading() );
460 // figure out how far off we are from desired heading
462 // Now it is time to deterime how far we should be rolled.
463 FG_LOG( FG_AUTOPILOT, FG_DEBUG, "RelHeading: " << RelHeading );
466 // Check if we are further from heading than the roll out point
467 if ( fabs( RelHeading ) > RollOut ) {
468 // set Target Roll to Max in desired direction
469 if ( RelHeading < 0 ) {
470 TargetRoll = 0 - MaxRoll;
472 TargetRoll = MaxRoll;
475 // We have to calculate the Target roll
477 // This calculation engine thinks that the Target roll
478 // should be a line from (RollOut,MaxRoll) to (-RollOut,
479 // -MaxRoll) I hope this works well. If I get ambitious
480 // some day this might become a fancier curve or
483 TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
488 // Target Roll has now been Found.
490 // Compare Target roll to Current Roll, Generate Rel Roll
492 FG_LOG( FG_COCKPIT, FG_BULK, "TargetRoll: " << TargetRoll );
494 RelRoll = NormalizeDegrees( TargetRoll - FGBFI::getRoll() );
496 // Check if we are further from heading than the roll out smooth point
497 if ( fabs( RelRoll ) > RollOutSmooth ) {
498 // set Target Roll to Max in desired direction
500 AileronSet = 0 - MaxAileron;
502 AileronSet = MaxAileron;
505 AileronSet = LinearExtrapolate( RelRoll, -RollOutSmooth,
511 controls.set_aileron( AileronSet );
512 controls.set_rudder( AileronSet / 4.0 );
513 // controls.set_rudder( 0.0 );
517 if ( altitude_hold ) {
518 double speed, max_climb, error;
519 double prop_error, int_error;
520 double prop_adj, int_adj, total_adj;
522 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
523 // normal altitude hold
524 // cout << "TargetAltitude = " << TargetAltitude
525 // << "Altitude = " << FGBFI::getAltitude() * FEET_TO_METER
528 ( TargetAltitude - FGBFI::getAltitude() * FEET_TO_METER ) * 8.0;
529 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
530 double x = current_radiostack->get_nav1_gs_dist();
531 double y = (FGBFI::getAltitude()
532 - current_radiostack->get_nav1_elev()) * FEET_TO_METER;
533 double angle = atan2( y, x ) * RAD_TO_DEG;
534 double gs_diff = current_radiostack->get_nav1_target_gs() - angle;
535 climb_error_accum += gs_diff * 2.0;
536 TargetClimbRate = gs_diff * 200.0 + climb_error_accum;
537 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
538 // brain dead ground hugging with no look ahead
540 ( TargetAGL - FGBFI::getAGL()*FEET_TO_METER ) * 16.0;
541 // cout << "target agl = " << TargetAGL
542 // << " current agl = " << fgAPget_agl()
543 // << " target climb rate = " << TargetClimbRate
546 // just try to zero out rate of climb ...
547 TargetClimbRate = 0.0;
552 if ( speed < min_climb ) {
554 } else if ( speed < best_climb ) {
555 max_climb = ((best_climb - min_climb) - (best_climb - speed))
557 / (best_climb - min_climb);
559 max_climb = ( speed - best_climb ) * 10.0 + ideal_climb_rate;
562 // this first one could be optional if we wanted to allow
563 // better climb performance assuming we have the airspeed to
565 if ( TargetClimbRate > ideal_climb_rate ) {
566 TargetClimbRate = ideal_climb_rate;
569 if ( TargetClimbRate > max_climb ) {
570 TargetClimbRate = max_climb;
573 if ( TargetClimbRate < -ideal_climb_rate ) {
574 TargetClimbRate = -ideal_climb_rate;
577 error = FGBFI::getVerticalSpeed() * FEET_TO_METER - TargetClimbRate;
578 // cout << "climb rate = " << fgAPget_climb()
579 // << " error = " << error << endl;
581 // accumulate the error under the curve ... this really should
583 alt_error_accum += error;
585 // calculate integral error, and adjustment amount
586 int_error = alt_error_accum;
587 // printf("error = %.2f int_error = %.2f\n", error, int_error);
588 int_adj = int_error / 8000.0;
590 // caclulate proportional error
592 prop_adj = prop_error / 2000.0;
594 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
595 // if ( total_adj > 0.6 ) {
597 // } else if ( total_adj < -0.2 ) {
600 if ( total_adj > 1.0 ) {
602 } else if ( total_adj < -1.0 ) {
606 controls.set_elevator( total_adj );
610 if ( auto_throttle ) {
612 double prop_error, int_error;
613 double prop_adj, int_adj, total_adj;
615 error = TargetSpeed - get_speed();
617 // accumulate the error under the curve ... this really should
619 speed_error_accum += error;
620 if ( speed_error_accum > 2000.0 ) {
621 speed_error_accum = 2000.0;
623 else if ( speed_error_accum < -2000.0 ) {
624 speed_error_accum = -2000.0;
627 // calculate integral error, and adjustment amount
628 int_error = speed_error_accum;
630 // printf("error = %.2f int_error = %.2f\n", error, int_error);
631 int_adj = int_error / 200.0;
633 // caclulate proportional error
635 prop_adj = 0.5 + prop_error / 50.0;
637 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
638 if ( total_adj > 1.0 ) {
641 else if ( total_adj < 0.0 ) {
645 controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
648 #ifdef THIS_CODE_IS_NOT_USED
649 if (Mode == 2) // Glide slope hold
654 // First, calculate Relative slope and normalize it
655 RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
657 // Now calculate the elevator offset from current angle
658 if ( abs(RelSlope) > SlopeSmooth )
660 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
661 RelElevator = -MaxElevator;
663 RelElevator = MaxElevator;
667 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
670 fgElevMove(RelElevator);
673 #endif // THIS_CODE_IS_NOT_USED
675 // stash this runs control settings
676 // update_old_control_values();
677 old_aileron = controls.get_aileron();
678 old_elevator = controls.get_elevator();
679 old_elevator_trim = controls.get_elevator_trim();
680 old_rudder = controls.get_rudder();
682 // for cross track error
691 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
694 if ( heading_mode == FG_HEADING_LOCK ) {
695 // set heading hold to current heading
696 TargetHeading = FGBFI::getHeading();
697 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
698 if ( globals->get_route()->size() ) {
699 double course, distance;
701 old_lat = FGBFI::getLatitude();
702 old_lon = FGBFI::getLongitude();
704 waypoint = globals->get_route()->get_first();
705 waypoint.CourseAndDistance( FGBFI::getLongitude(),
706 FGBFI::getLatitude(),
707 FGBFI::getLatitude() * FEET_TO_METER,
708 &course, &distance );
709 TargetHeading = course;
710 TargetDistance = distance;
711 MakeTargetLatLonStr( waypoint.get_target_lat(),
712 waypoint.get_target_lon() );
713 MakeTargetWPStr( distance );
715 if ( waypoint.get_target_alt() > 0.0 ) {
716 TargetAltitude = waypoint.get_target_alt();
717 altitude_mode = FG_ALTITUDE_LOCK;
718 set_AltitudeEnabled( true );
719 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
722 FG_LOG( FG_COCKPIT, FG_INFO, " set_HeadingMode: ( "
723 << get_TargetLatitude() << " "
724 << get_TargetLongitude() << " ) "
727 // no more way points, default to heading lock.
728 heading_mode = FG_HEADING_LOCK;
729 TargetHeading = FGBFI::getHeading();
733 MakeTargetHeadingStr( TargetHeading );
734 update_old_control_values();
738 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
739 altitude_mode = mode;
741 alt_error_accum = 0.0;
743 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
744 // lock at current altitude
745 TargetAltitude = FGBFI::getAltitude() * FEET_TO_METER;
747 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET ) {
748 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
750 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
752 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
753 climb_error_accum = 0.0;
755 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
756 TargetAGL = FGBFI::getAGL() * FEET_TO_METER;
758 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET ) {
759 MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
761 MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
765 update_old_control_values();
766 FG_LOG( FG_COCKPIT, FG_INFO, " set_AltitudeMode():" );
771 static inline double get_aoa( void ) {
772 return( cur_fdm_state->get_Gamma_vert_rad() * RAD_TO_DEG );
775 static inline double fgAPget_latitude( void ) {
776 return( cur_fdm_state->get_Latitude() * RAD_TO_DEG );
779 static inline double fgAPget_longitude( void ) {
780 return( cur_fdm_state->get_Longitude() * RAD_TO_DEG );
783 static inline double fgAPget_roll( void ) {
784 return( cur_fdm_state->get_Phi() * RAD_TO_DEG );
787 static inline double get_pitch( void ) {
788 return( cur_fdm_state->get_Theta() );
791 double fgAPget_heading( void ) {
792 return( cur_fdm_state->get_Psi() * RAD_TO_DEG );
795 static inline double fgAPget_altitude( void ) {
796 return( cur_fdm_state->get_Altitude() * FEET_TO_METER );
799 static inline double fgAPget_climb( void ) {
800 // return in meters per minute
801 return( cur_fdm_state->get_Climb_Rate() * FEET_TO_METER * 60 );
804 static inline double get_sideslip( void ) {
805 return( cur_fdm_state->get_Beta() );
808 static inline double fgAPget_agl( void ) {
811 agl = cur_fdm_state->get_Altitude() * FEET_TO_METER
819 void FGAutopilot::AltitudeSet( double new_altitude ) {
820 double target_alt = new_altitude;
822 // cout << "new altitude = " << new_altitude << endl;
824 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET ) {
825 target_alt = new_altitude * FEET_TO_METER;
828 if( target_alt < scenery.cur_elev ) {
829 target_alt = scenery.cur_elev;
832 TargetAltitude = target_alt;
833 altitude_mode = FG_ALTITUDE_LOCK;
835 // cout << "TargetAltitude = " << TargetAltitude << endl;
837 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET ) {
838 target_alt *= METER_TO_FEET;
840 // ApAltitudeDialogInput->setValue((float)target_alt);
841 MakeTargetAltitudeStr( target_alt );
843 update_old_control_values();
847 void FGAutopilot::AltitudeAdjust( double inc )
849 double target_alt, target_agl;
851 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET ) {
852 target_alt = TargetAltitude * METER_TO_FEET;
853 target_agl = TargetAGL * METER_TO_FEET;
855 target_alt = TargetAltitude;
856 target_agl = TargetAGL;
859 // cout << "target_agl = " << target_agl << endl;
860 // cout << "target_agl / inc = " << target_agl / inc << endl;
861 // cout << "(int)(target_agl / inc) = " << (int)(target_agl / inc) << endl;
863 if ( fabs((int)(target_alt / inc) * inc - target_alt) < FG_EPSILON ) {
866 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
869 if ( fabs((int)(target_agl / inc) * inc - target_agl) < FG_EPSILON ) {
872 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
875 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET ) {
876 target_alt *= FEET_TO_METER;
877 target_agl *= FEET_TO_METER;
880 TargetAltitude = target_alt;
881 TargetAGL = target_agl;
883 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET )
884 target_alt *= METER_TO_FEET;
885 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET )
886 target_agl *= METER_TO_FEET;
888 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
889 MakeTargetAltitudeStr( target_alt );
890 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
891 MakeTargetAltitudeStr( target_agl );
894 update_old_control_values();
898 void FGAutopilot::HeadingAdjust( double inc ) {
899 heading_mode = FG_HEADING_LOCK;
901 double target = ( int ) ( TargetHeading / inc ) * inc + inc;
903 TargetHeading = NormalizeDegrees( target );
904 // following cast needed ambiguous plib
905 // ApHeadingDialogInput -> setValue ((float)TargetHeading );
906 MakeTargetHeadingStr( TargetHeading );
907 update_old_control_values();
911 void FGAutopilot::HeadingSet( double new_heading ) {
912 heading_mode = FG_HEADING_LOCK;
914 new_heading = NormalizeDegrees( new_heading );
915 TargetHeading = new_heading;
916 // following cast needed ambiguous plib
917 // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
918 MakeTargetHeadingStr( TargetHeading );
919 update_old_control_values();
922 void FGAutopilot::AutoThrottleAdjust( double inc ) {
923 double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
925 TargetSpeed = target;
929 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
930 auto_throttle = value;
932 if ( auto_throttle == true ) {
933 TargetSpeed = FGBFI::getAirspeed();
934 speed_error_accum = 0.0;
937 update_old_control_values();
938 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAutoThrottle: ("
939 << auto_throttle << ") " << TargetSpeed );