1 // MagicCarpet.cxx -- interface to the "Magic Carpet" flight model
3 // Written by Curtis Olson, started October 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/math/sg_geodesy.hxx>
25 #include <simgear/math/point3d.hxx>
26 #include <simgear/math/polar3d.hxx>
28 #include <Controls/controls.hxx>
29 #include <Main/globals.hxx>
30 #include <Main/fg_props.hxx>
32 #include "MagicCarpet.hxx"
35 FGMagicCarpet::FGMagicCarpet( double dt ) {
40 FGMagicCarpet::~FGMagicCarpet() {
44 // Initialize the Magic Carpet flight model, dt is the time increment
45 // for each subsequent iteration through the EOM
46 void FGMagicCarpet::init() {
50 // Run an iteration of the EOM (equations of motion)
51 bool FGMagicCarpet::update( int multiloop ) {
52 // cout << "FGLaRCsim::update()" << endl;
54 double time_step = get_delta_t() * multiloop;
56 // speed and distance traveled
57 double speed = controls.get_throttle( 0 ) * 2000; // meters/sec
58 double dist = speed * time_step;
59 double kts = speed * SG_METER_TO_NM * 3600.0;
60 _set_V_equiv_kts( kts );
61 _set_V_calibrated_kts( kts );
62 _set_V_ground_speed( kts );
65 double turn_rate = controls.get_aileron() * SGD_PI_4; // radians/sec
66 double turn = turn_rate * time_step;
68 // update euler angles
69 _set_Euler_Angles( get_Phi(), get_Theta(),
70 fmod(get_Psi() + turn, SGD_2PI) );
71 _set_Euler_Rates(0,0,0);
73 // update (lon/lat) position
74 double lat2, lon2, az2;
75 if ( speed > SG_EPSILON ) {
76 geo_direct_wgs_84 ( get_Altitude(),
77 get_Latitude() * SGD_RADIANS_TO_DEGREES,
78 get_Longitude() * SGD_RADIANS_TO_DEGREES,
79 get_Psi() * SGD_RADIANS_TO_DEGREES,
80 dist, &lat2, &lon2, &az2 );
82 _set_Longitude( lon2 * SGD_DEGREES_TO_RADIANS );
83 _set_Latitude( lat2 * SGD_DEGREES_TO_RADIANS );
86 // cout << "lon error = " << fabs(end.x()*SGD_RADIANS_TO_DEGREES - lon2)
87 // << " lat error = " << fabs(end.y()*SGD_RADIANS_TO_DEGREES - lat2)
90 double sl_radius, lat_geoc;
91 sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc );
94 double real_climb_rate = -controls.get_elevator() * 5000; // feet/sec
95 _set_Climb_Rate( real_climb_rate / 500.0 );
96 double climb = real_climb_rate * time_step;
98 _set_Geocentric_Position( lat_geoc, get_Longitude(),
99 sl_radius + get_Altitude() + climb );
100 // cout << "sea level radius (ft) = " << sl_radius << endl;
101 // cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl;
102 _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET);
103 _set_Altitude( get_Altitude() + climb );