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