]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ADA.cxx
Additional failure modeling.
[flightgear.git] / src / FDM / ADA.cxx
1 // ADA.cxx -- interface to the "External"-ly driven ADA flight model
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 //
17 // $Id$
18
19 // Modified by Cdr. VS Renganthan <vsranga@ada.ernet.in>, 12 Oct 2K
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/io/iochannel.hxx>
26 #include <simgear/constants.h>
27
28 #include <Controls/controls.hxx>
29 #include <Main/globals.hxx>
30
31 #include <Main/fg_props.hxx> //to get ID of window (left/right or center)
32 #include <Scenery/scenery.hxx> //to pass ground elevation to FDM
33
34 #include "ADA.hxx"
35
36 #define numberofbytes 472 // from FDM to visuals
37 #define nbytes 8        //from visuals to FDM
38
39 struct {
40     double number_of_bytes;
41     double lat_geoc;
42     double lon_geoc;
43     double altitude;
44     double psirad;
45     double thetrad;
46     double phirad;
47     double earth_posn_angle;
48     double radius_to_vehicle;
49     double sea_level_radius;
50     double latitude;
51     double longitude;
52     double Vnorth;
53     double Veast;
54     double Vdown;
55     double Vcas_kts;
56     double prad;
57     double qrad;
58     double rrad;
59     double alpharad;
60     double betarad;
61     double latitude_dot;
62     double longitude_dot;
63     double radius_dot;
64     double Gamma_vert_rad;
65     double Runway_altitude;
66     double throttle;
67     double pstick;
68     double rstick;
69     double rpedal;
70     double U_local;
71     double V_local;
72     double W_local;
73     double U_dot_local;
74     double V_dot_local;
75     double W_dot_local;
76     double Machno;
77     double anxg;
78     double anyg;
79     double anzg;
80     double aux1;
81     double aux2;
82     double aux3;
83     double aux4;
84     double aux5;
85     double aux6;
86     double aux7;
87     double aux8;
88     int iaux1;
89     int iaux2;
90     int iaux3;
91     int iaux4;
92     int iaux5;
93     int iaux6;
94     int iaux7;
95     int iaux8;
96     int iaux9;
97     int iaux10;
98     int iaux11;
99     int iaux12;
100     float aux9;
101     float aux10;
102     float aux11;
103     float aux12;
104     float aux13;
105     float aux14;
106     float aux15;
107     float aux16;
108     float aux17;
109     float aux18;
110 } sixdof_to_visuals;
111
112 double view_offset; //if this zero, means center window
113
114 struct {
115         double ground_elevation;
116 } visuals_to_sixdof;
117
118 #define ground_elevation visuals_to_sixdof.ground_elevation
119
120 #define number_of_bytes sixdof_to_visuals.number_of_bytes
121 #define U_dot_local sixdof_to_visuals.U_dot_local
122 #define V_dot_local sixdof_to_visuals.V_dot_local
123 #define W_dot_local sixdof_to_visuals.W_dot_local
124 #define U_local sixdof_to_visuals.U_local
125 #define V_local sixdof_to_visuals.V_local
126 #define W_local sixdof_to_visuals.W_local
127 #define throttle sixdof_to_visuals.throttle
128 #define pstick sixdof_to_visuals.pstick
129 #define rstick sixdof_to_visuals.rstick
130 #define rpedal sixdof_to_visuals.rpedal
131 #define V_north sixdof_to_visuals.Vnorth
132 #define V_east sixdof_to_visuals.Veast
133 #define V_down sixdof_to_visuals.Vdown
134 #define V_calibrated_kts sixdof_to_visuals.Vcas_kts
135 #define P_body sixdof_to_visuals.prad
136 #define Q_body sixdof_to_visuals.qrad
137 #define R_body sixdof_to_visuals.rrad
138 #define Latitude_dot sixdof_to_visuals.latitude_dot
139 #define Longitude_dot sixdof_to_visuals.longitude_dot
140 #define Radius_dot sixdof_to_visuals.radius_dot
141 #define Latitude sixdof_to_visuals.latitude
142 #define Longitude sixdof_to_visuals.longitude
143 #define Lat_geocentric sixdof_to_visuals.lat_geoc
144 #define Lon_geocentric sixdof_to_visuals.lon_geoc
145 #define Radius_to_vehicle sixdof_to_visuals.radius_to_vehicle
146 #define Altitude sixdof_to_visuals.altitude
147 #define Phi sixdof_to_visuals.phirad
148 #define Theta sixdof_to_visuals.thetrad
149 #define Psi sixdof_to_visuals.psirad
150 #define Alpha sixdof_to_visuals.alpharad
151 #define Beta sixdof_to_visuals.betarad
152 #define Sea_level_radius sixdof_to_visuals.sea_level_radius
153 #define Earth_position_angle sixdof_to_visuals.earth_posn_angle
154 #define Runway_altitude sixdof_to_visuals.Runway_altitude
155 #define Gamma_vert_rad sixdof_to_visuals.Gamma_vert_rad
156 #define Machno sixdof_to_visuals.Machno
157 #define anxg sixdof_to_visuals.anxg
158 #define anyg sixdof_to_visuals.anyg
159 #define anzg sixdof_to_visuals.anzg
160
161
162 FGADA::FGADA( double dt ) {
163 //     set_delta_t( dt );
164 }
165
166
167 FGADA::~FGADA() {
168 }
169
170
171 // Initialize the ADA flight model, dt is the time increment
172 // for each subsequent iteration through the EOM
173 void FGADA::init() {
174
175     //do init common to all FDM"s
176     common_init();
177     
178     //now do ADA-specific init.
179
180     // cout << "FGADA::init()" << endl;
181
182     char OutBuffer[nbytes];
183     copy_to_FGADA();
184
185     printf("\nInitialising UDP sockets\n");
186     // initialise a "udp" socket
187     fdmsock = new SGSocket( "fdm_pc", "5001", "udp" );
188
189     // open as a client
190     bool result = fdmsock->open(SG_IO_OUT);
191     if (result == false) {
192         printf ("Socket Open Error\n");
193     } else {
194     copy_to_FGADA();
195         // Write FGExternal structure from socket to establish connection
196         int result = fdmsock->write(OutBuffer, nbytes);
197         printf("Connection established = %d.\n", result);
198     }
199 }
200
201
202 // Run an iteration of the EOM.  This is essentially a NOP here
203 // because these values are getting filled in elsewhere based on
204 // external input.
205 void FGADA::update( double dt ) {
206     // cout << "FGADA::update()" << endl;
207
208     if (is_suspended())
209       return;
210
211     char Buffer[numberofbytes];
212     char OutBuffer[nbytes];
213
214     // Read FGExternal structure from socket
215     while (1) {
216       int result = fdmsock->read(Buffer, numberofbytes);
217       if (result == numberofbytes) {
218          // Copy buffer into FGExternal structure
219          memcpy (&sixdof_to_visuals, &Buffer, sizeof (Buffer));
220              // Convert from the FGExternal struct to the FGInterface struct (input)
221                  copy_from_FGADA();
222       } else {
223          break;
224       }
225     }
226
227     copy_to_FGADA();
228     fgGetDouble("/sim/view/offset",view_offset);
229     if ( view_offset == 0.0) {
230         memcpy (&OutBuffer, &visuals_to_sixdof, sizeof (OutBuffer));
231         fdmsock->write(OutBuffer, nbytes);
232     }
233 }
234
235 // Convert from the FGInterface struct to the FGADA struct (output)
236 bool FGADA::copy_to_FGADA () {
237     ground_elevation = globals->get_scenery()->get_cur_elev();
238     return true;
239 }
240
241
242 // Convert from the FGADA struct to the FGInterface struct (input)
243 bool FGADA::copy_from_FGADA() {
244
245     //Positions and attitudes for The Rendering engine
246     _set_Geodetic_Position( Latitude, Longitude, Altitude );
247     _set_Euler_Angles( Phi, Theta, Psi );
248     _set_Geocentric_Position( Lat_geocentric, Lon_geocentric,
249                               Radius_to_vehicle );
250     _set_Sea_level_radius( Sea_level_radius );
251
252         _set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
253     _set_Earth_position_angle( Earth_position_angle );
254     _set_sin_lat_geocentric(Lat_geocentric);
255     _set_cos_lat_geocentric(Lat_geocentric);
256     _set_sin_cos_longitude(Longitude);
257     _set_sin_cos_latitude(Latitude);
258     
259         // Velocities and accelerations for the pitch ladder and velocity vector
260     _set_Accels_Local( U_dot_local, V_dot_local, W_dot_local );
261     _set_Velocities_Ground( U_local, V_local, W_local );//same as V_NED in mps
262     _set_Velocities_Local( V_north, V_east, V_down ); //same as UVW_local in fps
263
264     //Positions and attitude for ship
265         _set_daux(1,sixdof_to_visuals.aux1);//ship lat
266     _set_daux(2,sixdof_to_visuals.aux2);//ship lon
267     _set_daux(3,sixdof_to_visuals.aux3);//ship alt+heave
268     _set_daux(4,sixdof_to_visuals.aux4);//distance of a/c from ski-jump exit
269     _set_faux(1,sixdof_to_visuals.aux9);//ship pitch
270     _set_faux(2,sixdof_to_visuals.aux10);//ship roll
271     _set_faux(3,sixdof_to_visuals.aux11);//ship yaw
272     _set_iaux(1,sixdof_to_visuals.iaux1);//flag for drawing ship
273
274     // controls
275     globals->get_controls()->set_throttle(0,throttle/131.0);
276     globals->get_controls()->set_elevator(pstick);
277     globals->get_controls()->set_aileron(rstick);
278     globals->get_controls()->set_rudder(rpedal);
279
280     // auxilliary parameters for HUD
281     _set_V_calibrated_kts( V_calibrated_kts );
282     _set_Alpha( Alpha );
283     _set_Beta( Beta );
284     _set_Accels_CG_Body_N( anxg,anyg,anzg);
285     _set_Mach_number( Machno);
286     _set_Climb_Rate( W_local*SG_METER_TO_FEET ); //pressure alt in feet for lca(navy)
287
288     _set_iaux(2,sixdof_to_visuals.iaux2);//control law mode switch posn
289     _set_iaux(3,sixdof_to_visuals.iaux3);//ldg gear posn
290     _set_iaux(4,sixdof_to_visuals.iaux4);// wow nose status
291     _set_iaux(5,sixdof_to_visuals.iaux5);// wow main status
292     _set_iaux(6,sixdof_to_visuals.iaux6);// arrester hook posn
293     _set_iaux(7,sixdof_to_visuals.iaux7);
294     _set_iaux(8,sixdof_to_visuals.iaux8);
295     _set_iaux(9,sixdof_to_visuals.iaux9);
296     _set_iaux(10,sixdof_to_visuals.iaux10);
297     _set_iaux(11,sixdof_to_visuals.iaux11);
298     _set_iaux(12,sixdof_to_visuals.iaux12);
299
300     _set_daux(5,sixdof_to_visuals.aux5);
301     _set_daux(6,sixdof_to_visuals.aux6);
302     _set_daux(7,sixdof_to_visuals.aux7);
303     _set_daux(8,sixdof_to_visuals.aux8);
304
305     _set_faux(4,sixdof_to_visuals.aux12);
306     _set_faux(5,sixdof_to_visuals.aux13);
307     _set_faux(6,sixdof_to_visuals.aux14);
308     _set_faux(7,sixdof_to_visuals.aux15);
309     _set_faux(8,sixdof_to_visuals.aux16);
310     _set_faux(9,sixdof_to_visuals.aux17);
311     _set_faux(10,sixdof_to_visuals.aux18);
312
313     // Angular rates 
314     _set_Omega_Body( P_body, Q_body, R_body );
315
316     // Miscellaneous quantities
317     _set_Gamma_vert_rad( Gamma_vert_rad );
318     _set_Runway_altitude( Runway_altitude );
319
320     //    SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << Longitude 
321     //      << " lat_geoc = " << Lat_geocentric << " lat_geod = " << Latitude 
322     //      << " alt = " << Altitude << " sl_radius = " << Sea_level_radius 
323     //      << " radius_to_vehicle = " << Radius_to_vehicle );
324             
325
326     //    printf("sr=%f\n",Sea_level_radius);
327     //    printf("psi = %f %f\n",Psi,Psi*SGD_RADIANS_TO_DEGREES);    
328     
329     return true;
330 }