]> git.mxchange.org Git - flightgear.git/blob - src/FDM/SP/MagicCarpet.cxx
Merge branch 'maint2' into next
[flightgear.git] / src / FDM / SP / 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  - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/math/sg_geodesy.hxx>
29 #include <simgear/math/polar3d.hxx>
30
31 #include <Aircraft/controls.hxx>
32 #include <Main/globals.hxx>
33 #include <Main/fg_props.hxx>
34
35 #include "MagicCarpet.hxx"
36
37
38 FGMagicCarpet::FGMagicCarpet( double dt ) {
39 //     set_delta_t( dt );
40 }
41
42
43 FGMagicCarpet::~FGMagicCarpet() {
44 }
45
46
47 // Initialize the Magic Carpet flight model, dt is the time increment
48 // for each subsequent iteration through the EOM
49 void FGMagicCarpet::init() {
50     common_init();
51 }
52
53
54 // Run an iteration of the EOM (equations of motion)
55 void FGMagicCarpet::update( double dt ) {
56     // cout << "FGLaRCsim::update()" << endl;
57
58     if (is_suspended())
59       return;
60
61     // int multiloop = _calc_multiloop(dt);
62
63     double time_step = dt;
64
65     // speed and distance traveled
66     double speed = globals->get_controls()->get_throttle( 0 ) * 2000; // meters/sec
67     if ( globals->get_controls()->get_brake_left() > 0.0
68          || globals->get_controls()->get_brake_right() > 0.0 )
69     {
70         speed = -speed;
71     }
72
73     double dist = speed * time_step;
74     double kts = speed * SG_METER_TO_NM * 3600.0;
75     _set_V_equiv_kts( kts );
76     _set_V_calibrated_kts( kts );
77     _set_V_ground_speed( kts );
78
79     // angle of turn
80     double turn_rate = globals->get_controls()->get_aileron() * SGD_PI_4; // radians/sec
81     double turn = turn_rate * time_step;
82
83     // update euler angles
84     _set_Euler_Angles( get_Phi(), get_Theta(),
85                        fmod(get_Psi() + turn, SGD_2PI) );
86     _set_Euler_Rates(0,0,0);
87
88     // update (lon/lat) position
89     double lat2, lon2, az2;
90     if ( fabs( speed ) > SG_EPSILON ) {
91         geo_direct_wgs_84 ( get_Altitude(),
92                             get_Latitude() * SGD_RADIANS_TO_DEGREES,
93                             get_Longitude() * SGD_RADIANS_TO_DEGREES,
94                             get_Psi() * SGD_RADIANS_TO_DEGREES,
95                             dist, &lat2, &lon2, &az2 );
96
97         _set_Longitude( lon2 * SGD_DEGREES_TO_RADIANS );
98         _set_Latitude( lat2 * SGD_DEGREES_TO_RADIANS );
99     }
100
101     // cout << "lon error = " << fabs(end.x()*SGD_RADIANS_TO_DEGREES - lon2)
102     //      << "  lat error = " << fabs(end.y()*SGD_RADIANS_TO_DEGREES - lat2)
103     //      << endl;
104
105     double sl_radius, lat_geoc;
106     sgGeodToGeoc( get_Latitude(), get_Altitude(), &sl_radius, &lat_geoc );
107
108     // update altitude
109     double real_climb_rate = -globals->get_controls()->get_elevator() * 5000; // feet/sec
110     _set_Climb_Rate( real_climb_rate / 500.0 );
111     double climb = real_climb_rate * time_step;
112
113     _set_Geocentric_Position( lat_geoc, get_Longitude(), 
114                              sl_radius + get_Altitude() + climb );
115     // cout << "sea level radius (ft) = " << sl_radius << endl;
116     // cout << "(setto) sea level radius (ft) = " << get_Sea_level_radius() << endl;
117     _update_ground_elev_at_pos();
118     _set_Sea_level_radius( sl_radius * SG_METER_TO_FEET);
119     _set_Altitude( get_Altitude() + climb );
120 }