]> git.mxchange.org Git - flightgear.git/blob - src/FDM/MagicCarpet.cxx
David Megginson writes:
[flightgear.git] / src / FDM / MagicCarpet.cxx
1 // MagicCarpet.cxx -- interface to the "Magic Carpet" flight model
2 //
3 // Written by Curtis Olson, started October 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #include <simgear/math/sg_geodesy.hxx>
25 #include <simgear/math/point3d.hxx>
26 #include <simgear/math/polar3d.hxx>
27
28 #include <Controls/controls.hxx>
29 #include <Main/globals.hxx>
30 #include <Main/fg_props.hxx>
31
32 #include "MagicCarpet.hxx"
33
34
35 // Initialize the Magic Carpet flight model, dt is the time increment
36 // for each subsequent iteration through the EOM
37 bool FGMagicCarpet::init( double dt ) {
38     // set valid time for this record
39     stamp_time();
40     model_hz = fgGetValue("/sim/model-hz", true);
41
42     return true;
43 }
44
45
46 // Run an iteration of the EOM (equations of motion)
47 bool FGMagicCarpet::update( int multiloop ) {
48     // cout << "FGLaRCsim::update()" << endl;
49
50     double time_step = (1.0 / model_hz->getIntValue()) *
51         multiloop;
52
53     // speed and distance traveled
54     double speed = controls.get_throttle( 0 ) * 2000; // meters/sec
55     double dist = speed * time_step;
56     double kts = speed * METER_TO_NM * 3600.0;
57     _set_V_equiv_kts( kts );
58     _set_V_calibrated_kts( kts );
59     _set_V_ground_speed( kts );
60
61     // angle of turn
62     double turn_rate = controls.get_aileron() * FG_PI_4; // radians/sec
63     double turn = turn_rate * time_step;
64
65     // update euler angles
66     _set_Euler_Angles( get_Phi(), get_Theta(), fmod(get_Psi() + turn, FG_2PI) );
67     _set_Euler_Rates(0,0,0);
68
69     // update (lon/lat) position
70     double lat2, lon2, az2;
71     if ( speed > FG_EPSILON ) {
72         geo_direct_wgs_84 ( get_Altitude(),
73                             get_Latitude() * RAD_TO_DEG,
74                             get_Longitude() * RAD_TO_DEG,
75                             get_Psi() * RAD_TO_DEG,
76                             dist, &lat2, &lon2, &az2 );
77
78         _set_Longitude( lon2 * DEG_TO_RAD );
79         _set_Latitude( lat2 * DEG_TO_RAD );
80     }
81
82     // cout << "lon error = " << fabs(end.x()*RAD_TO_DEG - lon2)
83     //      << "  lat error = " << fabs(end.y()*RAD_TO_DEG - lat2)
84     //      << endl;
85
86     double sl_radius, lat_geoc;
87     sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc );
88
89     // update altitude
90     double real_climb_rate = -controls.get_elevator() * 5000; // feet/sec
91     _set_Climb_Rate( real_climb_rate / 500.0 );
92     double climb = real_climb_rate * time_step;
93
94     _set_Geocentric_Position( lat_geoc, get_Longitude(), 
95                              sl_radius + get_Altitude() + climb );
96     // cout << "sea level radius (ft) = " << sl_radius << endl;
97     // cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl;
98     _set_Sea_level_radius( sl_radius * METER_TO_FEET);
99     _set_Altitude( get_Altitude() + climb );
100
101     return true;
102 }