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