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