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