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 <Scenery/scenery.hxx>
43 #include "newauto.hxx"
46 FGAutopilot *current_autopilot;
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 * FEET_TO_METER; // fpm -> mpm
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;
62 static char NewTgtAirportId[16];
63 // static char NewTgtAirportLabel[] = "Enter New TgtAirport ID";
65 extern char *coord_format_lat(float);
66 extern char *coord_format_lon(float);
69 void FGAutopilot::MakeTargetLatLonStr( double lat, double lon ) {
70 sprintf( TargetLatitudeStr , "%s", coord_format_lat(get_TargetLatitude()));
71 sprintf( TargetLongitudeStr, "%s", coord_format_lon(get_TargetLongitude()));
72 sprintf( TargetLatLonStr, "%s %s", TargetLatitudeStr, TargetLongitudeStr );
76 void FGAutopilot::MakeTargetAltitudeStr( double altitude ) {
77 if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
78 sprintf( TargetAltitudeStr, "APAltitude %6.0f+", altitude );
80 sprintf( TargetAltitudeStr, "APAltitude %6.0f", altitude );
85 void FGAutopilot::MakeTargetHeadingStr( double bearing ) {
88 } else if (bearing > 360. ) {
91 sprintf( TargetHeadingStr, "APHeading %6.1f", bearing );
95 static inline double get_speed( void ) {
96 return( cur_fdm_state->get_V_equiv_kts() );
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 * fgGetInt("/sim/speed-up"); // FIXME: inefficient
103 double kts = ft_s * FEET_TO_METER * 3600 * METER_TO_NM;
109 void FGAutopilot::MakeTargetWPStr( double distance ) {
110 static time_t last_time = 0;
111 time_t current_time = time(NULL);
112 if ( last_time == current_time ) {
116 last_time = current_time;
120 int size = globals->get_route()->size();
122 // start by wiping the strings
129 SGWayPoint wp1 = globals->get_route()->get_waypoint( 0 );
131 double eta = accum * METER_TO_NM / get_ground_speed();
132 if ( eta >= 100.0 ) { eta = 99.999; }
134 if ( eta < (1.0/6.0) ) {
135 // within 10 minutes, bump up to min/secs
139 minor = (int)((eta - (int)eta) * 60.0);
140 sprintf( TargetWP1Str, "%s %.2f NM ETA %d:%02d",
141 wp1.get_id().c_str(),
142 accum*METER_TO_NM, major, minor );
143 // cout << "distance = " << distance*METER_TO_NM
144 // << " gndsp = " << get_ground_speed()
145 // << " time = " << eta
146 // << " major = " << major
147 // << " minor = " << minor
153 SGWayPoint wp2 = globals->get_route()->get_waypoint( 1 );
154 accum += wp2.get_distance();
156 double eta = accum * METER_TO_NM / get_ground_speed();
157 if ( eta >= 100.0 ) { eta = 99.999; }
159 if ( eta < (1.0/6.0) ) {
160 // within 10 minutes, bump up to min/secs
164 minor = (int)((eta - (int)eta) * 60.0);
165 sprintf( TargetWP2Str, "%s %.2f NM ETA %d:%02d",
166 wp2.get_id().c_str(),
167 accum*METER_TO_NM, major, minor );
172 for ( int i = 2; i < size; ++i ) {
173 accum += globals->get_route()->get_waypoint( i ).get_distance();
176 SGWayPoint wpn = globals->get_route()->get_waypoint( size - 1 );
178 double eta = accum * METER_TO_NM / get_ground_speed();
179 if ( eta >= 100.0 ) { eta = 99.999; }
181 if ( eta < (1.0/6.0) ) {
182 // within 10 minutes, bump up to min/secs
186 minor = (int)((eta - (int)eta) * 60.0);
187 sprintf( TargetWP3Str, "%s %.2f NM ETA %d:%02d",
188 wpn.get_id().c_str(),
189 accum*METER_TO_NM, major, minor );
194 void FGAutopilot::update_old_control_values() {
195 old_aileron = controls.get_aileron();
196 old_elevator = controls.get_elevator();
197 old_elevator_trim = controls.get_elevator_trim();
198 old_rudder = controls.get_rudder();
202 // Initialize autopilot subsystem
203 void FGAutopilot::init() {
204 FG_LOG( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem" );
206 heading_hold = false ; // turn the heading hold off
207 altitude_hold = false ; // turn the altitude hold off
208 auto_throttle = false ; // turn the auto throttle off
210 // Initialize target location to startup location
211 old_lat = FGBFI::getLatitude();
212 old_lon = FGBFI::getLongitude();
213 // set_WayPoint( old_lon, old_lat, 0.0, "default" );
215 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
217 TargetHeading = 0.0; // default direction, due north
218 TargetAltitude = 3000; // default altitude in meters
219 alt_error_accum = 0.0;
220 climb_error_accum = 0.0;
222 MakeTargetAltitudeStr( 3000.0);
223 MakeTargetHeadingStr( 0.0 );
225 // These eventually need to be read from current_aircaft somehow.
227 // the maximum roll, in Deg
230 // the deg from heading to start rolling out at, in Deg
233 // how far can I move the aleron from center.
236 // Smoothing distance for alerion control
239 // Hardwired for now should be in options
240 // 25% max control variablilty 0.5 / 2.0
241 disengage_threshold = 1.0;
243 #if !defined( USING_SLIDER_CLASS )
244 MaxRollAdjust = 2 * MaxRoll;
245 RollOutAdjust = 2 * RollOut;
246 MaxAileronAdjust = 2 * MaxAileron;
247 RollOutSmoothAdjust = 2 * RollOutSmooth;
248 #endif // !defined( USING_SLIDER_CLASS )
250 update_old_control_values();
252 // Initialize GUI components of autopilot
253 // NewTgtAirportInit();
254 // fgAPAdjustInit() ;
256 // NewAltitudeInit();
260 // Reset the autopilot system
261 void FGAutopilot::reset() {
263 heading_hold = false ; // turn the heading hold off
264 altitude_hold = false ; // turn the altitude hold off
265 auto_throttle = false ; // turn the auto throttle off
267 TargetHeading = 0.0; // default direction, due north
268 MakeTargetHeadingStr( TargetHeading );
270 TargetAltitude = 3000; // default altitude in meters
271 MakeTargetAltitudeStr( TargetAltitude );
273 alt_error_accum = 0.0;
274 climb_error_accum = 0.0;
276 update_old_control_values();
278 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id").c_str() );
280 // TargetLatitude = FGBFI::getLatitude();
281 // TargetLongitude = FGBFI::getLongitude();
282 // set_WayPoint( FGBFI::getLongitude(), FGBFI::getLatitude(), 0.0, "reset" );
284 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
288 static double NormalizeDegrees( double Input ) {
289 // normalize the input to the range (-180,180]
290 // Input should not be greater than -360 to 360.
291 // Current rules send the output to an undefined state.
295 else if ( Input <= -180 )
296 while ( Input <= -180 )
301 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
302 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
303 //assert(x1 != x2); // Divide by zero error. Cold abort for now
306 // static double y = 0.0;
307 // double dx = x2 -x1;
308 // if( (dx < -FG_EPSILON ) || ( dx > FG_EPSILON ) )
311 double m, b, y; // the constants to find in y=mx+b
314 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
316 b = y1 - m * x1; // calculate the b
318 y = m * x + b; // the final calculation
327 int FGAutopilot::run() {
328 // Remove the following lines when the calling funcitons start
329 // passing in the data pointer
331 // get control settings
332 // double aileron = FGBFI::getAileron();
333 // double elevator = FGBFI::getElevator();
334 // double elevator_trim = FGBFI::getElevatorTrim();
335 // double rudder = FGBFI::getRudder();
337 double lat = FGBFI::getLatitude();
338 double lon = FGBFI::getLongitude();
339 double alt = FGBFI::getAltitude() * FEET_TO_METER;
341 #ifdef FG_FORCE_AUTO_DISENGAGE
342 // see if somebody else has changed them
343 if( fabs(aileron - old_aileron) > disengage_threshold ||
344 fabs(elevator - old_elevator) > disengage_threshold ||
345 fabs(elevator_trim - old_elevator_trim) >
346 disengage_threshold ||
347 fabs(rudder - old_rudder) > disengage_threshold )
349 // if controls changed externally turn autopilot off
350 waypoint_hold = false ; // turn the target hold off
351 heading_hold = false ; // turn the heading hold off
352 altitude_hold = false ; // turn the altitude hold off
353 terrain_follow = false; // turn the terrain_follow hold off
354 // auto_throttle = false; // turn the auto_throttle off
356 // stash this runs control settings
357 old_aileron = aileron;
358 old_elevator = elevator;
359 old_elevator_trim = elevator_trim;
367 if ( heading_hold == true ) {
369 if ( heading_mode == FG_HEADING_LOCK ) {
370 // leave target heading alone
371 } else if ( heading_mode == FG_HEADING_NAV1 ) {
374 if ( current_radiostack->get_nav1_loc() ) {
375 // localizers radials are "true"
376 tgt_radial = current_radiostack->get_nav1_radial();
378 tgt_radial = current_radiostack->get_nav1_radial() +
379 current_radiostack->get_nav1_magvar();
381 cur_radial = current_radiostack->get_nav1_heading() +
382 current_radiostack->get_nav1_magvar();
383 // cout << "target rad (true) = " << tgt_radial
384 // << " current rad (true) = " << cur_radial
387 double diff = (tgt_radial - cur_radial);
388 while ( diff < -180.0 ) { diff += 360.0; }
389 while ( diff > 180.0 ) { diff -= 360.0; }
391 diff *= (current_radiostack->get_nav1_loc_dist() * METER_TO_NM);
392 if ( diff < -30.0 ) { diff = -30.0; }
393 if ( diff > 30.0 ) { diff = 30.0; }
395 TargetHeading = cur_radial - diff;
396 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
397 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
398 MakeTargetHeadingStr( TargetHeading );
399 // cout << "target course (true) = " << TargetHeading << endl;
400 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
401 // update target heading to waypoint
403 double wp_course, wp_distance;
405 #ifdef DO_fgAP_CORRECTED_COURSE
406 // compute course made good
407 // this needs lots of special casing before use
408 double course, reverse, distance, corrected_course;
409 // need to test for iter
410 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
418 #endif // DO_fgAP_CORRECTED_COURSE
420 // compute course to way_point
421 // need to test for iter
422 SGWayPoint wp = globals->get_route()->get_first();
423 wp.CourseAndDistance( lon, lat, alt,
424 &wp_course, &wp_distance );
426 #ifdef DO_fgAP_CORRECTED_COURSE
427 corrected_course = course - wp_course;
428 if( fabs(corrected_course) > 0.1 )
429 printf("fgAP: course %f wp_course %f %f %f\n",
430 course, wp_course, fabs(corrected_course),
432 #endif // DO_fgAP_CORRECTED_COURSE
434 if ( wp_distance > 100 ) {
435 // corrected_course = course - wp_course;
436 TargetHeading = NormalizeDegrees(wp_course);
438 cout << "Reached waypoint within " << wp_distance << "meters"
441 // pop off this waypoint from the list
442 if ( globals->get_route()->size() ) {
443 globals->get_route()->delete_first();
446 // see if there are more waypoints on the list
447 if ( globals->get_route()->size() ) {
449 set_HeadingMode( FG_HEADING_WAYPOINT );
452 heading_mode = FG_HEADING_LOCK;
453 // use current heading
454 TargetHeading = FGBFI::getHeading();
457 MakeTargetHeadingStr( TargetHeading );
458 // Force this just in case
459 TargetDistance = wp_distance;
460 MakeTargetWPStr( wp_distance );
468 RelHeading = NormalizeDegrees( TargetHeading - FGBFI::getHeading() );
469 // figure out how far off we are from desired heading
471 // Now it is time to deterime how far we should be rolled.
472 FG_LOG( FG_AUTOPILOT, FG_DEBUG, "RelHeading: " << RelHeading );
475 // Check if we are further from heading than the roll out point
476 if ( fabs( RelHeading ) > RollOut ) {
477 // set Target Roll to Max in desired direction
478 if ( RelHeading < 0 ) {
479 TargetRoll = 0 - MaxRoll;
481 TargetRoll = MaxRoll;
484 // We have to calculate the Target roll
486 // This calculation engine thinks that the Target roll
487 // should be a line from (RollOut,MaxRoll) to (-RollOut,
488 // -MaxRoll) I hope this works well. If I get ambitious
489 // some day this might become a fancier curve or
492 TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
497 // Target Roll has now been Found.
499 // Compare Target roll to Current Roll, Generate Rel Roll
501 FG_LOG( FG_COCKPIT, FG_BULK, "TargetRoll: " << TargetRoll );
503 RelRoll = NormalizeDegrees( TargetRoll - FGBFI::getRoll() );
505 // Check if we are further from heading than the roll out smooth point
506 if ( fabs( RelRoll ) > RollOutSmooth ) {
507 // set Target Roll to Max in desired direction
509 AileronSet = 0 - MaxAileron;
511 AileronSet = MaxAileron;
514 AileronSet = LinearExtrapolate( RelRoll, -RollOutSmooth,
520 controls.set_aileron( AileronSet );
521 controls.set_rudder( AileronSet / 4.0 );
522 // controls.set_rudder( 0.0 );
526 if ( altitude_hold ) {
527 double speed, max_climb, error;
528 double prop_error, int_error;
529 double prop_adj, int_adj, total_adj;
531 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
532 // normal altitude hold
533 // cout << "TargetAltitude = " << TargetAltitude
534 // << "Altitude = " << FGBFI::getAltitude() * FEET_TO_METER
537 ( TargetAltitude - FGBFI::getAltitude() * FEET_TO_METER ) * 8.0;
538 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
539 double x = current_radiostack->get_nav1_gs_dist();
540 double y = (FGBFI::getAltitude()
541 - current_radiostack->get_nav1_elev()) * FEET_TO_METER;
542 double current_angle = atan2( y, x ) * RAD_TO_DEG;
543 // cout << "current angle = " << current_angle << endl;
545 double target_angle = current_radiostack->get_nav1_target_gs();
546 // cout << "target angle = " << target_angle << endl;
548 double gs_diff = target_angle - current_angle;
549 // cout << "difference from desired = " << gs_diff << endl;
551 // convert desired vertical path angle into a climb rate
552 double des_angle = current_angle - 10 * gs_diff;
553 // cout << "desired angle = " << des_angle << endl;
555 // convert to meter/min
556 // cout << "raw ground speed = " << cur_fdm_state->get_V_ground_speed() << endl;
557 double horiz_vel = cur_fdm_state->get_V_ground_speed()
558 * FEET_TO_METER * 60.0;
559 // cout << "Horizontal vel = " << horiz_vel << endl;
560 TargetClimbRate = -sin( des_angle * DEG_TO_RAD ) * horiz_vel;
561 // cout << "TargetClimbRate = " << TargetClimbRate << endl;
562 /* climb_error_accum += gs_diff * 2.0; */
563 /* TargetClimbRate = gs_diff * 200.0 + climb_error_accum; */
564 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
565 // brain dead ground hugging with no look ahead
567 ( TargetAGL - FGBFI::getAGL()*FEET_TO_METER ) * 16.0;
568 // cout << "target agl = " << TargetAGL
569 // << " current agl = " << fgAPget_agl()
570 // << " target climb rate = " << TargetClimbRate
573 // just try to zero out rate of climb ...
574 TargetClimbRate = 0.0;
579 if ( speed < min_climb ) {
581 } else if ( speed < best_climb ) {
582 max_climb = ((best_climb - min_climb) - (best_climb - speed))
584 / (best_climb - min_climb);
586 max_climb = ( speed - best_climb ) * 10.0 + ideal_climb_rate;
589 // this first one could be optional if we wanted to allow
590 // better climb performance assuming we have the airspeed to
592 if ( TargetClimbRate > ideal_climb_rate ) {
593 TargetClimbRate = ideal_climb_rate;
596 if ( TargetClimbRate > max_climb ) {
597 TargetClimbRate = max_climb;
600 if ( TargetClimbRate < -ideal_climb_rate ) {
601 TargetClimbRate = -ideal_climb_rate;
604 error = FGBFI::getVerticalSpeed() * FEET_TO_METER - TargetClimbRate;
605 // cout << "climb rate = " << fgAPget_climb()
606 // << " error = " << error << endl;
608 // accumulate the error under the curve ... this really should
610 alt_error_accum += error;
612 // calculate integral error, and adjustment amount
613 int_error = alt_error_accum;
614 // printf("error = %.2f int_error = %.2f\n", error, int_error);
615 int_adj = int_error / 20000.0;
617 // caclulate proportional error
619 prop_adj = prop_error / 2000.0;
621 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
622 // if ( total_adj > 0.6 ) {
624 // } else if ( total_adj < -0.2 ) {
627 if ( total_adj > 1.0 ) {
629 } else if ( total_adj < -1.0 ) {
633 controls.set_elevator( total_adj );
637 if ( auto_throttle ) {
639 double prop_error, int_error;
640 double prop_adj, int_adj, total_adj;
642 error = TargetSpeed - get_speed();
644 // accumulate the error under the curve ... this really should
646 speed_error_accum += error;
647 if ( speed_error_accum > 2000.0 ) {
648 speed_error_accum = 2000.0;
650 else if ( speed_error_accum < -2000.0 ) {
651 speed_error_accum = -2000.0;
654 // calculate integral error, and adjustment amount
655 int_error = speed_error_accum;
657 // printf("error = %.2f int_error = %.2f\n", error, int_error);
658 int_adj = int_error / 200.0;
660 // caclulate proportional error
662 prop_adj = 0.5 + prop_error / 50.0;
664 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
665 if ( total_adj > 1.0 ) {
668 else if ( total_adj < 0.0 ) {
672 controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
675 #ifdef THIS_CODE_IS_NOT_USED
676 if (Mode == 2) // Glide slope hold
681 // First, calculate Relative slope and normalize it
682 RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
684 // Now calculate the elevator offset from current angle
685 if ( abs(RelSlope) > SlopeSmooth )
687 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
688 RelElevator = -MaxElevator;
690 RelElevator = MaxElevator;
694 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
697 fgElevMove(RelElevator);
700 #endif // THIS_CODE_IS_NOT_USED
702 // stash this runs control settings
703 // update_old_control_values();
704 old_aileron = controls.get_aileron();
705 old_elevator = controls.get_elevator();
706 old_elevator_trim = controls.get_elevator_trim();
707 old_rudder = controls.get_rudder();
709 // for cross track error
718 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
721 if ( heading_mode == FG_HEADING_LOCK ) {
722 // set heading hold to current heading
723 TargetHeading = FGBFI::getHeading();
724 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
725 if ( globals->get_route()->size() ) {
726 double course, distance;
728 old_lat = FGBFI::getLatitude();
729 old_lon = FGBFI::getLongitude();
731 waypoint = globals->get_route()->get_first();
732 waypoint.CourseAndDistance( FGBFI::getLongitude(),
733 FGBFI::getLatitude(),
734 FGBFI::getLatitude() * FEET_TO_METER,
735 &course, &distance );
736 TargetHeading = course;
737 TargetDistance = distance;
738 MakeTargetLatLonStr( waypoint.get_target_lat(),
739 waypoint.get_target_lon() );
740 MakeTargetWPStr( distance );
742 if ( waypoint.get_target_alt() > 0.0 ) {
743 TargetAltitude = waypoint.get_target_alt();
744 altitude_mode = FG_ALTITUDE_LOCK;
745 set_AltitudeEnabled( true );
746 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
749 FG_LOG( FG_COCKPIT, FG_INFO, " set_HeadingMode: ( "
750 << get_TargetLatitude() << " "
751 << get_TargetLongitude() << " ) "
754 // no more way points, default to heading lock.
755 heading_mode = FG_HEADING_LOCK;
756 TargetHeading = FGBFI::getHeading();
760 MakeTargetHeadingStr( TargetHeading );
761 update_old_control_values();
765 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
766 altitude_mode = mode;
768 alt_error_accum = 0.0;
770 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
771 // lock at current altitude
772 TargetAltitude = FGBFI::getAltitude() * FEET_TO_METER;
774 if ( fgGetString("/sim/startup/units") == "feet" ) {
775 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
777 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
779 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
780 climb_error_accum = 0.0;
782 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
783 TargetAGL = FGBFI::getAGL() * FEET_TO_METER;
785 if ( fgGetString("/sim/startup/units") == "feet" ) {
786 MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
788 MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
792 update_old_control_values();
793 FG_LOG( FG_COCKPIT, FG_INFO, " set_AltitudeMode():" );
798 static inline double get_aoa( void ) {
799 return( cur_fdm_state->get_Gamma_vert_rad() * RAD_TO_DEG );
802 static inline double fgAPget_latitude( void ) {
803 return( cur_fdm_state->get_Latitude() * RAD_TO_DEG );
806 static inline double fgAPget_longitude( void ) {
807 return( cur_fdm_state->get_Longitude() * RAD_TO_DEG );
810 static inline double fgAPget_roll( void ) {
811 return( cur_fdm_state->get_Phi() * RAD_TO_DEG );
814 static inline double get_pitch( void ) {
815 return( cur_fdm_state->get_Theta() );
818 double fgAPget_heading( void ) {
819 return( cur_fdm_state->get_Psi() * RAD_TO_DEG );
822 static inline double fgAPget_altitude( void ) {
823 return( cur_fdm_state->get_Altitude() * FEET_TO_METER );
826 static inline double fgAPget_climb( void ) {
827 // return in meters per minute
828 return( cur_fdm_state->get_Climb_Rate() * FEET_TO_METER * 60 );
831 static inline double get_sideslip( void ) {
832 return( cur_fdm_state->get_Beta() );
835 static inline double fgAPget_agl( void ) {
838 agl = cur_fdm_state->get_Altitude() * FEET_TO_METER
846 void FGAutopilot::AltitudeSet( double new_altitude ) {
847 double target_alt = new_altitude;
849 // cout << "new altitude = " << new_altitude << endl;
851 if ( fgGetString("/sim/startup/units") == "feet" ) {
852 target_alt = new_altitude * FEET_TO_METER;
855 if( target_alt < scenery.cur_elev ) {
856 target_alt = scenery.cur_elev;
859 TargetAltitude = target_alt;
860 altitude_mode = FG_ALTITUDE_LOCK;
862 // cout << "TargetAltitude = " << TargetAltitude << endl;
864 if ( fgGetString("/sim/startup/units") == "feet" ) {
865 target_alt *= METER_TO_FEET;
867 // ApAltitudeDialogInput->setValue((float)target_alt);
868 MakeTargetAltitudeStr( target_alt );
870 update_old_control_values();
874 void FGAutopilot::AltitudeAdjust( double inc )
876 double target_alt, target_agl;
878 if ( fgGetString("/sim/startup/units") == "feet" ) {
879 target_alt = TargetAltitude * METER_TO_FEET;
880 target_agl = TargetAGL * METER_TO_FEET;
882 target_alt = TargetAltitude;
883 target_agl = TargetAGL;
886 // cout << "target_agl = " << target_agl << endl;
887 // cout << "target_agl / inc = " << target_agl / inc << endl;
888 // cout << "(int)(target_agl / inc) = " << (int)(target_agl / inc) << endl;
890 if ( fabs((int)(target_alt / inc) * inc - target_alt) < FG_EPSILON ) {
893 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
896 if ( fabs((int)(target_agl / inc) * inc - target_agl) < FG_EPSILON ) {
899 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
902 if ( fgGetString("/sim/startup/units") == "feet" ) {
903 target_alt *= FEET_TO_METER;
904 target_agl *= FEET_TO_METER;
907 TargetAltitude = target_alt;
908 TargetAGL = target_agl;
910 if ( fgGetString("/sim/startup/units") == "feet" )
911 target_alt *= METER_TO_FEET;
912 if ( fgGetString("/sim/startup/units") == "feet" )
913 target_agl *= METER_TO_FEET;
915 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
916 MakeTargetAltitudeStr( target_alt );
917 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
918 MakeTargetAltitudeStr( target_agl );
921 update_old_control_values();
925 void FGAutopilot::HeadingAdjust( double inc ) {
926 heading_mode = FG_HEADING_LOCK;
928 double target = ( int ) ( TargetHeading / inc ) * inc + inc;
930 TargetHeading = NormalizeDegrees( target );
931 // following cast needed ambiguous plib
932 // ApHeadingDialogInput -> setValue ((float)TargetHeading );
933 MakeTargetHeadingStr( TargetHeading );
934 update_old_control_values();
938 void FGAutopilot::HeadingSet( double new_heading ) {
939 heading_mode = FG_HEADING_LOCK;
941 new_heading = NormalizeDegrees( new_heading );
942 TargetHeading = new_heading;
943 // following cast needed ambiguous plib
944 // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
945 MakeTargetHeadingStr( TargetHeading );
946 update_old_control_values();
949 void FGAutopilot::AutoThrottleAdjust( double inc ) {
950 double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
952 TargetSpeed = target;
956 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
957 auto_throttle = value;
959 if ( auto_throttle == true ) {
960 TargetSpeed = FGBFI::getAirspeed();
961 speed_error_accum = 0.0;
964 update_old_control_values();
965 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAutoThrottle: ("
966 << auto_throttle << ") " << TargetSpeed );