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