]> git.mxchange.org Git - flightgear.git/blob - src/FDM/MagicCarpet.cxx
I tested:
[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
31 #include "MagicCarpet.hxx"
32
33
34 // Initialize the Magic Carpet flight model, dt is the time increment
35 // for each subsequent iteration through the EOM
36 bool FGMagicCarpet::init( double dt ) {
37     // set valid time for this record
38     stamp_time();
39
40     return true;
41 }
42
43
44 // Run an iteration of the EOM (equations of motion)
45 bool FGMagicCarpet::update( int multiloop ) {
46     // cout << "FGLaRCsim::update()" << endl;
47
48     double time_step = (1.0 / globals->get_options()->get_model_hz()) *
49         multiloop;
50
51     // speed and distance traveled
52     double speed = controls.get_throttle( 0 ) * 2000; // meters/sec
53     double dist = speed * time_step;
54     double kts = speed * METER_TO_NM * 3600.0;
55     _set_V_equiv_kts( kts );
56     _set_V_calibrated_kts( kts );
57     _set_V_ground_speed( kts );
58
59     // angle of turn
60     double turn_rate = controls.get_aileron() * FG_PI_4; // radians/sec
61     double turn = turn_rate * time_step;
62
63     // update euler angles
64     _set_Euler_Angles( get_Phi(), get_Theta(), fmod(get_Psi() + turn, FG_2PI) );
65     _set_Euler_Rates(0,0,0);
66
67     // update (lon/lat) position
68     double lat2, lon2, az2;
69     if ( speed > FG_EPSILON ) {
70         geo_direct_wgs_84 ( get_Altitude(),
71                             get_Latitude() * RAD_TO_DEG,
72                             get_Longitude() * RAD_TO_DEG,
73                             get_Psi() * RAD_TO_DEG,
74                             dist, &lat2, &lon2, &az2 );
75
76         _set_Longitude( lon2 * DEG_TO_RAD );
77         _set_Latitude( lat2 * DEG_TO_RAD );
78     }
79
80     // cout << "lon error = " << fabs(end.x()*RAD_TO_DEG - lon2)
81     //      << "  lat error = " << fabs(end.y()*RAD_TO_DEG - lat2)
82     //      << endl;
83
84     double sl_radius, lat_geoc;
85     sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc );
86
87     // update altitude
88     double real_climb_rate = -controls.get_elevator() * 5000; // feet/sec
89     _set_Climb_Rate( real_climb_rate / 500.0 );
90     double climb = real_climb_rate * time_step;
91
92     _set_Geocentric_Position( lat_geoc, get_Longitude(), 
93                              sl_radius + get_Altitude() + climb );
94     // cout << "sea level radius (ft) = " << sl_radius << endl;
95     // cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl;
96     _set_Sea_level_radius( sl_radius * METER_TO_FEET);
97     _set_Altitude( get_Altitude() + climb );
98
99     return true;
100 }