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