]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/LaRCsim/LaRCsim.cxx
Replace round by simgear::SGMiscd::roundToInt()
[flightgear.git] / src / FDM / LaRCsim / LaRCsim.cxx
index 4235db3a0d50f18ec2c12de10b8b2c5dcce689f0..850532f34532e698e28c3bbb84cca0899f846377 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started October 1998.
 //
-// Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
+// Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include <math.h>
 #include <string.h>            // strcmp()
 
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
-#include <simgear/scene/model/location.hxx>
 #include <simgear/scene/model/placement.hxx>
 
-#include <Aircraft/aircraft.hxx>
-#include <Controls/controls.hxx>
+#include <Aircraft/controls.hxx>
 #include <FDM/flight.hxx>
-#include <FDM/IO360.hxx>
-#include <FDM/LaRCsimIC.hxx>
 #include <FDM/UIUCModel/uiuc_aircraft.h>
 #include <Main/fg_props.hxx>
 #include <Model/acmodel.hxx>
 #include "ls_interface.h"
 #include "ls_constants.h"
 
+#include "IO360.hxx"
+#include "LaRCsimIC.hxx"
 #include "LaRCsim.hxx"
 
 
 FGLaRCsim::FGLaRCsim( double dt ) {
+    mass=i_xx=i_yy=i_zz=i_xz=0;
+
 //     set_delta_t( dt );
 
     speed_up = fgGetNode("/sim/speed-up", true);
@@ -66,6 +70,22 @@ FGLaRCsim::FGLaRCsim( double dt ) {
         I_xz = 0.000000E+00;
     }
 
+    if ( !strcmp(aero->getStringValue(), "basic") ) {
+        copy_to_LaRCsim(); // initialize all of LaRCsim's vars
+
+        //this should go away someday -- formerly done in fg_init.cxx
+       Mass = 2./32.174;
+       I_xx   = 0.0454;
+       I_yy   = 0.0191;
+       I_zz   = 0.0721;
+       I_xz   = 0;
+//     Mass = 8.547270E+01;
+//     I_xx = 1.048000E+03;
+//     I_yy = 3.000000E+03;
+//     I_zz = 3.530000E+03;
+//     I_xz = 0.000000E+00;
+    }
+
     ls_set_model_dt(dt);
     
     // Initialize our little engine that hopefully might
@@ -161,14 +181,85 @@ void FGLaRCsim::update( double dt ) {
                        * dt);
        }
 
+       // Apparently the IO360 thrust model is not working.  
+       // F_X_engine is zero here.
        F_X_engine = eng.get_prop_thrust_lbs();
-       // cout << "F_X_engine = " << F_X_engine << '\n';
-       // end c172 if block
 
        Flap_handle = 30.0 * globals->get_controls()->get_flaps();
     }
     // done with c172-larcsim if-block
 
+    if ( !strcmp(aero->getStringValue(), "basic") ) {
+        // set control inputs
+        // cout << "V_calibrated_kts = " << V_calibrated_kts << '\n';
+       eng.set_IAS( V_calibrated_kts );
+       eng.set_Throttle_Lever_Pos( globals->get_controls()->get_throttle( 0 )
+                                   * 100.0 );
+       eng.set_Propeller_Lever_Pos( 100 );
+        eng.set_Mixture_Lever_Pos( globals->get_controls()->get_mixture( 0 )
+                                  * 100.0 );
+       eng.set_Magneto_Switch_Pos( globals->get_controls()->get_magnetos(0) );
+       eng.setStarterFlag( globals->get_controls()->get_starter(0) );
+       eng.set_p_amb( Static_pressure );
+       eng.set_T_amb( Static_temperature );
+
+       // update engine model
+       eng.update();
+
+       // Fake control-surface positions
+       fgSetDouble("/surface-positions/flap-pos-norm",
+                   fgGetDouble("/controls/flight/flaps"));
+                               // FIXME: ignoring trim
+       fgSetDouble("/surface-positions/elevator-pos-norm",
+                   fgGetDouble("/controls/flight/elevator"));
+                               // FIXME: ignoring trim
+       fgSetDouble("/surface-positions/left-aileron-pos-norm",
+                   fgGetDouble("/controls/flight/aileron"));
+                               // FIXME: ignoring trim
+       fgSetDouble("/surface-positions/right-aileron-pos-norm",
+                   -1 * fgGetDouble("/controls/flight/aileron"));
+                               // FIXME: ignoring trim
+       fgSetDouble("/surface-positions/rudder-pos-norm",
+                   fgGetDouble("/controls/flight/rudder"));
+
+       // copy engine state values onto "bus"
+       fgSetDouble("/engines/engine/rpm", eng.get_RPM());
+       fgSetDouble("/engines/engine/mp-osi", eng.get_Manifold_Pressure());
+       fgSetDouble("/engines/engine/max-hp", eng.get_MaxHP());
+       fgSetDouble("/engines/engine/power-pct", eng.get_Percentage_Power());
+       fgSetDouble("/engines/engine/egt-degf", eng.get_EGT());
+       fgSetDouble("/engines/engine/cht-degf", eng.get_CHT());
+       fgSetDouble("/engines/engine/prop-thrust", eng.get_prop_thrust_SI());
+       fgSetDouble("/engines/engine/fuel-flow-gph",
+                   eng.get_fuel_flow_gals_hr());
+       fgSetDouble("/engines/engine/oil-temperature-degf",
+                   eng.get_oil_temp());
+       fgSetDouble("/engines/engine/running", eng.getRunningFlag());
+       fgSetDouble("/engines/engine/cranking", eng.getCrankingFlag());
+
+       static const SGPropertyNode *fuel_freeze
+           = fgGetNode("/sim/freeze/fuel");
+
+       if ( ! fuel_freeze->getBoolValue() ) {
+           //Assume we are using both tanks equally for now
+           fgSetDouble("/consumables/fuel/tank[0]/level-gal_us",
+                       fgGetDouble("/consumables/fuel/tank[0]/level-gal_us")
+                       - (eng.get_fuel_flow_gals_hr() / (2 * 3600))
+                       * dt);
+           fgSetDouble("/consumables/fuel/tank[1]/level-gal_us",
+                       fgGetDouble("/consumables/fuel/tank[1]/level-gal_us")
+                       - (eng.get_fuel_flow_gals_hr() / (2 * 3600))
+                       * dt);
+       }
+
+       // Apparently the IO360 thrust model is not working.  
+       // F_X_engine is zero here.
+       F_X_engine = eng.get_prop_thrust_lbs();
+
+       Flap_handle = 30.0 * globals->get_controls()->get_flaps();
+    }
+    // done with basic-larcsim if-block
+
     double save_alt = 0.0;
 
     // lets try to avoid really screwing up the LaRCsim model
@@ -183,20 +274,25 @@ void FGLaRCsim::update( double dt ) {
     Long_trim = globals->get_controls()->get_elevator_trim();
     Rudder_pedal = globals->get_controls()->get_rudder() / speed_up->getIntValue();
 
-    if ( !strcmp(aero->getStringValue(), "c172") ) {
-        Use_External_Engine = 1;
-    } else {
-        Use_External_Engine = 0;
-    }
+    // IO360.cxx for the C172 thrust is broken (not sure why).  
+    // So force C172 to use engine model in c172_engine.c instead of the IO360.cxx.
+    //      if ( !strcmp(aero->getStringValue(), "c172") ) {
+    //          Use_External_Engine = 1;
+    //      } else {
+    //          Use_External_Engine = 0;
+    //      }
+    Use_External_Engine = 0;
 
     Throttle_pct = globals->get_controls()->get_throttle( 0 ) * 1.0;
 
-    Brake_pct[0] = globals->get_controls()->get_brake( 1 );
-    Brake_pct[1] = globals->get_controls()->get_brake( 0 );
+    Brake_pct[0] = globals->get_controls()->get_brake_right();
+    Brake_pct[1] = globals->get_controls()->get_brake_left();
 
     // Inform LaRCsim of the local terrain altitude
-    // Runway_altitude = get_Runway_altitude();
-    Runway_altitude = getACModel()->get3DModel()->getSGLocation()->get_cur_elev_m() * SG_METER_TO_FEET;
+    Runway_altitude
+      = get_groundlevel_m(Latitude, Longitude, Altitude * SG_FEET_TO_METER)
+      * SG_METER_TO_FEET;
+
     // Weather
     /* V_north_airmass = get_V_north_airmass();
        V_east_airmass =  get_V_east_airmass();
@@ -222,26 +318,29 @@ void FGLaRCsim::update( double dt ) {
     // if flying uiuc, set some properties and over-ride some previous ones
     if ( !strcmp(aero->getStringValue(), "uiuc")) {
 
-      // surface positions
-      fgSetDouble("/surface-positions/rudder-pos-norm",             fgGetDouble("/controls/flight/rudder"));
-      fgSetDouble("/surface-positions/elevator-pos-norm",           fgGetDouble("/controls/flight/elevator")); // FIXME: ignoring trim
-      fgSetDouble("/surface-positions/right-aileron-pos-norm", -1 * fgGetDouble("/controls/flight/aileron")); // FIXME: ignoring trim
-      fgSetDouble("/surface-positions/left-aileron-pos-norm",       fgGetDouble("/controls/flight/aileron")); // FIXME: ignoring trim
+      // surface positions and other general properties
+      fgSetDouble("/surface-positions/flight/rudder-pos-norm",             fgGetDouble("/controls/flight/rudder"));
+      fgSetDouble("/surface-positions/flight/elevator-pos-norm",           fgGetDouble("/controls/flight/elevator")); // FIXME: ignoring trim
+      fgSetDouble("/surface-positions/flight/right-aileron-pos-norm", -1 * fgGetDouble("/controls/flight/aileron")); // FIXME: ignoring trim
+      fgSetDouble("/surface-positions/flight/left-aileron-pos-norm",       fgGetDouble("/controls/flight/aileron")); // FIXME: ignoring trim
+
+      // used by Instruments/accel.xml
+      fgSetDouble("/accelerations/pilot-g",   -N_Z_cg);
 
       Flap_handle = flap_max * globals->get_controls()->get_flaps();
 
       // flaps with transition occuring in uiuc_aerodeflections.cpp
       if (use_flaps) {
-      fgSetDouble("/surface-positions/flight/flap-pos-norm",               flap_pos_pct);
+      fgSetDouble("/surface-positions/flight/flap-pos-norm",               flap_pos_norm);
       }
 
       // spoilers with transition occurring in uiuc_aerodeflections.cpp
       if(use_spoilers) {
-       Spoiler_handle = spoiler_max * fgGetDouble("/controls/spoilers");
+       Spoiler_handle = spoiler_max * fgGetDouble("/controls/flight/spoilers");
       }
       // gear with transition occurring here in LaRCsim.cxx
       if (use_gear) {
-       if(fgGetBool("/controls/gear-down")) {
+       if(fgGetBool("/controls/gear/gear-down")) {
          Gear_handle = 1.0;
        }
        else {
@@ -281,30 +380,28 @@ void FGLaRCsim::update( double dt ) {
       }
       else if ( !strcmp(uiuc_type->getStringValue(), "uiuc-jet")) {
        // uiuc jet aircraft, e.g. a4d
+       // used for setting the sound
        fgSetDouble("/engines/engine/n1", (75 + (globals->get_controls()->get_throttle( 0 ) * 100.0 )/400));
        fgSetDouble("/engines/engine/prop-thrust", (4000 + F_X_engine/2));
+       // used for setting the instruments
+       fgSetDouble("/engines/engine[0]/n1", (50 + (globals->get_controls()->get_throttle( 0 ) * 50)));
       }
       else if ( !strcmp(uiuc_type->getStringValue(), "uiuc-sailplane")) {
        // uiuc sailplane, e.g. asw20
        fgSetDouble("/engines/engine/cranking", 0);
-       // set the wind speed for use in setting wind sound level
-       fgSetDouble("/velocities/V_rel_wind_kts", (V_rel_wind * 1.274));
       }
       else if ( !strcmp(uiuc_type->getStringValue(), "uiuc-hangglider")) {
-       // uiuc sailplane, e.g. asw20
+       // uiuc hang glider, e.g. airwave
        fgSetDouble("/engines/engine/cranking", 0);
       }
       else if ( !strcmp(uiuc_type->getStringValue(), "uiuc-ornithopter")) {
-       // mechanical flapping wings
-       // flapping wings (using seahawk for now)
+       // flapping wings
        fgSetDouble("/canopy/position-norm", 0);
        fgSetDouble("/wing-phase/position-norm", sin(flapper_phi - 3 * LS_PI / 2));
-       //      fgSetDouble("/wing-phase/position-norm", fgGetDouble("/controls/rudder"));
        fgSetDouble("/wing-phase/position-deg", flapper_phi*RAD_TO_DEG);
        fgSetDouble("/wing-phase/position-one", 1);
        fgSetDouble("/thorax/volume", 0);
        fgSetDouble("/thorax/rpm",    0);
-       //      fgSetDouble("/wing-phase/position-norm", ((1+cos(flapper_phi - LS_PI/2))/2 -.36 ));
        //      fgSetDouble("/thorax/volume", ((1+sin(2*(flapper_phi+LS_PI)))/2));
        //      fgSetDouble("/thorax/rpm",    ((1+sin(2*(flapper_phi+LS_PI)))/2));
       }
@@ -314,14 +411,14 @@ void FGLaRCsim::update( double dt ) {
     // add Gamma_horiz_deg to properties, mss 021213
     if (use_gamma_horiz_on_speed) {
       if (V_rel_wind > gamma_horiz_on_speed) {
-       fgSetDouble("/orientation/Gamma_horiz_deg", (Gamma_horiz_rad * RAD_TO_DEG));
+       fgSetDouble("/orientation/gamma-horiz-deg", (Gamma_horiz_rad * RAD_TO_DEG));
       }
       else {
-       fgSetDouble("/orientation/Gamma_horiz_deg",  fgGetDouble("/orientation/heading-deg"));
+       fgSetDouble("/orientation/gamma-horiz-deg",  fgGetDouble("/orientation/heading-deg"));
       }
     }
     else {
-      fgSetDouble("/orientation/Gamma_horiz_deg",  fgGetDouble("/orientation/heading-deg"));
+      fgSetDouble("/orientation/gamma-horiz-deg",  fgGetDouble("/orientation/heading-deg"));
     }
     ls_update(multiloop);
 
@@ -599,23 +696,22 @@ bool FGLaRCsim::copy_from_LaRCsim() {
     _set_Altitude_AGL( Altitude - Runway_altitude );
 
     // Miscellaneous quantities
-    _set_T_Local_to_Body(T_local_to_body_m);
     // set_Gravity( Gravity );
     // set_Centrifugal_relief( Centrifugal_relief );
 
-    _set_Alpha( Alpha );
-    _set_Beta( Beta );
-    // set_Alpha_dot( Alpha_dot );
-    // set_Beta_dot( Beta_dot );
+    _set_Alpha( Std_Alpha );
+    _set_Beta( Std_Beta );
+    // set_Alpha_dot( Std_Alpha_dot );
+    // set_Beta_dot( Std_Beta_dot );
 
     // set_Cos_alpha( Cos_alpha );
     // set_Sin_alpha( Sin_alpha );
     // set_Cos_beta( Cos_beta );
     // set_Sin_beta( Sin_beta );
 
-    _set_Cos_phi( Cos_phi );
+    // _set_Cos_phi( Cos_phi );
     // set_Sin_phi( Sin_phi );
-    _set_Cos_theta( Cos_theta );
+    // _set_Cos_theta( Cos_theta );
     // set_Sin_theta( Sin_theta );
     // set_Cos_psi( Cos_psi );
     // set_Sin_psi( Sin_psi );
@@ -651,11 +747,6 @@ bool FGLaRCsim::copy_from_LaRCsim() {
     //                        D_pilot_above_rwy );
     // set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
 
-    _set_sin_lat_geocentric(Lat_geocentric);
-    _set_cos_lat_geocentric(Lat_geocentric);
-    _set_sin_cos_longitude(Longitude);
-    _set_sin_cos_latitude(Latitude);
-
     // printf("sin_lat_geo %f  cos_lat_geo %f\n", sin_Lat_geoc, cos_Lat_geoc);
     // printf("sin_lat     %f  cos_lat     %f\n", 
     //        get_sin_latitude(), get_cos_latitude());