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