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