]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
f1dc35790d6d16b67de0b3c8d77da33b1c1ea1ed
[flightgear.git] / src / Network / native_fdm.cxx
1 // native_fdm.cxx -- FGFS "Native" flight dynamics protocal class
2 //
3 // Written by Curtis Olson, started September 2001.
4 //
5 // Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/io/lowlevel.hxx> // endian tests
30 #include <simgear/io/iochannel.hxx>
31 #include <simgear/timing/sg_time.hxx>
32
33 #include <FDM/flight.hxx>
34 #include <Time/tmp.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
37
38 #include "native_fdm.hxx"
39
40 // FreeBSD works better with this included last ... (?)
41 #if defined(WIN32) && !defined(__CYGWIN__)
42 #  include <windows.h>
43 #else
44 #  include <netinet/in.h>       // htonl() ntohl()
45 #endif
46
47
48 // The function htond is defined this way due to the way some
49 // processors and OSes treat floating point values.  Some will raise
50 // an exception whenever a "bad" floating point value is loaded into a
51 // floating point register.  Solaris is notorious for this, but then
52 // so is LynxOS on the PowerPC.  By translating the data in place,
53 // there is no need to load a FP register with the "corruped" floating
54 // point value.  By doing the BIG_ENDIAN test, I can optimize the
55 // routine for big-endian processors so it can be as efficient as
56 // possible
57 static void htond (double &x)   
58 {
59     if ( sgIsLittleEndian() ) {
60         int    *Double_Overlay;
61         int     Holding_Buffer;
62     
63         Double_Overlay = (int *) &x;
64         Holding_Buffer = Double_Overlay [0];
65     
66         Double_Overlay [0] = htonl (Double_Overlay [1]);
67         Double_Overlay [1] = htonl (Holding_Buffer);
68     } else {
69         return;
70     }
71 }
72
73
74 FGNativeFDM::FGNativeFDM() {
75 }
76
77 FGNativeFDM::~FGNativeFDM() {
78 }
79
80
81 // open hailing frequencies
82 bool FGNativeFDM::open() {
83     if ( is_enabled() ) {
84         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
85                 << "is already in use, ignoring" );
86         return false;
87     }
88
89     SGIOChannel *io = get_io_channel();
90
91     if ( ! io->open( get_direction() ) ) {
92         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
93         return false;
94     }
95
96     set_enabled( true );
97
98     // Is this really needed here ????
99     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
100
101     return true;
102 }
103
104
105 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
106     int i;
107
108     // Version sanity checking
109     net->version = FG_NET_FDM_VERSION;
110
111     // Aero parameters
112     net->longitude = cur_fdm_state->get_Longitude();
113     net->latitude = cur_fdm_state->get_Latitude();
114     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
115     net->phi = cur_fdm_state->get_Phi();
116     net->theta = cur_fdm_state->get_Theta();
117     net->psi = cur_fdm_state->get_Psi();
118     net->phidot = cur_fdm_state->get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
119     net->thetadot = cur_fdm_state->get_Theta_dot_degps()
120         * SG_DEGREES_TO_RADIANS;
121     net->psidot = cur_fdm_state->get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
122
123     net->vcas = cur_fdm_state->get_V_calibrated_kts();
124     net->climb_rate = cur_fdm_state->get_Climb_Rate();
125
126     net->v_north = cur_fdm_state->get_V_north();
127     net->v_east = cur_fdm_state->get_V_east();
128     net->v_down = cur_fdm_state->get_V_down();
129     net->v_wind_body_north = cur_fdm_state->get_uBody();
130     net->v_wind_body_east = cur_fdm_state->get_vBody();
131     net->v_wind_body_down = cur_fdm_state->get_wBody();
132     net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
133
134     net->A_X_pilot = cur_fdm_state->get_A_X_pilot();
135     net->A_Y_pilot = cur_fdm_state->get_A_Y_pilot();
136     net->A_Z_pilot = cur_fdm_state->get_A_Z_pilot();
137
138     // Engine parameters
139     net->num_engines = FGNetFDM::FG_MAX_ENGINES;
140     for ( i = 0; i < net->num_engines; ++i ) {
141         SGPropertyNode *node = fgGetNode("engines/engine", i, true);
142         if ( node->getBoolValue( "running" ) ) {
143             net->eng_state[0] = 2;
144         } else if ( node->getBoolValue( "cranking" ) ) {
145             net->eng_state[0] = 1;
146         } else {
147             net->eng_state[0] = 0;
148         }
149         net->rpm[i] = node->getDoubleValue( "rpm" );
150         net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
151         net->EGT[i] = node->getDoubleValue( "egt-degf" );
152         // cout << "egt = " << aero->EGT << endl;
153         net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
154         net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
155     }
156
157     // Consumables
158     net->num_tanks = FGNetFDM::FG_MAX_TANKS;
159     for ( i = 0; i < net->num_tanks; ++i ) {
160         SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
161         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
162     }
163
164     // Gear and flaps
165     net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
166     for (i = 0; i < net->num_wheels; ++i ) {
167         SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
168         net->wow[i] = node->getDoubleValue("wow");
169     }
170
171     // cout << "Flap deflection = " << aero->dflap << endl;
172     net->flap_deflection = fgGetDouble("/surface-positions/flap-pos-norm" );
173
174     // the following really aren't used in this context
175     net->cur_time = globals->get_time_params()->get_cur_time();
176     net->warp = globals->get_warp();
177     net->visibility = fgGetDouble("/environment/visibility-m");
178
179     if ( net_byte_order ) {
180         // Convert the net buffer to network format
181         net->version = htonl(net->version);
182
183         htond(net->longitude);
184         htond(net->latitude);
185         htond(net->altitude);
186         htond(net->phi);
187         htond(net->theta);
188         htond(net->psi);
189
190         htond(net->phidot);
191         htond(net->thetadot);
192         htond(net->psidot);
193         htond(net->vcas);
194         htond(net->climb_rate);
195         htond(net->v_north);
196         htond(net->v_east);
197         htond(net->v_down);
198         htond(net->v_wind_body_north);
199         htond(net->v_wind_body_east);
200         htond(net->v_wind_body_down);
201         htond(net->stall_warning);
202
203         htond(net->A_X_pilot);
204         htond(net->A_Y_pilot);
205         htond(net->A_Z_pilot);
206
207         for ( i = 0; i < net->num_engines; ++i ) {
208             htonl(net->eng_state[i]);
209             htond(net->rpm[i]);
210             htond(net->fuel_flow[i]);
211             htond(net->EGT[i]);
212             htond(net->oil_temp[i]);
213             htond(net->oil_px[i]);
214         }
215         net->num_engines = htonl(net->num_engines);
216
217         for ( i = 0; i < net->num_tanks; ++i ) {
218             htond(net->fuel_quantity[i]);
219         }
220         net->num_tanks = htonl(net->num_tanks);
221
222         for ( i = 0; i < net->num_wheels; ++i ) {
223             net->wow[i] = htonl(net->wow[i]);
224         }
225         net->num_wheels = htonl(net->num_wheels);
226         htond(net->flap_deflection);
227
228         net->cur_time = htonl( net->cur_time );
229         net->warp = htonl( net->warp );
230         htond(net->visibility);
231     }
232 }
233
234
235 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
236     int i;
237
238     if ( net_byte_order ) {
239         // Convert to the net buffer from network format
240         net->version = ntohl(net->version);
241
242         htond(net->longitude);
243         htond(net->latitude);
244         htond(net->altitude);
245         htond(net->phi);
246         htond(net->theta);
247         htond(net->psi);
248
249         htond(net->phidot);
250         htond(net->thetadot);
251         htond(net->psidot);
252         htond(net->vcas);
253         htond(net->climb_rate);
254         htond(net->v_north);
255         htond(net->v_east);
256         htond(net->v_down);
257         htond(net->v_wind_body_north);
258         htond(net->v_wind_body_east);
259         htond(net->v_wind_body_down);
260         htond(net->stall_warning);
261
262         htond(net->A_X_pilot);
263         htond(net->A_Y_pilot);
264         htond(net->A_Z_pilot);
265
266         net->num_engines = htonl(net->num_engines);
267         for ( i = 0; i < net->num_engines; ++i ) {
268             htonl(net->eng_state[i]);
269             htond(net->rpm[i]);
270             htond(net->fuel_flow[i]);
271             htond(net->EGT[i]);
272             htond(net->oil_temp[i]);
273             htond(net->oil_px[i]);
274         }
275
276         net->num_tanks = htonl(net->num_tanks);
277         for ( i = 0; i < net->num_tanks; ++i ) {
278             htond(net->fuel_quantity[i]);
279         }
280
281         net->num_wheels = htonl(net->num_wheels);
282         // I don't need to convert the Wow flags, since they are one
283         // byte in size
284         htond(net->flap_deflection);
285
286         net->cur_time = ntohl(net->cur_time);
287         net->warp = ntohl(net->warp);
288         htond(net->visibility);
289     }
290
291     if ( net->version == FG_NET_FDM_VERSION ) {
292         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
293         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
294         //      << endl;
295         cur_fdm_state->_updateGeodeticPosition( net->latitude,
296                                                 net->longitude,
297                                                 net->altitude
298                                                   * SG_METER_TO_FEET );
299         cur_fdm_state->_set_Euler_Angles( net->phi,
300                                           net->theta,
301                                           net->psi );
302         cur_fdm_state->_set_Euler_Rates( net->phidot,
303                                          net->thetadot,
304                                          net->psidot );
305         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
306         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
307         cur_fdm_state->_set_Velocities_Local( net->v_north,
308                                               net->v_east,
309                                               net->v_down );
310         cur_fdm_state->_set_Velocities_Wind_Body( net->v_wind_body_north,
311                                                   net->v_wind_body_east,
312                                                   net->v_wind_body_down );
313
314         fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
315         cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
316                                                net->A_Y_pilot,
317                                                net->A_Z_pilot );
318
319         for ( i = 0; i < net->num_engines; ++i ) {
320             SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
321             
322             // node->setBoolValue("running", t->isRunning());
323             // node->setBoolValue("cranking", t->isCranking());
324             
325             // cout << net->eng_state[i] << endl;
326             if ( net->eng_state[i] == 0 ) {
327                 node->setBoolValue( "cranking", false );
328                 node->setBoolValue( "running", false );
329             } else if ( net->eng_state[i] == 1 ) {
330                 node->setBoolValue( "cranking", true );
331                 node->setBoolValue( "running", false );
332             } else if ( net->eng_state[i] == 2 ) {
333                 node->setBoolValue( "cranking", false );
334                 node->setBoolValue( "running", true );
335             }
336
337             node->setDoubleValue( "rpm", net->rpm[i] );
338             node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
339             node->setDoubleValue( "egt-degf", net->EGT[i] );
340             node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
341             node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );         
342         }
343
344         for (i = 0; i < net->num_tanks; ++i ) {
345             SGPropertyNode * node
346                 = fgGetNode("/consumables/fuel/tank", i, true);
347             node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
348         }
349
350         for (i = 0; i < net->num_wheels; ++i ) {
351             SGPropertyNode * node
352                 = fgGetNode("/gear/gear", i, true);
353             node->setDoubleValue("wow", net->wow[i] );
354         }
355
356         fgSetDouble("/surface-positions/flap-pos-norm", net->flap_deflection);
357         SGPropertyNode * node = fgGetNode("/controls", true);
358         fgSetDouble("/surface-positions/elevator-pos-norm", 
359                     node->getDoubleValue( "elevator" ));
360         fgSetDouble("/surface-positions/rudder-pos-norm", 
361                     node->getDoubleValue( "rudder" ));
362         fgSetDouble("/surface-positions/left-aileron-pos-norm", 
363                     node->getDoubleValue( "aileron" ));
364         fgSetDouble("/surface-positions/right-aileron-pos-norm", 
365                     -node->getDoubleValue( "aileron" ));
366
367         /* these are ignored for now  ... */
368         /*
369         if ( net->cur_time ) {
370             fgSetLong("/sim/time/cur-time-override", net->cur_time);
371         }
372
373         globals->set_warp( net->warp );
374         last_warp = net->warp;
375         */
376     } else {
377         SG_LOG( SG_IO, SG_ALERT,
378                 "Error: version mismatch in FGNetFDM2Props()" );
379         SG_LOG( SG_IO, SG_ALERT,
380                 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
381         SG_LOG( SG_IO, SG_ALERT,
382                 "\tNeeds to upgrade net_fdm.hxx and recompile." );
383     }
384 }
385
386
387 // process work for this port
388 bool FGNativeFDM::process() {
389     SGIOChannel *io = get_io_channel();
390     int length = sizeof(buf);
391
392     if ( get_direction() == SG_IO_OUT ) {
393         // cout << "size of cur_fdm_state = " << length << endl;
394         FGProps2NetFDM( &buf );
395         if ( ! io->write( (char *)(& buf), length ) ) {
396             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
397             return false;
398         }
399     } else if ( get_direction() == SG_IO_IN ) {
400         if ( io->get_type() == sgFileType ) {
401             if ( io->read( (char *)(& buf), length ) == length ) {
402                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
403                 FGNetFDM2Props( &buf );
404             }
405         } else {
406             int result;
407             result = io->read( (char *)(& buf), length );
408             while ( result == length ) {
409                 if ( result == length ) {
410                     SG_LOG( SG_IO, SG_DEBUG, "  Success reading data." );
411                     FGNetFDM2Props( &buf );
412                 }
413                 result = io->read( (char *)(& buf), length );
414             }
415         }
416     }
417
418     return true;
419 }
420
421
422 // close the channel
423 bool FGNativeFDM::close() {
424     SGIOChannel *io = get_io_channel();
425
426     set_enabled( false );
427
428     if ( ! io->close() ) {
429         return false;
430     }
431
432     return true;
433 }