]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
Merge branch 'jmt/track-bug' into next
[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/flight.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     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
119
120     return true;
121 }
122
123
124 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
125     unsigned int i;
126
127     // Version sanity checking
128     net->version = FG_NET_FDM_VERSION;
129
130     // Aero parameters
131     net->longitude = cur_fdm_state->get_Longitude();
132     net->latitude = cur_fdm_state->get_Latitude();
133     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
134     net->agl = cur_fdm_state->get_Altitude_AGL() * SG_FEET_TO_METER;
135     net->phi = cur_fdm_state->get_Phi();
136     net->theta = cur_fdm_state->get_Theta();
137     net->psi = cur_fdm_state->get_Psi();
138     net->alpha = cur_fdm_state->get_Alpha();
139     net->beta = cur_fdm_state->get_Beta();
140     net->phidot = cur_fdm_state->get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
141     net->thetadot = cur_fdm_state->get_Theta_dot_degps()
142         * SG_DEGREES_TO_RADIANS;
143     net->psidot = cur_fdm_state->get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
144
145     net->vcas = cur_fdm_state->get_V_calibrated_kts();
146     net->climb_rate = cur_fdm_state->get_Climb_Rate();
147
148     net->v_north = cur_fdm_state->get_V_north();
149     net->v_east = cur_fdm_state->get_V_east();
150     net->v_down = cur_fdm_state->get_V_down();
151     net->v_wind_body_north = cur_fdm_state->get_uBody();
152     net->v_wind_body_east = cur_fdm_state->get_vBody();
153     net->v_wind_body_down = cur_fdm_state->get_wBody();
154
155     net->A_X_pilot = cur_fdm_state->get_A_X_pilot();
156     net->A_Y_pilot = cur_fdm_state->get_A_Y_pilot();
157     net->A_Z_pilot = cur_fdm_state->get_A_Z_pilot();
158
159     net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
160     net->slip_deg
161       = fgGetDouble("/instrumentation/slip-skid-ball/indicated-slip-skid");
162
163     // Engine parameters
164     net->num_engines = FGNetFDM::FG_MAX_ENGINES;
165     for ( i = 0; i < net->num_engines; ++i ) {
166         SGPropertyNode *node = fgGetNode("engines/engine", i, true);
167         if ( node->getBoolValue( "running" ) ) {
168             net->eng_state[i] = 2;
169         } else if ( node->getBoolValue( "cranking" ) ) {
170             net->eng_state[i] = 1;
171         } else {
172             net->eng_state[i] = 0;
173         }
174         net->rpm[i] = node->getDoubleValue( "rpm" );
175         net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
176         net->fuel_px[i] = node->getDoubleValue( "fuel-px-psi" );
177         net->egt[i] = node->getDoubleValue( "egt-degf" );
178         // cout << "egt = " << aero->EGT << endl;
179         net->cht[i] = node->getDoubleValue( "cht-degf" );
180         net->mp_osi[i] = node->getDoubleValue( "mp-osi" );
181         net->tit[i] = node->getDoubleValue( "tit" );
182         net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
183         net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
184     }
185
186     // Consumables
187     net->num_tanks = FGNetFDM::FG_MAX_TANKS;
188     for ( i = 0; i < net->num_tanks; ++i ) {
189         SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
190         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
191     }
192
193     // Gear and flaps
194     net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
195     for (i = 0; i < net->num_wheels; ++i ) {
196         SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
197         net->wow[i] = node->getIntValue("wow");
198         net->gear_pos[i] = node->getDoubleValue("position-norm");
199         net->gear_steer[i] = node->getDoubleValue("steering-norm");
200         net->gear_compression[i] = node->getDoubleValue("compression-norm");
201     }
202
203     // the following really aren't used in this context
204     net->cur_time = globals->get_time_params()->get_cur_time();
205     net->warp = globals->get_warp();
206     net->visibility = fgGetDouble("/environment/visibility-m");
207
208     // Control surface positions
209     SGPropertyNode *node = fgGetNode("/surface-positions", true);
210     net->elevator = node->getDoubleValue( "elevator-pos-norm" );
211     net->elevator_trim_tab
212         = node->getDoubleValue( "elevator-trim-tab-pos-norm" );
213     // FIXME: CLO 10/28/04 - This really should be separated out into 2 values
214     net->left_flap = node->getDoubleValue( "flap-pos-norm" );
215     net->right_flap = node->getDoubleValue( "flap-pos-norm" );
216     net->left_aileron = node->getDoubleValue( "left-aileron-pos-norm" );
217     net->right_aileron = node->getDoubleValue( "right-aileron-pos-norm" );
218     net->rudder = node->getDoubleValue( "rudder-pos-norm" );
219     net->nose_wheel = node->getDoubleValue( "nose-wheel-pos-norm" );
220     net->speedbrake = node->getDoubleValue( "speedbrake-pos-norm" );
221     net->spoilers = node->getDoubleValue( "spoilers-pos-norm" );
222
223     if ( net_byte_order ) {
224         // Convert the net buffer to network format
225         net->version = htonl(net->version);
226
227         htond(net->longitude);
228         htond(net->latitude);
229         htond(net->altitude);
230         htonf(net->agl);
231         htonf(net->phi);
232         htonf(net->theta);
233         htonf(net->psi);
234         htonf(net->alpha);
235         htonf(net->beta);
236
237         htonf(net->phidot);
238         htonf(net->thetadot);
239         htonf(net->psidot);
240         htonf(net->vcas);
241         htonf(net->climb_rate);
242         htonf(net->v_north);
243         htonf(net->v_east);
244         htonf(net->v_down);
245         htonf(net->v_wind_body_north);
246         htonf(net->v_wind_body_east);
247         htonf(net->v_wind_body_down);
248
249         htonf(net->A_X_pilot);
250         htonf(net->A_Y_pilot);
251         htonf(net->A_Z_pilot);
252
253         htonf(net->stall_warning);
254         htonf(net->slip_deg);
255
256         for ( i = 0; i < net->num_engines; ++i ) {
257             net->eng_state[i] = htonl(net->eng_state[i]);
258             htonf(net->rpm[i]);
259             htonf(net->fuel_flow[i]);
260             htonf(net->fuel_px[i]);
261             htonf(net->egt[i]);
262             htonf(net->cht[i]);
263             htonf(net->mp_osi[i]);
264             htonf(net->tit[i]);
265             htonf(net->oil_temp[i]);
266             htonf(net->oil_px[i]);
267         }
268         net->num_engines = htonl(net->num_engines);
269
270         for ( i = 0; i < net->num_tanks; ++i ) {
271             htonf(net->fuel_quantity[i]);
272         }
273         net->num_tanks = htonl(net->num_tanks);
274
275         for ( i = 0; i < net->num_wheels; ++i ) {
276             net->wow[i] = htonl(net->wow[i]);
277             htonf(net->gear_pos[i]);
278             htonf(net->gear_steer[i]);
279             htonf(net->gear_compression[i]);
280         }
281         net->num_wheels = htonl(net->num_wheels);
282
283         net->cur_time = htonl( net->cur_time );
284         net->warp = htonl( net->warp );
285         htonf(net->visibility);
286
287         htonf(net->elevator);
288         htonf(net->elevator_trim_tab);
289         htonf(net->left_flap);
290         htonf(net->right_flap);
291         htonf(net->left_aileron);
292         htonf(net->right_aileron);
293         htonf(net->rudder);
294         htonf(net->nose_wheel);
295         htonf(net->speedbrake);
296         htonf(net->spoilers);
297     }
298 }
299
300
301 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
302     unsigned int i;
303
304     if ( net_byte_order ) {
305         // Convert to the net buffer from network format
306         net->version = ntohl(net->version);
307
308         htond(net->longitude);
309         htond(net->latitude);
310         htond(net->altitude);
311         htonf(net->agl);
312         htonf(net->phi);
313         htonf(net->theta);
314         htonf(net->psi);
315         htonf(net->alpha);
316         htonf(net->beta);
317
318         htonf(net->phidot);
319         htonf(net->thetadot);
320         htonf(net->psidot);
321         htonf(net->vcas);
322         htonf(net->climb_rate);
323         htonf(net->v_north);
324         htonf(net->v_east);
325         htonf(net->v_down);
326         htonf(net->v_wind_body_north);
327         htonf(net->v_wind_body_east);
328         htonf(net->v_wind_body_down);
329
330         htonf(net->A_X_pilot);
331         htonf(net->A_Y_pilot);
332         htonf(net->A_Z_pilot);
333
334         htonf(net->stall_warning);
335         htonf(net->slip_deg);
336
337         net->num_engines = htonl(net->num_engines);
338         for ( i = 0; i < net->num_engines; ++i ) {
339             net->eng_state[i] = htonl(net->eng_state[i]);
340             htonf(net->rpm[i]);
341             htonf(net->fuel_flow[i]);
342             htonf(net->fuel_px[i]);
343             htonf(net->egt[i]);
344             htonf(net->cht[i]);
345             htonf(net->mp_osi[i]);
346             htonf(net->tit[i]);
347             htonf(net->oil_temp[i]);
348             htonf(net->oil_px[i]);
349         }
350
351         net->num_tanks = htonl(net->num_tanks);
352         for ( i = 0; i < net->num_tanks; ++i ) {
353             htonf(net->fuel_quantity[i]);
354         }
355
356         net->num_wheels = htonl(net->num_wheels);
357         for ( i = 0; i < net->num_wheels; ++i ) {
358             net->wow[i] = htonl(net->wow[i]);
359             htonf(net->gear_pos[i]);
360             htonf(net->gear_steer[i]);
361             htonf(net->gear_compression[i]);
362         }
363
364         net->cur_time = htonl(net->cur_time);
365         net->warp = ntohl(net->warp);
366         htonf(net->visibility);
367
368         htonf(net->elevator);
369         htonf(net->elevator_trim_tab);
370         htonf(net->left_flap);
371         htonf(net->right_flap);
372         htonf(net->left_aileron);
373         htonf(net->right_aileron);
374         htonf(net->rudder);
375         htonf(net->nose_wheel);
376         htonf(net->speedbrake);
377         htonf(net->spoilers);
378     }
379
380     if ( net->version == FG_NET_FDM_VERSION ) {
381         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
382         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
383         //      << endl;
384         cur_fdm_state->_updateGeodeticPosition( net->latitude,
385                                                 net->longitude,
386                                                 net->altitude
387                                                   * SG_METER_TO_FEET );
388         if ( net->agl > -9000 ) {
389             cur_fdm_state->_set_Altitude_AGL( net->agl * SG_METER_TO_FEET );
390         } else {
391             double agl_m = net->altitude
392               - cur_fdm_state->get_Runway_altitude_m();
393             cur_fdm_state->_set_Altitude_AGL( agl_m * SG_METER_TO_FEET );
394         }
395         cur_fdm_state->_set_Euler_Angles( net->phi,
396                                           net->theta,
397                                           net->psi );
398         cur_fdm_state->_set_Alpha( net->alpha );
399         cur_fdm_state->_set_Beta( net->beta );
400         cur_fdm_state->_set_Euler_Rates( net->phidot,
401                                          net->thetadot,
402                                          net->psidot );
403         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
404         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
405         cur_fdm_state->_set_Velocities_Local( net->v_north,
406                                               net->v_east,
407                                               net->v_down );
408         cur_fdm_state->_set_Velocities_Wind_Body( net->v_wind_body_north,
409                                                   net->v_wind_body_east,
410                                                   net->v_wind_body_down );
411
412         cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
413                                                net->A_Y_pilot,
414                                                net->A_Z_pilot );
415
416         fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
417         fgSetDouble( "/instrumentation/slip-skid-ball/indicated-slip-skid",
418                      net->slip_deg );
419         fgSetBool( "/instrumentation/slip-skid-ball/override", true );
420
421         for ( i = 0; i < net->num_engines; ++i ) {
422             SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
423             
424             // node->setBoolValue("running", t->isRunning());
425             // node->setBoolValue("cranking", t->isCranking());
426             
427             // cout << net->eng_state[i] << endl;
428             if ( net->eng_state[i] == 0 ) {
429                 node->setBoolValue( "cranking", false );
430                 node->setBoolValue( "running", false );
431             } else if ( net->eng_state[i] == 1 ) {
432                 node->setBoolValue( "cranking", true );
433                 node->setBoolValue( "running", false );
434             } else if ( net->eng_state[i] == 2 ) {
435                 node->setBoolValue( "cranking", false );
436                 node->setBoolValue( "running", true );
437             }
438
439             node->setDoubleValue( "rpm", net->rpm[i] );
440             node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
441             node->setDoubleValue( "fuel-px-psi", net->fuel_px[i] );
442             node->setDoubleValue( "egt-degf", net->egt[i] );
443             node->setDoubleValue( "cht-degf", net->cht[i] );
444             node->setDoubleValue( "mp-osi", net->mp_osi[i] );
445             node->setDoubleValue( "tit", net->tit[i] );
446             node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
447             node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );         
448         }
449
450         for (i = 0; i < net->num_tanks; ++i ) {
451             SGPropertyNode * node
452                 = fgGetNode("/consumables/fuel/tank", i, true);
453             node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
454         }
455
456         for (i = 0; i < net->num_wheels; ++i ) {
457             SGPropertyNode * node = fgGetNode("/gear/gear", i, true);
458             node->setDoubleValue("wow", net->wow[i] );
459             node->setDoubleValue("position-norm", net->gear_pos[i] );
460             node->setDoubleValue("steering-norm", net->gear_steer[i] );
461             node->setDoubleValue("compression-norm", net->gear_compression[i] );
462         }
463
464         /* these are ignored for now  ... */
465         /*
466         if ( net->cur_time ) {
467             fgSetLong("/sim/time/cur-time-override", net->cur_time);
468         }
469
470         globals->set_warp( net->warp );
471         last_warp = net->warp;
472         */
473
474         SGPropertyNode *node = fgGetNode("/surface-positions", true);
475         node->setDoubleValue("elevator-pos-norm", net->elevator);
476         node->setDoubleValue("elevator-trim-tab-pos-norm",
477                              net->elevator_trim_tab);
478         // FIXME: CLO 10/28/04 - This really should be separated out
479         // into 2 values
480         node->setDoubleValue("flap-pos-norm", net->left_flap);
481         node->setDoubleValue("flap-pos-norm", net->right_flap);
482         node->setDoubleValue("left-aileron-pos-norm", net->left_aileron);
483         node->setDoubleValue("right-aileron-pos-norm", net->right_aileron);
484         node->setDoubleValue("rudder-pos-norm", net->rudder);
485         node->setDoubleValue("nose-wheel-pos-norm", net->nose_wheel);
486         node->setDoubleValue("speedbrake-pos-norm", net->speedbrake);
487         node->setDoubleValue("spoilers-pos-norm", net->spoilers);
488     } else {
489         SG_LOG( SG_IO, SG_ALERT,
490                 "Error: version mismatch in FGNetFDM2Props()" );
491         SG_LOG( SG_IO, SG_ALERT,
492                 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
493         SG_LOG( SG_IO, SG_ALERT,
494                 "\tNeeds to upgrade net_fdm.hxx and recompile." );
495     }
496 }
497
498
499 // process work for this port
500 bool FGNativeFDM::process() {
501     SGIOChannel *io = get_io_channel();
502     int length = sizeof(buf);
503
504     if ( get_direction() == SG_IO_OUT ) {
505         // cout << "size of cur_fdm_state = " << length << endl;
506         FGProps2NetFDM( &buf );
507         if ( ! io->write( (char *)(& buf), length ) ) {
508             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
509             return false;
510         }
511     } else if ( get_direction() == SG_IO_IN ) {
512         if ( io->get_type() == sgFileType ) {
513             if ( io->read( (char *)(& buf), length ) == length ) {
514                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
515                 FGNetFDM2Props( &buf );
516             }
517         } else {
518             int result;
519             result = io->read( (char *)(& buf), length );
520             while ( result == length ) {
521                 if ( result == length ) {
522                     SG_LOG( SG_IO, SG_DEBUG, "  Success reading data." );
523                     FGNetFDM2Props( &buf );
524                 }
525                 result = io->read( (char *)(& buf), length );
526             }
527         }
528     }
529
530     return true;
531 }
532
533
534 // close the channel
535 bool FGNativeFDM::close() {
536     SGIOChannel *io = get_io_channel();
537
538     set_enabled( false );
539
540     if ( ! io->close() ) {
541         return false;
542     }
543
544     return true;
545 }