]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
4f7afe65f20fe0a6dc02d51ec497cbedbaafa5b0
[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     // initialize time stamp to something reasonably current
99     last_time.stamp();
100
101     // Is this really needed here ????
102     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
103
104     return true;
105 }
106
107
108 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
109     int i;
110
111     // Version sanity checking
112     net->version = FG_NET_FDM_VERSION;
113
114     // Aero parameters
115     net->longitude = cur_fdm_state->get_Longitude();
116     net->latitude = cur_fdm_state->get_Latitude();
117     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
118     net->phi = cur_fdm_state->get_Phi();
119     net->theta = cur_fdm_state->get_Theta();
120     net->psi = cur_fdm_state->get_Psi();
121     net->phidot = cur_fdm_state->get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
122     net->thetadot = cur_fdm_state->get_Theta_dot_degps()
123         * SG_DEGREES_TO_RADIANS;
124     net->psidot = cur_fdm_state->get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
125
126     net->vcas = cur_fdm_state->get_V_calibrated_kts();
127     net->climb_rate = cur_fdm_state->get_Climb_Rate();
128
129     net->v_north = cur_fdm_state->get_V_north();
130     net->v_east = cur_fdm_state->get_V_east();
131     net->v_down = cur_fdm_state->get_V_down();
132     net->v_wind_body_north = cur_fdm_state->get_uBody();
133     net->v_wind_body_east = cur_fdm_state->get_vBody();
134     net->v_wind_body_down = cur_fdm_state->get_wBody();
135     net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
136
137     net->A_X_pilot = cur_fdm_state->get_A_X_pilot();
138     net->A_Y_pilot = cur_fdm_state->get_A_Y_pilot();
139     net->A_Z_pilot = cur_fdm_state->get_A_Z_pilot();
140
141     // Engine parameters
142     net->num_engines = FGNetFDM::FG_MAX_ENGINES;
143     for ( i = 0; i < net->num_engines; ++i ) {
144         SGPropertyNode *node = fgGetNode("engines/engine", i, true);
145         if ( node->getBoolValue( "running" ) ) {
146             net->eng_state[0] = 2;
147         } else if ( node->getBoolValue( "cranking" ) ) {
148             net->eng_state[0] = 1;
149         } else {
150             net->eng_state[0] = 0;
151         }
152         net->rpm[i] = node->getDoubleValue( "rpm" );
153         net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
154         net->EGT[i] = node->getDoubleValue( "egt-degf" );
155         // cout << "egt = " << aero->EGT << endl;
156         net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
157         net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
158     }
159
160     // Consumables
161     net->num_tanks = FGNetFDM::FG_MAX_TANKS;
162     for ( i = 0; i < net->num_tanks; ++i ) {
163         SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
164         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
165     }
166
167     // Gear and flaps
168     net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
169     for (i = 0; i < net->num_wheels; ++i ) {
170         SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
171         net->wow[i] = node->getDoubleValue("wow");
172     }
173
174     // cout << "Flap deflection = " << aero->dflap << endl;
175     net->flap_deflection = fgGetDouble("/surface-positions/flap-pos-norm" );
176
177     // the following really aren't used in this context
178     net->cur_time = globals->get_time_params()->get_cur_time();
179     net->warp = globals->get_warp();
180     net->visibility = fgGetDouble("/environment/visibility-m");
181
182     if ( net_byte_order ) {
183         // Convert the net buffer to network format
184         net->version = htonl(net->version);
185
186         htond(net->longitude);
187         htond(net->latitude);
188         htond(net->altitude);
189         htond(net->phi);
190         htond(net->theta);
191         htond(net->psi);
192
193         htond(net->phidot);
194         htond(net->thetadot);
195         htond(net->psidot);
196         htond(net->vcas);
197         htond(net->climb_rate);
198         htond(net->v_north);
199         htond(net->v_east);
200         htond(net->v_down);
201         htond(net->v_wind_body_north);
202         htond(net->v_wind_body_east);
203         htond(net->v_wind_body_down);
204         htond(net->stall_warning);
205
206         htond(net->A_X_pilot);
207         htond(net->A_Y_pilot);
208         htond(net->A_Z_pilot);
209
210         for ( i = 0; i < net->num_engines; ++i ) {
211             htonl(net->eng_state[i]);
212             htond(net->rpm[i]);
213             htond(net->fuel_flow[i]);
214             htond(net->EGT[i]);
215             htond(net->oil_temp[i]);
216             htond(net->oil_px[i]);
217         }
218         net->num_engines = htonl(net->num_engines);
219
220         for ( i = 0; i < net->num_tanks; ++i ) {
221             htond(net->fuel_quantity[i]);
222         }
223         net->num_tanks = htonl(net->num_tanks);
224
225         for ( i = 0; i < net->num_wheels; ++i ) {
226             net->wow[i] = htonl(net->wow[i]);
227         }
228         net->num_wheels = htonl(net->num_wheels);
229         htond(net->flap_deflection);
230
231         net->cur_time = htonl( net->cur_time );
232         net->warp = htonl( net->warp );
233         htond(net->visibility);
234     }
235 }
236
237
238 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
239     int i;
240
241     if ( net_byte_order ) {
242         // Convert to the net buffer from network format
243         net->version = ntohl(net->version);
244
245         htond(net->longitude);
246         htond(net->latitude);
247         htond(net->altitude);
248         htond(net->phi);
249         htond(net->theta);
250         htond(net->psi);
251
252         htond(net->phidot);
253         htond(net->thetadot);
254         htond(net->psidot);
255         htond(net->vcas);
256         htond(net->climb_rate);
257         htond(net->v_north);
258         htond(net->v_east);
259         htond(net->v_down);
260         htond(net->v_wind_body_north);
261         htond(net->v_wind_body_east);
262         htond(net->v_wind_body_down);
263         htond(net->stall_warning);
264
265         htond(net->A_X_pilot);
266         htond(net->A_Y_pilot);
267         htond(net->A_Z_pilot);
268
269         net->num_engines = htonl(net->num_engines);
270         for ( i = 0; i < net->num_engines; ++i ) {
271             htonl(net->eng_state[i]);
272             htond(net->rpm[i]);
273             htond(net->fuel_flow[i]);
274             htond(net->EGT[i]);
275             htond(net->oil_temp[i]);
276             htond(net->oil_px[i]);
277         }
278
279         net->num_tanks = htonl(net->num_tanks);
280         for ( i = 0; i < net->num_tanks; ++i ) {
281             htond(net->fuel_quantity[i]);
282         }
283
284         net->num_wheels = htonl(net->num_wheels);
285         // I don't need to convert the Wow flags, since they are one
286         // byte in size
287         htond(net->flap_deflection);
288
289         net->cur_time = ntohl(net->cur_time);
290         net->warp = ntohl(net->warp);
291         htond(net->visibility);
292     }
293
294     if ( net->version == FG_NET_FDM_VERSION ) {
295         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
296         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
297         //      << endl;
298         cur_fdm_state->_updateGeodeticPosition( net->latitude,
299                                                 net->longitude,
300                                                 net->altitude
301                                                   * SG_METER_TO_FEET );
302         cur_fdm_state->_set_Euler_Angles( net->phi,
303                                           net->theta,
304                                           net->psi );
305         cur_fdm_state->_set_Euler_Rates( net->phidot,
306                                          net->thetadot,
307                                          net->psidot );
308         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
309         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
310         cur_fdm_state->_set_Velocities_Local( net->v_north,
311                                               net->v_east,
312                                               net->v_down );
313         cur_fdm_state->_set_Velocities_Wind_Body( net->v_wind_body_north,
314                                                   net->v_wind_body_east,
315                                                   net->v_wind_body_down );
316
317         fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
318         cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
319                                                net->A_Y_pilot,
320                                                net->A_Z_pilot );
321
322         for ( i = 0; i < net->num_engines; ++i ) {
323             SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
324             
325             // node->setBoolValue("running", t->isRunning());
326             // node->setBoolValue("cranking", t->isCranking());
327             
328             // cout << net->eng_state[i] << endl;
329             if ( net->eng_state[i] == 0 ) {
330                 node->setBoolValue( "cranking", false );
331                 node->setBoolValue( "running", false );
332             } else if ( net->eng_state[i] == 1 ) {
333                 node->setBoolValue( "cranking", true );
334                 node->setBoolValue( "running", false );
335             } else if ( net->eng_state[i] == 2 ) {
336                 node->setBoolValue( "cranking", false );
337                 node->setBoolValue( "running", true );
338             }
339
340             node->setDoubleValue( "rpm", net->rpm[i] );
341             node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
342             node->setDoubleValue( "egt-degf", net->EGT[i] );
343             node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
344             node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );         
345         }
346
347         for (i = 0; i < net->num_tanks; ++i ) {
348             SGPropertyNode * node
349                 = fgGetNode("/consumables/fuel/tank", i, true);
350             node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
351         }
352
353         for (i = 0; i < net->num_wheels; ++i ) {
354             SGPropertyNode * node
355                 = fgGetNode("/gear/gear", i, true);
356             node->setDoubleValue("wow", net->wow[i] );
357         }
358
359         fgSetDouble("/surface-positions/flap-pos-norm", net->flap_deflection);
360         SGPropertyNode * node = fgGetNode("/controls", true);
361         fgSetDouble("/surface-positions/elevator-pos-norm", 
362                     node->getDoubleValue( "elevator" ));
363         fgSetDouble("/surface-positions/rudder-pos-norm", 
364                     node->getDoubleValue( "rudder" ));
365         fgSetDouble("/surface-positions/left-aileron-pos-norm", 
366                     node->getDoubleValue( "aileron" ));
367         fgSetDouble("/surface-positions/right-aileron-pos-norm", 
368                     -node->getDoubleValue( "aileron" ));
369
370         /* these are ignored for now  ... */
371         /*
372         if ( net->cur_time ) {
373             fgSetLong("/sim/time/cur-time-override", net->cur_time);
374         }
375
376         globals->set_warp( net->warp );
377         last_warp = net->warp;
378         */
379     } else {
380         SG_LOG( SG_IO, SG_ALERT,
381                 "Error: version mismatch in FGNetFDM2Props()" );
382         SG_LOG( SG_IO, SG_ALERT,
383                 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
384         SG_LOG( SG_IO, SG_ALERT,
385                 "\tNeeds to upgrade net_fdm.hxx and recompile." );
386     }
387 }
388
389
390 // process work for this port
391 bool FGNativeFDM::process() {
392     SGIOChannel *io = get_io_channel();
393     int length = sizeof(buf);
394
395     if ( get_direction() == SG_IO_OUT ) {
396         // cout << "size of cur_fdm_state = " << length << endl;
397         FGProps2NetFDM( &buf );
398         if ( ! io->write( (char *)(& buf), length ) ) {
399             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
400             return false;
401         }
402     } else if ( get_direction() == SG_IO_IN ) {
403         if ( io->get_type() == sgFileType ) {
404             if ( io->read( (char *)(& buf), length ) == length ) {
405                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
406                 FGNetFDM2Props( &buf );
407             }
408         } else {
409             int result;
410             result = io->read( (char *)(& buf), length );
411             while ( result == length ) {
412                 if ( result == length ) {
413                     SG_LOG( SG_IO, SG_DEBUG, "  Success reading data." );
414                     FGNetFDM2Props( &buf );
415                 }
416                 result = io->read( (char *)(& buf), length );
417             }
418         }
419     }
420
421     return true;
422 }
423
424
425 // close the channel
426 bool FGNativeFDM::close() {
427     SGIOChannel *io = get_io_channel();
428
429     set_enabled( false );
430
431     if ( ! io->close() ) {
432         return false;
433     }
434
435     return true;
436 }