]> git.mxchange.org Git - flightgear.git/blob - FDM/flight.cxx
cadf164c7d2748b1ef3e99e4060f2892ba73b0ba
[flightgear.git] / FDM / flight.cxx
1 // flight.c -- a general interface to the various flight models
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
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 // (Log is kept at end of this file)
23
24
25 #include <stdio.h>
26
27 #include "flight.hxx"
28 #include "LaRCsim.hxx"
29
30 #include <Debug/logstream.hxx>
31 #include <Flight/External/external.hxx>
32 #include <Flight/LaRCsim/ls_interface.h>
33 #include <Include/fg_constants.h>
34 #include <Math/fg_geodesy.hxx>
35
36
37 FGState cur_fdm_state;
38
39
40 // Initialize the flight model parameters
41 int fgFlightModelInit(int model, FGState& f, double dt) {
42     double save_alt = 0.0;
43     int result;
44
45     FG_LOG( FG_FLIGHT ,FG_INFO, "Initializing flight model" );
46
47     if ( model == FGState::FG_SLEW ) {
48         // fgSlewInit(dt);
49     } else if ( model == FGState::FG_LARCSIM ) {
50         // lets try to avoid really screwing up the LaRCsim model
51         if ( f.get_Altitude() < -9000.0 ) {
52             save_alt = f.get_Altitude();
53             f.set_Altitude( 0.0 );
54         }
55
56         FGState_2_LaRCsim(f);  // translate FG to LaRCsim structure
57         fgLaRCsimInit(dt);
58         FG_LOG( FG_FLIGHT, FG_INFO, "FG pos = " << f.get_Latitude() );
59         fgLaRCsim_2_FGState(f);  // translate LaRCsim back to FG structure
60
61         // but lets restore our original bogus altitude when we are done
62         if ( save_alt < -9000.0 ) {
63             f.set_Altitude( save_alt );
64         }
65     } else if ( model == FGState::FG_EXTERNAL ) {
66         fgExternalInit(f, dt);
67     } else {
68         FG_LOG( FG_FLIGHT, FG_WARN,
69                   "Unimplemented flight model == " << model );
70     }
71
72     result = 1;
73
74     return(result);
75 }
76
77
78 // Run multiloop iterations of the flight model
79 int fgFlightModelUpdate(int model, FGState& f, int multiloop) {
80     double time_step, start_elev, end_elev;
81     int result;
82     // printf("Altitude = %.2f\n", FG_Altitude * 0.3048);
83
84     time_step = (1.0 / DEFAULT_MODEL_HZ) * multiloop;
85     start_elev = f.get_Altitude();
86
87     if ( model == FGState::FG_SLEW ) {
88         // fgSlewUpdate(f, multiloop);
89     } else if ( model == FGState::FG_LARCSIM ) {
90         fgLaRCsimUpdate(f, multiloop);
91     } else if ( model == FGState::FG_EXTERNAL ) {
92         // fgExternalUpdate(f, multiloop);
93     } else {
94         FG_LOG( FG_FLIGHT, FG_WARN,
95                 "Unimplemented flight model == " <<  model );
96     }
97
98     end_elev = f.get_Altitude();
99
100     if ( time_step > 0.0 ) {
101         // feet per second
102         f.set_Climb_Rate( (end_elev - start_elev) / time_step );
103     }
104
105     result = 1;
106
107     return(result);
108 }
109
110
111 // Set the altitude (force)
112 void fgFlightModelSetAltitude(int model, FGState& f, double alt_meters) {
113     double sea_level_radius_meters;
114     double lat_geoc;
115     // Set the FG variables first
116     fgGeodToGeoc( f.get_Latitude(), alt_meters, 
117                   &sea_level_radius_meters, &lat_geoc);
118
119     f.set_Altitude( alt_meters * METER_TO_FEET );
120     f.set_Radius_to_vehicle( f.get_Altitude() + 
121                               (sea_level_radius_meters * METER_TO_FEET) );
122
123     // additional work needed for some flight models
124     if ( model == FGState::FG_LARCSIM ) {
125         ls_ForceAltitude( f.get_Altitude() );
126     }
127
128 }
129
130
131 // $Log$
132 // Revision 1.7  1998/12/18 23:37:07  curt
133 // Collapsed out the FGState variables not currently needed.  They are just
134 // commented out and can be readded easily at any time.  The point of this
135 // exersize is to determine which variables were or were not currently being
136 // used.
137 //
138 // Revision 1.6  1998/12/05 15:54:11  curt
139 // Renamed class fgFLIGHT to class FGState as per request by JSB.
140 //
141 // Revision 1.5  1998/12/04 01:29:39  curt
142 // Stubbed in a new flight model called "External" which is expected to be driven
143 // from some external source.
144 //
145 // Revision 1.4  1998/12/03 01:16:40  curt
146 // Converted fgFLIGHT to a class.
147 //
148 // Revision 1.3  1998/11/06 21:18:03  curt
149 // Converted to new logstream debugging facility.  This allows release
150 // builds with no messages at all (and no performance impact) by using
151 // the -DFG_NDEBUGNDEBUG flag.
152 //
153 // Revision 1.2  1998/10/16 23:27:40  curt
154 // C++-ifying.
155 //
156 // Revision 1.1  1998/10/16 20:16:41  curt
157 // Renamed flight.[ch] to flight.[ch]xx
158 //
159 // Revision 1.19  1998/09/29 14:57:38  curt
160 // c++-ified comments.
161 //
162 // Revision 1.18  1998/09/29 02:02:40  curt
163 // Added a rate of climb calculation.
164 //
165 // Revision 1.17  1998/08/24 20:09:07  curt
166 // .
167 //
168 // Revision 1.16  1998/08/22  14:49:55  curt
169 // Attempting to iron out seg faults and crashes.
170 // Did some shuffling to fix a initialization order problem between view
171 // position, scenery elevation.
172 //
173 // Revision 1.15  1998/07/30 23:44:36  curt
174 // Beginning to add support for multiple flight models.
175 //
176 // Revision 1.14  1998/07/12 03:08:27  curt
177 // Added fgFlightModelSetAltitude() to force the altitude to something
178 // other than the current altitude.  LaRCsim doesn't let you do this by just
179 // changing FG_Altitude.
180 //
181 // Revision 1.13  1998/04/25 22:06:28  curt
182 // Edited cvs log messages in source files ... bad bad bad!
183 //
184 // Revision 1.12  1998/04/21 16:59:33  curt
185 // Integrated autopilot.
186 // Prepairing for C++ integration.
187 //
188 // Revision 1.11  1998/04/18 04:14:04  curt
189 // Moved fg_debug.c to it's own library.
190 //
191 // Revision 1.10  1998/02/07 15:29:37  curt
192 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
193 // <chotchkiss@namg.us.anritsu.com>
194 //
195 // Revision 1.9  1998/01/27 00:47:53  curt
196 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
197 // system and commandline/config file processing code.
198 //
199 // Revision 1.8  1998/01/19 19:27:03  curt
200 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
201 // This should simplify things tremendously.
202 //
203 // Revision 1.7  1998/01/19 18:40:23  curt
204 // Tons of little changes to clean up the code and to remove fatal errors
205 // when building with the c++ compiler.
206 //
207 // Revision 1.6  1998/01/19 18:35:43  curt
208 // Minor tweaks and fixes for cygwin32.
209 //
210 // Revision 1.5  1997/12/30 20:47:37  curt
211 // Integrated new event manager with subsystem initializations.
212 //
213 // Revision 1.4  1997/12/10 22:37:42  curt
214 // Prepended "fg" on the name of all global structures that didn't have it yet.
215 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
216 //
217 // Revision 1.3  1997/08/27 03:30:04  curt
218 // Changed naming scheme of basic shared structures.
219 //
220 // Revision 1.2  1997/05/29 22:39:57  curt
221 // Working on incorporating the LaRCsim flight model.
222 //
223 // Revision 1.1  1997/05/29 02:35:04  curt
224 // Initial revision.
225 //