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