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/globals.hxx>
42 #include <Scenery/scenery.hxx>
44 #include "newauto.hxx"
47 #define DEFAULT_AP_HEADING_LOCK FGAutopilot::FG_DG_HEADING_LOCK
49 FGAutopilot *current_autopilot;
52 // Climb speed constants
53 const double min_climb = 70.0; // kts
54 const double best_climb = 75.0; // kts
55 // const double ideal_climb_rate = 500.0 * SG_FEET_TO_METER; // fpm -> mpm
56 // const double ideal_decent_rate = 1000.0 * SG_FEET_TO_METER; // fpm -> mpm
58 /// These statics will eventually go into the class
59 /// they are just here while I am experimenting -- NHV :-)
60 // AutoPilot Gain Adjuster members
61 static double MaxRollAdjust; // MaxRollAdjust = 2 * APData->MaxRoll;
62 static double RollOutAdjust; // RollOutAdjust = 2 * APData->RollOut;
63 static double MaxAileronAdjust; // MaxAileronAdjust = 2 * APData->MaxAileron;
64 static double RollOutSmoothAdjust; // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
66 static char NewTgtAirportId[16];
67 // static char NewTgtAirportLabel[] = "Enter New TgtAirport ID";
69 extern char *coord_format_lat(float);
70 extern char *coord_format_lon(float);
74 FGAutopilot::FGAutopilot():
75 TargetClimbRate(1000 * SG_FEET_TO_METER)
80 FGAutopilot::~FGAutopilot() {}
83 void FGAutopilot::MakeTargetLatLonStr( double lat, double lon ) {
84 sprintf( TargetLatitudeStr , "%s", coord_format_lat(get_TargetLatitude()));
85 sprintf( TargetLongitudeStr, "%s", coord_format_lon(get_TargetLongitude()));
86 sprintf( TargetLatLonStr, "%s %s", TargetLatitudeStr, TargetLongitudeStr );
90 void FGAutopilot::MakeTargetAltitudeStr( double altitude ) {
91 if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
92 sprintf( TargetAltitudeStr, "APAltitude %6.0f+", altitude );
94 sprintf( TargetAltitudeStr, "APAltitude %6.0f", altitude );
99 void FGAutopilot::MakeTargetHeadingStr( double bearing ) {
102 } else if (bearing > 360. ) {
105 sprintf( TargetHeadingStr, "APHeading %6.1f", bearing );
109 static inline double get_speed( void ) {
110 return( cur_fdm_state->get_V_equiv_kts() );
113 static inline double get_ground_speed() {
114 // starts in ft/s so we convert to kts
115 double ft_s = cur_fdm_state->get_V_ground_speed()
116 * fgGetInt("/sim/speed-up"); // FIXME: inefficient
117 double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
123 void FGAutopilot::MakeTargetWPStr( double distance ) {
124 static time_t last_time = 0;
125 time_t current_time = time(NULL);
126 if ( last_time == current_time ) {
130 last_time = current_time;
134 int size = globals->get_route()->size();
136 // start by wiping the strings
143 SGWayPoint wp1 = globals->get_route()->get_waypoint( 0 );
145 double eta = accum * SG_METER_TO_NM / get_ground_speed();
146 if ( eta >= 100.0 ) { eta = 99.999; }
148 if ( eta < (1.0/6.0) ) {
149 // within 10 minutes, bump up to min/secs
153 minor = (int)((eta - (int)eta) * 60.0);
154 sprintf( TargetWP1Str, "%s %.2f NM ETA %d:%02d",
155 wp1.get_id().c_str(),
156 accum*SG_METER_TO_NM, major, minor );
157 // cout << "distance = " << distance*SG_METER_TO_NM
158 // << " gndsp = " << get_ground_speed()
159 // << " time = " << eta
160 // << " major = " << major
161 // << " minor = " << minor
167 SGWayPoint wp2 = globals->get_route()->get_waypoint( 1 );
168 accum += wp2.get_distance();
170 double eta = accum * SG_METER_TO_NM / get_ground_speed();
171 if ( eta >= 100.0 ) { eta = 99.999; }
173 if ( eta < (1.0/6.0) ) {
174 // within 10 minutes, bump up to min/secs
178 minor = (int)((eta - (int)eta) * 60.0);
179 sprintf( TargetWP2Str, "%s %.2f NM ETA %d:%02d",
180 wp2.get_id().c_str(),
181 accum*SG_METER_TO_NM, major, minor );
186 for ( int i = 2; i < size; ++i ) {
187 accum += globals->get_route()->get_waypoint( i ).get_distance();
190 SGWayPoint wpn = globals->get_route()->get_waypoint( size - 1 );
192 double eta = accum * SG_METER_TO_NM / get_ground_speed();
193 if ( eta >= 100.0 ) { eta = 99.999; }
195 if ( eta < (1.0/6.0) ) {
196 // within 10 minutes, bump up to min/secs
200 minor = (int)((eta - (int)eta) * 60.0);
201 sprintf( TargetWP3Str, "%s %.2f NM ETA %d:%02d",
202 wpn.get_id().c_str(),
203 accum*SG_METER_TO_NM, major, minor );
208 void FGAutopilot::update_old_control_values() {
209 old_aileron = controls.get_aileron();
210 old_elevator = controls.get_elevator();
211 old_elevator_trim = controls.get_elevator_trim();
212 old_rudder = controls.get_rudder();
216 // Initialize autopilot subsystem
217 void FGAutopilot::init() {
218 SG_LOG( SG_AUTOPILOT, SG_INFO, "Init AutoPilot Subsystem" );
220 heading_hold = false ; // turn the heading hold off
221 altitude_hold = false ; // turn the altitude hold off
222 auto_throttle = false ; // turn the auto throttle off
223 heading_mode = DEFAULT_AP_HEADING_LOCK;
226 DGTargetHeading = sg_random() * 360.0;
228 // Initialize target location to startup location
229 old_lat = fgGetDouble("/position/latitude");
230 old_lon = fgGetDouble("/position/longitude");
231 // set_WayPoint( old_lon, old_lat, 0.0, "default" );
233 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
235 TargetHeading = 0.0; // default direction, due north
236 TargetAltitude = 3000; // default altitude in meters
237 alt_error_accum = 0.0;
238 climb_error_accum = 0.0;
240 MakeTargetAltitudeStr( TargetAltitude );
241 MakeTargetHeadingStr( TargetHeading );
243 // These eventually need to be read from current_aircaft somehow.
245 // the maximum roll, in Deg
248 // the deg from heading to start rolling out at, in Deg
251 // how far can I move the aleron from center.
254 // Smoothing distance for alerion control
257 // Hardwired for now should be in options
258 // 25% max control variablilty 0.5 / 2.0
259 disengage_threshold = 1.0;
261 #if !defined( USING_SLIDER_CLASS )
262 MaxRollAdjust = 2 * MaxRoll;
263 RollOutAdjust = 2 * RollOut;
264 MaxAileronAdjust = 2 * MaxAileron;
265 RollOutSmoothAdjust = 2 * RollOutSmooth;
266 #endif // !defined( USING_SLIDER_CLASS )
268 update_old_control_values();
270 // Initialize GUI components of autopilot
271 // NewTgtAirportInit();
272 // fgAPAdjustInit() ;
274 // NewAltitudeInit();
278 // Reset the autopilot system
279 void FGAutopilot::reset() {
281 heading_hold = false ; // turn the heading hold off
282 altitude_hold = false ; // turn the altitude hold off
283 auto_throttle = false ; // turn the auto throttle off
284 heading_mode = DEFAULT_AP_HEADING_LOCK;
286 // TargetHeading = 0.0; // default direction, due north
287 MakeTargetHeadingStr( TargetHeading );
289 // TargetAltitude = 3000; // default altitude in meters
290 MakeTargetAltitudeStr( TargetAltitude );
292 alt_error_accum = 0.0;
293 climb_error_accum = 0.0;
295 update_old_control_values();
297 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id").c_str() );
299 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
303 static double NormalizeDegrees( double Input ) {
304 // normalize the input to the range (-180,180]
305 // Input should not be greater than -360 to 360.
306 // Current rules send the output to an undefined state.
310 else if ( Input <= -180 )
311 while ( Input <= -180 )
316 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
317 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
318 //assert(x1 != x2); // Divide by zero error. Cold abort for now
321 // static double y = 0.0;
322 // double dx = x2 -x1;
323 // if( (dx < -SG_EPSILON ) || ( dx > SG_EPSILON ) )
326 double m, b, y; // the constants to find in y=mx+b
329 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
331 b = y1 - m * x1; // calculate the b
333 y = m * x + b; // the final calculation
342 int FGAutopilot::run() {
343 // Remove the following lines when the calling funcitons start
344 // passing in the data pointer
346 // get control settings
348 double lat = fgGetDouble("/position/latitude");
349 double lon = fgGetDouble("/position/longitude");
350 double alt = fgGetDouble("/position/altitude") * SG_FEET_TO_METER;
352 #ifdef FG_FORCE_AUTO_DISENGAGE
353 // see if somebody else has changed them
354 if( fabs(aileron - old_aileron) > disengage_threshold ||
355 fabs(elevator - old_elevator) > disengage_threshold ||
356 fabs(elevator_trim - old_elevator_trim) >
357 disengage_threshold ||
358 fabs(rudder - old_rudder) > disengage_threshold )
360 // if controls changed externally turn autopilot off
361 waypoint_hold = false ; // turn the target hold off
362 heading_hold = false ; // turn the heading hold off
363 altitude_hold = false ; // turn the altitude hold off
364 terrain_follow = false; // turn the terrain_follow hold off
365 // auto_throttle = false; // turn the auto_throttle off
367 // stash this runs control settings
368 old_aileron = aileron;
369 old_elevator = elevator;
370 old_elevator_trim = elevator_trim;
378 if ( heading_hold == true ) {
379 if ( heading_mode == FG_DG_HEADING_LOCK ) {
380 // cout << "DG heading = " << FGSteam::get_DG_deg()
381 // << " DG error = " << FGSteam::get_DG_err() << endl;
383 TargetHeading = DGTargetHeading + FGSteam::get_DG_err();
384 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
385 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
386 MakeTargetHeadingStr( TargetHeading );
387 } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
388 // we don't set a specific target heading in
389 // TC_HEADING_LOCK mode, we instead try to keep the turn
390 // coordinator zero'd
391 } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
392 // leave "true" target heading as is
393 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
394 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
395 MakeTargetHeadingStr( TargetHeading );
396 } else if ( heading_mode == FG_HEADING_NAV1 ) {
397 // track the NAV1 heading needle deflection
399 // determine our current radial position relative to the
400 // navaid in "true" heading.
401 double cur_radial = current_radiostack->get_nav1_heading();
402 if ( current_radiostack->get_nav1_loc() ) {
403 // ILS localizers radials are already "true" in our
406 cur_radial += current_radiostack->get_nav1_magvar();
408 if ( current_radiostack->get_nav1_from_flag() ) {
410 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
413 // determine the target radial in "true" heading
414 double tgt_radial = current_radiostack->get_nav1_radial();
415 if ( current_radiostack->get_nav1_loc() ) {
416 // ILS localizers radials are already "true" in our
419 // VOR radials need to have that vor's offset added in
420 tgt_radial += current_radiostack->get_nav1_magvar();
423 // determine the heading adjustment needed.
425 current_radiostack->get_nav1_heading_needle_deflection()
426 * (current_radiostack->get_nav1_loc_dist() * SG_METER_TO_NM);
427 if ( adjustment < -30.0 ) { adjustment = -30.0; }
428 if ( adjustment > 30.0 ) { adjustment = 30.0; }
430 // determine the target heading to fly to intercept the
432 TargetHeading = tgt_radial + adjustment;
433 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
434 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
436 MakeTargetHeadingStr( TargetHeading );
437 // cout << "target course (true) = " << TargetHeading << endl;
438 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
439 // update target heading to waypoint
441 double wp_course, wp_distance;
443 #ifdef DO_fgAP_CORRECTED_COURSE
444 // compute course made good
445 // this needs lots of special casing before use
446 double course, reverse, distance, corrected_course;
447 // need to test for iter
448 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
456 #endif // DO_fgAP_CORRECTED_COURSE
458 // compute course to way_point
459 // need to test for iter
460 SGWayPoint wp = globals->get_route()->get_first();
461 wp.CourseAndDistance( lon, lat, alt,
462 &wp_course, &wp_distance );
464 #ifdef DO_fgAP_CORRECTED_COURSE
465 corrected_course = course - wp_course;
466 if( fabs(corrected_course) > 0.1 )
467 printf("fgAP: course %f wp_course %f %f %f\n",
468 course, wp_course, fabs(corrected_course),
470 #endif // DO_fgAP_CORRECTED_COURSE
472 if ( wp_distance > 100 ) {
473 // corrected_course = course - wp_course;
474 TargetHeading = NormalizeDegrees(wp_course);
476 cout << "Reached waypoint within " << wp_distance << "meters"
479 // pop off this waypoint from the list
480 if ( globals->get_route()->size() ) {
481 globals->get_route()->delete_first();
484 // see if there are more waypoints on the list
485 if ( globals->get_route()->size() ) {
487 set_HeadingMode( FG_HEADING_WAYPOINT );
490 heading_mode = FG_TRUE_HEADING_LOCK;
491 // use current heading
492 TargetHeading = fgGetDouble("/orientation/heading");
495 MakeTargetHeadingStr( TargetHeading );
496 // Force this just in case
497 TargetDistance = wp_distance;
498 MakeTargetWPStr( wp_distance );
501 if ( heading_mode == FG_TC_HEADING_LOCK ) {
502 // drive the turn coordinator to zero
503 double turn = FGSteam::get_TC_std();
504 // cout << "turn rate = " << turn << endl;
505 double AileronSet = -turn / 2.0;
506 if ( AileronSet < -1.0 ) { AileronSet = -1.0; }
507 if ( AileronSet > 1.0 ) { AileronSet = 1.0; }
508 controls.set_aileron( AileronSet );
509 controls.set_rudder( AileronSet / 4.0 );
511 // steer towards the target heading
519 = NormalizeDegrees( TargetHeading
520 - fgGetDouble("/orientation/heading") );
521 // figure out how far off we are from desired heading
523 // Now it is time to deterime how far we should be rolled.
524 SG_LOG( SG_AUTOPILOT, SG_DEBUG, "RelHeading: " << RelHeading );
527 // Check if we are further from heading than the roll out point
528 if ( fabs( RelHeading ) > RollOut ) {
529 // set Target Roll to Max in desired direction
530 if ( RelHeading < 0 ) {
531 TargetRoll = 0 - MaxRoll;
533 TargetRoll = MaxRoll;
536 // We have to calculate the Target roll
538 // This calculation engine thinks that the Target roll
539 // should be a line from (RollOut,MaxRoll) to (-RollOut,
540 // -MaxRoll) I hope this works well. If I get ambitious
541 // some day this might become a fancier curve or
544 TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
549 // Target Roll has now been Found.
551 // Compare Target roll to Current Roll, Generate Rel Roll
553 SG_LOG( SG_COCKPIT, SG_BULK, "TargetRoll: " << TargetRoll );
555 RelRoll = NormalizeDegrees( TargetRoll
556 - fgGetDouble("/orientation/roll") );
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 ) {
589 ( TargetAltitude - FGSteam::get_ALT_ft() * SG_FEET_TO_METER ) * 8.0;
590 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
591 double x = current_radiostack->get_nav1_gs_dist();
592 double y = (fgGetDouble("/position/altitude")
593 - current_radiostack->get_nav1_elev()) * SG_FEET_TO_METER;
594 double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
595 // cout << "current angle = " << current_angle << endl;
597 double target_angle = current_radiostack->get_nav1_target_gs();
598 // cout << "target angle = " << target_angle << endl;
600 double gs_diff = target_angle - current_angle;
601 // cout << "difference from desired = " << gs_diff << endl;
603 // convert desired vertical path angle into a climb rate
604 double des_angle = current_angle - 10 * gs_diff;
605 // cout << "desired angle = " << des_angle << endl;
607 // convert to meter/min
608 // cout << "raw ground speed = " << cur_fdm_state->get_V_ground_speed() << endl;
609 double horiz_vel = cur_fdm_state->get_V_ground_speed()
610 * SG_FEET_TO_METER * 60.0;
611 // cout << "Horizontal vel = " << horiz_vel << endl;
612 climb_rate = -sin( des_angle * SGD_DEGREES_TO_RADIANS ) * horiz_vel;
613 // cout << "climb_rate = " << climb_rate << endl;
614 /* climb_error_accum += gs_diff * 2.0; */
615 /* climb_rate = gs_diff * 200.0 + climb_error_accum; */
616 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
617 // brain dead ground hugging with no look ahead
619 ( TargetAGL - fgGetDouble("/position/altitude-agl")
620 * SG_FEET_TO_METER ) * 16.0;
621 // cout << "target agl = " << TargetAGL
622 // << " current agl = " << fgAPget_agl()
623 // << " target climb rate = " << climb_rate
626 // just try to zero out rate of climb ...
632 if ( speed < min_climb ) {
634 } else if ( speed < best_climb ) {
635 max_climb = ((best_climb - min_climb) - (best_climb - speed))
636 * fabs(TargetClimbRate)
637 / (best_climb - min_climb);
639 max_climb = ( speed - best_climb ) * 10.0 + fabs(TargetClimbRate);
642 // this first one could be optional if we wanted to allow
643 // better climb performance assuming we have the airspeed to
645 if ( climb_rate > fabs(TargetClimbRate) ) {
646 climb_rate = fabs(TargetClimbRate);
649 if ( climb_rate > max_climb ) {
650 climb_rate = max_climb;
653 if ( climb_rate < -fabs(TargetClimbRate) ) {
654 climb_rate = -fabs(TargetClimbRate);
656 // cout << "Target climb rate = " << TargetClimbRate << endl;
657 // cout << "given our speed, modified desired climb rate = "
658 // << climb_rate * SG_METER_TO_FEET
659 // << " fpm" << endl;
661 error = fgGetDouble("/velocities/vertical-speed")
662 * SG_FEET_TO_METER - climb_rate;
664 // accumulate the error under the curve ... this really should
666 alt_error_accum += error;
668 // calculate integral error, and adjustment amount
669 int_error = alt_error_accum;
670 // printf("error = %.2f int_error = %.2f\n", error, int_error);
671 int_adj = int_error / 20000.0;
673 // caclulate proportional error
675 prop_adj = prop_error / 2000.0;
677 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
678 // if ( total_adj > 0.6 ) {
680 // } else if ( total_adj < -0.2 ) {
683 if ( total_adj > 1.0 ) {
685 } else if ( total_adj < -1.0 ) {
689 controls.set_elevator( total_adj );
693 if ( auto_throttle ) {
695 double prop_error, int_error;
696 double prop_adj, int_adj, total_adj;
698 error = TargetSpeed - get_speed();
700 // accumulate the error under the curve ... this really should
702 speed_error_accum += error;
703 if ( speed_error_accum > 2000.0 ) {
704 speed_error_accum = 2000.0;
706 else if ( speed_error_accum < -2000.0 ) {
707 speed_error_accum = -2000.0;
710 // calculate integral error, and adjustment amount
711 int_error = speed_error_accum;
713 // printf("error = %.2f int_error = %.2f\n", error, int_error);
714 int_adj = int_error / 200.0;
716 // caclulate proportional error
718 prop_adj = 0.5 + prop_error / 50.0;
720 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
721 if ( total_adj > 1.0 ) {
724 else if ( total_adj < 0.0 ) {
728 controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
731 #ifdef THIS_CODE_IS_NOT_USED
732 if (Mode == 2) // Glide slope hold
737 // First, calculate Relative slope and normalize it
738 RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
740 // Now calculate the elevator offset from current angle
741 if ( abs(RelSlope) > SlopeSmooth )
743 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
744 RelElevator = -MaxElevator;
746 RelElevator = MaxElevator;
750 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
753 fgElevMove(RelElevator);
756 #endif // THIS_CODE_IS_NOT_USED
758 // stash this runs control settings
759 // update_old_control_values();
760 old_aileron = controls.get_aileron();
761 old_elevator = controls.get_elevator();
762 old_elevator_trim = controls.get_elevator_trim();
763 old_rudder = controls.get_rudder();
765 // for cross track error
774 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
777 if ( heading_mode == FG_DG_HEADING_LOCK ) {
778 // set heading hold to current heading (as read from DG)
779 // ... no, leave target heading along ... just use the current
781 // DGTargetHeading = FGSteam::get_DG_deg();
782 } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
783 // set autopilot to hold a zero turn (as reported by the TC)
784 } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
785 // set heading hold to current heading
786 TargetHeading = fgGetDouble("/orientation/heading");
787 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
788 if ( globals->get_route()->size() ) {
789 double course, distance;
791 old_lat = fgGetDouble("/position/latitude");
792 old_lon = fgGetDouble("/position/longitude");
794 waypoint = globals->get_route()->get_first();
795 waypoint.CourseAndDistance( fgGetDouble("/position/longitude"),
796 fgGetDouble("/position/latitude"),
797 fgGetDouble("/position/latitude")
799 &course, &distance );
800 TargetHeading = course;
801 TargetDistance = distance;
802 MakeTargetLatLonStr( waypoint.get_target_lat(),
803 waypoint.get_target_lon() );
804 MakeTargetWPStr( distance );
806 if ( waypoint.get_target_alt() > 0.0 ) {
807 TargetAltitude = waypoint.get_target_alt();
808 altitude_mode = FG_ALTITUDE_LOCK;
809 set_AltitudeEnabled( true );
810 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
813 SG_LOG( SG_COCKPIT, SG_INFO, " set_HeadingMode: ( "
814 << get_TargetLatitude() << " "
815 << get_TargetLongitude() << " ) "
818 // no more way points, default to heading lock.
819 heading_mode = FG_TC_HEADING_LOCK;
823 MakeTargetHeadingStr( TargetHeading );
824 update_old_control_values();
828 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
829 altitude_mode = mode;
831 alt_error_accum = 0.0;
833 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
834 if ( TargetAltitude < fgGetDouble("/position/altitude-agl")
835 * SG_FEET_TO_METER ) {
838 if ( fgGetString("/sim/startup/units") == "feet" ) {
839 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
841 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
843 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
844 climb_error_accum = 0.0;
846 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
847 TargetAGL = fgGetDouble("/position/altitude-agl") * SG_FEET_TO_METER;
849 if ( fgGetString("/sim/startup/units") == "feet" ) {
850 MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
852 MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
856 update_old_control_values();
857 SG_LOG( SG_COCKPIT, SG_INFO, " set_AltitudeMode():" );
862 static inline double get_aoa( void ) {
863 return( cur_fdm_state->get_Gamma_vert_rad() * SGD_RADIANS_TO_DEGREES );
866 static inline double fgAPget_latitude( void ) {
867 return( cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
870 static inline double fgAPget_longitude( void ) {
871 return( cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES );
874 static inline double fgAPget_roll( void ) {
875 return( cur_fdm_state->get_Phi() * SGD_RADIANS_TO_DEGREES );
878 static inline double get_pitch( void ) {
879 return( cur_fdm_state->get_Theta() );
882 double fgAPget_heading( void ) {
883 return( cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
886 static inline double fgAPget_altitude( void ) {
887 return( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER );
890 static inline double fgAPget_climb( void ) {
891 // return in meters per minute
892 return( cur_fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60 );
895 static inline double get_sideslip( void ) {
896 return( cur_fdm_state->get_Beta() );
899 static inline double fgAPget_agl( void ) {
902 agl = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER
910 void FGAutopilot::AltitudeSet( double new_altitude ) {
911 double target_alt = new_altitude;
913 // cout << "new altitude = " << new_altitude << endl;
915 if ( fgGetString("/sim/startup/units") == "feet" ) {
916 target_alt = new_altitude * SG_FEET_TO_METER;
919 if( target_alt < scenery.cur_elev ) {
920 target_alt = scenery.cur_elev;
923 TargetAltitude = target_alt;
924 altitude_mode = FG_ALTITUDE_LOCK;
926 // cout << "TargetAltitude = " << TargetAltitude << endl;
928 if ( fgGetString("/sim/startup/units") == "feet" ) {
929 target_alt *= SG_METER_TO_FEET;
931 // ApAltitudeDialogInput->setValue((float)target_alt);
932 MakeTargetAltitudeStr( target_alt );
934 update_old_control_values();
938 void FGAutopilot::AltitudeAdjust( double inc )
940 double target_alt, target_agl;
942 if ( fgGetString("/sim/startup/units") == "feet" ) {
943 target_alt = TargetAltitude * SG_METER_TO_FEET;
944 target_agl = TargetAGL * SG_METER_TO_FEET;
946 target_alt = TargetAltitude;
947 target_agl = TargetAGL;
950 // cout << "target_agl = " << target_agl << endl;
951 // cout << "target_agl / inc = " << target_agl / inc << endl;
952 // cout << "(int)(target_agl / inc) = " << (int)(target_agl / inc) << endl;
954 if ( fabs((int)(target_alt / inc) * inc - target_alt) < SG_EPSILON ) {
957 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
960 if ( fabs((int)(target_agl / inc) * inc - target_agl) < SG_EPSILON ) {
963 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
966 if ( fgGetString("/sim/startup/units") == "feet" ) {
967 target_alt *= SG_FEET_TO_METER;
968 target_agl *= SG_FEET_TO_METER;
971 TargetAltitude = target_alt;
972 TargetAGL = target_agl;
974 if ( fgGetString("/sim/startup/units") == "feet" )
975 target_alt *= SG_METER_TO_FEET;
976 if ( fgGetString("/sim/startup/units") == "feet" )
977 target_agl *= SG_METER_TO_FEET;
979 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
980 MakeTargetAltitudeStr( target_alt );
981 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
982 MakeTargetAltitudeStr( target_agl );
985 update_old_control_values();
989 void FGAutopilot::HeadingAdjust( double inc ) {
990 if ( heading_mode != FG_DG_HEADING_LOCK
991 && heading_mode != FG_TRUE_HEADING_LOCK )
993 heading_mode = FG_DG_HEADING_LOCK;
996 if ( heading_mode == FG_DG_HEADING_LOCK ) {
997 double target = ( int ) ( DGTargetHeading / inc ) * inc + inc;
998 DGTargetHeading = NormalizeDegrees( target );
1000 double target = ( int ) ( TargetHeading / inc ) * inc + inc;
1001 TargetHeading = NormalizeDegrees( target );
1004 update_old_control_values();
1008 void FGAutopilot::HeadingSet( double new_heading ) {
1009 if( heading_mode == FG_TRUE_HEADING_LOCK ) {
1010 new_heading = NormalizeDegrees( new_heading );
1011 TargetHeading = new_heading;
1012 MakeTargetHeadingStr( TargetHeading );
1014 heading_mode = FG_DG_HEADING_LOCK;
1016 new_heading = NormalizeDegrees( new_heading );
1017 DGTargetHeading = new_heading;
1018 // following cast needed ambiguous plib
1019 // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1020 MakeTargetHeadingStr( DGTargetHeading );
1022 update_old_control_values();
1025 void FGAutopilot::AutoThrottleAdjust( double inc ) {
1026 double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
1028 TargetSpeed = target;
1032 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
1033 auto_throttle = value;
1035 if ( auto_throttle == true ) {
1036 TargetSpeed = fgGetDouble("/velocities/airspeed");
1037 speed_error_accum = 0.0;
1040 update_old_control_values();
1041 SG_LOG( SG_COCKPIT, SG_INFO, " fgAPSetAutoThrottle: ("
1042 << auto_throttle << ") " << TargetSpeed );