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