]> git.mxchange.org Git - flightgear.git/blobdiff - src/Autopilot/newauto.cxx
Autopilot now honors tunable altitude hold parameters which are defined in
[flightgear.git] / src / Autopilot / newauto.cxx
index 8c7eb6f8643297f54ab81229130086ad92c73ebb..c427fb93759f888a40d0045ffd5a893e431276c7 100644 (file)
@@ -38,7 +38,6 @@
 #include <Cockpit/radiostack.hxx>
 #include <Controls/controls.hxx>
 #include <FDM/flight.hxx>
-#include <Main/bfi.hxx>
 #include <Main/globals.hxx>
 #include <Scenery/scenery.hxx>
 
@@ -49,8 +48,8 @@ FGAutopilot *current_autopilot;
 
 
 // Climb speed constants
-const double min_climb = 70.0; // kts
-const double best_climb = 75.0;        // kts
+// 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
 
@@ -70,9 +69,32 @@ extern char *coord_format_lon(float);
                        
 
 // constructor
-FGAutopilot::FGAutopilot():
-TargetClimbRate(1000 * SG_FEET_TO_METER)
+FGAutopilot::FGAutopilot()
 {
+    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);
+    cout << "elevadj = " << elevator_adj_factor->getFloatValue() << endl;
+    // initialize 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 );
 }
 
 // destructor
@@ -111,8 +133,10 @@ static inline double get_speed( void ) {
 
 static inline double get_ground_speed() {
     // starts in ft/s so we convert to kts
+    static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
+
     double ft_s = cur_fdm_state->get_V_ground_speed() 
-      * fgGetInt("/sim/speed-up"); // FIXME: inefficient
+        * speedup_node->getIntValue();
     double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
 
     return kts;
@@ -205,10 +229,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();
 }
 
 
@@ -216,6 +240,14 @@ void FGAutopilot::update_old_control_values() {
 void FGAutopilot::init() {
     SG_LOG( SG_AUTOPILOT, SG_INFO, "Init AutoPilot Subsystem" );
 
+    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);
+
     heading_hold = false ;      // turn the heading hold off
     altitude_hold = false ;     // turn the altitude hold off
     auto_throttle = false ;    // turn the auto throttle off
@@ -225,8 +257,8 @@ void FGAutopilot::init() {
     DGTargetHeading = sg_random() * 360.0;
 
     // Initialize target location to startup location
-    old_lat = FGBFI::getLatitude();
-    old_lon = FGBFI::getLongitude();
+    old_lat = latitude_node->getDoubleValue();
+    old_lon = longitude_node->getDoubleValue();
     // set_WayPoint( old_lon, old_lat, 0.0, "default" );
 
     MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
@@ -295,10 +327,6 @@ void FGAutopilot::reset() {
 
     sprintf( NewTgtAirportId, "%s", fgGetString("/sim/startup/airport-id").c_str() );
        
-    // TargetLatitude = FGBFI::getLatitude();
-    // TargetLongitude = FGBFI::getLongitude();
-    // set_WayPoint( FGBFI::getLongitude(), FGBFI::getLatitude(), 0.0, "reset" );
-
     MakeTargetLatLonStr( get_TargetLatitude(), get_TargetLongitude() );
 }
 
@@ -347,15 +375,14 @@ int FGAutopilot::run() {
     // passing in the data pointer
 
     // get control settings 
-    // double aileron = FGBFI::getAileron();
-    // double elevator = FGBFI::getElevator();
-    // double elevator_trim = FGBFI::getElevatorTrim();
-    // double rudder = FGBFI::getRudder();
        
-    double lat = FGBFI::getLatitude();
-    double lon = FGBFI::getLongitude();
-    double alt = FGBFI::getAltitude() * SG_FEET_TO_METER;
+    double lat = latitude_node->getDoubleValue();
+    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 ||
@@ -496,7 +523,7 @@ int FGAutopilot::run() {
                    // end of the line
                    heading_mode = FG_TRUE_HEADING_LOCK;
                    // use current heading
-                   TargetHeading = FGBFI::getHeading();
+                   TargetHeading = heading_node->getDoubleValue();
                }
            }
            MakeTargetHeadingStr( TargetHeading );
@@ -512,8 +539,8 @@ int FGAutopilot::run() {
            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 );
+           globals->get_controls()->set_aileron( AileronSet );
+           globals->get_controls()->set_rudder( AileronSet / 4.0 );
        } else {
            // steer towards the target heading
 
@@ -523,7 +550,8 @@ int FGAutopilot::run() {
            double AileronSet;
 
            RelHeading
-               = NormalizeDegrees( TargetHeading - FGBFI::getHeading() );
+               = NormalizeDegrees( TargetHeading
+                                   - heading_node->getDoubleValue() );
            // figure out how far off we are from desired heading
 
            // Now it is time to deterime how far we should be rolled.
@@ -558,7 +586,8 @@ int FGAutopilot::run() {
 
            SG_LOG( SG_COCKPIT, SG_BULK, "TargetRoll: " << TargetRoll );
 
-           RelRoll = NormalizeDegrees( TargetRoll - FGBFI::getRoll() );
+           RelRoll = NormalizeDegrees( TargetRoll 
+                                       - roll_node->getDoubleValue() );
 
            // Check if we are further from heading than the roll out
            // smooth point
@@ -576,8 +605,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 );
        }
     }
@@ -590,15 +619,11 @@ int FGAutopilot::run() {
        double prop_adj, int_adj, total_adj;
 
        if ( altitude_mode == FG_ALTITUDE_LOCK ) {
-           // normal altitude hold
-           // cout << "TargetAltitude = " << TargetAltitude
-           //      << "Altitude = " << FGBFI::getAltitude() * SG_FEET_TO_METER
-           //      << endl;
            climb_rate =
                ( TargetAltitude - FGSteam::get_ALT_ft() * SG_FEET_TO_METER ) * 8.0;
        } else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
            double x = current_radiostack->get_nav1_gs_dist();
-           double y = (FGBFI::getAltitude() 
+           double y = (altitude_node->getDoubleValue()
                        - current_radiostack->get_nav1_elev()) * SG_FEET_TO_METER;
            double current_angle = atan2( y, x ) * SGD_RADIANS_TO_DEGREES;
            // cout << "current angle = " << current_angle << endl;
@@ -625,7 +650,8 @@ int FGAutopilot::run() {
        } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
            // brain dead ground hugging with no look ahead
            climb_rate =
-               ( TargetAGL - FGBFI::getAGL()*SG_FEET_TO_METER ) * 16.0;
+               ( TargetAGL - altitude_agl_node->getDoubleValue()
+                  * SG_FEET_TO_METER ) * 16.0;
            // cout << "target agl = " << TargetAGL 
            //      << "  current agl = " << fgAPget_agl() 
            //      << "  target climb rate = " << climb_rate 
@@ -637,38 +663,47 @@ 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(TargetClimbRate) ) {
-           climb_rate = -fabs(TargetClimbRate);
+       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 << "given our speed, modified desired climb rate = "
        //      << climb_rate * SG_METER_TO_FEET 
        //      << " fpm" << endl;
+        // cout << "Current climb rate = "
+        //      << vertical_speed_node->getDoubleValue() * 60 << " fpm" << endl;
 
-       error = FGBFI::getVerticalSpeed() * SG_FEET_TO_METER - climb_rate;
-       // cout << "climb rate = " << FGBFI::getVerticalSpeed()
-       //      << "  vsi rate = " << FGSteam::get_VSI_fps() << endl;
+       error = vertical_speed_node->getDoubleValue() * 60
+            - climb_rate * SG_METER_TO_FEET;
 
        // accumulate the error under the curve ... this really should
        // be *= delta t
@@ -677,13 +712,14 @@ 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;
+       prop_adj = prop_error / elevator_adj_factor->getFloatValue();
 
-       total_adj = 0.9 * prop_adj + 0.1 * int_adj;
+       total_adj = (1.0 - integral_contrib->getFloatValue()) * prop_adj
+            + integral_contrib->getFloatValue() * int_adj;
        // if ( total_adj > 0.6 ) {
        //     total_adj = 0.6;
        // } else if ( total_adj < -0.2 ) {
@@ -695,7 +731,7 @@ int FGAutopilot::run() {
            total_adj = -1.0;
        }
 
-       controls.set_elevator( total_adj );
+       globals->get_controls()->set_elevator_trim( total_adj );
     }
 
     // auto throttle
@@ -734,7 +770,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
@@ -766,16 +803,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;
 }
 
@@ -792,18 +831,19 @@ void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
        // set autopilot to hold a zero turn (as reported by the TC)
     } else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
        // set heading hold to current heading
-       TargetHeading = FGBFI::getHeading();
+       TargetHeading = heading_node->getDoubleValue();
     } else if ( heading_mode == FG_HEADING_WAYPOINT ) {
        if ( globals->get_route()->size() ) {
            double course, distance;
 
-           old_lat = FGBFI::getLatitude();
-           old_lon = FGBFI::getLongitude();
+           old_lat = latitude_node->getDoubleValue();
+           old_lon = longitude_node->getDoubleValue();
 
            waypoint = globals->get_route()->get_first();
-           waypoint.CourseAndDistance( FGBFI::getLongitude(),
-                                       FGBFI::getLatitude(),
-                                       FGBFI::getLatitude() * SG_FEET_TO_METER,
+           waypoint.CourseAndDistance( longitude_node->getDoubleValue(),
+                                       latitude_node->getDoubleValue(),
+                                       altitude_node->getDoubleValue()
+                                        * SG_FEET_TO_METER,
                                        &course, &distance );
            TargetHeading = course;
            TargetDistance = distance;
@@ -825,7 +865,6 @@ void FGAutopilot::set_HeadingMode( fgAutoHeadingMode mode ) {
        } else {
            // no more way points, default to heading lock.
            heading_mode = FG_TC_HEADING_LOCK;
-           // TargetHeading = FGBFI::getHeading();
        }
     }
 
@@ -840,8 +879,8 @@ void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
     alt_error_accum = 0.0;
 
     if ( altitude_mode == FG_ALTITUDE_LOCK ) {
-       if ( TargetAltitude < FGBFI::getAGL() * SG_FEET_TO_METER ) {
-           // TargetAltitude = FGBFI::getAltitude() * SG_FEET_TO_METER;
+       if ( TargetAltitude < altitude_agl_node->getDoubleValue()
+             * SG_FEET_TO_METER ) {
        }
 
        if ( fgGetString("/sim/startup/units") == "feet" ) {
@@ -853,7 +892,7 @@ void FGAutopilot::set_AltitudeMode( fgAutoAltitudeMode mode ) {
        climb_error_accum = 0.0;
 
     } else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
-       TargetAGL = FGBFI::getAGL() * SG_FEET_TO_METER;
+       TargetAGL = altitude_agl_node->getDoubleValue() * SG_FEET_TO_METER;
 
        if ( fgGetString("/sim/startup/units") == "feet" ) {
            MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
@@ -909,7 +948,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 );
 }
@@ -925,8 +964,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;
@@ -1042,7 +1081,7 @@ void FGAutopilot::set_AutoThrottleEnabled( bool value ) {
     auto_throttle = value;
 
     if ( auto_throttle == true ) {
-       TargetSpeed = FGBFI::getAirspeed();
+        TargetSpeed = fgGetDouble("/velocities/airspeed-kt");
        speed_error_accum = 0.0;
     }