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