#include <Cockpit/radiostack.hxx>
#include <Controls/controls.hxx>
#include <FDM/flight.hxx>
-#include <Main/bfi.hxx>
#include <Main/globals.hxx>
#include <Scenery/scenery.hxx>
#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 * FEET_TO_METER; // fpm -> mpm
-// const double ideal_decent_rate = 1000.0 * FEET_TO_METER; // fpm -> mpm
+// 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 :-)
// constructor
FGAutopilot::FGAutopilot():
-TargetClimbRate(1000 * FEET_TO_METER)
+TargetClimbRate(500 * SG_FEET_TO_METER),
+TargetDecentRate(1000 * SG_FEET_TO_METER)
{
}
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
- double kts = ft_s * FEET_TO_METER * 3600 * METER_TO_NM;
+ * speedup_node->getIntValue();
+ double kts = ft_s * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM;
return kts;
}
if ( size > 0 ) {
SGWayPoint wp1 = globals->get_route()->get_waypoint( 0 );
accum += distance;
- double eta = accum * METER_TO_NM / get_ground_speed();
+ double eta = accum * SG_METER_TO_NM / get_ground_speed();
if ( eta >= 100.0 ) { eta = 99.999; }
int major, minor;
if ( eta < (1.0/6.0) ) {
minor = (int)((eta - (int)eta) * 60.0);
sprintf( TargetWP1Str, "%s %.2f NM ETA %d:%02d",
wp1.get_id().c_str(),
- accum*METER_TO_NM, major, minor );
- // cout << "distance = " << distance*METER_TO_NM
+ accum*SG_METER_TO_NM, major, minor );
+ // cout << "distance = " << distance*SG_METER_TO_NM
// << " gndsp = " << get_ground_speed()
// << " time = " << eta
// << " major = " << major
SGWayPoint wp2 = globals->get_route()->get_waypoint( 1 );
accum += wp2.get_distance();
- double eta = accum * METER_TO_NM / get_ground_speed();
+ double eta = accum * SG_METER_TO_NM / get_ground_speed();
if ( eta >= 100.0 ) { eta = 99.999; }
int major, minor;
if ( eta < (1.0/6.0) ) {
minor = (int)((eta - (int)eta) * 60.0);
sprintf( TargetWP2Str, "%s %.2f NM ETA %d:%02d",
wp2.get_id().c_str(),
- accum*METER_TO_NM, major, minor );
+ accum*SG_METER_TO_NM, major, minor );
}
// next route
SGWayPoint wpn = globals->get_route()->get_waypoint( size - 1 );
- double eta = accum * METER_TO_NM / get_ground_speed();
+ double eta = accum * SG_METER_TO_NM / get_ground_speed();
if ( eta >= 100.0 ) { eta = 99.999; }
int major, minor;
if ( eta < (1.0/6.0) ) {
minor = (int)((eta - (int)eta) * 60.0);
sprintf( TargetWP3Str, "%s %.2f NM ETA %d:%02d",
wpn.get_id().c_str(),
- accum*METER_TO_NM, major, minor );
+ accum*SG_METER_TO_NM, major, minor );
}
}
// Initialize autopilot subsystem
void FGAutopilot::init() {
- FG_LOG( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem" );
+ 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);
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;
// 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() );
alt_error_accum = 0.0;
climb_error_accum = 0.0;
- MakeTargetAltitudeStr( 3000.0);
- MakeTargetHeadingStr( 0.0 );
+ MakeTargetAltitudeStr( TargetAltitude );
+ MakeTargetHeadingStr( TargetHeading );
// These eventually need to be read from current_aircaft somehow.
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;
- TargetHeading = 0.0; // default direction, due north
+ // TargetHeading = 0.0; // default direction, due north
MakeTargetHeadingStr( TargetHeading );
- TargetAltitude = 3000; // default altitude in meters
+ // TargetAltitude = 3000; // default altitude in meters
MakeTargetAltitudeStr( TargetAltitude );
alt_error_accum = 0.0;
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() );
}
// 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() * FEET_TO_METER;
+ double lat = latitude_node->getDoubleValue();
+ double lon = longitude_node->getDoubleValue();
+ double alt = altitude_node->getDoubleValue() * SG_FEET_TO_METER;
#ifdef FG_FORCE_AUTO_DISENGAGE
// see if somebody else has changed them
// coordinator zero'd
} else if ( heading_mode == FG_TRUE_HEADING_LOCK ) {
// leave "true" target heading as is
+ while ( TargetHeading < 0.0 ) { TargetHeading += 360.0; }
+ while ( TargetHeading > 360.0 ) { TargetHeading -= 360.0; }
+ MakeTargetHeadingStr( TargetHeading );
} else if ( heading_mode == FG_HEADING_NAV1 ) {
// track the NAV1 heading needle deflection
// determine the heading adjustment needed.
double adjustment =
current_radiostack->get_nav1_heading_needle_deflection()
- * (current_radiostack->get_nav1_loc_dist() * METER_TO_NM);
+ * (current_radiostack->get_nav1_loc_dist() * SG_METER_TO_NM);
if ( adjustment < -30.0 ) { adjustment = -30.0; }
if ( adjustment > 30.0 ) { adjustment = 30.0; }
// end of the line
heading_mode = FG_TRUE_HEADING_LOCK;
// use current heading
- TargetHeading = FGBFI::getHeading();
+ TargetHeading = heading_node->getDoubleValue();
}
}
MakeTargetHeadingStr( TargetHeading );
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.
- FG_LOG( FG_AUTOPILOT, FG_DEBUG, "RelHeading: " << RelHeading );
+ SG_LOG( SG_AUTOPILOT, SG_DEBUG, "RelHeading: " << RelHeading );
// Check if we are further from heading than the roll out point
// Compare Target roll to Current Roll, Generate Rel Roll
- FG_LOG( FG_COCKPIT, FG_BULK, "TargetRoll: " << TargetRoll );
+ 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
double prop_adj, int_adj, total_adj;
if ( altitude_mode == FG_ALTITUDE_LOCK ) {
- // normal altitude hold
- // cout << "TargetAltitude = " << TargetAltitude
- // << "Altitude = " << FGBFI::getAltitude() * FEET_TO_METER
- // << endl;
climb_rate =
- ( TargetAltitude - FGSteam::get_ALT_ft() * FEET_TO_METER ) * 8.0;
+ ( 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()
- - current_radiostack->get_nav1_elev()) * FEET_TO_METER;
- double current_angle = atan2( y, x ) * RAD_TO_DEG;
+ 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;
double target_angle = current_radiostack->get_nav1_target_gs();
// convert to meter/min
// cout << "raw ground speed = " << cur_fdm_state->get_V_ground_speed() << endl;
double horiz_vel = cur_fdm_state->get_V_ground_speed()
- * FEET_TO_METER * 60.0;
+ * SG_FEET_TO_METER * 60.0;
// cout << "Horizontal vel = " << horiz_vel << endl;
- climb_rate = -sin( des_angle * DEG_TO_RAD ) * horiz_vel;
+ climb_rate = -sin( des_angle * SGD_DEGREES_TO_RADIANS ) * horiz_vel;
// cout << "climb_rate = " << climb_rate << endl;
/* climb_error_accum += gs_diff * 2.0; */
/* climb_rate = gs_diff * 200.0 + climb_error_accum; */
} else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
// brain dead ground hugging with no look ahead
climb_rate =
- ( TargetAGL - FGBFI::getAGL()*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
climb_rate = max_climb;
}
- if ( climb_rate < -fabs(TargetClimbRate) ) {
- climb_rate = -fabs(TargetClimbRate);
+ if ( climb_rate < -fabs(TargetDecentRate) ) {
+ climb_rate = -fabs(TargetDecentRate);
}
+
// cout << "Target climb rate = " << TargetClimbRate << endl;
// cout << "given our speed, modified desired climb rate = "
- // << climb_rate * METER_TO_FEET
+ // << climb_rate * SG_METER_TO_FEET
// << " fpm" << endl;
+ // cout << "Current climb rate = "
+ // << vertical_speed_node->getDoubleValue() * 60 << " fpm" << endl;
- error = FGBFI::getVerticalSpeed() * 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
// 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() * FEET_TO_METER,
+ waypoint.CourseAndDistance( longitude_node->getDoubleValue(),
+ latitude_node->getDoubleValue(),
+ altitude_node->getDoubleValue()
+ * SG_FEET_TO_METER,
&course, &distance );
TargetHeading = course;
TargetDistance = distance;
TargetAltitude = waypoint.get_target_alt();
altitude_mode = FG_ALTITUDE_LOCK;
set_AltitudeEnabled( true );
- MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
+ MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
}
- FG_LOG( FG_COCKPIT, FG_INFO, " set_HeadingMode: ( "
+ SG_LOG( SG_COCKPIT, SG_INFO, " set_HeadingMode: ( "
<< get_TargetLatitude() << " "
<< get_TargetLongitude() << " ) "
);
} else {
// no more way points, default to heading lock.
heading_mode = FG_TC_HEADING_LOCK;
- // TargetHeading = FGBFI::getHeading();
}
}
alt_error_accum = 0.0;
if ( altitude_mode == FG_ALTITUDE_LOCK ) {
- if ( TargetAltitude < FGBFI::getAGL() * FEET_TO_METER ) {
- // TargetAltitude = FGBFI::getAltitude() * FEET_TO_METER;
+ if ( TargetAltitude < altitude_agl_node->getDoubleValue()
+ * SG_FEET_TO_METER ) {
}
if ( fgGetString("/sim/startup/units") == "feet" ) {
- MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
+ MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
} else {
- MakeTargetAltitudeStr( TargetAltitude * METER_TO_FEET );
+ MakeTargetAltitudeStr( TargetAltitude * SG_METER_TO_FEET );
}
} else if ( altitude_mode == FG_ALTITUDE_GS1 ) {
climb_error_accum = 0.0;
} else if ( altitude_mode == FG_ALTITUDE_TERRAIN ) {
- TargetAGL = FGBFI::getAGL() * FEET_TO_METER;
+ TargetAGL = altitude_agl_node->getDoubleValue() * SG_FEET_TO_METER;
if ( fgGetString("/sim/startup/units") == "feet" ) {
- MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
+ MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
} else {
- MakeTargetAltitudeStr( TargetAGL * METER_TO_FEET );
+ MakeTargetAltitudeStr( TargetAGL * SG_METER_TO_FEET );
}
}
update_old_control_values();
- FG_LOG( FG_COCKPIT, FG_INFO, " set_AltitudeMode():" );
+ SG_LOG( SG_COCKPIT, SG_INFO, " set_AltitudeMode():" );
}
#if 0
static inline double get_aoa( void ) {
- return( cur_fdm_state->get_Gamma_vert_rad() * RAD_TO_DEG );
+ return( cur_fdm_state->get_Gamma_vert_rad() * SGD_RADIANS_TO_DEGREES );
}
static inline double fgAPget_latitude( void ) {
- return( cur_fdm_state->get_Latitude() * RAD_TO_DEG );
+ return( cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES );
}
static inline double fgAPget_longitude( void ) {
- return( cur_fdm_state->get_Longitude() * RAD_TO_DEG );
+ return( cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES );
}
static inline double fgAPget_roll( void ) {
- return( cur_fdm_state->get_Phi() * RAD_TO_DEG );
+ return( cur_fdm_state->get_Phi() * SGD_RADIANS_TO_DEGREES );
}
static inline double get_pitch( void ) {
}
double fgAPget_heading( void ) {
- return( cur_fdm_state->get_Psi() * RAD_TO_DEG );
+ return( cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
}
static inline double fgAPget_altitude( void ) {
- return( cur_fdm_state->get_Altitude() * FEET_TO_METER );
+ return( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER );
}
static inline double fgAPget_climb( void ) {
// return in meters per minute
- return( cur_fdm_state->get_Climb_Rate() * FEET_TO_METER * 60 );
+ return( cur_fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60 );
}
static inline double get_sideslip( void ) {
static inline double fgAPget_agl( void ) {
double agl;
- agl = cur_fdm_state->get_Altitude() * FEET_TO_METER
+ agl = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER
- scenery.cur_elev;
return( agl );
// cout << "new altitude = " << new_altitude << endl;
if ( fgGetString("/sim/startup/units") == "feet" ) {
- target_alt = new_altitude * FEET_TO_METER;
+ target_alt = new_altitude * SG_FEET_TO_METER;
}
if( target_alt < scenery.cur_elev ) {
// cout << "TargetAltitude = " << TargetAltitude << endl;
if ( fgGetString("/sim/startup/units") == "feet" ) {
- target_alt *= METER_TO_FEET;
+ target_alt *= SG_METER_TO_FEET;
}
// ApAltitudeDialogInput->setValue((float)target_alt);
MakeTargetAltitudeStr( target_alt );
double target_alt, target_agl;
if ( fgGetString("/sim/startup/units") == "feet" ) {
- target_alt = TargetAltitude * METER_TO_FEET;
- target_agl = TargetAGL * METER_TO_FEET;
+ target_alt = TargetAltitude * SG_METER_TO_FEET;
+ target_agl = TargetAGL * SG_METER_TO_FEET;
} else {
target_alt = TargetAltitude;
target_agl = TargetAGL;
}
if ( fgGetString("/sim/startup/units") == "feet" ) {
- target_alt *= FEET_TO_METER;
- target_agl *= FEET_TO_METER;
+ target_alt *= SG_FEET_TO_METER;
+ target_agl *= SG_FEET_TO_METER;
}
TargetAltitude = target_alt;
TargetAGL = target_agl;
if ( fgGetString("/sim/startup/units") == "feet" )
- target_alt *= METER_TO_FEET;
+ target_alt *= SG_METER_TO_FEET;
if ( fgGetString("/sim/startup/units") == "feet" )
- target_agl *= METER_TO_FEET;
+ target_agl *= SG_METER_TO_FEET;
if ( altitude_mode == FG_ALTITUDE_LOCK ) {
MakeTargetAltitudeStr( target_alt );
void FGAutopilot::HeadingSet( double new_heading ) {
- heading_mode = FG_DG_HEADING_LOCK;
-
- new_heading = NormalizeDegrees( new_heading );
- DGTargetHeading = new_heading;
- // following cast needed ambiguous plib
- // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
- MakeTargetHeadingStr( DGTargetHeading );
+ if( heading_mode == FG_TRUE_HEADING_LOCK ) {
+ new_heading = NormalizeDegrees( new_heading );
+ TargetHeading = new_heading;
+ MakeTargetHeadingStr( TargetHeading );
+ } else {
+ heading_mode = FG_DG_HEADING_LOCK;
+
+ new_heading = NormalizeDegrees( new_heading );
+ DGTargetHeading = new_heading;
+ // following cast needed ambiguous plib
+ // ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
+ MakeTargetHeadingStr( DGTargetHeading );
+ }
update_old_control_values();
}
auto_throttle = value;
if ( auto_throttle == true ) {
- TargetSpeed = FGBFI::getAirspeed();
+ TargetSpeed = fgGetDouble("/velocities/airspeed");
speed_error_accum = 0.0;
}
update_old_control_values();
- FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAutoThrottle: ("
+ SG_LOG( SG_COCKPIT, SG_INFO, " fgAPSetAutoThrottle: ("
<< auto_throttle << ") " << TargetSpeed );
}