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