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