]> git.mxchange.org Git - flightgear.git/blob - FDM/JSBsim.cxx
Added initial support for native SGI compilers.
[flightgear.git] / FDM / JSBsim.cxx
1 // JSBsim.cxx -- interface to the JSBsim flight model
2 //
3 // Written by Curtis Olson, started February 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson  - curt@flightgear.org
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 // (Log is kept at end of this file)
23
24
25 #include <Include/compiler.h>
26
27 #include STL_STRING
28
29 #include <Aircraft/aircraft.hxx>
30 #include <Controls/controls.hxx>
31 #include <Debug/logstream.hxx>
32 #include <Include/fg_constants.h>
33 #include <Main/options.hxx>
34 #include <Math/fg_geodesy.hxx>
35
36 #include <FDM/JSBsim/FGFDMExec.h>
37 #include <FDM/JSBsim/FGAircraft.h>
38 #include <FDM/JSBsim/FGFCS.h>
39 #include <FDM/JSBsim/FGPosition.h>
40 #include <FDM/JSBsim/FGRotation.h>
41 #include <FDM/JSBsim/FGState.h>
42 #include <FDM/JSBsim/FGTranslation.h>
43
44 #include "JSBsim.hxx"
45
46
47 // The default aircraft
48 FGFDMExec FDMExec;
49
50
51 // Initialize the JSBsim flight model, dt is the time increment for
52 // each subsequent iteration through the EOM
53 int fgJSBsimInit(double dt) {
54     FG_LOG( FG_FLIGHT, FG_INFO, "Starting initializing JSBsim" );
55
56     FG_LOG( FG_FLIGHT, FG_INFO, "  created FDMExec" );
57
58     string aircraft_path = current_options.get_fg_root() + "/Aircraft";
59     string engine_path = current_options.get_fg_root() + "/Engine";
60
61     FDMExec.GetAircraft()->LoadAircraft(aircraft_path, engine_path, "X15");
62     FG_LOG( FG_FLIGHT, FG_INFO, "  loaded aircraft" );
63
64     FDMExec.GetState()->Reset(aircraft_path, "Reset00");
65     FG_LOG( FG_FLIGHT, FG_INFO, "  loaded initial conditions" );
66
67     FDMExec.GetState()->Setdt(dt);
68     FG_LOG( FG_FLIGHT, FG_INFO, "  set dt" );
69
70     FG_LOG( FG_FLIGHT, FG_INFO, "Finished initializing JSBsim" );
71
72     return 1;
73 }
74
75
76 // Run an iteration of the EOM (equations of motion)
77 int fgJSBsimUpdate(FGInterface& f, int multiloop) {
78     double save_alt = 0.0;
79
80     // lets try to avoid really screwing up the JSBsim model
81     if ( f.get_Altitude() < -9000 ) {
82         save_alt = f.get_Altitude();
83         f.set_Altitude( 0.0 );
84     }
85
86     // copy control positions into the JSBsim structure
87     FDMExec.GetFCS()->SetDa( controls.get_aileron() );
88     FDMExec.GetFCS()->SetDe( controls.get_elevator() 
89                              + controls.get_elevator_trim() );
90     FDMExec.GetFCS()->SetDr( controls.get_rudder() );
91     FDMExec.GetFCS()->SetDf( 0.0 );
92     FDMExec.GetFCS()->SetDs( 0.0 );
93     FDMExec.GetFCS()->SetThrottle( FGControls::ALL_ENGINES, 
94                                    controls.get_throttle( 0 ) );
95     // FCS->SetBrake( controls.get_brake( 0 ) );
96
97     // Inform JSBsim of the local terrain altitude
98     // Runway_altitude =   f.get_Runway_altitude();
99
100     // old -- FGInterface_2_JSBsim() not needed except for Init()
101     // translate FG to JSBsim structure
102     // FGInterface_2_JSBsim(f);
103     // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
104     // printf("Altitude = %.2f\n", Altitude * 0.3048);
105     // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
106
107     /* FDMExec.GetState()->Setsim_time(State->Getsim_time() 
108                                     + State->Getdt() * multiloop); */
109
110     for ( int i = 0; i < multiloop; i++ ) {
111         FDMExec.Run();
112     }
113
114     // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
115     // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
116     
117     // translate JSBsim back to FG structure so that the
118     // autopilot (and the rest of the sim can use the updated
119     // values
120
121     fgJSBsim_2_FGInterface(f);
122
123     // but lets restore our original bogus altitude when we are done
124     if ( save_alt < -9000.0 ) {
125         f.set_Altitude( save_alt );
126     }
127
128     return 1;
129 }
130
131
132 // Convert from the FGInterface struct to the JSBsim generic_ struct
133 int FGInterface_2_JSBsim (FGInterface& f) {
134
135     return 1;
136 }
137
138
139 // Convert from the JSBsim generic_ struct to the FGInterface struct
140 int fgJSBsim_2_FGInterface (FGInterface& f) {
141
142     // Velocities
143     f.set_Velocities_Local( FDMExec.GetPosition()->GetVn(), 
144                             FDMExec.GetPosition()->GetVe(), 
145                             FDMExec.GetPosition()->GetVd() );
146     // f.set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground, 
147     //               V_down_rel_ground );
148     // f.set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
149     //                      V_down_airmass );
150     // f.set_Velocities_Local_Rel_Airmass( V_north_rel_airmass, 
151     //                          V_east_rel_airmass, V_down_rel_airmass );
152     // f.set_Velocities_Gust( U_gust, V_gust, W_gust );
153     // f.set_Velocities_Wind_Body( U_body, V_body, W_body );
154
155     // f.set_V_rel_wind( V_rel_wind );
156     // f.set_V_true_kts( V_true_kts );
157     // f.set_V_rel_ground( V_rel_ground );
158     // f.set_V_inertial( V_inertial );
159     // f.set_V_ground_speed( V_ground_speed );
160     // f.set_V_equiv( V_equiv );
161
162     /* ***FIXME*** */ f.set_V_equiv_kts( FDMExec.GetState()->GetVt() );
163     // f.set_V_calibrated( V_calibrated );
164     // f.set_V_calibrated_kts( V_calibrated_kts );
165
166     f.set_Omega_Body( FDMExec.GetRotation()->GetP(), 
167                       FDMExec.GetRotation()->GetQ(), 
168                       FDMExec.GetRotation()->GetR() );
169     // f.set_Omega_Local( P_local, Q_local, R_local );
170     // f.set_Omega_Total( P_total, Q_total, R_total );
171     
172     // f.set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
173     // ***FIXME*** f.set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
174
175     // Positions
176     double lat_geoc = FDMExec.GetState()->Getlatitude();
177     double lon = FDMExec.GetState()->Getlongitude();
178     double alt = FDMExec.GetState()->Geth();
179     double lat_geod, tmp_alt, sl_radius1, sl_radius2, tmp_lat_geoc;
180     fgGeocToGeod( lat_geoc, EQUATORIAL_RADIUS_M + alt * FEET_TO_METER,
181                   &lat_geod, &tmp_alt, &sl_radius1 );
182     fgGeodToGeoc( lat_geod, alt * FEET_TO_METER, &sl_radius2, &tmp_lat_geoc );
183
184     FG_LOG( FG_FLIGHT, FG_DEBUG, "lon = " << lon << " lat_geod = " << lat_geod
185             << " lat_geoc = " << lat_geoc
186             << " alt = " << alt << " tmp_alt = " << tmp_alt * METER_TO_FEET
187             << " sl_radius1 = " << sl_radius1 * METER_TO_FEET
188             << " sl_radius2 = " << sl_radius2 * METER_TO_FEET
189             << " Equator = " << EQUATORIAL_RADIUS_FT );
190             
191     f.set_Geocentric_Position( lat_geoc, lon, 
192                                sl_radius2 * METER_TO_FEET + alt );
193     f.set_Geodetic_Position( lat_geod, lon, alt );
194     f.set_Euler_Angles( FDMExec.GetRotation()->Getphi(), 
195                         FDMExec.GetRotation()->Gettht(),
196                         FDMExec.GetRotation()->Getpsi() );
197
198     // Miscellaneous quantities
199     // f.set_T_Local_to_Body(T_local_to_body_m);
200     // f.set_Gravity( Gravity );
201     // f.set_Centrifugal_relief( Centrifugal_relief );
202
203     f.set_Alpha( FDMExec.GetTranslation()->Getalpha() );
204     f.set_Beta( FDMExec.GetTranslation()->Getbeta() );
205     // f.set_Alpha_dot( Alpha_dot );
206     // f.set_Beta_dot( Beta_dot );
207
208     // f.set_Cos_alpha( Cos_alpha );
209     // f.set_Sin_alpha( Sin_alpha );
210     // f.set_Cos_beta( Cos_beta );
211     // f.set_Sin_beta( Sin_beta );
212
213     // f.set_Cos_phi( Cos_phi );
214     // f.set_Sin_phi( Sin_phi );
215     // f.set_Cos_theta( Cos_theta );
216     // f.set_Sin_theta( Sin_theta );
217     // f.set_Cos_psi( Cos_psi );
218     // f.set_Sin_psi( Sin_psi );
219
220     // ***ATTENDTOME*** f.set_Gamma_vert_rad( Gamma_vert_rad );
221     // f.set_Gamma_horiz_rad( Gamma_horiz_rad );
222
223     // f.set_Sigma( Sigma );
224     // f.set_Density( Density );
225     // f.set_V_sound( V_sound );
226     // f.set_Mach_number( Mach_number );
227
228     // f.set_Static_pressure( Static_pressure );
229     // f.set_Total_pressure( Total_pressure );
230     // f.set_Impact_pressure( Impact_pressure );
231     // f.set_Dynamic_pressure( Dynamic_pressure );
232
233     // f.set_Static_temperature( Static_temperature );
234     // f.set_Total_temperature( Total_temperature );
235
236     /* **FIXME*** */ f.set_Sea_level_radius( sl_radius2 * METER_TO_FEET );
237     /* **FIXME*** */ f.set_Earth_position_angle( 0.0 );
238
239     /* ***FIXME*** */ f.set_Runway_altitude( 0.0 );
240     // f.set_Runway_latitude( Runway_latitude );
241     // f.set_Runway_longitude( Runway_longitude );
242     // f.set_Runway_heading( Runway_heading );
243     // f.set_Radius_to_rwy( Radius_to_rwy );
244
245     // f.set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy);
246     // f.set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
247     // f.set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy, 
248     //                        D_pilot_above_rwy );
249     // f.set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
250
251
252     return 0;
253 }
254
255
256 // $Log$
257 // Revision 1.3  1999/02/26 22:09:10  curt
258 // Added initial support for native SGI compilers.
259 // Integrated Jon's next version of JSBsim.
260 //
261 // Revision 1.2  1999/02/11 21:09:40  curt
262 // Interface with Jon's submitted JSBsim changes.
263 //
264 // Revision 1.1  1999/02/05 21:29:38  curt
265 // Incorporating Jon S. Berndt's flight model code.
266 //