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