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