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 * SG_FEET_TO_METER; // fpm -> mpm
55 // const double ideal_decent_rate = 1000.0 * SG_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 * SG_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 * SG_FEET_TO_METER * 3600 * SG_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 * SG_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*SG_METER_TO_NM, major, minor );
156 // cout << "distance = " << distance*SG_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 * SG_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*SG_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 * SG_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*SG_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 SG_LOG( SG_AUTOPILOT, SG_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
222 heading_mode = DEFAULT_AP_HEADING_LOCK;
225 DGTargetHeading = sg_random() * 360.0;
227 // Initialize target location to startup location
228 old_lat = FGBFI::getLatitude();
229 old_lon = FGBFI::getLongitude();
230 // set_WayPoint( old_lon, old_lat, 0.0, "default" );
232 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
234 TargetHeading = 0.0; // default direction, due north
235 TargetAltitude = 3000; // default altitude in meters
236 alt_error_accum = 0.0;
237 climb_error_accum = 0.0;
239 MakeTargetAltitudeStr( TargetAltitude );
240 MakeTargetHeadingStr( TargetHeading );
242 // These eventually need to be read from current_aircaft somehow.
244 // the maximum roll, in Deg
247 // the deg from heading to start rolling out at, in Deg
250 // how far can I move the aleron from center.
253 // Smoothing distance for alerion control
256 // Hardwired for now should be in options
257 // 25% max control variablilty 0.5 / 2.0
258 disengage_threshold = 1.0;
260 #if !defined( USING_SLIDER_CLASS )
261 MaxRollAdjust = 2 * MaxRoll;
262 RollOutAdjust = 2 * RollOut;
263 MaxAileronAdjust = 2 * MaxAileron;
264 RollOutSmoothAdjust = 2 * RollOutSmooth;
265 #endif // !defined( USING_SLIDER_CLASS )
267 update_old_control_values();
269 // Initialize GUI components of autopilot
270 // NewTgtAirportInit();
271 // fgAPAdjustInit() ;
273 // NewAltitudeInit();
277 // Reset the autopilot system
278 void FGAutopilot::reset() {
280 heading_hold = false ; // turn the heading hold off
281 altitude_hold = false ; // turn the altitude hold off
282 auto_throttle = false ; // turn the auto throttle off
283 heading_mode = DEFAULT_AP_HEADING_LOCK;
285 // TargetHeading = 0.0; // default direction, due north
286 MakeTargetHeadingStr( TargetHeading );
288 // TargetAltitude = 3000; // default altitude in meters
289 MakeTargetAltitudeStr( TargetAltitude );
291 alt_error_accum = 0.0;
292 climb_error_accum = 0.0;
294 update_old_control_values();
296 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id").c_str() );
298 // TargetLatitude = FGBFI::getLatitude();
299 // TargetLongitude = FGBFI::getLongitude();
300 // set_WayPoint( FGBFI::getLongitude(), FGBFI::getLatitude(), 0.0, "reset" );
302 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
306 static double NormalizeDegrees( double Input ) {
307 // normalize the input to the range (-180,180]
308 // Input should not be greater than -360 to 360.
309 // Current rules send the output to an undefined state.
313 else if ( Input <= -180 )
314 while ( Input <= -180 )
319 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
320 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
321 //assert(x1 != x2); // Divide by zero error. Cold abort for now
324 // static double y = 0.0;
325 // double dx = x2 -x1;
326 // if( (dx < -SG_EPSILON ) || ( dx > SG_EPSILON ) )
329 double m, b, y; // the constants to find in y=mx+b
332 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
334 b = y1 - m * x1; // calculate the b
336 y = m * x + b; // the final calculation
345 int FGAutopilot::run() {
346 // Remove the following lines when the calling funcitons start
347 // passing in the data pointer
349 // get control settings
350 // double aileron = FGBFI::getAileron();
351 // double elevator = FGBFI::getElevator();
352 // double elevator_trim = FGBFI::getElevatorTrim();
353 // double rudder = FGBFI::getRudder();
355 double lat = FGBFI::getLatitude();
356 double lon = FGBFI::getLongitude();
357 double alt = FGBFI::getAltitude() * SG_FEET_TO_METER;
359 #ifdef FG_FORCE_AUTO_DISENGAGE
360 // see if somebody else has changed them
361 if( fabs(aileron - old_aileron) > disengage_threshold ||
362 fabs(elevator - old_elevator) > disengage_threshold ||
363 fabs(elevator_trim - old_elevator_trim) >
364 disengage_threshold ||
365 fabs(rudder - old_rudder) > disengage_threshold )
367 // if controls changed externally turn autopilot off
368 waypoint_hold = false ; // turn the target hold off
369 heading_hold = false ; // turn the heading hold off
370 altitude_hold = false ; // turn the altitude hold off
371 terrain_follow = false; // turn the terrain_follow hold off
372 // auto_throttle = false; // turn the auto_throttle off
374 // stash this runs control settings
375 old_aileron = aileron;
376 old_elevator = elevator;
377 old_elevator_trim = elevator_trim;
385 if ( heading_hold == true ) {
386 if ( heading_mode == FG_DG_HEADING_LOCK ) {
387 // cout << "DG heading = " << FGSteam::get_DG_deg()
388 // << " DG error = " << FGSteam::get_DG_err() << endl;
390 TargetHeading = DGTargetHeading + FGSteam::get_DG_err();
391 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
392 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
393 MakeTargetHeadingStr( TargetHeading );
394 } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
395 // we don't set a specific target heading in
396 // TC_HEADING_LOCK mode, we instead try to keep the turn
397 // coordinator zero'd
398 } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
399 // leave "true" target heading as is
400 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
401 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
402 MakeTargetHeadingStr( TargetHeading );
403 } else if ( heading_mode == FG_HEADING_NAV1 ) {
404 // track the NAV1 heading needle deflection
406 // determine our current radial position relative to the
407 // navaid in "true" heading.
408 double cur_radial = current_radiostack->get_nav1_heading();
409 if ( current_radiostack->get_nav1_loc() ) {
410 // ILS localizers radials are already "true" in our
413 cur_radial += current_radiostack->get_nav1_magvar();
415 if ( current_radiostack->get_nav1_from_flag() ) {
417 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
420 // determine the target radial in "true" heading
421 double tgt_radial = current_radiostack->get_nav1_radial();
422 if ( current_radiostack->get_nav1_loc() ) {
423 // ILS localizers radials are already "true" in our
426 // VOR radials need to have that vor's offset added in
427 tgt_radial += current_radiostack->get_nav1_magvar();
430 // determine the heading adjustment needed.
432 current_radiostack->get_nav1_heading_needle_deflection()
433 * (current_radiostack->get_nav1_loc_dist() * SG_METER_TO_NM);
434 if ( adjustment < -30.0 ) { adjustment = -30.0; }
435 if ( adjustment > 30.0 ) { adjustment = 30.0; }
437 // determine the target heading to fly to intercept the
439 TargetHeading = tgt_radial + adjustment;
440 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
441 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
443 MakeTargetHeadingStr( TargetHeading );
444 // cout << "target course (true) = " << TargetHeading << endl;
445 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
446 // update target heading to waypoint
448 double wp_course, wp_distance;
450 #ifdef DO_fgAP_CORRECTED_COURSE
451 // compute course made good
452 // this needs lots of special casing before use
453 double course, reverse, distance, corrected_course;
454 // need to test for iter
455 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
463 #endif // DO_fgAP_CORRECTED_COURSE
465 // compute course to way_point
466 // need to test for iter
467 SGWayPoint wp = globals->get_route()->get_first();
468 wp.CourseAndDistance( lon, lat, alt,
469 &wp_course, &wp_distance );
471 #ifdef DO_fgAP_CORRECTED_COURSE
472 corrected_course = course - wp_course;
473 if( fabs(corrected_course) > 0.1 )
474 printf("fgAP: course %f wp_course %f %f %f\n",
475 course, wp_course, fabs(corrected_course),
477 #endif // DO_fgAP_CORRECTED_COURSE
479 if ( wp_distance > 100 ) {
480 // corrected_course = course - wp_course;
481 TargetHeading = NormalizeDegrees(wp_course);
483 cout << "Reached waypoint within " << wp_distance << "meters"
486 // pop off this waypoint from the list
487 if ( globals->get_route()->size() ) {
488 globals->get_route()->delete_first();
491 // see if there are more waypoints on the list
492 if ( globals->get_route()->size() ) {
494 set_HeadingMode( FG_HEADING_WAYPOINT );
497 heading_mode = FG_TRUE_HEADING_LOCK;
498 // use current heading
499 TargetHeading = FGBFI::getHeading();
502 MakeTargetHeadingStr( TargetHeading );
503 // Force this just in case
504 TargetDistance = wp_distance;
505 MakeTargetWPStr( wp_distance );
508 if ( heading_mode == FG_TC_HEADING_LOCK ) {
509 // drive the turn coordinator to zero
510 double turn = FGSteam::get_TC_std();
511 // cout << "turn rate = " << turn << endl;
512 double AileronSet = -turn / 2.0;
513 if ( AileronSet < -1.0 ) { AileronSet = -1.0; }
514 if ( AileronSet > 1.0 ) { AileronSet = 1.0; }
515 controls.set_aileron( AileronSet );
516 controls.set_rudder( AileronSet / 4.0 );
518 // steer towards the target heading
526 = NormalizeDegrees( TargetHeading - FGBFI::getHeading() );
527 // figure out how far off we are from desired heading
529 // Now it is time to deterime how far we should be rolled.
530 SG_LOG( SG_AUTOPILOT, SG_DEBUG, "RelHeading: " << RelHeading );
533 // Check if we are further from heading than the roll out point
534 if ( fabs( RelHeading ) > RollOut ) {
535 // set Target Roll to Max in desired direction
536 if ( RelHeading < 0 ) {
537 TargetRoll = 0 - MaxRoll;
539 TargetRoll = MaxRoll;
542 // We have to calculate the Target roll
544 // This calculation engine thinks that the Target roll
545 // should be a line from (RollOut,MaxRoll) to (-RollOut,
546 // -MaxRoll) I hope this works well. If I get ambitious
547 // some day this might become a fancier curve or
550 TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
555 // Target Roll has now been Found.
557 // Compare Target roll to Current Roll, Generate Rel Roll
559 SG_LOG( SG_COCKPIT, SG_BULK, "TargetRoll: " << TargetRoll );
561 RelRoll = NormalizeDegrees( TargetRoll - FGBFI::getRoll() );
563 // Check if we are further from heading than the roll out
565 if ( fabs( RelRoll ) > RollOutSmooth ) {
566 // set Target Roll to Max in desired direction
568 AileronSet = 0 - MaxAileron;
570 AileronSet = MaxAileron;
573 AileronSet = LinearExtrapolate( RelRoll, -RollOutSmooth,
579 controls.set_aileron( AileronSet );
580 controls.set_rudder( AileronSet / 4.0 );
581 // controls.set_rudder( 0.0 );
586 if ( altitude_hold ) {
588 double speed, max_climb, error;
589 double prop_error, int_error;
590 double prop_adj, int_adj, total_adj;
592 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
593 // normal altitude hold
594 // cout << "TargetAltitude = " << TargetAltitude
595 // << "Altitude = " << FGBFI::getAltitude() * SG_FEET_TO_METER
598 ( TargetAltitude - FGSteam::get_ALT_ft() * SG_FEET_TO_METER ) * 8.0;
599 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
600 double x = current_radiostack->get_nav1_gs_dist();
601 double y = (FGBFI::getAltitude()
602 - current_radiostack->get_nav1_elev()) * SG_FEET_TO_METER;
603 double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
604 // cout << "current angle = " << current_angle << endl;
606 double target_angle = current_radiostack->get_nav1_target_gs();
607 // cout << "target angle = " << target_angle << endl;
609 double gs_diff = target_angle - current_angle;
610 // cout << "difference from desired = " << gs_diff << endl;
612 // convert desired vertical path angle into a climb rate
613 double des_angle = current_angle - 10 * gs_diff;
614 // cout << "desired angle = " << des_angle << endl;
616 // convert to meter/min
617 // cout << "raw ground speed = " << cur_fdm_state->get_V_ground_speed() << endl;
618 double horiz_vel = cur_fdm_state->get_V_ground_speed()
619 * SG_FEET_TO_METER * 60.0;
620 // cout << "Horizontal vel = " << horiz_vel << endl;
621 climb_rate = -sin( des_angle * SGD_DEGREES_TO_RADIANS ) * horiz_vel;
622 // cout << "climb_rate = " << climb_rate << endl;
623 /* climb_error_accum += gs_diff * 2.0; */
624 /* climb_rate = gs_diff * 200.0 + climb_error_accum; */
625 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
626 // brain dead ground hugging with no look ahead
628 ( TargetAGL - FGBFI::getAGL()*SG_FEET_TO_METER ) * 16.0;
629 // cout << "target agl = " << TargetAGL
630 // << " current agl = " << fgAPget_agl()
631 // << " target climb rate = " << climb_rate
634 // just try to zero out rate of climb ...
640 if ( speed < min_climb ) {
642 } else if ( speed < best_climb ) {
643 max_climb = ((best_climb - min_climb) - (best_climb - speed))
644 * fabs(TargetClimbRate)
645 / (best_climb - min_climb);
647 max_climb = ( speed - best_climb ) * 10.0 + fabs(TargetClimbRate);
650 // this first one could be optional if we wanted to allow
651 // better climb performance assuming we have the airspeed to
653 if ( climb_rate > fabs(TargetClimbRate) ) {
654 climb_rate = fabs(TargetClimbRate);
657 if ( climb_rate > max_climb ) {
658 climb_rate = max_climb;
661 if ( climb_rate < -fabs(TargetClimbRate) ) {
662 climb_rate = -fabs(TargetClimbRate);
664 // cout << "Target climb rate = " << TargetClimbRate << endl;
665 // cout << "given our speed, modified desired climb rate = "
666 // << climb_rate * SG_METER_TO_FEET
667 // << " fpm" << endl;
669 error = FGBFI::getVerticalSpeed() * SG_FEET_TO_METER - climb_rate;
670 // cout << "climb rate = " << FGBFI::getVerticalSpeed()
671 // << " vsi rate = " << FGSteam::get_VSI_fps() << endl;
673 // accumulate the error under the curve ... this really should
675 alt_error_accum += error;
677 // calculate integral error, and adjustment amount
678 int_error = alt_error_accum;
679 // printf("error = %.2f int_error = %.2f\n", error, int_error);
680 int_adj = int_error / 20000.0;
682 // caclulate proportional error
684 prop_adj = prop_error / 2000.0;
686 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
687 // if ( total_adj > 0.6 ) {
689 // } else if ( total_adj < -0.2 ) {
692 if ( total_adj > 1.0 ) {
694 } else if ( total_adj < -1.0 ) {
698 controls.set_elevator( total_adj );
702 if ( auto_throttle ) {
704 double prop_error, int_error;
705 double prop_adj, int_adj, total_adj;
707 error = TargetSpeed - get_speed();
709 // accumulate the error under the curve ... this really should
711 speed_error_accum += error;
712 if ( speed_error_accum > 2000.0 ) {
713 speed_error_accum = 2000.0;
715 else if ( speed_error_accum < -2000.0 ) {
716 speed_error_accum = -2000.0;
719 // calculate integral error, and adjustment amount
720 int_error = speed_error_accum;
722 // printf("error = %.2f int_error = %.2f\n", error, int_error);
723 int_adj = int_error / 200.0;
725 // caclulate proportional error
727 prop_adj = 0.5 + prop_error / 50.0;
729 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
730 if ( total_adj > 1.0 ) {
733 else if ( total_adj < 0.0 ) {
737 controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
740 #ifdef THIS_CODE_IS_NOT_USED
741 if (Mode == 2) // Glide slope hold
746 // First, calculate Relative slope and normalize it
747 RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
749 // Now calculate the elevator offset from current angle
750 if ( abs(RelSlope) > SlopeSmooth )
752 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
753 RelElevator = -MaxElevator;
755 RelElevator = MaxElevator;
759 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
762 fgElevMove(RelElevator);
765 #endif // THIS_CODE_IS_NOT_USED
767 // stash this runs control settings
768 // update_old_control_values();
769 old_aileron = controls.get_aileron();
770 old_elevator = controls.get_elevator();
771 old_elevator_trim = controls.get_elevator_trim();
772 old_rudder = controls.get_rudder();
774 // for cross track error
783 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
786 if ( heading_mode == FG_DG_HEADING_LOCK ) {
787 // set heading hold to current heading (as read from DG)
788 // ... no, leave target heading along ... just use the current
790 // DGTargetHeading = FGSteam::get_DG_deg();
791 } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
792 // set autopilot to hold a zero turn (as reported by the TC)
793 } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
794 // set heading hold to current heading
795 TargetHeading = FGBFI::getHeading();
796 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
797 if ( globals->get_route()->size() ) {
798 double course, distance;
800 old_lat = FGBFI::getLatitude();
801 old_lon = FGBFI::getLongitude();
803 waypoint = globals->get_route()->get_first();
804 waypoint.CourseAndDistance( FGBFI::getLongitude(),
805 FGBFI::getLatitude(),
806 FGBFI::getLatitude() * SG_FEET_TO_METER,
807 &course, &distance );
808 TargetHeading = course;
809 TargetDistance = distance;
810 MakeTargetLatLonStr( waypoint.get_target_lat(),
811 waypoint.get_target_lon() );
812 MakeTargetWPStr( distance );
814 if ( waypoint.get_target_alt() > 0.0 ) {
815 TargetAltitude = waypoint.get_target_alt();
816 altitude_mode = FG_ALTITUDE_LOCK;
817 set_AltitudeEnabled( true );
818 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
821 SG_LOG( SG_COCKPIT, SG_INFO, " set_HeadingMode: ( "
822 << get_TargetLatitude() << " "
823 << get_TargetLongitude() << " ) "
826 // no more way points, default to heading lock.
827 heading_mode = FG_TC_HEADING_LOCK;
828 // TargetHeading = FGBFI::getHeading();
832 MakeTargetHeadingStr( TargetHeading );
833 update_old_control_values();
837 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
838 altitude_mode = mode;
840 alt_error_accum = 0.0;
842 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
843 if ( TargetAltitude < FGBFI::getAGL() * SG_FEET_TO_METER ) {
844 // TargetAltitude = FGBFI::getAltitude() * SG_FEET_TO_METER;
847 if ( fgGetString("/sim/startup/units") == "feet" ) {
848 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
850 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
852 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
853 climb_error_accum = 0.0;
855 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
856 TargetAGL = FGBFI::getAGL() * SG_FEET_TO_METER;
858 if ( fgGetString("/sim/startup/units") == "feet" ) {
859 MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
861 MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
865 update_old_control_values();
866 SG_LOG( SG_COCKPIT, SG_INFO, " set_AltitudeMode():" );
871 static inline double get_aoa( void ) {
872 return( cur_fdm_state->get_Gamma_vert_rad() * SGD_RADIANS_TO_DEGREES );
875 static inline double fgAPget_latitude( void ) {
876 return( cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
879 static inline double fgAPget_longitude( void ) {
880 return( cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES );
883 static inline double fgAPget_roll( void ) {
884 return( cur_fdm_state->get_Phi() * SGD_RADIANS_TO_DEGREES );
887 static inline double get_pitch( void ) {
888 return( cur_fdm_state->get_Theta() );
891 double fgAPget_heading( void ) {
892 return( cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
895 static inline double fgAPget_altitude( void ) {
896 return( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER );
899 static inline double fgAPget_climb( void ) {
900 // return in meters per minute
901 return( cur_fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60 );
904 static inline double get_sideslip( void ) {
905 return( cur_fdm_state->get_Beta() );
908 static inline double fgAPget_agl( void ) {
911 agl = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER
919 void FGAutopilot::AltitudeSet( double new_altitude ) {
920 double target_alt = new_altitude;
922 // cout << "new altitude = " << new_altitude << endl;
924 if ( fgGetString("/sim/startup/units") == "feet" ) {
925 target_alt = new_altitude * SG_FEET_TO_METER;
928 if( target_alt < scenery.cur_elev ) {
929 target_alt = scenery.cur_elev;
932 TargetAltitude = target_alt;
933 altitude_mode = FG_ALTITUDE_LOCK;
935 // cout << "TargetAltitude = " << TargetAltitude << endl;
937 if ( fgGetString("/sim/startup/units") == "feet" ) {
938 target_alt *= SG_METER_TO_FEET;
940 // ApAltitudeDialogInput->setValue((float)target_alt);
941 MakeTargetAltitudeStr( target_alt );
943 update_old_control_values();
947 void FGAutopilot::AltitudeAdjust( double inc )
949 double target_alt, target_agl;
951 if ( fgGetString("/sim/startup/units") == "feet" ) {
952 target_alt = TargetAltitude * SG_METER_TO_FEET;
953 target_agl = TargetAGL * SG_METER_TO_FEET;
955 target_alt = TargetAltitude;
956 target_agl = TargetAGL;
959 // cout << "target_agl = " << target_agl << endl;
960 // cout << "target_agl / inc = " << target_agl / inc << endl;
961 // cout << "(int)(target_agl / inc) = " << (int)(target_agl / inc) << endl;
963 if ( fabs((int)(target_alt / inc) * inc - target_alt) < SG_EPSILON ) {
966 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
969 if ( fabs((int)(target_agl / inc) * inc - target_agl) < SG_EPSILON ) {
972 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
975 if ( fgGetString("/sim/startup/units") == "feet" ) {
976 target_alt *= SG_FEET_TO_METER;
977 target_agl *= SG_FEET_TO_METER;
980 TargetAltitude = target_alt;
981 TargetAGL = target_agl;
983 if ( fgGetString("/sim/startup/units") == "feet" )
984 target_alt *= SG_METER_TO_FEET;
985 if ( fgGetString("/sim/startup/units") == "feet" )
986 target_agl *= SG_METER_TO_FEET;
988 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
989 MakeTargetAltitudeStr( target_alt );
990 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
991 MakeTargetAltitudeStr( target_agl );
994 update_old_control_values();
998 void FGAutopilot::HeadingAdjust( double inc ) {
999 if ( heading_mode != FG_DG_HEADING_LOCK
1000 && heading_mode != FG_TRUE_HEADING_LOCK )
1002 heading_mode = FG_DG_HEADING_LOCK;
1005 if ( heading_mode == FG_DG_HEADING_LOCK ) {
1006 double target = ( int ) ( DGTargetHeading / inc ) * inc + inc;
1007 DGTargetHeading = NormalizeDegrees( target );
1009 double target = ( int ) ( TargetHeading / inc ) * inc + inc;
1010 TargetHeading = NormalizeDegrees( target );
1013 update_old_control_values();
1017 void FGAutopilot::HeadingSet( double new_heading ) {
1018 if( heading_mode == FG_TRUE_HEADING_LOCK ) {
1019 new_heading = NormalizeDegrees( new_heading );
1020 TargetHeading = new_heading;
1021 MakeTargetHeadingStr( TargetHeading );
1023 heading_mode = FG_DG_HEADING_LOCK;
1025 new_heading = NormalizeDegrees( new_heading );
1026 DGTargetHeading = new_heading;
1027 // following cast needed ambiguous plib
1028 // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1029 MakeTargetHeadingStr( DGTargetHeading );
1031 update_old_control_values();
1034 void FGAutopilot::AutoThrottleAdjust( double inc ) {
1035 double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
1037 TargetSpeed = target;
1041 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
1042 auto_throttle = value;
1044 if ( auto_throttle == true ) {
1045 TargetSpeed = FGBFI::getAirspeed();
1046 speed_error_accum = 0.0;
1049 update_old_control_values();
1050 SG_LOG( SG_COCKPIT, SG_INFO, " fgAPSetAutoThrottle: ("
1051 << auto_throttle << ") " << TargetSpeed );