]> git.mxchange.org Git - flightgear.git/blobdiff - src/Autopilot/newauto.cxx
This is the change where autopilot bindings are moved from fg_props.cxx to
[flightgear.git] / src / Autopilot / newauto.cxx
index 8fd0014efc7c06057cde7b9c3a2a5f77c0837693..eaf69cdf7b6f25a3b461a74d494de83c891b6e70 100644 (file)
@@ -30,6 +30,7 @@
 #include <stdio.h>             // sprintf()
 
 #include <simgear/constants.h>
+#include <simgear/sg_inlines.h>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/math/sg_random.h>
 #include "newauto.hxx"
 
 
-#define DEFAULT_AP_HEADING_LOCK FGAutopilot::FG_DG_HEADING_LOCK
-
 FGAutopilot *current_autopilot;
 
 
-// Climb speed constants
-const double min_climb = 70.0; // kts
-const double best_climb = 75.0;        // kts
-// const double ideal_climb_rate = 500.0 * SG_FEET_TO_METER; // fpm -> mpm
-// const double ideal_decent_rate = 1000.0 * SG_FEET_TO_METER; // fpm -> mpm
-
 /// These statics will eventually go into the class
 /// they are just here while I am experimenting -- NHV :-)
 // AutoPilot Gain Adjuster members
@@ -71,9 +64,7 @@ extern char *coord_format_lon(float);
                        
 
 // constructor
-FGAutopilot::FGAutopilot():
-TargetClimbRate(500 * SG_FEET_TO_METER),
-TargetDecentRate(1000 * SG_FEET_TO_METER)
+FGAutopilot::FGAutopilot()
 {
 }
 
@@ -98,7 +89,7 @@ void FGAutopilot::MakeTargetAltitudeStr( double altitude ) {
 
 
 void FGAutopilot::MakeTargetHeadingStr( double bearing ) {
-    if( bearing < 0. ) {
+    if ( bearing < 0. ) {
        bearing += 360.;
     } else if (bearing > 360. ) {
        bearing -= 360.;
@@ -209,10 +200,10 @@ void FGAutopilot::MakeTargetWPStr( double distance ) {
 
 
 void FGAutopilot::update_old_control_values() {
-    old_aileron = controls.get_aileron();
-    old_elevator = controls.get_elevator();
-    old_elevator_trim = controls.get_elevator_trim();
-    old_rudder = controls.get_rudder();
+    old_aileron = globals->get_controls()->get_aileron();
+    old_elevator = globals->get_controls()->get_elevator();
+    old_elevator_trim = globals->get_controls()->get_elevator_trim();
+    old_rudder = globals->get_controls()->get_rudder();
 }
 
 
@@ -220,21 +211,96 @@ void FGAutopilot::update_old_control_values() {
 void FGAutopilot::init() {
     SG_LOG( SG_AUTOPILOT, SG_INFO, "Init AutoPilot Subsystem" );
 
-    latitude_node = fgGetNode("/position/latitude", true);
-    longitude_node = fgGetNode("/position/longitude", true);
-    altitude_node = fgGetNode("/position/altitude", true);
-    altitude_agl_node = fgGetNode("/position/altitude-agl", true);
-    vertical_speed_node = fgGetNode("/velocities/vertical-speed", true);
-    heading_node = fgGetNode("/orientation/heading", true);
-    roll_node = fgGetNode("/orientation/roll", true);
-
+    // Autopilot control property static get/set bindings
+    fgTie("/autopilot/locks/altitude", getAPAltitudeLock, setAPAltitudeLock);
+    fgSetArchivable("/autopilot/locks/altitude");
+    fgTie("/autopilot/settings/altitude-ft", getAPAltitude, setAPAltitude);
+    fgSetArchivable("/autopilot/settings/altitude-ft");
+    fgTie("/autopilot/locks/glide-slope", getAPGSLock, setAPGSLock);
+    fgSetDouble("/autopilot/settings/altitude-ft", 3000.0f);
+    fgSetArchivable("/autopilot/locks/glide-slope");
+    fgTie("/autopilot/locks/terrain", getAPTerrainLock, setAPTerrainLock);
+    fgSetArchivable("/autopilot/locks/terrain");
+    fgTie("/autopilot/settings/climb-rate-fpm", getAPClimb, setAPClimb, false);
+    fgSetArchivable("/autopilot/settings/climb-rate-fpm");
+    fgTie("/autopilot/locks/heading", getAPHeadingLock, setAPHeadingLock);
+    fgSetArchivable("/autopilot/locks/heading");
+    fgTie("/autopilot/settings/heading-bug-deg",
+       getAPHeadingBug, setAPHeadingBug);
+    fgSetArchivable("/autopilot/settings/heading-bug-deg");
+    fgSetDouble("/autopilot/settings/heading-bug-deg", 0.0f);
+    fgTie("/autopilot/locks/wing-leveler", getAPWingLeveler, setAPWingLeveler);
+    fgSetArchivable("/autopilot/locks/wing-leveler");
+    fgTie("/autopilot/locks/nav[0]", getAPNAV1Lock, setAPNAV1Lock);
+    fgSetArchivable("/autopilot/locks/nav[0]");
+    fgTie("/autopilot/locks/auto-throttle",
+       getAPAutoThrottleLock, setAPAutoThrottleLock);
+    fgSetArchivable("/autopilot/locks/auto-throttle");
+    fgTie("/autopilot/control-overrides/rudder",
+       getAPRudderControl, setAPRudderControl);
+    fgSetArchivable("/autopilot/control-overrides/rudder");
+    fgTie("/autopilot/control-overrides/elevator",
+       getAPElevatorControl, setAPElevatorControl);
+    fgSetArchivable("/autopilot/control-overrides/elevator");
+    fgTie("/autopilot/control-overrides/throttle",
+       getAPThrottleControl, setAPThrottleControl);
+    fgSetArchivable("/autopilot/control-overrides/throttle");
+
+
+    // bind data input property nodes...
+    latitude_node = fgGetNode("/position/latitude-deg", true);
+    longitude_node = fgGetNode("/position/longitude-deg", true);
+    altitude_node = fgGetNode("/position/altitude-ft", true);
+    altitude_agl_node = fgGetNode("/position/altitude-agl-ft", true);
+    vertical_speed_node = fgGetNode("/velocities/vertical-speed-fps", true);
+    heading_node = fgGetNode("/orientation/heading-deg", true);
+    roll_node = fgGetNode("/orientation/roll-deg", true);
+    pitch_node = fgGetNode("/orientation/pitch-deg", true);
+
+    // bind config property nodes...
+    TargetClimbRate
+        = fgGetNode("/autopilot/config/target-climb-rate-fpm", true);
+    TargetDescentRate
+        = fgGetNode("/autopilot/config/target-descent-rate-fpm", true);
+    min_climb = fgGetNode("/autopilot/config/min-climb-speed-kt", true);
+    best_climb = fgGetNode("/autopilot/config/best-climb-speed-kt", true);
+    elevator_adj_factor
+        = fgGetNode("/autopilot/config/elevator-adj-factor", true);
+    integral_contrib
+        = fgGetNode("/autopilot/config/integral-contribution", true);
+    zero_pitch_throttle
+        = fgGetNode("/autopilot/config/zero-pitch-throttle", true);
+    zero_pitch_trim_full_throttle
+        = fgGetNode("/autopilot/config/zero-pitch-trim-full-throttle", true);
+    current_throttle = fgGetNode("/controls/throttle");
+
+    // initialize config properties with defaults (in case config isn't there)
+    if ( TargetClimbRate->getFloatValue() < 1 )
+        fgSetFloat( "/autopilot/config/target-climb-rate-fpm", 500);
+    if ( TargetDescentRate->getFloatValue() < 1 )
+        fgSetFloat( "/autopilot/config/target-descent-rate-fpm", 1000 );
+    if ( min_climb->getFloatValue() < 1)
+        fgSetFloat( "/autopilot/config/min-climb-speed-kt", 70 );
+    if (best_climb->getFloatValue() < 1)
+        fgSetFloat( "/autopilot/config/best-climb-speed-kt", 120 );
+    if (elevator_adj_factor->getFloatValue() < 1)
+        fgSetFloat( "/autopilot/config/elevator-adj-factor", 5000 );
+    if ( integral_contrib->getFloatValue() < 0.0000001 )
+        fgSetFloat( "/autopilot/config/integral-contribution", 0.01 );
+    if ( zero_pitch_throttle->getFloatValue() < 0.0000001 )
+        fgSetFloat( "/autopilot/config/zero-pitch-throttle", 0.60 );
+    if ( zero_pitch_trim_full_throttle->getFloatValue() < 0.0000001 )
+        fgSetFloat( "/autopilot/config/zero-pitch-trim-full-throttle", 0.15 );
+
+    /* set defaults */
     heading_hold = false ;      // turn the heading hold off
     altitude_hold = false ;     // turn the altitude hold off
     auto_throttle = false ;    // turn the auto throttle off
     heading_mode = DEFAULT_AP_HEADING_LOCK;
 
-    sg_srandom_time();
-    DGTargetHeading = sg_random() * 360.0;
+    DGTargetHeading = fgGetDouble("/autopilot/settings/heading-bug-deg");
+    TargetHeading = fgGetDouble("/autopilot/settings/heading-bug-deg");
+    TargetAltitude = fgGetDouble("/autopilot/settings/altitude-ft") * SG_FEET_TO_METER;
 
     // Initialize target location to startup location
     old_lat = latitude_node->getDoubleValue();
@@ -243,8 +309,6 @@ void FGAutopilot::init() {
 
     MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
        
-    TargetHeading = 0.0;       // default direction, due north
-    TargetAltitude = 3000;     // default altitude in meters
     alt_error_accum = 0.0;
     climb_error_accum = 0.0;
 
@@ -278,11 +342,6 @@ void FGAutopilot::init() {
 
     update_old_control_values();
        
-    // Initialize GUI components of autopilot
-    // NewTgtAirportInit();
-    // fgAPAdjustInit() ;
-    // NewHeadingInit();
-    // NewAltitudeInit();
 };
 
 
@@ -315,13 +374,10 @@ static double NormalizeDegrees( double Input ) {
     // normalize the input to the range (-180,180]
     // Input should not be greater than -360 to 360.
     // Current rules send the output to an undefined state.
-    if ( Input > 180 )
-       while(Input > 180 )
-           Input -= 360;
-    else if ( Input <= -180 )
-       while ( Input <= -180 )
-           Input += 360;
-    return ( Input );
+    while ( Input > 180.0 ) { Input -= 360.0; }
+    while ( Input <= -180.0 ) { Input += 360.0; }
+
+    return Input;
 };
 
 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
@@ -360,6 +416,9 @@ int FGAutopilot::run() {
     double lon = longitude_node->getDoubleValue();
     double alt = altitude_node->getDoubleValue() * SG_FEET_TO_METER;
 
+    SG_LOG( SG_ALL, SG_DEBUG, "FGAutopilot::run()  lat = " << lat <<
+            "  lon = " << lon << "  alt = " << alt );
+       
 #ifdef FG_FORCE_AUTO_DISENGAGE
     // see if somebody else has changed them
     if( fabs(aileron - old_aileron) > disengage_threshold ||
@@ -435,8 +494,7 @@ int FGAutopilot::run() {
            double adjustment = 
                current_radiostack->get_nav1_heading_needle_deflection()
                * (current_radiostack->get_nav1_loc_dist() * SG_METER_TO_NM);
-           if ( adjustment < -30.0 ) { adjustment = -30.0; }
-           if ( adjustment >  30.0 ) { adjustment =  30.0; }
+           SG_CLAMP_RANGE( adjustment, -30.0, 30.0 );
 
            // determine the target heading to fly to intercept the
            // tgt_radial
@@ -514,10 +572,9 @@ int FGAutopilot::run() {
            double turn = FGSteam::get_TC_std();
            // cout << "turn rate = " << turn << endl;
            double AileronSet = -turn / 2.0;
-           if ( AileronSet < -1.0 ) { AileronSet = -1.0; }
-           if ( AileronSet >  1.0 ) { AileronSet =  1.0; }
-           controls.set_aileron( AileronSet );
-           controls.set_rudder( AileronSet / 4.0 );
+            SG_CLAMP_RANGE( AileronSet, -1.0, 1.0 );
+           globals->get_controls()->set_aileron( AileronSet );
+           globals->get_controls()->set_rudder( AileronSet / 4.0 );
        } else {
            // steer towards the target heading
 
@@ -582,8 +639,8 @@ int FGAutopilot::run() {
                                                MaxAileron );
            }
 
-           controls.set_aileron( AileronSet );
-           controls.set_rudder( AileronSet / 4.0 );
+           globals->get_controls()->set_aileron( AileronSet );
+           globals->get_controls()->set_rudder( AileronSet / 4.0 );
            // controls.set_rudder( 0.0 );
        }
     }
@@ -640,32 +697,39 @@ int FGAutopilot::run() {
 
        speed = get_speed();
 
-       if ( speed < min_climb ) {
+       if ( speed < min_climb->getFloatValue() ) {
            max_climb = 0.0;
-       } else if ( speed < best_climb ) {
-           max_climb = ((best_climb - min_climb) - (best_climb - speed)) 
-               * fabs(TargetClimbRate) 
-               / (best_climb - min_climb);
+       } else if ( speed < best_climb->getFloatValue() ) {
+           max_climb = ((best_climb->getFloatValue()
+                          - min_climb->getFloatValue())
+                         - (best_climb->getFloatValue() - speed)) 
+               * fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER) 
+               / (best_climb->getFloatValue() - min_climb->getFloatValue());
        } else {                        
-           max_climb = ( speed - best_climb ) * 10.0 + fabs(TargetClimbRate);
+           max_climb = ( speed - best_climb->getFloatValue() ) * 10.0
+                + fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER);
        }
 
        // this first one could be optional if we wanted to allow
        // better climb performance assuming we have the airspeed to
        // support it.
-       if ( climb_rate > fabs(TargetClimbRate) ) {
-           climb_rate = fabs(TargetClimbRate);
+       if ( climb_rate >
+             fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER) ) {
+           climb_rate
+                = fabs(TargetClimbRate->getFloatValue() * SG_FEET_TO_METER);
        }
 
        if ( climb_rate > max_climb ) {
            climb_rate = max_climb;
        }
 
-       if ( climb_rate < -fabs(TargetDecentRate) ) {
-           climb_rate = -fabs(TargetDecentRate);
+       if ( climb_rate <
+             -fabs(TargetDescentRate->getFloatValue() * SG_FEET_TO_METER) ) {
+           climb_rate
+                = -fabs(TargetDescentRate->getFloatValue() * SG_FEET_TO_METER);
        }
 
-       // cout << "Target climb rate = " << TargetClimbRate << endl;
+       // cout << "Target climb rate = " << TargetClimbRate->getFloatValue() << endl;
        // cout << "given our speed, modified desired climb rate = "
        //      << climb_rate * SG_METER_TO_FEET 
        //      << " fpm" << endl;
@@ -682,25 +746,35 @@ int FGAutopilot::run() {
        // calculate integral error, and adjustment amount
        int_error = alt_error_accum;
        // printf("error = %.2f  int_error = %.2f\n", error, int_error);
-       int_adj = int_error / 20000.0;
+       int_adj = int_error / elevator_adj_factor->getFloatValue();
 
        // caclulate proportional error
        prop_error = error;
-       prop_adj = prop_error / 2000.0;
-
-       total_adj = 0.9 * prop_adj + 0.1 * int_adj;
-       // if ( total_adj > 0.6 ) {
-       //     total_adj = 0.6;
-       // } else if ( total_adj < -0.2 ) {
-       //     total_adj = -0.2;
-       // }
-       if ( total_adj > 1.0 ) {
-           total_adj = 1.0;
-       } else if ( total_adj < -1.0 ) {
-           total_adj = -1.0;
-       }
-
-       controls.set_elevator( total_adj );
+       prop_adj = prop_error / elevator_adj_factor->getDoubleValue();
+
+        // cout << "Error=" << error << endl;
+        // cout << "integral_error=" << int_error << endl;
+        // cout << "integral_contrib=" << integral_contrib->getFloatValue() << endl;
+        // cout << "Proportional Adj=" << prop_adj << endl;
+        // cout << "Integral Adj" << int_adj << endl;
+       total_adj = ((double) 1.0 - (double) integral_contrib->getFloatValue()) * prop_adj
+            + (double) integral_contrib->getFloatValue() * int_adj;
+
+        // stop on autopilot trim at 30% +/-
+//     if ( total_adj > 0.3 ) {
+//          total_adj = 0.3;
+//      } else if ( total_adj < -0.3 ) {
+//          total_adj = -0.3;
+//      }
+
+        // adjust for throttle pitch gain
+        total_adj += ((current_throttle->getFloatValue() - zero_pitch_throttle->getFloatValue())
+                     / (1 - zero_pitch_throttle->getFloatValue()))
+                     * zero_pitch_trim_full_throttle->getFloatValue();
+
+        // cout << "Total Adj" << total_adj << endl;
+
+       globals->get_controls()->set_elevator_trim( total_adj );
     }
 
     // auto throttle
@@ -739,7 +813,8 @@ int FGAutopilot::run() {
            total_adj = 0.0;
        }
 
-       controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
+       globals->get_controls()->set_throttle( FGControls::ALL_ENGINES,
+                                              total_adj );
     }
 
 #ifdef THIS_CODE_IS_NOT_USED
@@ -771,16 +846,18 @@ int FGAutopilot::run() {
 
     // stash this runs control settings
     // update_old_control_values();
-    old_aileron = controls.get_aileron();
-    old_elevator = controls.get_elevator();
-    old_elevator_trim = controls.get_elevator_trim();
-    old_rudder = controls.get_rudder();
+    old_aileron = globals->get_controls()->get_aileron();
+    old_elevator = globals->get_controls()->get_elevator();
+    old_elevator_trim = globals->get_controls()->get_elevator_trim();
+    old_rudder = globals->get_controls()->get_rudder();
 
     // for cross track error
     old_lat = lat;
     old_lon = lon;
        
-       // Ok, we are done
+    // Ok, we are done
+    SG_LOG( SG_ALL, SG_DEBUG, "FGAutopilot::run( returns )" );
+
     return 0;
 }
 
@@ -844,6 +921,7 @@ void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
 
     alt_error_accum = 0.0;
 
+
     if ( altitude_mode == FG_ALTITUDE_LOCK ) {
        if ( TargetAltitude < altitude_agl_node->getDoubleValue()
              * SG_FEET_TO_METER ) {
@@ -914,7 +992,7 @@ static inline double fgAPget_agl( void ) {
     double agl;
 
     agl = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER
-       - scenery.cur_elev;
+       - scenery.get_cur_elev();
 
     return( agl );
 }
@@ -930,8 +1008,8 @@ void FGAutopilot::AltitudeSet( double new_altitude ) {
        target_alt = new_altitude * SG_FEET_TO_METER;
     }
 
-    if( target_alt < scenery.cur_elev ) {
-       target_alt = scenery.cur_elev;
+    if( target_alt < scenery.get_cur_elev() ) {
+       target_alt = scenery.get_cur_elev();
     }
 
     TargetAltitude = target_alt;
@@ -1047,7 +1125,7 @@ void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
     auto_throttle = value;
 
     if ( auto_throttle == true ) {
-        TargetSpeed = fgGetDouble("/velocities/airspeed");
+        TargetSpeed = fgGetDouble("/velocities/airspeed-kt");
        speed_error_accum = 0.0;
     }