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