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