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 FGAutopilot *current_autopilot;
50 // Climb speed constants
51 const double min_climb = 70.0; // kts
52 const double best_climb = 75.0; // kts
53 // const double ideal_climb_rate = 500.0 * SG_FEET_TO_METER; // fpm -> mpm
54 // const double ideal_decent_rate = 1000.0 * SG_FEET_TO_METER; // fpm -> mpm
56 /// These statics will eventually go into the class
57 /// they are just here while I am experimenting -- NHV :-)
58 // AutoPilot Gain Adjuster members
59 static double MaxRollAdjust; // MaxRollAdjust = 2 * APData->MaxRoll;
60 static double RollOutAdjust; // RollOutAdjust = 2 * APData->RollOut;
61 static double MaxAileronAdjust; // MaxAileronAdjust = 2 * APData->MaxAileron;
62 static double RollOutSmoothAdjust; // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
64 static char NewTgtAirportId[16];
65 // static char NewTgtAirportLabel[] = "Enter New TgtAirport ID";
67 extern char *coord_format_lat(float);
68 extern char *coord_format_lon(float);
72 FGAutopilot::FGAutopilot():
73 TargetClimbRate(500 * SG_FEET_TO_METER),
74 TargetDecentRate(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 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
116 double ft_s = cur_fdm_state->get_V_ground_speed()
117 * speedup_node->getIntValue();
118 double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
124 void FGAutopilot::MakeTargetWPStr( double distance ) {
125 static time_t last_time = 0;
126 time_t current_time = time(NULL);
127 if ( last_time == current_time ) {
131 last_time = current_time;
135 int size = globals->get_route()->size();
137 // start by wiping the strings
144 SGWayPoint wp1 = globals->get_route()->get_waypoint( 0 );
146 double eta = accum * SG_METER_TO_NM / get_ground_speed();
147 if ( eta >= 100.0 ) { eta = 99.999; }
149 if ( eta < (1.0/6.0) ) {
150 // within 10 minutes, bump up to min/secs
154 minor = (int)((eta - (int)eta) * 60.0);
155 sprintf( TargetWP1Str, "%s %.2f NM ETA %d:%02d",
156 wp1.get_id().c_str(),
157 accum*SG_METER_TO_NM, major, minor );
158 // cout << "distance = " << distance*SG_METER_TO_NM
159 // << " gndsp = " << get_ground_speed()
160 // << " time = " << eta
161 // << " major = " << major
162 // << " minor = " << minor
168 SGWayPoint wp2 = globals->get_route()->get_waypoint( 1 );
169 accum += wp2.get_distance();
171 double eta = accum * SG_METER_TO_NM / get_ground_speed();
172 if ( eta >= 100.0 ) { eta = 99.999; }
174 if ( eta < (1.0/6.0) ) {
175 // within 10 minutes, bump up to min/secs
179 minor = (int)((eta - (int)eta) * 60.0);
180 sprintf( TargetWP2Str, "%s %.2f NM ETA %d:%02d",
181 wp2.get_id().c_str(),
182 accum*SG_METER_TO_NM, major, minor );
187 for ( int i = 2; i < size; ++i ) {
188 accum += globals->get_route()->get_waypoint( i ).get_distance();
191 SGWayPoint wpn = globals->get_route()->get_waypoint( size - 1 );
193 double eta = accum * SG_METER_TO_NM / get_ground_speed();
194 if ( eta >= 100.0 ) { eta = 99.999; }
196 if ( eta < (1.0/6.0) ) {
197 // within 10 minutes, bump up to min/secs
201 minor = (int)((eta - (int)eta) * 60.0);
202 sprintf( TargetWP3Str, "%s %.2f NM ETA %d:%02d",
203 wpn.get_id().c_str(),
204 accum*SG_METER_TO_NM, major, minor );
209 void FGAutopilot::update_old_control_values() {
210 old_aileron = globals->get_controls()->get_aileron();
211 old_elevator = globals->get_controls()->get_elevator();
212 old_elevator_trim = globals->get_controls()->get_elevator_trim();
213 old_rudder = globals->get_controls()->get_rudder();
217 // Initialize autopilot subsystem
218 void FGAutopilot::init() {
219 SG_LOG( SG_AUTOPILOT, SG_INFO, "Init AutoPilot Subsystem" );
221 latitude_node = fgGetNode("/position/latitude-deg", true);
222 longitude_node = fgGetNode("/position/longitude-deg", true);
223 altitude_node = fgGetNode("/position/altitude-ft", true);
224 altitude_agl_node = fgGetNode("/position/altitude-agl-ft", true);
225 vertical_speed_node = fgGetNode("/velocities/vertical-speed-fps", true);
226 heading_node = fgGetNode("/orientation/heading-deg", true);
227 roll_node = fgGetNode("/orientation/roll-deg", true);
229 heading_hold = false ; // turn the heading hold off
230 altitude_hold = false ; // turn the altitude hold off
231 auto_throttle = false ; // turn the auto throttle off
232 heading_mode = DEFAULT_AP_HEADING_LOCK;
235 DGTargetHeading = sg_random() * 360.0;
237 // Initialize target location to startup location
238 old_lat = latitude_node->getDoubleValue();
239 old_lon = longitude_node->getDoubleValue();
240 // set_WayPoint( old_lon, old_lat, 0.0, "default" );
242 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
244 TargetHeading = 0.0; // default direction, due north
245 TargetAltitude = 3000; // default altitude in meters
246 alt_error_accum = 0.0;
247 climb_error_accum = 0.0;
249 MakeTargetAltitudeStr( TargetAltitude );
250 MakeTargetHeadingStr( TargetHeading );
252 // These eventually need to be read from current_aircaft somehow.
254 // the maximum roll, in Deg
257 // the deg from heading to start rolling out at, in Deg
260 // how far can I move the aleron from center.
263 // Smoothing distance for alerion control
266 // Hardwired for now should be in options
267 // 25% max control variablilty 0.5 / 2.0
268 disengage_threshold = 1.0;
270 #if !defined( USING_SLIDER_CLASS )
271 MaxRollAdjust = 2 * MaxRoll;
272 RollOutAdjust = 2 * RollOut;
273 MaxAileronAdjust = 2 * MaxAileron;
274 RollOutSmoothAdjust = 2 * RollOutSmooth;
275 #endif // !defined( USING_SLIDER_CLASS )
277 update_old_control_values();
279 // Initialize GUI components of autopilot
280 // NewTgtAirportInit();
281 // fgAPAdjustInit() ;
283 // NewAltitudeInit();
287 // Reset the autopilot system
288 void FGAutopilot::reset() {
290 heading_hold = false ; // turn the heading hold off
291 altitude_hold = false ; // turn the altitude hold off
292 auto_throttle = false ; // turn the auto throttle off
293 heading_mode = DEFAULT_AP_HEADING_LOCK;
295 // TargetHeading = 0.0; // default direction, due north
296 MakeTargetHeadingStr( TargetHeading );
298 // TargetAltitude = 3000; // default altitude in meters
299 MakeTargetAltitudeStr( TargetAltitude );
301 alt_error_accum = 0.0;
302 climb_error_accum = 0.0;
304 update_old_control_values();
306 sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id").c_str() );
308 MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
312 static double NormalizeDegrees( double Input ) {
313 // normalize the input to the range (-180,180]
314 // Input should not be greater than -360 to 360.
315 // Current rules send the output to an undefined state.
319 else if ( Input <= -180 )
320 while ( Input <= -180 )
325 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
326 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
327 //assert(x1 != x2); // Divide by zero error. Cold abort for now
330 // static double y = 0.0;
331 // double dx = x2 -x1;
332 // if( (dx < -SG_EPSILON ) || ( dx > SG_EPSILON ) )
335 double m, b, y; // the constants to find in y=mx+b
338 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
340 b = y1 - m * x1; // calculate the b
342 y = m * x + b; // the final calculation
351 int FGAutopilot::run() {
352 // Remove the following lines when the calling funcitons start
353 // passing in the data pointer
355 // get control settings
357 double lat = latitude_node->getDoubleValue();
358 double lon = longitude_node->getDoubleValue();
359 double alt = altitude_node->getDoubleValue() * SG_FEET_TO_METER;
361 #ifdef FG_FORCE_AUTO_DISENGAGE
362 // see if somebody else has changed them
363 if( fabs(aileron - old_aileron) > disengage_threshold ||
364 fabs(elevator - old_elevator) > disengage_threshold ||
365 fabs(elevator_trim - old_elevator_trim) >
366 disengage_threshold ||
367 fabs(rudder - old_rudder) > disengage_threshold )
369 // if controls changed externally turn autopilot off
370 waypoint_hold = false ; // turn the target hold off
371 heading_hold = false ; // turn the heading hold off
372 altitude_hold = false ; // turn the altitude hold off
373 terrain_follow = false; // turn the terrain_follow hold off
374 // auto_throttle = false; // turn the auto_throttle off
376 // stash this runs control settings
377 old_aileron = aileron;
378 old_elevator = elevator;
379 old_elevator_trim = elevator_trim;
387 if ( heading_hold == true ) {
388 if ( heading_mode == FG_DG_HEADING_LOCK ) {
389 // cout << "DG heading = " << FGSteam::get_DG_deg()
390 // << " DG error = " << FGSteam::get_DG_err() << endl;
392 TargetHeading = DGTargetHeading + FGSteam::get_DG_err();
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_TC_HEADING_LOCK ) {
397 // we don't set a specific target heading in
398 // TC_HEADING_LOCK mode, we instead try to keep the turn
399 // coordinator zero'd
400 } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
401 // leave "true" target heading as is
402 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
403 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
404 MakeTargetHeadingStr( TargetHeading );
405 } else if ( heading_mode == FG_HEADING_NAV1 ) {
406 // track the NAV1 heading needle deflection
408 // determine our current radial position relative to the
409 // navaid in "true" heading.
410 double cur_radial = current_radiostack->get_nav1_heading();
411 if ( current_radiostack->get_nav1_loc() ) {
412 // ILS localizers radials are already "true" in our
415 cur_radial += current_radiostack->get_nav1_magvar();
417 if ( current_radiostack->get_nav1_from_flag() ) {
419 while ( cur_radial >= 360.0 ) { cur_radial -= 360.0; }
422 // determine the target radial in "true" heading
423 double tgt_radial = current_radiostack->get_nav1_radial();
424 if ( current_radiostack->get_nav1_loc() ) {
425 // ILS localizers radials are already "true" in our
428 // VOR radials need to have that vor's offset added in
429 tgt_radial += current_radiostack->get_nav1_magvar();
432 // determine the heading adjustment needed.
434 current_radiostack->get_nav1_heading_needle_deflection()
435 * (current_radiostack->get_nav1_loc_dist() * SG_METER_TO_NM);
436 if ( adjustment < -30.0 ) { adjustment = -30.0; }
437 if ( adjustment > 30.0 ) { adjustment = 30.0; }
439 // determine the target heading to fly to intercept the
441 TargetHeading = tgt_radial + adjustment;
442 while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
443 while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
445 MakeTargetHeadingStr( TargetHeading );
446 // cout << "target course (true) = " << TargetHeading << endl;
447 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
448 // update target heading to waypoint
450 double wp_course, wp_distance;
452 #ifdef DO_fgAP_CORRECTED_COURSE
453 // compute course made good
454 // this needs lots of special casing before use
455 double course, reverse, distance, corrected_course;
456 // need to test for iter
457 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
465 #endif // DO_fgAP_CORRECTED_COURSE
467 // compute course to way_point
468 // need to test for iter
469 SGWayPoint wp = globals->get_route()->get_first();
470 wp.CourseAndDistance( lon, lat, alt,
471 &wp_course, &wp_distance );
473 #ifdef DO_fgAP_CORRECTED_COURSE
474 corrected_course = course - wp_course;
475 if( fabs(corrected_course) > 0.1 )
476 printf("fgAP: course %f wp_course %f %f %f\n",
477 course, wp_course, fabs(corrected_course),
479 #endif // DO_fgAP_CORRECTED_COURSE
481 if ( wp_distance > 100 ) {
482 // corrected_course = course - wp_course;
483 TargetHeading = NormalizeDegrees(wp_course);
485 cout << "Reached waypoint within " << wp_distance << "meters"
488 // pop off this waypoint from the list
489 if ( globals->get_route()->size() ) {
490 globals->get_route()->delete_first();
493 // see if there are more waypoints on the list
494 if ( globals->get_route()->size() ) {
496 set_HeadingMode( FG_HEADING_WAYPOINT );
499 heading_mode = FG_TRUE_HEADING_LOCK;
500 // use current heading
501 TargetHeading = heading_node->getDoubleValue();
504 MakeTargetHeadingStr( TargetHeading );
505 // Force this just in case
506 TargetDistance = wp_distance;
507 MakeTargetWPStr( wp_distance );
510 if ( heading_mode == FG_TC_HEADING_LOCK ) {
511 // drive the turn coordinator to zero
512 double turn = FGSteam::get_TC_std();
513 // cout << "turn rate = " << turn << endl;
514 double AileronSet = -turn / 2.0;
515 if ( AileronSet < -1.0 ) { AileronSet = -1.0; }
516 if ( AileronSet > 1.0 ) { AileronSet = 1.0; }
517 globals->get_controls()->set_aileron( AileronSet );
518 globals->get_controls()->set_rudder( AileronSet / 4.0 );
520 // steer towards the target heading
528 = NormalizeDegrees( TargetHeading
529 - heading_node->getDoubleValue() );
530 // figure out how far off we are from desired heading
532 // Now it is time to deterime how far we should be rolled.
533 SG_LOG( SG_AUTOPILOT, SG_DEBUG, "RelHeading: " << RelHeading );
536 // Check if we are further from heading than the roll out point
537 if ( fabs( RelHeading ) > RollOut ) {
538 // set Target Roll to Max in desired direction
539 if ( RelHeading < 0 ) {
540 TargetRoll = 0 - MaxRoll;
542 TargetRoll = MaxRoll;
545 // We have to calculate the Target roll
547 // This calculation engine thinks that the Target roll
548 // should be a line from (RollOut,MaxRoll) to (-RollOut,
549 // -MaxRoll) I hope this works well. If I get ambitious
550 // some day this might become a fancier curve or
553 TargetRoll = LinearExtrapolate( RelHeading, -RollOut,
558 // Target Roll has now been Found.
560 // Compare Target roll to Current Roll, Generate Rel Roll
562 SG_LOG( SG_COCKPIT, SG_BULK, "TargetRoll: " << TargetRoll );
564 RelRoll = NormalizeDegrees( TargetRoll
565 - roll_node->getDoubleValue() );
567 // Check if we are further from heading than the roll out
569 if ( fabs( RelRoll ) > RollOutSmooth ) {
570 // set Target Roll to Max in desired direction
572 AileronSet = 0 - MaxAileron;
574 AileronSet = MaxAileron;
577 AileronSet = LinearExtrapolate( RelRoll, -RollOutSmooth,
583 globals->get_controls()->set_aileron( AileronSet );
584 globals->get_controls()->set_rudder( AileronSet / 4.0 );
585 // controls.set_rudder( 0.0 );
590 if ( altitude_hold ) {
592 double speed, max_climb, error;
593 double prop_error, int_error;
594 double prop_adj, int_adj, total_adj;
596 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
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 = (altitude_node->getDoubleValue()
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 - altitude_agl_node->getDoubleValue()
629 * SG_FEET_TO_METER ) * 16.0;
630 // cout << "target agl = " << TargetAGL
631 // << " current agl = " << fgAPget_agl()
632 // << " target climb rate = " << climb_rate
635 // just try to zero out rate of climb ...
641 if ( speed < min_climb ) {
643 } else if ( speed < best_climb ) {
644 max_climb = ((best_climb - min_climb) - (best_climb - speed))
645 * fabs(TargetClimbRate)
646 / (best_climb - min_climb);
648 max_climb = ( speed - best_climb ) * 10.0 + fabs(TargetClimbRate);
651 // this first one could be optional if we wanted to allow
652 // better climb performance assuming we have the airspeed to
654 if ( climb_rate > fabs(TargetClimbRate) ) {
655 climb_rate = fabs(TargetClimbRate);
658 if ( climb_rate > max_climb ) {
659 climb_rate = max_climb;
662 if ( climb_rate < -fabs(TargetDecentRate) ) {
663 climb_rate = -fabs(TargetDecentRate);
666 // cout << "Target climb rate = " << TargetClimbRate << endl;
667 // cout << "given our speed, modified desired climb rate = "
668 // << climb_rate * SG_METER_TO_FEET
669 // << " fpm" << endl;
670 // cout << "Current climb rate = "
671 // << vertical_speed_node->getDoubleValue() * 60 << " fpm" << endl;
673 error = vertical_speed_node->getDoubleValue() * 60
674 - climb_rate * SG_METER_TO_FEET;
676 // accumulate the error under the curve ... this really should
678 alt_error_accum += error;
680 // calculate integral error, and adjustment amount
681 int_error = alt_error_accum;
682 // printf("error = %.2f int_error = %.2f\n", error, int_error);
683 int_adj = int_error / 20000.0;
685 // caclulate proportional error
687 prop_adj = prop_error / 2000.0;
689 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
690 // if ( total_adj > 0.6 ) {
692 // } else if ( total_adj < -0.2 ) {
695 if ( total_adj > 1.0 ) {
697 } else if ( total_adj < -1.0 ) {
701 globals->get_controls()->set_elevator_trim( total_adj );
705 if ( auto_throttle ) {
707 double prop_error, int_error;
708 double prop_adj, int_adj, total_adj;
710 error = TargetSpeed - get_speed();
712 // accumulate the error under the curve ... this really should
714 speed_error_accum += error;
715 if ( speed_error_accum > 2000.0 ) {
716 speed_error_accum = 2000.0;
718 else if ( speed_error_accum < -2000.0 ) {
719 speed_error_accum = -2000.0;
722 // calculate integral error, and adjustment amount
723 int_error = speed_error_accum;
725 // printf("error = %.2f int_error = %.2f\n", error, int_error);
726 int_adj = int_error / 200.0;
728 // caclulate proportional error
730 prop_adj = 0.5 + prop_error / 50.0;
732 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
733 if ( total_adj > 1.0 ) {
736 else if ( total_adj < 0.0 ) {
740 globals->get_controls()->set_throttle( FGControls::ALL_ENGINES,
744 #ifdef THIS_CODE_IS_NOT_USED
745 if (Mode == 2) // Glide slope hold
750 // First, calculate Relative slope and normalize it
751 RelSlope = NormalizeDegrees( TargetSlope - get_pitch());
753 // Now calculate the elevator offset from current angle
754 if ( abs(RelSlope) > SlopeSmooth )
756 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
757 RelElevator = -MaxElevator;
759 RelElevator = MaxElevator;
763 RelElevator = LinearExtrapolate(RelSlope,-SlopeSmooth,-MaxElevator,SlopeSmooth,MaxElevator);
766 fgElevMove(RelElevator);
769 #endif // THIS_CODE_IS_NOT_USED
771 // stash this runs control settings
772 // update_old_control_values();
773 old_aileron = globals->get_controls()->get_aileron();
774 old_elevator = globals->get_controls()->get_elevator();
775 old_elevator_trim = globals->get_controls()->get_elevator_trim();
776 old_rudder = globals->get_controls()->get_rudder();
778 // for cross track error
787 void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
790 if ( heading_mode == FG_DG_HEADING_LOCK ) {
791 // set heading hold to current heading (as read from DG)
792 // ... no, leave target heading along ... just use the current
794 // DGTargetHeading = FGSteam::get_DG_deg();
795 } else if ( heading_mode == FG_TC_HEADING_LOCK ) {
796 // set autopilot to hold a zero turn (as reported by the TC)
797 } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
798 // set heading hold to current heading
799 TargetHeading = heading_node->getDoubleValue();
800 } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
801 if ( globals->get_route()->size() ) {
802 double course, distance;
804 old_lat = latitude_node->getDoubleValue();
805 old_lon = longitude_node->getDoubleValue();
807 waypoint = globals->get_route()->get_first();
808 waypoint.CourseAndDistance( longitude_node->getDoubleValue(),
809 latitude_node->getDoubleValue(),
810 altitude_node->getDoubleValue()
812 &course, &distance );
813 TargetHeading = course;
814 TargetDistance = distance;
815 MakeTargetLatLonStr( waypoint.get_target_lat(),
816 waypoint.get_target_lon() );
817 MakeTargetWPStr( distance );
819 if ( waypoint.get_target_alt() > 0.0 ) {
820 TargetAltitude = waypoint.get_target_alt();
821 altitude_mode = FG_ALTITUDE_LOCK;
822 set_AltitudeEnabled( true );
823 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
826 SG_LOG( SG_COCKPIT, SG_INFO, " set_HeadingMode: ( "
827 << get_TargetLatitude() << " "
828 << get_TargetLongitude() << " ) "
831 // no more way points, default to heading lock.
832 heading_mode = FG_TC_HEADING_LOCK;
836 MakeTargetHeadingStr( TargetHeading );
837 update_old_control_values();
841 void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
842 altitude_mode = mode;
844 alt_error_accum = 0.0;
846 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
847 if ( TargetAltitude < altitude_agl_node->getDoubleValue()
848 * SG_FEET_TO_METER ) {
851 if ( fgGetString("/sim/startup/units") == "feet" ) {
852 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
854 MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
856 } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
857 climb_error_accum = 0.0;
859 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
860 TargetAGL = altitude_agl_node->getDoubleValue() * SG_FEET_TO_METER;
862 if ( fgGetString("/sim/startup/units") == "feet" ) {
863 MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
865 MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
869 update_old_control_values();
870 SG_LOG( SG_COCKPIT, SG_INFO, " set_AltitudeMode():" );
875 static inline double get_aoa( void ) {
876 return( cur_fdm_state->get_Gamma_vert_rad() * SGD_RADIANS_TO_DEGREES );
879 static inline double fgAPget_latitude( void ) {
880 return( cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
883 static inline double fgAPget_longitude( void ) {
884 return( cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES );
887 static inline double fgAPget_roll( void ) {
888 return( cur_fdm_state->get_Phi() * SGD_RADIANS_TO_DEGREES );
891 static inline double get_pitch( void ) {
892 return( cur_fdm_state->get_Theta() );
895 double fgAPget_heading( void ) {
896 return( cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
899 static inline double fgAPget_altitude( void ) {
900 return( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER );
903 static inline double fgAPget_climb( void ) {
904 // return in meters per minute
905 return( cur_fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60 );
908 static inline double get_sideslip( void ) {
909 return( cur_fdm_state->get_Beta() );
912 static inline double fgAPget_agl( void ) {
915 agl = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER
923 void FGAutopilot::AltitudeSet( double new_altitude ) {
924 double target_alt = new_altitude;
926 // cout << "new altitude = " << new_altitude << endl;
928 if ( fgGetString("/sim/startup/units") == "feet" ) {
929 target_alt = new_altitude * SG_FEET_TO_METER;
932 if( target_alt < scenery.cur_elev ) {
933 target_alt = scenery.cur_elev;
936 TargetAltitude = target_alt;
937 altitude_mode = FG_ALTITUDE_LOCK;
939 // cout << "TargetAltitude = " << TargetAltitude << endl;
941 if ( fgGetString("/sim/startup/units") == "feet" ) {
942 target_alt *= SG_METER_TO_FEET;
944 // ApAltitudeDialogInput->setValue((float)target_alt);
945 MakeTargetAltitudeStr( target_alt );
947 update_old_control_values();
951 void FGAutopilot::AltitudeAdjust( double inc )
953 double target_alt, target_agl;
955 if ( fgGetString("/sim/startup/units") == "feet" ) {
956 target_alt = TargetAltitude * SG_METER_TO_FEET;
957 target_agl = TargetAGL * SG_METER_TO_FEET;
959 target_alt = TargetAltitude;
960 target_agl = TargetAGL;
963 // cout << "target_agl = " << target_agl << endl;
964 // cout << "target_agl / inc = " << target_agl / inc << endl;
965 // cout << "(int)(target_agl / inc) = " << (int)(target_agl / inc) << endl;
967 if ( fabs((int)(target_alt / inc) * inc - target_alt) < SG_EPSILON ) {
970 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
973 if ( fabs((int)(target_agl / inc) * inc - target_agl) < SG_EPSILON ) {
976 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
979 if ( fgGetString("/sim/startup/units") == "feet" ) {
980 target_alt *= SG_FEET_TO_METER;
981 target_agl *= SG_FEET_TO_METER;
984 TargetAltitude = target_alt;
985 TargetAGL = target_agl;
987 if ( fgGetString("/sim/startup/units") == "feet" )
988 target_alt *= SG_METER_TO_FEET;
989 if ( fgGetString("/sim/startup/units") == "feet" )
990 target_agl *= SG_METER_TO_FEET;
992 if ( altitude_mode == FG_ALTITUDE_LOCK ) {
993 MakeTargetAltitudeStr( target_alt );
994 } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
995 MakeTargetAltitudeStr( target_agl );
998 update_old_control_values();
1002 void FGAutopilot::HeadingAdjust( double inc ) {
1003 if ( heading_mode != FG_DG_HEADING_LOCK
1004 && heading_mode != FG_TRUE_HEADING_LOCK )
1006 heading_mode = FG_DG_HEADING_LOCK;
1009 if ( heading_mode == FG_DG_HEADING_LOCK ) {
1010 double target = ( int ) ( DGTargetHeading / inc ) * inc + inc;
1011 DGTargetHeading = NormalizeDegrees( target );
1013 double target = ( int ) ( TargetHeading / inc ) * inc + inc;
1014 TargetHeading = NormalizeDegrees( target );
1017 update_old_control_values();
1021 void FGAutopilot::HeadingSet( double new_heading ) {
1022 if( heading_mode == FG_TRUE_HEADING_LOCK ) {
1023 new_heading = NormalizeDegrees( new_heading );
1024 TargetHeading = new_heading;
1025 MakeTargetHeadingStr( TargetHeading );
1027 heading_mode = FG_DG_HEADING_LOCK;
1029 new_heading = NormalizeDegrees( new_heading );
1030 DGTargetHeading = new_heading;
1031 // following cast needed ambiguous plib
1032 // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1033 MakeTargetHeadingStr( DGTargetHeading );
1035 update_old_control_values();
1038 void FGAutopilot::AutoThrottleAdjust( double inc ) {
1039 double target = ( int ) ( TargetSpeed / inc ) * inc + inc;
1041 TargetSpeed = target;
1045 void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
1046 auto_throttle = value;
1048 if ( auto_throttle == true ) {
1049 TargetSpeed = fgGetDouble("/velocities/airspeed-kt");
1050 speed_error_accum = 0.0;
1053 update_old_control_values();
1054 SG_LOG( SG_COCKPIT, SG_INFO, " fgAPSetAutoThrottle: ("
1055 << auto_throttle << ") " << TargetSpeed );