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.
22 // (Log is kept at end of this file)
25 #include "LaRCsim.hxx"
27 #include <Aircraft/aircraft.hxx>
28 #include <Controls/controls.hxx>
29 #include <Flight/flight.hxx>
30 #include <Flight/LaRCsim/ls_cockpit.h>
31 #include <Flight/LaRCsim/ls_generic.h>
32 #include <Flight/LaRCsim/ls_interface.h>
35 // Initialize the LaRCsim flight model, dt is the time increment for
36 // each subsequent iteration through the EOM
37 int fgLaRCsimInit(double dt) {
44 // Run an iteration of the EOM (equations of motion)
45 int fgLaRCsimUpdate(fgFLIGHT& f, int multiloop) {
46 double save_alt = 0.0;
48 // lets try to avoid really screwing up the LaRCsim model
49 if ( f.get_Altitude() < -9000 ) {
50 save_alt = f.get_Altitude();
51 f.set_Altitude( 0.0 );
54 // translate FG to LaRCsim structure
55 fgFlight_2_LaRCsim(f);
56 // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
57 // printf("Altitude = %.2f\n", Altitude * 0.3048);
58 // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
62 // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
63 // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
65 // translate LaRCsim back to FG structure so that the
66 // autopilot (and the rest of the sim can use the updated
68 fgLaRCsim_2_Flight(f);
70 // but lets restore our original bogus altitude when we are done
71 if ( save_alt < -9000.0 ) {
72 f.set_Altitude( save_alt );
79 // Convert from the fgFLIGHT struct to the LaRCsim generic_ struct
80 int fgFlight_2_LaRCsim (fgFLIGHT& f) {
82 Lat_control = controls.get_aileron();
83 Long_control = controls.get_elevator();
84 Long_trim = controls.get_elevator_trim();
85 Rudder_pedal = controls.get_rudder();
86 Throttle_pct = controls.get_throttle( 0 );
87 Brake_pct = controls.get_brake( 0 );
94 Dx_pilot = f.get_Dx_pilot();
95 Dy_pilot = f.get_Dy_pilot();
96 Dz_pilot = f.get_Dz_pilot();
97 Dx_cg = f.get_Dx_cg();
98 Dy_cg = f.get_Dy_cg();
99 Dz_cg = f.get_Dz_cg();
103 F_north = f.get_F_north();
104 F_east = f.get_F_east();
105 F_down = f.get_F_down();
106 F_X_aero = f.get_F_X_aero();
107 F_Y_aero = f.get_F_Y_aero();
108 F_Z_aero = f.get_F_Z_aero();
109 F_X_engine = f.get_F_X_engine();
110 F_Y_engine = f.get_F_Y_engine();
111 F_Z_engine = f.get_F_Z_engine();
112 F_X_gear = f.get_F_X_gear();
113 F_Y_gear = f.get_F_Y_gear();
114 F_Z_gear = f.get_F_Z_gear();
115 M_l_rp = f.get_M_l_rp();
116 M_m_rp = f.get_M_m_rp();
117 M_n_rp = f.get_M_n_rp();
118 M_l_cg = f.get_M_l_cg();
119 M_m_cg = f.get_M_m_cg();
120 M_n_cg = f.get_M_n_cg();
121 M_l_aero = f.get_M_l_aero();
122 M_m_aero = f.get_M_m_aero();
123 M_n_aero = f.get_M_n_aero();
124 M_l_engine = f.get_M_l_engine();
125 M_m_engine = f.get_M_m_engine();
126 M_n_engine = f.get_M_n_engine();
127 M_l_gear = f.get_M_l_gear();
128 M_m_gear = f.get_M_m_gear();
129 M_n_gear = f.get_M_n_gear();
130 V_dot_north = f.get_V_dot_north();
131 V_dot_east = f.get_V_dot_east();
132 V_dot_down = f.get_V_dot_down();
133 U_dot_body = f.get_U_dot_body();
134 V_dot_body = f.get_V_dot_body();
135 W_dot_body = f.get_W_dot_body();
136 A_X_cg = f.get_A_X_cg();
137 A_Y_cg = f.get_A_Y_cg();
138 A_Z_cg = f.get_A_Z_cg();
139 A_X_pilot = f.get_A_X_pilot();
140 A_Y_pilot = f.get_A_Y_pilot();
141 A_Z_pilot = f.get_A_Z_pilot();
142 N_X_cg = f.get_N_X_cg();
143 N_Y_cg = f.get_N_Y_cg();
144 N_Z_cg = f.get_N_Z_cg();
145 N_X_pilot = f.get_N_X_pilot();
146 N_Y_pilot = f.get_N_Y_pilot();
147 N_Z_pilot = f.get_N_Z_pilot();
148 P_dot_body = f.get_P_dot_body();
149 Q_dot_body = f.get_Q_dot_body();
150 R_dot_body = f.get_R_dot_body();
151 V_north = f.get_V_north();
152 V_east = f.get_V_east();
153 V_down = f.get_V_down();
154 V_north_rel_ground = f.get_V_north_rel_ground();
155 V_east_rel_ground = f.get_V_east_rel_ground();
156 V_down_rel_ground = f.get_V_down_rel_ground();
157 V_north_airmass = f.get_V_north_airmass();
158 V_east_airmass = f.get_V_east_airmass();
159 V_down_airmass = f.get_V_down_airmass();
160 V_north_rel_airmass = f.get_V_north_rel_airmass();
161 V_east_rel_airmass = f.get_V_east_rel_airmass();
162 V_down_rel_airmass = f.get_V_down_rel_airmass();
163 U_gust = f.get_U_gust();
164 V_gust = f.get_V_gust();
165 W_gust = f.get_W_gust();
166 U_body = f.get_U_body();
167 V_body = f.get_V_body();
168 W_body = f.get_W_body();
169 V_rel_wind = f.get_V_rel_wind();
170 V_true_kts = f.get_V_true_kts();
171 V_rel_ground = f.get_V_rel_ground();
172 V_inertial = f.get_V_inertial();
173 V_ground_speed = f.get_V_ground_speed();
174 V_equiv = f.get_V_equiv();
175 V_equiv_kts = f.get_V_equiv_kts();
176 V_calibrated = f.get_V_calibrated();
177 V_calibrated_kts = f.get_V_calibrated_kts();
178 P_body = f.get_P_body();
179 Q_body = f.get_Q_body();
180 R_body = f.get_R_body();
181 P_local = f.get_P_local();
182 Q_local = f.get_Q_local();
183 R_local = f.get_R_local();
184 P_total = f.get_P_total();
185 Q_total = f.get_Q_total();
186 R_total = f.get_R_total();
187 Phi_dot = f.get_Phi_dot();
188 Theta_dot = f.get_Theta_dot();
189 Psi_dot = f.get_Psi_dot();
190 Latitude_dot = f.get_Latitude_dot();
191 Longitude_dot = f.get_Longitude_dot();
192 Radius_dot = f.get_Radius_dot();
193 Lat_geocentric = f.get_Lat_geocentric();
194 Lon_geocentric = f.get_Lon_geocentric();
195 Radius_to_vehicle = f.get_Radius_to_vehicle();
196 Latitude = f.get_Latitude();
197 Longitude = f.get_Longitude();
198 Altitude = f.get_Altitude();
200 Theta = f.get_Theta();
202 T_local_to_body_11 = f.get_T_local_to_body_11();
203 T_local_to_body_12 = f.get_T_local_to_body_12();
204 T_local_to_body_13 = f.get_T_local_to_body_13();
205 T_local_to_body_21 = f.get_T_local_to_body_21();
206 T_local_to_body_22 = f.get_T_local_to_body_22();
207 T_local_to_body_23 = f.get_T_local_to_body_23();
208 T_local_to_body_31 = f.get_T_local_to_body_31();
209 T_local_to_body_32 = f.get_T_local_to_body_32();
210 T_local_to_body_33 = f.get_T_local_to_body_33();
211 Gravity = f.get_Gravity();
212 Centrifugal_relief = f.get_Centrifugal_relief();
213 Alpha = f.get_Alpha();
215 Alpha_dot = f.get_Alpha_dot();
216 Beta_dot = f.get_Beta_dot();
217 Cos_alpha = f.get_Cos_alpha();
218 Sin_alpha = f.get_Sin_alpha();
219 Cos_beta = f.get_Cos_beta();
220 Sin_beta = f.get_Sin_beta();
221 Cos_phi = f.get_Cos_phi();
222 Sin_phi = f.get_Sin_phi();
223 Cos_theta = f.get_Cos_theta();
224 Sin_theta = f.get_Sin_theta();
225 Cos_psi = f.get_Cos_psi();
226 Sin_psi = f.get_Sin_psi();
227 Gamma_vert_rad = f.get_Gamma_vert_rad();
228 Gamma_horiz_rad = f.get_Gamma_horiz_rad();
229 Sigma = f.get_Sigma();
230 Density = f.get_Density();
231 V_sound = f.get_V_sound();
232 Mach_number = f.get_Mach_number();
233 Static_pressure = f.get_Static_pressure();
234 Total_pressure = f.get_Total_pressure();
235 Impact_pressure = f.get_Impact_pressure();
236 Dynamic_pressure = f.get_Dynamic_pressure();
237 Static_temperature = f.get_Static_temperature();
238 Total_temperature = f.get_Total_temperature();
239 Sea_level_radius = f.get_Sea_level_radius();
240 Earth_position_angle = f.get_Earth_position_angle();
241 Runway_altitude = f.get_Runway_altitude();
242 Runway_latitude = f.get_Runway_latitude();
243 Runway_longitude = f.get_Runway_longitude();
244 Runway_heading = f.get_Runway_heading();
245 Radius_to_rwy = f.get_Radius_to_rwy();
246 D_cg_north_of_rwy = f.get_D_cg_north_of_rwy();
247 D_cg_east_of_rwy = f.get_D_cg_east_of_rwy();
248 D_cg_above_rwy = f.get_D_cg_above_rwy();
249 X_cg_rwy = f.get_X_cg_rwy();
250 Y_cg_rwy = f.get_Y_cg_rwy();
251 H_cg_rwy = f.get_H_cg_rwy();
252 D_pilot_north_of_rwy = f.get_D_pilot_north_of_rwy();
253 D_pilot_east_of_rwy = f.get_D_pilot_east_of_rwy();
254 D_pilot_above_rwy = f.get_D_pilot_above_rwy();
255 X_pilot_rwy = f.get_X_pilot_rwy();
256 Y_pilot_rwy = f.get_Y_pilot_rwy();
257 H_pilot_rwy = f.get_H_pilot_rwy();
263 // Convert from the LaRCsim generic_ struct to the fgFLIGHT struct
264 int fgLaRCsim_2_Flight (fgFLIGHT& f) {
265 // Mass properties and geometry values
266 f.set_Inertias( Mass, I_xx, I_yy, I_zz, I_xz );
267 f.set_Pilot_Location( Dx_pilot, Dy_pilot, Dz_pilot );
268 f.set_CG_Position( Dx_cg, Dy_cg, Dz_cg );
271 f.set_Forces_Body_Total( F_X, F_Y, F_Z );
272 f.set_Forces_Local_Total( F_north, F_east, F_down );
273 f.set_Forces_Aero( F_X_aero, F_Y_aero, F_Z_aero );
274 f.set_Forces_Engine( F_X_engine, F_Y_engine, F_Z_engine );
275 f.set_Forces_Gear( F_X_gear, F_Y_gear, F_Z_gear );
278 f.set_Moments_Total_RP( M_l_rp, M_m_rp, M_n_rp );
279 f.set_Moments_Total_CG( M_l_cg, M_m_cg, M_n_cg );
280 f.set_Moments_Aero( M_l_aero, M_m_aero, M_n_aero );
281 f.set_Moments_Engine( M_l_engine, M_m_engine, M_n_engine );
282 f.set_Moments_Gear( M_l_gear, M_m_gear, M_n_gear );
285 f.set_Accels_Local( V_dot_north, V_dot_east, V_dot_down );
286 f.set_Accels_Body( U_dot_body, V_dot_body, W_dot_body );
287 f.set_Accels_CG_Body( A_X_cg, A_Y_cg, A_Z_cg );
288 f.set_Accels_Pilot_Body( A_X_pilot, A_Y_pilot, A_Z_pilot );
289 f.set_Accels_CG_Body_N( N_X_cg, N_Y_cg, N_Z_cg );
290 f.set_Accels_Pilot_Body_N( N_X_pilot, N_Y_pilot, N_Z_pilot );
291 f.set_Accels_Omega( P_dot_body, Q_dot_body, R_dot_body );
294 f.set_Velocities_Local( V_north, V_east, V_down );
295 f.set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground,
297 f.set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
299 f.set_Velocities_Local_Rel_Airmass( V_north_rel_airmass, V_east_rel_airmass,
300 V_down_rel_airmass );
301 f.set_Velocities_Gust( U_gust, V_gust, W_gust );
302 f.set_Velocities_Wind_Body( U_body, V_body, W_body );
304 f.set_V_rel_wind( V_rel_wind );
305 f.set_V_true_kts( V_true_kts );
306 f.set_V_rel_ground( V_rel_ground );
307 f.set_V_inertial( V_inertial );
308 f.set_V_ground_speed( V_ground_speed );
309 f.set_V_equiv( V_equiv );
310 f.set_V_equiv_kts( V_equiv_kts );
311 f.set_V_calibrated( V_calibrated );
312 f.set_V_calibrated_kts( V_calibrated_kts );
314 f.set_Omega_Body( P_body, Q_body, R_body );
315 f.set_Omega_Local( P_local, Q_local, R_local );
316 f.set_Omega_Total( P_total, Q_total, R_total );
318 f.set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
319 f.set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
322 f.set_Geocentric_Position( Lat_geocentric, Lon_geocentric,
324 f.set_Geodetic_Position( Latitude, Longitude, Altitude );
325 f.set_Euler_Angles( Phi, Theta, Psi );
327 // Miscellaneous quantities
328 f.set_T_Local_to_Body(T_local_to_body_m);
329 f.set_Gravity( Gravity );
330 f.set_Centrifugal_relief( Centrifugal_relief );
332 f.set_Alpha( Alpha );
334 f.set_Alpha_dot( Alpha_dot );
335 f.set_Beta_dot( Beta_dot );
337 f.set_Cos_alpha( Cos_alpha );
338 f.set_Sin_alpha( Sin_alpha );
339 f.set_Cos_beta( Cos_beta );
340 f.set_Sin_beta( Sin_beta );
342 f.set_Cos_phi( Cos_phi );
343 f.set_Sin_phi( Sin_phi );
344 f.set_Cos_theta( Cos_theta );
345 f.set_Sin_theta( Sin_theta );
346 f.set_Cos_psi( Cos_psi );
347 f.set_Sin_psi( Sin_psi );
349 f.set_Gamma_vert_rad( Gamma_vert_rad );
350 f.set_Gamma_horiz_rad( Gamma_horiz_rad );
352 f.set_Sigma( Sigma );
353 f.set_Density( Density );
354 f.set_V_sound( V_sound );
355 f.set_Mach_number( Mach_number );
357 f.set_Static_pressure( Static_pressure );
358 f.set_Total_pressure( Total_pressure );
359 f.set_Impact_pressure( Impact_pressure );
360 f.set_Dynamic_pressure( Dynamic_pressure );
362 f.set_Static_temperature( Static_temperature );
363 f.set_Total_temperature( Total_temperature );
365 f.set_Sea_level_radius( Sea_level_radius );
366 f.set_Earth_position_angle( Earth_position_angle );
368 f.set_Runway_altitude( Runway_altitude );
369 f.set_Runway_latitude( Runway_latitude );
370 f.set_Runway_longitude( Runway_longitude );
371 f.set_Runway_heading( Runway_heading );
372 f.set_Radius_to_rwy( Radius_to_rwy );
374 f.set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy );
375 f.set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
376 f.set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy,
378 f.set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
385 // Revision 1.5 1998/12/03 04:25:02 curt
386 // Working on fixing up new fgFLIGHT class.
388 // Revision 1.4 1998/12/03 01:16:37 curt
389 // Converted fgFLIGHT to a class.
391 // Revision 1.3 1998/10/25 14:08:43 curt
392 // Turned "struct fgCONTROLS" into a class, with inlined accessor functions.
394 // Revision 1.2 1998/10/17 01:34:11 curt
397 // Revision 1.1 1998/10/17 00:43:58 curt