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>
35 #include <simgear/math/sg_random.h>
37 #include <Cockpit/steam.hxx>
38 #include <Cockpit/radiostack.hxx>
39 #include <Controls/controls.hxx>
40 #include <FDM/flight.hxx>
41 #include <Main/bfi.hxx>
42 #include <Main/globals.hxx>
43 #include <Scenery/scenery.hxx>
45 #include "newauto.hxx"
48 FGAutopilot *current_autopilot;
51 // Climb speed constants
52 const double min_climb = 70.0; // kts
53 const double best_climb = 75.0; // kts
54 // const double ideal_climb_rate = 500.0 * FEET_TO_METER; // fpm -> mpm
55 // const double ideal_decent_rate = 1000.0 * FEET_TO_METER; // fpm -> mpm
57 /// These statics will eventually go into the class
58 /// they are just here while I am experimenting -- NHV :-)
59 // AutoPilot Gain Adjuster members
60 static double MaxRollAdjust; // MaxRollAdjust = 2 * APData->MaxRoll;
61 static double RollOutAdjust; // RollOutAdjust = 2 * APData->RollOut;
62 static double MaxAileronAdjust; // MaxAileronAdjust = 2 * APData->MaxAileron;
63 static double RollOutSmoothAdjust; // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
65 static char NewTgtAirportId[16];
66 // static char NewTgtAirportLabel[] = "Enter New TgtAirport ID";
68 extern char *coord_format_lat(float);
69 extern char *coord_format_lon(float);
73 FGAutopilot::FGAutopilot():
74 TargetClimbRate(1000 * FEET_TO_METER)
79 FGAutopilot::~FGAutopilot() {}
82 void FGAutopilot::MakeTargetLatLonStr( double lat, double lon ) {
83 sprintf( TargetLatitudeStr , "%s", coord_format_lat(get_TargetLatitude()));
84 sprintf( TargetLongitudeStr, "%s", coord_format_lon(get_TargetLongitude()));
85 sprintf( TargetLatLonStr, "%s %s", TargetLatitudeStr, TargetLongitudeStr );
89 void FGAutopilot::MakeTargetAltitudeStr( double altitude ) {
90 if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
91 sprintf( TargetAltitudeStr, "APAltitude %6.0f+", altitude );
93 sprintf( TargetAltitudeStr, "APAltitude %6.0f", altitude );
98 void FGAutopilot::MakeTargetHeadingStr( double bearing ) {
101 } else if (bearing > 360. ) {
104 sprintf( TargetHeadingStr, "APHeading %6.1f", bearing );
108 static inline double get_speed( void ) {
109 return( cur_fdm_state->get_V_equiv_kts() );
112 static inline double get_ground_speed() {
113 // starts in ft/s so we convert to kts
114 double ft_s = cur_fdm_state->get_V_ground_speed()
115 * fgGetInt("/sim/speed-up"); // FIXME: inefficient
116 double kts = ft_s * FEET_TO_METER * 3600 * METER_TO_NM;
122 void FGAutopilot::MakeTargetWPStr( double distance ) {
123 static time_t last_time = 0;
124 time_t current_time = time(NULL);
125 if ( last_time == current_time ) {
129 last_time = current_time;
133 int size = globals->get_route()->size();
135 // start by wiping the strings
142 SGWayPoint wp1 = globals->get_route()->get_waypoint( 0 );
144 double eta = accum * METER_TO_NM / get_ground_speed();
145 if ( eta >= 100.0 ) { eta = 99.999; }
147 if ( eta < (1.0/6.0) ) {
148 // within 10 minutes, bump up to min/secs
152 minor = (int)((eta - (int)eta) * 60.0);
153 sprintf( TargetWP1Str, "%s %.2f NM ETA %d:%02d",
154 wp1.get_id().c_str(),
155 accum*METER_TO_NM, major, minor );
156 // cout << "distance = " << distance*METER_TO_NM
157 // << " gndsp = " << get_ground_speed()
158 // << " time = " << eta
159 // << " major = " << major
160 // << " minor = " << minor
166 SGWayPoint wp2 = globals->get_route()->get_waypoint( 1 );
167 accum += wp2.get_distance();
169 double eta = accum * METER_TO_NM / get_ground_speed();
170 if ( eta >= 100.0 ) { eta = 99.999; }
172 if ( eta < (1.0/6.0) ) {
173 // within 10 minutes, bump up to min/secs
177 minor = (int)((eta - (int)eta) * 60.0);
178 sprintf( TargetWP2Str, "%s %.2f NM ETA %d:%02d",
179 wp2.get_id().c_str(),
180 accum*METER_TO_NM, major, minor );
185 for ( int i = 2; i < size; ++i ) {
186 accum += globals->get_route()->get_waypoint( i ).get_distance();
189 SGWayPoint wpn = globals->get_route()->get_waypoint( size - 1 );
191 double eta = accum * METER_TO_NM / get_ground_speed();
192 if ( eta >= 100.0 ) { eta = 99.999; }
194 if ( eta < (1.0/6.0) ) {
195 // within 10 minutes, bump up to min/secs
199 minor = (int)((eta - (int)eta) * 60.0);
200 sprintf( TargetWP3Str, "%s %.2f NM ETA %d:%02d",
201 wpn.get_id().c_str(),
202 accum*METER_TO_NM, major, minor );
207 void FGAutopilot::update_old_control_values() {
208 old_aileron = controls.get_aileron();
209 old_elevator = controls.get_elevator();
210 old_elevator_trim = controls.get_elevator_trim();
211 old_rudder = controls.get_rudder();
215 // Initialize autopilot subsystem
216 void FGAutopilot::init() {
217 FG_LOG( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem" );
219 heading_hold = false ; // turn the heading hold off
220 altitude_hold = false ; // turn the altitude hold off
221 auto_throttle = false ; // turn the auto throttle off
224 DGTargetHeading = sg_random() * 360.0;
226 // Initialize target location to startup location
227 old_lat = FGBFI::getLatitude();
228 old_lon = FGBFI::getLongitude();
229 // set_WayPoint( old_lon, old_lat, 0.0, "default" );
231 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
233 TargetHeading = 0.0; // default direction, due north
234 TargetAltitude = 3000; // default altitude in meters
235 alt_error_accum = 0.0;
236 climb_error_accum = 0.0;
238 MakeTargetAltitudeStr( 3000.0);
239 MakeTargetHeadingStr( 0.0 );
241 // These eventually need to be read from current_aircaft somehow.
243 // the maximum roll, in Deg
246 // the deg from heading to start rolling out at, in Deg
249 // how far can I move the aleron from center.
252 // Smoothing distance for alerion control
255 // Hardwired for now should be in options
256 // 25% max control variablilty 0.5 / 2.0
257 disengage_threshold = 1.0;
259 #if !defined( USING_SLIDER_CLASS )
260 MaxRollAdjust = 2 * MaxRoll;
261 RollOutAdjust = 2 * RollOut;
262 MaxAileronAdjust = 2 * MaxAileron;
263 RollOutSmoothAdjust = 2 * RollOutSmooth;
264 #endif // !defined( USING_SLIDER_CLASS )
266 update_old_control_values();
268 // Initialize GUI components of autopilot
269 // NewTgtAirportInit();
270 // fgAPAdjustInit() ;
272 // NewAltitudeInit();
276 // Reset the autopilot system
277 void FGAutopilot::reset() {
279 heading_hold = false ; // turn the heading hold off
280 altitude_hold = false ; // turn the altitude hold off
281 auto_throttle = false ; // turn the auto throttle off
283 TargetHeading = 0.0; // default direction, due north
284 MakeTargetHeadingStr( TargetHeading );
286 TargetAltitude = 3000; // default altitude in meters
287 MakeTargetAltitudeStr( TargetAltitude );
289 alt_error_accum = 0.0;
290 climb_error_accum = 0.0;
292 update_old_control_values();
294 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id").c_str() );
296 // TargetLatitude = FGBFI::getLatitude();
297 // TargetLongitude = FGBFI::getLongitude();
298 // set_WayPoint( FGBFI::getLongitude(), FGBFI::getLatitude(), 0.0, "reset" );
300 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
304 static double NormalizeDegrees( double Input ) {
305 // normalize the input to the range (-180,180]
306 // Input should not be greater than -360 to 360.
307 // Current rules send the output to an undefined state.
311 else if ( Input <= -180 )
312 while ( Input <= -180 )
317 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
318 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
319 //assert(x1 != x2); // Divide by zero error. Cold abort for now
322 // static double y = 0.0;
323 // double dx = x2 -x1;
324 // if( (dx < -FG_EPSILON ) || ( dx > FG_EPSILON ) )
327 double m, b, y; // the constants to find in y=mx+b
330 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
332 b = y1 - m * x1; // calculate the b
334 y = m * x + b; // the final calculation
343 int FGAutopilot::run() {
344 // Remove the following lines when the calling funcitons start
345 // passing in the data pointer
347 // get control settings
348 // double aileron = FGBFI::getAileron();
349 // double elevator = FGBFI::getElevator();
350 // double elevator_trim = FGBFI::getElevatorTrim();
351 // double rudder = FGBFI::getRudder();
353 double lat = FGBFI::getLatitude();
354 double lon = FGBFI::getLongitude();
355 double alt = FGBFI::getAltitude() * FEET_TO_METER;
357 #ifdef FG_FORCE_AUTO_DISENGAGE
358 // see if somebody else has changed them
359 if( fabs(aileron - old_aileron) > disengage_threshold ||
360 fabs(elevator - old_elevator) > disengage_threshold ||
361 fabs(elevator_trim - old_elevator_trim) >
362 disengage_threshold ||
363 fabs(rudder - old_rudder) > disengage_threshold )
365 // if controls changed externally turn autopilot off
366 waypoint_hold = false ; // turn the target hold off
367 heading_hold = false ; // turn the heading hold off
368 altitude_hold = false ; // turn the altitude hold off
369 terrain_follow = false; // turn the terrain_follow hold off
370 // auto_throttle = false; // turn the auto_throttle off
372 // stash this runs control settings
373 old_aileron = aileron;
374 old_elevator = elevator;
375 old_elevator_trim = elevator_trim;
383 if ( heading_hold == true ) {
384 if ( heading_mode == FG_DG_HEADING_LOCK ) {
385 // cout << "DG heading = " << FGSteam::get_DG_deg()
386 // << " DG error = " << FGSteam::get_DG_err() << endl;
388 TargetHeading = DGTargetHeading + FGSteam::get_DG_err();
389 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
390 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
391 MakeTargetHeadingStr( TargetHeading );
392 } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
393 // we don't set a specific target heading in
394 // TC_HEADING_LOCK mode, we instead try to keep the turn
395 // coordinator zero'd
396 } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
397 // leave "true" target heading as is
398 } else if ( heading_mode == FG_HEADING_NAV1 ) {
399 // track the NAV1 heading needle deflection
401 // determine our current radial position relative to the
402 // navaid in "true" heading.
403 double cur_radial = current_radiostack->get_nav1_heading();
404 if ( current_radiostack->get_nav1_loc() ) {
405 // ILS localizers radials are already "true" in our
408 cur_radial += current_radiostack->get_nav1_magvar();
410 if ( current_radiostack->get_nav1_from_flag() ) {
412 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
415 // determine the target radial in "true" heading
416 double tgt_radial = current_radiostack->get_nav1_radial();
417 if ( current_radiostack->get_nav1_loc() ) {
418 // ILS localizers radials are already "true" in our
421 // VOR radials need to have that vor's offset added in
422 tgt_radial += current_radiostack->get_nav1_magvar();
425 // determine the heading adjustment needed.
427 current_radiostack->get_nav1_heading_needle_deflection()
428 * (current_radiostack->get_nav1_loc_dist() * METER_TO_NM);
429 if ( adjustment < -30.0 ) { adjustment = -30.0; }
430 if ( adjustment > 30.0 ) { adjustment = 30.0; }
432 // determine the target heading to fly to intercept the
434 TargetHeading = tgt_radial + adjustment;
435 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
436 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
438 MakeTargetHeadingStr( TargetHeading );
439 // cout << "target course (true) = " << TargetHeading << endl;
440 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
441 // update target heading to waypoint
443 double wp_course, wp_distance;
445 #ifdef DO_fgAP_CORRECTED_COURSE
446 // compute course made good
447 // this needs lots of special casing before use
448 double course, reverse, distance, corrected_course;
449 // need to test for iter
450 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
458 #endif // DO_fgAP_CORRECTED_COURSE
460 // compute course to way_point
461 // need to test for iter
462 SGWayPoint wp = globals->get_route()->get_first();
463 wp.CourseAndDistance( lon, lat, alt,
464 &wp_course, &wp_distance );
466 #ifdef DO_fgAP_CORRECTED_COURSE
467 corrected_course = course - wp_course;
468 if( fabs(corrected_course) > 0.1 )
469 printf("fgAP: course %f wp_course %f %f %f\n",
470 course, wp_course, fabs(corrected_course),
472 #endif // DO_fgAP_CORRECTED_COURSE
474 if ( wp_distance > 100 ) {
475 // corrected_course = course - wp_course;
476 TargetHeading = NormalizeDegrees(wp_course);
478 cout << "Reached waypoint within " << wp_distance << "meters"
481 // pop off this waypoint from the list
482 if ( globals->get_route()->size() ) {
483 globals->get_route()->delete_first();
486 // see if there are more waypoints on the list
487 if ( globals->get_route()->size() ) {
489 set_HeadingMode( FG_HEADING_WAYPOINT );
492 heading_mode = FG_TRUE_HEADING_LOCK;
493 // use current heading
494 TargetHeading = FGBFI::getHeading();
497 MakeTargetHeadingStr( TargetHeading );
498 // Force this just in case
499 TargetDistance = wp_distance;
500 MakeTargetWPStr( wp_distance );
503 if ( heading_mode == FG_TC_HEADING_LOCK ) {
504 // drive the turn coordinator to zero
505 double turn = FGSteam::get_TC_std();
506 // cout << "turn rate = " << turn << endl;
507 double AileronSet = -turn / 2.0;
508 if ( AileronSet < -1.0 ) { AileronSet = -1.0; }
509 if ( AileronSet > 1.0 ) { AileronSet = 1.0; }
510 controls.set_aileron( AileronSet );
511 controls.set_rudder( AileronSet / 4.0 );
513 // steer towards the target heading
521 = NormalizeDegrees( TargetHeading - FGBFI::getHeading() );
522 // figure out how far off we are from desired heading
524 // Now it is time to deterime how far we should be rolled.
525 FG_LOG( FG_AUTOPILOT, FG_DEBUG, "RelHeading: " << RelHeading );
528 // Check if we are further from heading than the roll out point
529 if ( fabs( RelHeading ) > RollOut ) {
530 // set Target Roll to Max in desired direction
531 if ( RelHeading < 0 ) {
532 TargetRoll = 0 - MaxRoll;
534 TargetRoll = MaxRoll;
537 // We have to calculate the Target roll
539 // This calculation engine thinks that the Target roll
540 // should be a line from (RollOut,MaxRoll) to (-RollOut,
541 // -MaxRoll) I hope this works well. If I get ambitious
542 // some day this might become a fancier curve or
545 TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
550 // Target Roll has now been Found.
552 // Compare Target roll to Current Roll, Generate Rel Roll
554 FG_LOG( FG_COCKPIT, FG_BULK, "TargetRoll: " << TargetRoll );
556 RelRoll = NormalizeDegrees( TargetRoll - FGBFI::getRoll() );
558 // Check if we are further from heading than the roll out
560 if ( fabs( RelRoll ) > RollOutSmooth ) {
561 // set Target Roll to Max in desired direction
563 AileronSet = 0 - MaxAileron;
565 AileronSet = MaxAileron;
568 AileronSet = LinearExtrapolate( RelRoll, -RollOutSmooth,
574 controls.set_aileron( AileronSet );
575 controls.set_rudder( AileronSet / 4.0 );
576 // controls.set_rudder( 0.0 );
581 if ( altitude_hold ) {
583 double speed, max_climb, error;
584 double prop_error, int_error;
585 double prop_adj, int_adj, total_adj;
587 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
588 // normal altitude hold
589 // cout << "TargetAltitude = " << TargetAltitude
590 // << "Altitude = " << FGBFI::getAltitude() * FEET_TO_METER
593 ( TargetAltitude - FGSteam::get_ALT_ft() * FEET_TO_METER ) * 8.0;
594 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
595 double x = current_radiostack->get_nav1_gs_dist();
596 double y = (FGBFI::getAltitude()
597 - current_radiostack->get_nav1_elev()) * FEET_TO_METER;
598 double current_angle = atan2( y, x ) * RAD_TO_DEG;
599 // cout << "current angle = " << current_angle << endl;
601 double target_angle = current_radiostack->get_nav1_target_gs();
602 // cout << "target angle = " << target_angle << endl;
604 double gs_diff = target_angle - current_angle;
605 // cout << "difference from desired = " << gs_diff << endl;
607 // convert desired vertical path angle into a climb rate
608 double des_angle = current_angle - 10 * gs_diff;
609 // cout << "desired angle = " << des_angle << endl;
611 // convert to meter/min
612 // cout << "raw ground speed = " << cur_fdm_state->get_V_ground_speed() << endl;
613 double horiz_vel = cur_fdm_state->get_V_ground_speed()
614 * FEET_TO_METER * 60.0;
615 // cout << "Horizontal vel = " << horiz_vel << endl;
616 climb_rate = -sin( des_angle * DEG_TO_RAD ) * horiz_vel;
617 // cout << "climb_rate = " << climb_rate << endl;
618 /* climb_error_accum += gs_diff * 2.0; */
619 /* climb_rate = gs_diff * 200.0 + climb_error_accum; */
620 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
621 // brain dead ground hugging with no look ahead
623 ( TargetAGL - FGBFI::getAGL()*FEET_TO_METER ) * 16.0;
624 // cout << "target agl = " << TargetAGL
625 // << " current agl = " << fgAPget_agl()
626 // << " target climb rate = " << climb_rate
629 // just try to zero out rate of climb ...
635 if ( speed < min_climb ) {
637 } else if ( speed < best_climb ) {
638 max_climb = ((best_climb - min_climb) - (best_climb - speed))
639 * fabs(TargetClimbRate)
640 / (best_climb - min_climb);
642 max_climb = ( speed - best_climb ) * 10.0 + fabs(TargetClimbRate);
645 // this first one could be optional if we wanted to allow
646 // better climb performance assuming we have the airspeed to
648 if ( climb_rate > fabs(TargetClimbRate) ) {
649 climb_rate = fabs(TargetClimbRate);
652 if ( climb_rate > max_climb ) {
653 climb_rate = max_climb;
656 if ( climb_rate < -fabs(TargetClimbRate) ) {
657 climb_rate = -fabs(TargetClimbRate);
659 // cout << "Target climb rate = " << TargetClimbRate << endl;
660 // cout << "given our speed, modified desired climb rate = "
661 // << climb_rate * METER_TO_FEET
662 // << " fpm" << endl;
664 error = FGBFI::getVerticalSpeed() * FEET_TO_METER - climb_rate;
665 // cout << "climb rate = " << FGBFI::getVerticalSpeed()
666 // << " vsi rate = " << FGSteam::get_VSI_fps() << endl;
668 // accumulate the error under the curve ... this really should
670 alt_error_accum += error;
672 // calculate integral error, and adjustment amount
673 int_error = alt_error_accum;
674 // printf("error = %.2f int_error = %.2f\n", error, int_error);
675 int_adj = int_error / 20000.0;
677 // caclulate proportional error
679 prop_adj = prop_error / 2000.0;
681 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
682 // if ( total_adj > 0.6 ) {
684 // } else if ( total_adj < -0.2 ) {
687 if ( total_adj > 1.0 ) {
689 } else if ( total_adj < -1.0 ) {
693 controls.set_elevator( total_adj );
697 if ( auto_throttle ) {
699 double prop_error, int_error;
700 double prop_adj, int_adj, total_adj;
702 error = TargetSpeed - get_speed();
704 // accumulate the error under the curve ... this really should
706 speed_error_accum += error;
707 if ( speed_error_accum > 2000.0 ) {
708 speed_error_accum = 2000.0;
710 else if ( speed_error_accum < -2000.0 ) {
711 speed_error_accum = -2000.0;
714 // calculate integral error, and adjustment amount
715 int_error = speed_error_accum;
717 // printf("error = %.2f int_error = %.2f\n", error, int_error);
718 int_adj = int_error / 200.0;
720 // caclulate proportional error
722 prop_adj = 0.5 + prop_error / 50.0;
724 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
725 if ( total_adj > 1.0 ) {
728 else if ( total_adj < 0.0 ) {
732 controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
735 #ifdef THIS_CODE_IS_NOT_USED
736 if (Mode == 2) // Glide slope hold
741 // First, calculate Relative slope and normalize it
742 RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
744 // Now calculate the elevator offset from current angle
745 if ( abs(RelSlope) > SlopeSmooth )
747 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
748 RelElevator = -MaxElevator;
750 RelElevator = MaxElevator;
754 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
757 fgElevMove(RelElevator);
760 #endif // THIS_CODE_IS_NOT_USED
762 // stash this runs control settings
763 // update_old_control_values();
764 old_aileron = controls.get_aileron();
765 old_elevator = controls.get_elevator();
766 old_elevator_trim = controls.get_elevator_trim();
767 old_rudder = controls.get_rudder();
769 // for cross track error
778 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
781 if ( heading_mode == FG_DG_HEADING_LOCK ) {
782 // set heading hold to current heading (as read from DG)
783 // ... no, leave target heading along ... just use the current
785 // DGTargetHeading = FGSteam::get_DG_deg();
786 } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
787 // set autopilot to hold a zero turn (as reported by the TC)
788 } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
789 // set heading hold to current heading
790 TargetHeading = FGBFI::getHeading();
791 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
792 if ( globals->get_route()->size() ) {
793 double course, distance;
795 old_lat = FGBFI::getLatitude();
796 old_lon = FGBFI::getLongitude();
798 waypoint = globals->get_route()->get_first();
799 waypoint.CourseAndDistance( FGBFI::getLongitude(),
800 FGBFI::getLatitude(),
801 FGBFI::getLatitude() * FEET_TO_METER,
802 &course, &distance );
803 TargetHeading = course;
804 TargetDistance = distance;
805 MakeTargetLatLonStr( waypoint.get_target_lat(),
806 waypoint.get_target_lon() );
807 MakeTargetWPStr( distance );
809 if ( waypoint.get_target_alt() > 0.0 ) {
810 TargetAltitude = waypoint.get_target_alt();
811 altitude_mode = FG_ALTITUDE_LOCK;
812 set_AltitudeEnabled( true );
813 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
816 FG_LOG( FG_COCKPIT, FG_INFO, " set_HeadingMode: ( "
817 << get_TargetLatitude() << " "
818 << get_TargetLongitude() << " ) "
821 // no more way points, default to heading lock.
822 heading_mode = FG_TC_HEADING_LOCK;
823 // TargetHeading = FGBFI::getHeading();
827 MakeTargetHeadingStr( TargetHeading );
828 update_old_control_values();
832 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
833 altitude_mode = mode;
835 alt_error_accum = 0.0;
837 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
838 if ( TargetAltitude < FGBFI::getAGL() * FEET_TO_METER ) {
839 // TargetAltitude = FGBFI::getAltitude() * FEET_TO_METER;
842 if ( fgGetString("/sim/startup/units") == "feet" ) {
843 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
845 MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
847 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
848 climb_error_accum = 0.0;
850 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
851 TargetAGL = FGBFI::getAGL() * FEET_TO_METER;
853 if ( fgGetString("/sim/startup/units") == "feet" ) {
854 MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
856 MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
860 update_old_control_values();
861 FG_LOG( FG_COCKPIT, FG_INFO, " set_AltitudeMode():" );
866 static inline double get_aoa( void ) {
867 return( cur_fdm_state->get_Gamma_vert_rad() * RAD_TO_DEG );
870 static inline double fgAPget_latitude( void ) {
871 return( cur_fdm_state->get_Latitude() * RAD_TO_DEG );
874 static inline double fgAPget_longitude( void ) {
875 return( cur_fdm_state->get_Longitude() * RAD_TO_DEG );
878 static inline double fgAPget_roll( void ) {
879 return( cur_fdm_state->get_Phi() * RAD_TO_DEG );
882 static inline double get_pitch( void ) {
883 return( cur_fdm_state->get_Theta() );
886 double fgAPget_heading( void ) {
887 return( cur_fdm_state->get_Psi() * RAD_TO_DEG );
890 static inline double fgAPget_altitude( void ) {
891 return( cur_fdm_state->get_Altitude() * FEET_TO_METER );
894 static inline double fgAPget_climb( void ) {
895 // return in meters per minute
896 return( cur_fdm_state->get_Climb_Rate() * FEET_TO_METER * 60 );
899 static inline double get_sideslip( void ) {
900 return( cur_fdm_state->get_Beta() );
903 static inline double fgAPget_agl( void ) {
906 agl = cur_fdm_state->get_Altitude() * FEET_TO_METER
914 void FGAutopilot::AltitudeSet( double new_altitude ) {
915 double target_alt = new_altitude;
917 // cout << "new altitude = " << new_altitude << endl;
919 if ( fgGetString("/sim/startup/units") == "feet" ) {
920 target_alt = new_altitude * FEET_TO_METER;
923 if( target_alt < scenery.cur_elev ) {
924 target_alt = scenery.cur_elev;
927 TargetAltitude = target_alt;
928 altitude_mode = FG_ALTITUDE_LOCK;
930 // cout << "TargetAltitude = " << TargetAltitude << endl;
932 if ( fgGetString("/sim/startup/units") == "feet" ) {
933 target_alt *= METER_TO_FEET;
935 // ApAltitudeDialogInput->setValue((float)target_alt);
936 MakeTargetAltitudeStr( target_alt );
938 update_old_control_values();
942 void FGAutopilot::AltitudeAdjust( double inc )
944 double target_alt, target_agl;
946 if ( fgGetString("/sim/startup/units") == "feet" ) {
947 target_alt = TargetAltitude * METER_TO_FEET;
948 target_agl = TargetAGL * METER_TO_FEET;
950 target_alt = TargetAltitude;
951 target_agl = TargetAGL;
954 // cout << "target_agl = " << target_agl << endl;
955 // cout << "target_agl / inc = " << target_agl / inc << endl;
956 // cout << "(int)(target_agl / inc) = " << (int)(target_agl / inc) << endl;
958 if ( fabs((int)(target_alt / inc) * inc - target_alt) < FG_EPSILON ) {
961 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
964 if ( fabs((int)(target_agl / inc) * inc - target_agl) < FG_EPSILON ) {
967 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
970 if ( fgGetString("/sim/startup/units") == "feet" ) {
971 target_alt *= FEET_TO_METER;
972 target_agl *= FEET_TO_METER;
975 TargetAltitude = target_alt;
976 TargetAGL = target_agl;
978 if ( fgGetString("/sim/startup/units") == "feet" )
979 target_alt *= METER_TO_FEET;
980 if ( fgGetString("/sim/startup/units") == "feet" )
981 target_agl *= METER_TO_FEET;
983 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
984 MakeTargetAltitudeStr( target_alt );
985 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
986 MakeTargetAltitudeStr( target_agl );
989 update_old_control_values();
993 void FGAutopilot::HeadingAdjust( double inc ) {
994 if ( heading_mode != FG_DG_HEADING_LOCK
995 && heading_mode != FG_TRUE_HEADING_LOCK )
997 heading_mode = FG_DG_HEADING_LOCK;
1000 if ( heading_mode == FG_DG_HEADING_LOCK ) {
1001 double target = ( int ) ( DGTargetHeading / inc ) * inc + inc;
1002 DGTargetHeading = NormalizeDegrees( target );
1004 double target = ( int ) ( TargetHeading / inc ) * inc + inc;
1005 TargetHeading = NormalizeDegrees( target );
1008 update_old_control_values();
1012 void FGAutopilot::HeadingSet( double new_heading ) {
1013 heading_mode = FG_DG_HEADING_LOCK;
1015 new_heading = NormalizeDegrees( new_heading );
1016 DGTargetHeading = new_heading;
1017 // following cast needed ambiguous plib
1018 // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1019 MakeTargetHeadingStr( DGTargetHeading );
1020 update_old_control_values();
1023 void FGAutopilot::AutoThrottleAdjust( double inc ) {
1024 double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
1026 TargetSpeed = target;
1030 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
1031 auto_throttle = value;
1033 if ( auto_throttle == true ) {
1034 TargetSpeed = FGBFI::getAirspeed();
1035 speed_error_accum = 0.0;
1038 update_old_control_values();
1039 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAutoThrottle: ("
1040 << auto_throttle << ") " << TargetSpeed );