]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ADA.cxx
SG-ified logstream.
[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
23 #include <Controls/controls.hxx>
24 #include <GUI/gui.h>
25
26 #include "ADA.hxx"
27
28 #define numberofbytes 472
29
30 struct {
31     double number_of_bytes;
32     double lat_geoc;
33     double lon_geoc;
34     double altitude;
35     double psirad;
36     double thetrad;
37     double phirad;
38     double earth_posn_angle;
39     double radius_to_vehicle;
40     double sea_level_radius;
41     double latitude;
42     double longitude;
43     double Vnorth;
44     double Veast;
45     double Vdown;
46     double Vcas_kts;
47     double prad;
48     double qrad;
49     double rrad;
50     double alpharad;
51     double betarad;
52     double latitude_dot;
53     double longitude_dot;
54     double radius_dot;
55     double Gamma_vert_rad;
56     double Runway_altitude;
57     double throttle;
58     double pstick;
59     double rstick;
60     double rpedal;
61     double U_local;
62     double V_local;
63     double W_local;
64     double U_dot_local;
65     double V_dot_local;
66     double W_dot_local;
67     double Machno;
68     double anxg;
69     double anyg;
70     double anzg;
71     double aux1;
72     double aux2;
73     double aux3;
74     double aux4;
75     double aux5;
76     double aux6;
77     double aux7;
78     double aux8;
79     int iaux1;
80     int iaux2;
81     int iaux3;
82     int iaux4;
83     int iaux5;
84     int iaux6;
85     int iaux7;
86     int iaux8;
87     int iaux9;
88     int iaux10;
89     int iaux11;
90     int iaux12;
91     float aux9;
92     float aux10;
93     float aux11;
94     float aux12;
95     float aux13;
96     float aux14;
97     float aux15;
98     float aux16;
99     float aux17;
100     float aux18;
101 } sixdof_to_visuals;
102
103 #define number_of_bytes sixdof_to_visuals.number_of_bytes
104 #define U_dot_local sixdof_to_visuals.U_dot_local
105 #define V_dot_local sixdof_to_visuals.V_dot_local
106 #define W_dot_local sixdof_to_visuals.W_dot_local
107 #define U_local sixdof_to_visuals.U_local
108 #define V_local sixdof_to_visuals.V_local
109 #define W_local sixdof_to_visuals.W_local
110 #define throttle sixdof_to_visuals.throttle
111 #define pstick sixdof_to_visuals.pstick
112 #define rstick sixdof_to_visuals.rstick
113 #define rpedal sixdof_to_visuals.rpedal
114 #define V_north sixdof_to_visuals.Vnorth
115 #define V_east sixdof_to_visuals.Veast
116 #define V_down sixdof_to_visuals.Vdown
117 #define V_calibrated_kts sixdof_to_visuals.Vcas_kts
118 #define P_body sixdof_to_visuals.prad
119 #define Q_body sixdof_to_visuals.qrad
120 #define R_body sixdof_to_visuals.rrad
121 #define Latitude_dot sixdof_to_visuals.latitude_dot
122 #define Longitude_dot sixdof_to_visuals.longitude_dot
123 #define Radius_dot sixdof_to_visuals.radius_dot
124 #define Latitude sixdof_to_visuals.latitude
125 #define Longitude sixdof_to_visuals.longitude
126 #define Lat_geocentric sixdof_to_visuals.lat_geoc
127 #define Lon_geocentric sixdof_to_visuals.lon_geoc
128 #define Radius_to_vehicle sixdof_to_visuals.radius_to_vehicle
129 #define Altitude sixdof_to_visuals.altitude
130 #define Phi sixdof_to_visuals.phirad
131 #define Theta sixdof_to_visuals.thetrad
132 #define Psi sixdof_to_visuals.psirad
133 #define Alpha sixdof_to_visuals.alpharad
134 #define Beta sixdof_to_visuals.betarad
135 #define Sea_level_radius sixdof_to_visuals.sea_level_radius
136 #define Earth_position_angle sixdof_to_visuals.earth_posn_angle
137 #define Runway_altitude sixdof_to_visuals.Runway_altitude
138 #define Gamma_vert_rad sixdof_to_visuals.Gamma_vert_rad
139 #define Machno sixdof_to_visuals.Machno
140 #define anxg sixdof_to_visuals.anxg
141 #define anyg sixdof_to_visuals.anyg
142 #define anzg sixdof_to_visuals.anzg
143
144
145 FGADA::FGADA( double dt ) {
146     set_delta_t( dt );
147 }
148
149
150 FGADA::~FGADA() {
151 }
152
153
154 // Initialize the ADA flight model, dt is the time increment
155 // for each subsequent iteration through the EOM
156 void FGADA::init() {
157     // cout << "FGADA::init()" << endl;
158
159     char Buffer[numberofbytes];
160
161     printf("\nInitialising UDP sockets\n");
162     // initialise a "udp" socket
163     fdmsock = new SGSocket( "reddy_pc", "5001", "udp" );
164
165     // open as a client
166     bool result = fdmsock->open(SG_IO_OUT);
167     if (result == false) {
168         printf ("Socket Open Error\n");
169     } else {
170         // Dummy Write FGExternal structure from socket to establish connection
171         int result = fdmsock->write(Buffer, numberofbytes);
172         printf("Connection established.\n");
173     }
174 }
175
176
177 // Run an iteration of the EOM.  This is essentially a NOP here
178 // because these values are getting filled in elsewhere based on
179 // external input.
180 bool FGADA::update( int multiloop ) {
181     // cout << "FGADA::update()" << endl;
182
183     char Buffer[numberofbytes];
184
185     // Read FGExternal structure from socket
186     while (1) {
187       int result = fdmsock->read(Buffer, numberofbytes);
188       if (result == numberofbytes) {
189          // Copy buffer into FGExternal structure
190          memcpy (&sixdof_to_visuals, &Buffer, sizeof (Buffer));
191       } else {
192          break;
193       }
194     }
195
196     //cout << endl << sixdof_to_visuals.aux18 << endl;
197     // Close Visuals through message/flag from Flight model
198     if (sixdof_to_visuals.aux18 == 1) {
199         fdmsock->close();
200         ConfirmExitDialog();           
201     }
202     //cout << endl << sixdof_to_visuals.aux18 << endl;
203
204     // Convert from the FGExternal struct to the FGInterface struct (input)
205     copy_from_FGADA();
206
207     return true;
208 }
209
210 // Convert from the FGInterface struct to the FGADA struct (output)
211 bool FGADA::copy_to_FGADA () {
212
213     return true;
214 }
215
216
217 // Convert from the FGADA struct to the FGInterface struct (input)
218 bool FGADA::copy_from_FGADA() {
219
220     // Velocities
221     _set_Velocities_Local( V_north, V_east, V_down );
222     _set_V_calibrated_kts( V_calibrated_kts );
223
224     // Angular rates 
225     _set_Omega_Body( P_body, Q_body, R_body );
226     _set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
227
228     //    SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << Longitude 
229     //      << " lat_geoc = " << Lat_geocentric << " lat_geod = " << Latitude 
230     //      << " alt = " << Altitude << " sl_radius = " << Sea_level_radius 
231     //      << " radius_to_vehicle = " << Radius_to_vehicle );
232             
233     // Positions
234     _set_Geocentric_Position( Lat_geocentric, Lon_geocentric,
235                               Radius_to_vehicle );
236     _set_Geodetic_Position( Latitude, Longitude, Altitude );
237     _set_Euler_Angles( Phi, Theta, Psi );
238
239     // Miscellaneous quantities
240     _set_Alpha( Alpha );
241     _set_Beta( Beta );
242     _set_Gamma_vert_rad( Gamma_vert_rad );
243     _set_Sea_level_radius( Sea_level_radius );
244     _set_Earth_position_angle( Earth_position_angle );
245     _set_Runway_altitude( Runway_altitude );
246     _set_sin_lat_geocentric(Lat_geocentric);
247     _set_cos_lat_geocentric(Lat_geocentric);
248     _set_sin_cos_longitude(Longitude);
249     _set_sin_cos_latitude(Latitude);
250     _set_Accels_Local( U_dot_local, V_dot_local, W_dot_local );
251     _set_Velocities_Ground( U_local, V_local, W_local );
252     _set_Accels_CG_Body_N( anxg,anyg,anzg);
253     _set_Mach_number( Machno);
254
255     //    printf("sr=%f\n",Sea_level_radius);
256     //    printf("psi = %f %f\n",Psi,Psi*SGD_RADIANS_TO_DEGREES);    
257
258     // controls
259     controls.set_throttle(0,throttle/131.0);
260     controls.set_elevator(pstick);
261     controls.set_aileron(rstick);
262     controls.set_rudder(rpedal);
263
264     // auxilliary parameters for HUD
265     set_iaux1(sixdof_to_visuals.iaux1);
266     set_iaux2(sixdof_to_visuals.iaux2);
267     set_iaux3(sixdof_to_visuals.iaux3);
268     set_iaux4(sixdof_to_visuals.iaux4);
269     set_iaux5(sixdof_to_visuals.iaux5);
270     set_iaux6(sixdof_to_visuals.iaux6);
271     set_iaux7(sixdof_to_visuals.iaux7);
272     set_iaux8(sixdof_to_visuals.iaux8);
273     set_iaux9(sixdof_to_visuals.iaux9);
274     set_iaux10(sixdof_to_visuals.iaux10);
275     set_iaux11(sixdof_to_visuals.iaux11);
276     set_iaux12(sixdof_to_visuals.iaux12);
277     set_aux1(sixdof_to_visuals.aux1);
278     set_aux2(sixdof_to_visuals.aux2);
279     set_aux3(sixdof_to_visuals.aux3);
280     set_aux4(sixdof_to_visuals.aux4);
281     set_aux5(sixdof_to_visuals.aux5);
282     set_aux6(sixdof_to_visuals.aux6);
283     set_aux7(sixdof_to_visuals.aux7);
284     set_aux8(sixdof_to_visuals.aux8);
285     set_aux9(sixdof_to_visuals.aux9);
286     set_aux10(sixdof_to_visuals.aux10);
287     set_aux11(sixdof_to_visuals.aux11);
288     set_aux12(sixdof_to_visuals.aux12);
289     set_aux13(sixdof_to_visuals.aux13);
290     set_aux14(sixdof_to_visuals.aux14);
291     set_aux15(sixdof_to_visuals.aux15);
292     set_aux16(sixdof_to_visuals.aux16);
293     set_aux17(sixdof_to_visuals.aux17);
294     set_aux18(sixdof_to_visuals.aux18);
295     
296     cout << endl << sixdof_to_visuals.aux18 << endl;
297
298     return true;
299 }