]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
Fix bug 141, by ensuring certain subsystems are assigned to the 'post FDM' group...
[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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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/flightProperties.hxx>
34 #include <Time/tmp.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
37 #include <Scenery/scenery.hxx>
38
39 #include "native_fdm.hxx"
40
41 // FreeBSD works better with this included last ... (?)
42 #if defined( _MSC_VER )
43 #  include <windows.h>
44 #elif defined( __MINGW32__ )
45 #  include <winsock2.h>
46 #else
47 #  include <netinet/in.h>       // htonl() ntohl()
48 #endif
49
50
51 // The function htond is defined this way due to the way some
52 // processors and OSes treat floating point values.  Some will raise
53 // an exception whenever a "bad" floating point value is loaded into a
54 // floating point register.  Solaris is notorious for this, but then
55 // so is LynxOS on the PowerPC.  By translating the data in place,
56 // there is no need to load a FP register with the "corruped" floating
57 // point value.  By doing the BIG_ENDIAN test, I can optimize the
58 // routine for big-endian processors so it can be as efficient as
59 // possible
60 static void htond (double &x)   
61 {
62     if ( sgIsLittleEndian() ) {
63         int    *Double_Overlay;
64         int     Holding_Buffer;
65     
66         Double_Overlay = (int *) &x;
67         Holding_Buffer = Double_Overlay [0];
68     
69         Double_Overlay [0] = htonl (Double_Overlay [1]);
70         Double_Overlay [1] = htonl (Holding_Buffer);
71     } else {
72         return;
73     }
74 }
75
76 // Float version
77 static void htonf (float &x)    
78 {
79     if ( sgIsLittleEndian() ) {
80         int    *Float_Overlay;
81         int     Holding_Buffer;
82     
83         Float_Overlay = (int *) &x;
84         Holding_Buffer = Float_Overlay [0];
85     
86         Float_Overlay [0] = htonl (Holding_Buffer);
87     } else {
88         return;
89     }
90 }
91
92
93 FGNativeFDM::FGNativeFDM() {
94 }
95
96 FGNativeFDM::~FGNativeFDM() {
97 }
98
99
100 // open hailing frequencies
101 bool FGNativeFDM::open() {
102     if ( is_enabled() ) {
103         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
104                 << "is already in use, ignoring" );
105         return false;
106     }
107
108     SGIOChannel *io = get_io_channel();
109
110     if ( ! io->open( get_direction() ) ) {
111         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
112         return false;
113     }
114
115     set_enabled( true );
116
117     // Is this really needed here ????
118     fgSetDouble("/position/sea-level-radius-ft", SG_EQUATORIAL_RADIUS_FT);
119
120     
121
122     return true;
123 }
124
125
126 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
127     unsigned int i;
128
129     FlightProperties fdm_state;
130
131     // Version sanity checking
132     net->version = FG_NET_FDM_VERSION;
133
134     // Aero parameters
135     net->longitude = fdm_state.get_Longitude();
136     net->latitude = fdm_state.get_Latitude();
137     net->altitude = fdm_state.get_Altitude() * SG_FEET_TO_METER;
138     net->agl = fdm_state.get_Altitude_AGL() * SG_FEET_TO_METER;
139     net->phi = fdm_state.get_Phi();
140     net->theta = fdm_state.get_Theta();
141     net->psi = fdm_state.get_Psi();
142     net->alpha = fdm_state.get_Alpha();
143     net->beta = fdm_state.get_Beta();
144     net->phidot = fdm_state.get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
145     net->thetadot = fdm_state.get_Theta_dot_degps()
146         * SG_DEGREES_TO_RADIANS;
147     net->psidot = fdm_state.get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
148
149     net->vcas = fdm_state.get_V_calibrated_kts();
150     net->climb_rate = fdm_state.get_Climb_Rate();
151
152     net->v_north = fdm_state.get_V_north();
153     net->v_east = fdm_state.get_V_east();
154     net->v_down = fdm_state.get_V_down();
155     net->v_wind_body_north = fdm_state.get_uBody();
156     net->v_wind_body_east = fdm_state.get_vBody();
157     net->v_wind_body_down = fdm_state.get_wBody();
158
159     net->A_X_pilot = fdm_state.get_A_X_pilot();
160     net->A_Y_pilot = fdm_state.get_A_Y_pilot();
161     net->A_Z_pilot = fdm_state.get_A_Z_pilot();
162
163     net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
164     net->slip_deg
165       = fgGetDouble("/instrumentation/slip-skid-ball/indicated-slip-skid");
166
167     // Engine parameters
168     net->num_engines = FGNetFDM::FG_MAX_ENGINES;
169     for ( i = 0; i < net->num_engines; ++i ) {
170         SGPropertyNode *node = fgGetNode("engines/engine", i, true);
171         if ( node->getBoolValue( "running" ) ) {
172             net->eng_state[i] = 2;
173         } else if ( node->getBoolValue( "cranking" ) ) {
174             net->eng_state[i] = 1;
175         } else {
176             net->eng_state[i] = 0;
177         }
178         net->rpm[i] = node->getDoubleValue( "rpm" );
179         net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
180         net->fuel_px[i] = node->getDoubleValue( "fuel-px-psi" );
181         net->egt[i] = node->getDoubleValue( "egt-degf" );
182         // cout << "egt = " << aero->EGT << endl;
183         net->cht[i] = node->getDoubleValue( "cht-degf" );
184         net->mp_osi[i] = node->getDoubleValue( "mp-osi" );
185         net->tit[i] = node->getDoubleValue( "tit" );
186         net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
187         net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
188     }
189
190     // Consumables
191     net->num_tanks = FGNetFDM::FG_MAX_TANKS;
192     for ( i = 0; i < net->num_tanks; ++i ) {
193         SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
194         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
195     }
196
197     // Gear and flaps
198     net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
199     for (i = 0; i < net->num_wheels; ++i ) {
200         SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
201         net->wow[i] = node->getIntValue("wow");
202         net->gear_pos[i] = node->getDoubleValue("position-norm");
203         net->gear_steer[i] = node->getDoubleValue("steering-norm");
204         net->gear_compression[i] = node->getDoubleValue("compression-norm");
205     }
206
207     // the following really aren't used in this context
208     net->cur_time = globals->get_time_params()->get_cur_time();
209     net->warp = globals->get_warp();
210     net->visibility = fgGetDouble("/environment/visibility-m");
211
212     // Control surface positions
213     SGPropertyNode *node = fgGetNode("/surface-positions", true);
214     net->elevator = node->getDoubleValue( "elevator-pos-norm" );
215     net->elevator_trim_tab
216         = node->getDoubleValue( "elevator-trim-tab-pos-norm" );
217     // FIXME: CLO 10/28/04 - This really should be separated out into 2 values
218     net->left_flap = node->getDoubleValue( "flap-pos-norm" );
219     net->right_flap = node->getDoubleValue( "flap-pos-norm" );
220     net->left_aileron = node->getDoubleValue( "left-aileron-pos-norm" );
221     net->right_aileron = node->getDoubleValue( "right-aileron-pos-norm" );
222     net->rudder = node->getDoubleValue( "rudder-pos-norm" );
223     net->nose_wheel = node->getDoubleValue( "nose-wheel-pos-norm" );
224     net->speedbrake = node->getDoubleValue( "speedbrake-pos-norm" );
225     net->spoilers = node->getDoubleValue( "spoilers-pos-norm" );
226
227     if ( net_byte_order ) {
228         // Convert the net buffer to network format
229         net->version = htonl(net->version);
230
231         htond(net->longitude);
232         htond(net->latitude);
233         htond(net->altitude);
234         htonf(net->agl);
235         htonf(net->phi);
236         htonf(net->theta);
237         htonf(net->psi);
238         htonf(net->alpha);
239         htonf(net->beta);
240
241         htonf(net->phidot);
242         htonf(net->thetadot);
243         htonf(net->psidot);
244         htonf(net->vcas);
245         htonf(net->climb_rate);
246         htonf(net->v_north);
247         htonf(net->v_east);
248         htonf(net->v_down);
249         htonf(net->v_wind_body_north);
250         htonf(net->v_wind_body_east);
251         htonf(net->v_wind_body_down);
252
253         htonf(net->A_X_pilot);
254         htonf(net->A_Y_pilot);
255         htonf(net->A_Z_pilot);
256
257         htonf(net->stall_warning);
258         htonf(net->slip_deg);
259
260         for ( i = 0; i < net->num_engines; ++i ) {
261             net->eng_state[i] = htonl(net->eng_state[i]);
262             htonf(net->rpm[i]);
263             htonf(net->fuel_flow[i]);
264             htonf(net->fuel_px[i]);
265             htonf(net->egt[i]);
266             htonf(net->cht[i]);
267             htonf(net->mp_osi[i]);
268             htonf(net->tit[i]);
269             htonf(net->oil_temp[i]);
270             htonf(net->oil_px[i]);
271         }
272         net->num_engines = htonl(net->num_engines);
273
274         for ( i = 0; i < net->num_tanks; ++i ) {
275             htonf(net->fuel_quantity[i]);
276         }
277         net->num_tanks = htonl(net->num_tanks);
278
279         for ( i = 0; i < net->num_wheels; ++i ) {
280             net->wow[i] = htonl(net->wow[i]);
281             htonf(net->gear_pos[i]);
282             htonf(net->gear_steer[i]);
283             htonf(net->gear_compression[i]);
284         }
285         net->num_wheels = htonl(net->num_wheels);
286
287         net->cur_time = htonl( net->cur_time );
288         net->warp = htonl( net->warp );
289         htonf(net->visibility);
290
291         htonf(net->elevator);
292         htonf(net->elevator_trim_tab);
293         htonf(net->left_flap);
294         htonf(net->right_flap);
295         htonf(net->left_aileron);
296         htonf(net->right_aileron);
297         htonf(net->rudder);
298         htonf(net->nose_wheel);
299         htonf(net->speedbrake);
300         htonf(net->spoilers);
301     }
302 }
303
304
305 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
306     unsigned int i;
307     FlightProperties fdm_state;
308     
309     if ( net_byte_order ) {
310         // Convert to the net buffer from network format
311         net->version = ntohl(net->version);
312
313         htond(net->longitude);
314         htond(net->latitude);
315         htond(net->altitude);
316         htonf(net->agl);
317         htonf(net->phi);
318         htonf(net->theta);
319         htonf(net->psi);
320         htonf(net->alpha);
321         htonf(net->beta);
322
323         htonf(net->phidot);
324         htonf(net->thetadot);
325         htonf(net->psidot);
326         htonf(net->vcas);
327         htonf(net->climb_rate);
328         htonf(net->v_north);
329         htonf(net->v_east);
330         htonf(net->v_down);
331         htonf(net->v_wind_body_north);
332         htonf(net->v_wind_body_east);
333         htonf(net->v_wind_body_down);
334
335         htonf(net->A_X_pilot);
336         htonf(net->A_Y_pilot);
337         htonf(net->A_Z_pilot);
338
339         htonf(net->stall_warning);
340         htonf(net->slip_deg);
341
342         net->num_engines = htonl(net->num_engines);
343         for ( i = 0; i < net->num_engines; ++i ) {
344             net->eng_state[i] = htonl(net->eng_state[i]);
345             htonf(net->rpm[i]);
346             htonf(net->fuel_flow[i]);
347             htonf(net->fuel_px[i]);
348             htonf(net->egt[i]);
349             htonf(net->cht[i]);
350             htonf(net->mp_osi[i]);
351             htonf(net->tit[i]);
352             htonf(net->oil_temp[i]);
353             htonf(net->oil_px[i]);
354         }
355
356         net->num_tanks = htonl(net->num_tanks);
357         for ( i = 0; i < net->num_tanks; ++i ) {
358             htonf(net->fuel_quantity[i]);
359         }
360
361         net->num_wheels = htonl(net->num_wheels);
362         for ( i = 0; i < net->num_wheels; ++i ) {
363             net->wow[i] = htonl(net->wow[i]);
364             htonf(net->gear_pos[i]);
365             htonf(net->gear_steer[i]);
366             htonf(net->gear_compression[i]);
367         }
368
369         net->cur_time = htonl(net->cur_time);
370         net->warp = ntohl(net->warp);
371         htonf(net->visibility);
372
373         htonf(net->elevator);
374         htonf(net->elevator_trim_tab);
375         htonf(net->left_flap);
376         htonf(net->right_flap);
377         htonf(net->left_aileron);
378         htonf(net->right_aileron);
379         htonf(net->rudder);
380         htonf(net->nose_wheel);
381         htonf(net->speedbrake);
382         htonf(net->spoilers);
383     }
384
385     if ( net->version == FG_NET_FDM_VERSION ) {
386         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
387         // cout << "sea level rad = " << fdm_state.get_Sea_level_radius()
388         //      << endl;
389                                       
390       fdm_state.set_Latitude(net->latitude);
391         fdm_state.set_Longitude(net->longitude);
392         fdm_state.set_Altitude(net->altitude * SG_METER_TO_FEET);
393         
394         if ( net->agl > -9000 ) {
395             fdm_state.set_Altitude_AGL( net->agl * SG_METER_TO_FEET );
396         } else {
397             double agl_m = net->altitude
398               - fdm_state.get_Runway_altitude_m();
399             fdm_state.set_Altitude_AGL( agl_m * SG_METER_TO_FEET );
400         }
401         fdm_state.set_Euler_Angles( net->phi,
402                                           net->theta,
403                                           net->psi );
404         fdm_state.set_Alpha( net->alpha );
405         fdm_state.set_Beta( net->beta );
406         fdm_state.set_Euler_Rates( net->phidot,
407                                          net->thetadot,
408                                          net->psidot );
409         fdm_state.set_V_calibrated_kts( net->vcas );
410         fdm_state.set_Climb_Rate( net->climb_rate );
411         fdm_state.set_Velocities_Local( net->v_north,
412                                               net->v_east,
413                                               net->v_down );
414         fdm_state.set_Velocities_Wind_Body( net->v_wind_body_north,
415                                                   net->v_wind_body_east,
416                                                   net->v_wind_body_down );
417
418         fdm_state.set_Accels_Pilot_Body( net->A_X_pilot,
419                                                net->A_Y_pilot,
420                                                net->A_Z_pilot );
421
422         fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
423         fgSetDouble( "/instrumentation/slip-skid-ball/indicated-slip-skid",
424                      net->slip_deg );
425         fgSetBool( "/instrumentation/slip-skid-ball/override", true );
426
427         for ( i = 0; i < net->num_engines; ++i ) {
428             SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
429             
430             // node->setBoolValue("running", t->isRunning());
431             // node->setBoolValue("cranking", t->isCranking());
432             
433             // cout << net->eng_state[i] << endl;
434             if ( net->eng_state[i] == 0 ) {
435                 node->setBoolValue( "cranking", false );
436                 node->setBoolValue( "running", false );
437             } else if ( net->eng_state[i] == 1 ) {
438                 node->setBoolValue( "cranking", true );
439                 node->setBoolValue( "running", false );
440             } else if ( net->eng_state[i] == 2 ) {
441                 node->setBoolValue( "cranking", false );
442                 node->setBoolValue( "running", true );
443             }
444
445             node->setDoubleValue( "rpm", net->rpm[i] );
446             node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
447             node->setDoubleValue( "fuel-px-psi", net->fuel_px[i] );
448             node->setDoubleValue( "egt-degf", net->egt[i] );
449             node->setDoubleValue( "cht-degf", net->cht[i] );
450             node->setDoubleValue( "mp-osi", net->mp_osi[i] );
451             node->setDoubleValue( "tit", net->tit[i] );
452             node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
453             node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );         
454         }
455
456         for (i = 0; i < net->num_tanks; ++i ) {
457             SGPropertyNode * node
458                 = fgGetNode("/consumables/fuel/tank", i, true);
459             node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
460         }
461
462         for (i = 0; i < net->num_wheels; ++i ) {
463             SGPropertyNode * node = fgGetNode("/gear/gear", i, true);
464             node->setDoubleValue("wow", net->wow[i] );
465             node->setDoubleValue("position-norm", net->gear_pos[i] );
466             node->setDoubleValue("steering-norm", net->gear_steer[i] );
467             node->setDoubleValue("compression-norm", net->gear_compression[i] );
468         }
469
470         /* these are ignored for now  ... */
471         /*
472         if ( net->cur_time ) {
473             fgSetLong("/sim/time/cur-time-override", net->cur_time);
474         }
475
476         globals->set_warp( net->warp );
477         last_warp = net->warp;
478         */
479
480         SGPropertyNode *node = fgGetNode("/surface-positions", true);
481         node->setDoubleValue("elevator-pos-norm", net->elevator);
482         node->setDoubleValue("elevator-trim-tab-pos-norm",
483                              net->elevator_trim_tab);
484         // FIXME: CLO 10/28/04 - This really should be separated out
485         // into 2 values
486         node->setDoubleValue("flap-pos-norm", net->left_flap);
487         node->setDoubleValue("flap-pos-norm", net->right_flap);
488         node->setDoubleValue("left-aileron-pos-norm", net->left_aileron);
489         node->setDoubleValue("right-aileron-pos-norm", net->right_aileron);
490         node->setDoubleValue("rudder-pos-norm", net->rudder);
491         node->setDoubleValue("nose-wheel-pos-norm", net->nose_wheel);
492         node->setDoubleValue("speedbrake-pos-norm", net->speedbrake);
493         node->setDoubleValue("spoilers-pos-norm", net->spoilers);
494     } else {
495         SG_LOG( SG_IO, SG_ALERT,
496                 "Error: version mismatch in FGNetFDM2Props()" );
497         SG_LOG( SG_IO, SG_ALERT,
498                 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
499         SG_LOG( SG_IO, SG_ALERT,
500                 "\tNeeds to upgrade net_fdm.hxx and recompile." );
501     }
502 }
503
504
505 // process work for this port
506 bool FGNativeFDM::process() {
507     SGIOChannel *io = get_io_channel();
508     int length = sizeof(buf);
509
510     if ( get_direction() == SG_IO_OUT ) {
511
512         FGProps2NetFDM( &buf );
513         if ( ! io->write( (char *)(& buf), length ) ) {
514             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
515             return false;
516         }
517     } else if ( get_direction() == SG_IO_IN ) {
518         if ( io->get_type() == sgFileType ) {
519             if ( io->read( (char *)(& buf), length ) == length ) {
520                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
521                 FGNetFDM2Props( &buf );
522             }
523         } else {
524             int result;
525             result = io->read( (char *)(& buf), length );
526             while ( result == length ) {
527                 if ( result == length ) {
528                     SG_LOG( SG_IO, SG_DEBUG, "  Success reading data." );
529                     FGNetFDM2Props( &buf );
530                 }
531                 result = io->read( (char *)(& buf), length );
532             }
533         }
534     }
535
536     return true;
537 }
538
539
540 // close the channel
541 bool FGNativeFDM::close() {
542     SGIOChannel *io = get_io_channel();
543
544     set_enabled( false );
545
546     if ( ! io->close() ) {
547         return false;
548     }
549
550     return true;
551 }