]> git.mxchange.org Git - flightgear.git/blob - FDM/LaRCsim.cxx
LaRCsim maintains all it's variables internally. I had been copying all of
[flightgear.git] / FDM / LaRCsim.cxx
1 // LaRCsim.cxx -- interface to the LaRCsim flight model
2 //
3 // Written by Curtis Olson, started October 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
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 "LaRCsim.hxx"
26
27 #include <Aircraft/aircraft.hxx>
28 #include <Controls/controls.hxx>
29 #include <Flight/flight.hxx>
30 #include <Flight/LaRCsim/ls_cockpit.h>
31 #include <Flight/LaRCsim/ls_generic.h>
32 #include <Flight/LaRCsim/ls_interface.h>
33
34
35 // Initialize the LaRCsim flight model, dt is the time increment for
36 // each subsequent iteration through the EOM
37 int fgLaRCsimInit(double dt) {
38     ls_toplevel_init(dt);
39
40     return(1);
41 }
42
43
44 // Run an iteration of the EOM (equations of motion)
45 int fgLaRCsimUpdate(FGState& f, int multiloop) {
46     double save_alt = 0.0;
47
48     // lets try to avoid really screwing up the LaRCsim model
49     if ( f.get_Altitude() < -9000 ) {
50         save_alt = f.get_Altitude();
51         f.set_Altitude( 0.0 );
52     }
53
54     // copy control positions into the LaRCsim structure
55     Lat_control = controls.get_aileron();
56     Long_control = controls.get_elevator();
57     Long_trim = controls.get_elevator_trim();
58     Rudder_pedal = controls.get_rudder();
59     Throttle_pct = controls.get_throttle( 0 );
60     Brake_pct = controls.get_brake( 0 );
61
62     // Inform LaRCsim of the local terrain altitude
63     Runway_altitude =   f.get_Runway_altitude();
64
65     // old -- FGstate_2_LaRCsim() not needed except for Init()
66     // translate FG to LaRCsim structure
67     // FGState_2_LaRCsim(f);
68     // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
69     // printf("Altitude = %.2f\n", Altitude * 0.3048);
70     // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
71
72     ls_update(multiloop);
73
74     // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
75     // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
76     
77     // translate LaRCsim back to FG structure so that the
78     // autopilot (and the rest of the sim can use the updated
79     // values
80     fgLaRCsim_2_FGState(f);
81
82     // but lets restore our original bogus altitude when we are done
83     if ( save_alt < -9000.0 ) {
84         f.set_Altitude( save_alt );
85     }
86
87     return 1;
88 }
89
90
91 // Convert from the FGState struct to the LaRCsim generic_ struct
92 int FGState_2_LaRCsim (FGState& f) {
93
94     Mass =      f.get_Mass();
95     I_xx =      f.get_I_xx();
96     I_yy =      f.get_I_yy();
97     I_zz =      f.get_I_zz();
98     I_xz =      f.get_I_xz();
99     Dx_pilot =  f.get_Dx_pilot();
100     Dy_pilot =  f.get_Dy_pilot();
101     Dz_pilot =  f.get_Dz_pilot();
102     Dx_cg =     f.get_Dx_cg();
103     Dy_cg =     f.get_Dy_cg();
104     Dz_cg =     f.get_Dz_cg();
105     F_X =       f.get_F_X();
106     F_Y =       f.get_F_Y();
107     F_Z =       f.get_F_Z();
108     F_north =   f.get_F_north();
109     F_east =    f.get_F_east();
110     F_down =    f.get_F_down();
111     F_X_aero =  f.get_F_X_aero();
112     F_Y_aero =  f.get_F_Y_aero();
113     F_Z_aero =  f.get_F_Z_aero();
114     F_X_engine =        f.get_F_X_engine();
115     F_Y_engine =        f.get_F_Y_engine();
116     F_Z_engine =        f.get_F_Z_engine();
117     F_X_gear =  f.get_F_X_gear();
118     F_Y_gear =  f.get_F_Y_gear();
119     F_Z_gear =  f.get_F_Z_gear();
120     M_l_rp =    f.get_M_l_rp();
121     M_m_rp =    f.get_M_m_rp();
122     M_n_rp =    f.get_M_n_rp();
123     M_l_cg =    f.get_M_l_cg();
124     M_m_cg =    f.get_M_m_cg();
125     M_n_cg =    f.get_M_n_cg();
126     M_l_aero =  f.get_M_l_aero();
127     M_m_aero =  f.get_M_m_aero();
128     M_n_aero =  f.get_M_n_aero();
129     M_l_engine =        f.get_M_l_engine();
130     M_m_engine =        f.get_M_m_engine();
131     M_n_engine =        f.get_M_n_engine();
132     M_l_gear =  f.get_M_l_gear();
133     M_m_gear =  f.get_M_m_gear();
134     M_n_gear =  f.get_M_n_gear();
135     V_dot_north =       f.get_V_dot_north();
136     V_dot_east =        f.get_V_dot_east();
137     V_dot_down =        f.get_V_dot_down();
138     U_dot_body =        f.get_U_dot_body();
139     V_dot_body =        f.get_V_dot_body();
140     W_dot_body =        f.get_W_dot_body();
141     A_X_cg =    f.get_A_X_cg();
142     A_Y_cg =    f.get_A_Y_cg();
143     A_Z_cg =    f.get_A_Z_cg();
144     A_X_pilot = f.get_A_X_pilot();
145     A_Y_pilot = f.get_A_Y_pilot();
146     A_Z_pilot = f.get_A_Z_pilot();
147     N_X_cg =    f.get_N_X_cg();
148     N_Y_cg =    f.get_N_Y_cg();
149     N_Z_cg =    f.get_N_Z_cg();
150     N_X_pilot = f.get_N_X_pilot();
151     N_Y_pilot = f.get_N_Y_pilot();
152     N_Z_pilot = f.get_N_Z_pilot();
153     P_dot_body =        f.get_P_dot_body();
154     Q_dot_body =        f.get_Q_dot_body();
155     R_dot_body =        f.get_R_dot_body();
156     V_north =   f.get_V_north();
157     V_east =    f.get_V_east();
158     V_down =    f.get_V_down();
159     V_north_rel_ground =        f.get_V_north_rel_ground();
160     V_east_rel_ground = f.get_V_east_rel_ground();
161     V_down_rel_ground = f.get_V_down_rel_ground();
162     V_north_airmass =   f.get_V_north_airmass();
163     V_east_airmass =    f.get_V_east_airmass();
164     V_down_airmass =    f.get_V_down_airmass();
165     V_north_rel_airmass =       f.get_V_north_rel_airmass();
166     V_east_rel_airmass =        f.get_V_east_rel_airmass();
167     V_down_rel_airmass =        f.get_V_down_rel_airmass();
168     U_gust =    f.get_U_gust();
169     V_gust =    f.get_V_gust();
170     W_gust =    f.get_W_gust();
171     U_body =    f.get_U_body();
172     V_body =    f.get_V_body();
173     W_body =    f.get_W_body();
174     V_rel_wind =        f.get_V_rel_wind();
175     V_true_kts =        f.get_V_true_kts();
176     V_rel_ground =      f.get_V_rel_ground();
177     V_inertial =        f.get_V_inertial();
178     V_ground_speed =    f.get_V_ground_speed();
179     V_equiv =   f.get_V_equiv();
180     V_equiv_kts =       f.get_V_equiv_kts();
181     V_calibrated =      f.get_V_calibrated();
182     V_calibrated_kts =  f.get_V_calibrated_kts();
183     P_body =    f.get_P_body();
184     Q_body =    f.get_Q_body();
185     R_body =    f.get_R_body();
186     P_local =   f.get_P_local();
187     Q_local =   f.get_Q_local();
188     R_local =   f.get_R_local();
189     P_total =   f.get_P_total();
190     Q_total =   f.get_Q_total();
191     R_total =   f.get_R_total();
192     Phi_dot =   f.get_Phi_dot();
193     Theta_dot = f.get_Theta_dot();
194     Psi_dot =   f.get_Psi_dot();
195     Latitude_dot =      f.get_Latitude_dot();
196     Longitude_dot =     f.get_Longitude_dot();
197     Radius_dot =        f.get_Radius_dot();
198     Lat_geocentric =    f.get_Lat_geocentric();
199     Lon_geocentric =    f.get_Lon_geocentric();
200     Radius_to_vehicle = f.get_Radius_to_vehicle();
201     Latitude =  f.get_Latitude();
202     Longitude = f.get_Longitude();
203     Altitude =  f.get_Altitude();
204     Phi =       f.get_Phi();
205     Theta =     f.get_Theta();
206     Psi =       f.get_Psi();
207     T_local_to_body_11 =        f.get_T_local_to_body_11();
208     T_local_to_body_12 =        f.get_T_local_to_body_12();
209     T_local_to_body_13 =        f.get_T_local_to_body_13();
210     T_local_to_body_21 =        f.get_T_local_to_body_21();
211     T_local_to_body_22 =        f.get_T_local_to_body_22();
212     T_local_to_body_23 =        f.get_T_local_to_body_23();
213     T_local_to_body_31 =        f.get_T_local_to_body_31();
214     T_local_to_body_32 =        f.get_T_local_to_body_32();
215     T_local_to_body_33 =        f.get_T_local_to_body_33();
216     Gravity =   f.get_Gravity();
217     Centrifugal_relief =        f.get_Centrifugal_relief();
218     Alpha =     f.get_Alpha();
219     Beta =      f.get_Beta();
220     Alpha_dot = f.get_Alpha_dot();
221     Beta_dot =  f.get_Beta_dot();
222     Cos_alpha = f.get_Cos_alpha();
223     Sin_alpha = f.get_Sin_alpha();
224     Cos_beta =  f.get_Cos_beta();
225     Sin_beta =  f.get_Sin_beta();
226     Cos_phi =   f.get_Cos_phi();
227     Sin_phi =   f.get_Sin_phi();
228     Cos_theta = f.get_Cos_theta();
229     Sin_theta = f.get_Sin_theta();
230     Cos_psi =   f.get_Cos_psi();
231     Sin_psi =   f.get_Sin_psi();
232     Gamma_vert_rad =    f.get_Gamma_vert_rad();
233     Gamma_horiz_rad =   f.get_Gamma_horiz_rad();
234     Sigma =     f.get_Sigma();
235     Density =   f.get_Density();
236     V_sound =   f.get_V_sound();
237     Mach_number =       f.get_Mach_number();
238     Static_pressure =   f.get_Static_pressure();
239     Total_pressure =    f.get_Total_pressure();
240     Impact_pressure =   f.get_Impact_pressure();
241     Dynamic_pressure =  f.get_Dynamic_pressure();
242     Static_temperature =        f.get_Static_temperature();
243     Total_temperature = f.get_Total_temperature();
244     Sea_level_radius =  f.get_Sea_level_radius();
245     Earth_position_angle =      f.get_Earth_position_angle();
246     Runway_altitude =   f.get_Runway_altitude();
247     Runway_latitude =   f.get_Runway_latitude();
248     Runway_longitude =  f.get_Runway_longitude();
249     Runway_heading =    f.get_Runway_heading();
250     Radius_to_rwy =     f.get_Radius_to_rwy();
251     D_cg_north_of_rwy = f.get_D_cg_north_of_rwy();
252     D_cg_east_of_rwy =  f.get_D_cg_east_of_rwy();
253     D_cg_above_rwy =    f.get_D_cg_above_rwy();
254     X_cg_rwy =  f.get_X_cg_rwy();
255     Y_cg_rwy =  f.get_Y_cg_rwy();
256     H_cg_rwy =  f.get_H_cg_rwy();
257     D_pilot_north_of_rwy =      f.get_D_pilot_north_of_rwy();
258     D_pilot_east_of_rwy =       f.get_D_pilot_east_of_rwy();
259     D_pilot_above_rwy = f.get_D_pilot_above_rwy();
260     X_pilot_rwy =       f.get_X_pilot_rwy();
261     Y_pilot_rwy =       f.get_Y_pilot_rwy();
262     H_pilot_rwy =       f.get_H_pilot_rwy();
263
264     return( 0 );
265 }
266
267
268 // Convert from the LaRCsim generic_ struct to the FGState struct
269 int fgLaRCsim_2_FGState (FGState& f) {
270     // Mass properties and geometry values
271     f.set_Inertias( Mass, I_xx, I_yy, I_zz, I_xz );
272     f.set_Pilot_Location( Dx_pilot, Dy_pilot, Dz_pilot );
273     f.set_CG_Position( Dx_cg, Dy_cg, Dz_cg );
274
275     // Forces
276     f.set_Forces_Body_Total( F_X, F_Y, F_Z );
277     f.set_Forces_Local_Total( F_north, F_east, F_down );
278     f.set_Forces_Aero( F_X_aero, F_Y_aero, F_Z_aero );
279     f.set_Forces_Engine( F_X_engine, F_Y_engine, F_Z_engine );
280     f.set_Forces_Gear( F_X_gear, F_Y_gear, F_Z_gear );
281
282     // Moments
283     f.set_Moments_Total_RP( M_l_rp, M_m_rp, M_n_rp );
284     f.set_Moments_Total_CG( M_l_cg, M_m_cg, M_n_cg );
285     f.set_Moments_Aero( M_l_aero, M_m_aero, M_n_aero );
286     f.set_Moments_Engine( M_l_engine, M_m_engine, M_n_engine );
287     f.set_Moments_Gear( M_l_gear, M_m_gear, M_n_gear );
288
289     // Accelerations
290     f.set_Accels_Local( V_dot_north, V_dot_east, V_dot_down );
291     f.set_Accels_Body( U_dot_body, V_dot_body, W_dot_body );
292     f.set_Accels_CG_Body( A_X_cg, A_Y_cg, A_Z_cg );
293     f.set_Accels_Pilot_Body( A_X_pilot, A_Y_pilot, A_Z_pilot );
294     f.set_Accels_CG_Body_N( N_X_cg, N_Y_cg, N_Z_cg );
295     f.set_Accels_Pilot_Body_N( N_X_pilot, N_Y_pilot, N_Z_pilot );
296     f.set_Accels_Omega( P_dot_body, Q_dot_body, R_dot_body );
297
298     // Velocities
299     f.set_Velocities_Local( V_north, V_east, V_down );
300     f.set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground, 
301                              V_down_rel_ground );
302     f.set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
303                                     V_down_airmass );
304     f.set_Velocities_Local_Rel_Airmass( V_north_rel_airmass, V_east_rel_airmass,
305                                         V_down_rel_airmass );
306     f.set_Velocities_Gust( U_gust, V_gust, W_gust );
307     f.set_Velocities_Wind_Body( U_body, V_body, W_body );
308
309     f.set_V_rel_wind( V_rel_wind );
310     f.set_V_true_kts( V_true_kts );
311     f.set_V_rel_ground( V_rel_ground );
312     f.set_V_inertial( V_inertial );
313     f.set_V_ground_speed( V_ground_speed );
314     f.set_V_equiv( V_equiv );
315     f.set_V_equiv_kts( V_equiv_kts );
316     f.set_V_calibrated( V_calibrated );
317     f.set_V_calibrated_kts( V_calibrated_kts );
318
319     f.set_Omega_Body( P_body, Q_body, R_body );
320     f.set_Omega_Local( P_local, Q_local, R_local );
321     f.set_Omega_Total( P_total, Q_total, R_total );
322     
323     f.set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
324     f.set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
325
326     // Positions
327     f.set_Geocentric_Position( Lat_geocentric, Lon_geocentric, 
328                                Radius_to_vehicle );
329     f.set_Geodetic_Position( Latitude, Longitude, Altitude );
330     f.set_Euler_Angles( Phi, Theta, Psi );
331
332     // Miscellaneous quantities
333     f.set_T_Local_to_Body(T_local_to_body_m);
334     f.set_Gravity( Gravity );
335     f.set_Centrifugal_relief( Centrifugal_relief );
336
337     f.set_Alpha( Alpha );
338     f.set_Beta( Beta );
339     f.set_Alpha_dot( Alpha_dot );
340     f.set_Beta_dot( Beta_dot );
341
342     f.set_Cos_alpha( Cos_alpha );
343     f.set_Sin_alpha( Sin_alpha );
344     f.set_Cos_beta( Cos_beta );
345     f.set_Sin_beta( Sin_beta );
346
347     f.set_Cos_phi( Cos_phi );
348     f.set_Sin_phi( Sin_phi );
349     f.set_Cos_theta( Cos_theta );
350     f.set_Sin_theta( Sin_theta );
351     f.set_Cos_psi( Cos_psi );
352     f.set_Sin_psi( Sin_psi );
353
354     f.set_Gamma_vert_rad( Gamma_vert_rad );
355     f.set_Gamma_horiz_rad( Gamma_horiz_rad );
356
357     f.set_Sigma( Sigma );
358     f.set_Density( Density );
359     f.set_V_sound( V_sound );
360     f.set_Mach_number( Mach_number );
361
362     f.set_Static_pressure( Static_pressure );
363     f.set_Total_pressure( Total_pressure );
364     f.set_Impact_pressure( Impact_pressure );
365     f.set_Dynamic_pressure( Dynamic_pressure );
366
367     f.set_Static_temperature( Static_temperature );
368     f.set_Total_temperature( Total_temperature );
369
370     f.set_Sea_level_radius( Sea_level_radius );
371     f.set_Earth_position_angle( Earth_position_angle );
372
373     f.set_Runway_altitude( Runway_altitude );
374     f.set_Runway_latitude( Runway_latitude );
375     f.set_Runway_longitude( Runway_longitude );
376     f.set_Runway_heading( Runway_heading );
377     f.set_Radius_to_rwy( Radius_to_rwy );
378
379     f.set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy );
380     f.set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
381     f.set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy, 
382                            D_pilot_above_rwy );
383     f.set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
384
385     return 0;
386 }
387
388
389 // $Log$
390 // Revision 1.7  1998/12/14 13:31:06  curt
391 // LaRCsim maintains all it's variables internally.  I had been copying all of
392 // them back and forth to the FG struture everytime I updated the flight model.
393 // However, I have realized that this is not necessary.  I just need to copy
394 // the control positions and environmental parameters into the LaRCsim structure
395 // before updating the FDM, then copy every thing back out into the publick FGFS
396 // structure afterwords.  This seems to solve (or at least help) a westward
397 // drift problem some poeple had been observing.
398 //
399 // Revision 1.6  1998/12/05 15:54:08  curt
400 // Renamed class fgFLIGHT to class FGState as per request by JSB.
401 //
402 // Revision 1.5  1998/12/03 04:25:02  curt
403 // Working on fixing up new fgFLIGHT class.
404 //
405 // Revision 1.4  1998/12/03 01:16:37  curt
406 // Converted fgFLIGHT to a class.
407 //
408 // Revision 1.3  1998/10/25 14:08:43  curt
409 // Turned "struct fgCONTROLS" into a class, with inlined accessor functions.
410 //
411 // Revision 1.2  1998/10/17 01:34:11  curt
412 // C++ ifying ...
413 //
414 // Revision 1.1  1998/10/17 00:43:58  curt
415 // Initial revision.
416 //
417 //