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