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