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