]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
eb2a9a5ca9a1ccd692b6342e748a8b2764c2e432
[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
38 #include "native_fdm.hxx"
39
40 // FreeBSD works better with this included last ... (?)
41 #if defined(WIN32) && !defined(__CYGWIN__)
42 #  include <windows.h>
43 #else
44 #  include <netinet/in.h>       // htonl() ntohl()
45 #endif
46
47
48 // The function htond is defined this way due to the way some
49 // processors and OSes treat floating point values.  Some will raise
50 // an exception whenever a "bad" floating point value is loaded into a
51 // floating point register.  Solaris is notorious for this, but then
52 // so is LynxOS on the PowerPC.  By translating the data in place,
53 // there is no need to load a FP register with the "corruped" floating
54 // point value.  By doing the BIG_ENDIAN test, I can optimize the
55 // routine for big-endian processors so it can be as efficient as
56 // possible
57 static void htond (double &x)   
58 {
59     if ( sgIsLittleEndian() ) {
60         int    *Double_Overlay;
61         int     Holding_Buffer;
62     
63         Double_Overlay = (int *) &x;
64         Holding_Buffer = Double_Overlay [0];
65     
66         Double_Overlay [0] = htonl (Double_Overlay [1]);
67         Double_Overlay [1] = htonl (Holding_Buffer);
68     } else {
69         return;
70     }
71 }
72
73
74 FGNativeFDM::FGNativeFDM() {
75 }
76
77 FGNativeFDM::~FGNativeFDM() {
78 }
79
80
81 // open hailing frequencies
82 bool FGNativeFDM::open() {
83     if ( is_enabled() ) {
84         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
85                 << "is already in use, ignoring" );
86         return false;
87     }
88
89     SGIOChannel *io = get_io_channel();
90
91     if ( ! io->open( get_direction() ) ) {
92         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
93         return false;
94     }
95
96     set_enabled( true );
97
98     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
99     return true;
100 }
101
102
103 void FGProps2NetFDM( FGNetFDM *net ) {
104     int i;
105
106     // Version sanity checking
107     net->version = FG_NET_FDM_VERSION;
108
109     // Aero parameters
110     net->longitude = cur_fdm_state->get_Longitude();
111     net->latitude = cur_fdm_state->get_Latitude();
112     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
113     net->phi = cur_fdm_state->get_Phi();
114     net->theta = cur_fdm_state->get_Theta();
115     net->psi = cur_fdm_state->get_Psi();
116     net->phidot = cur_fdm_state->get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
117     net->thetadot = cur_fdm_state->get_Theta_dot_degps()
118         * SG_DEGREES_TO_RADIANS;
119     net->psidot = cur_fdm_state->get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
120
121     net->vcas = cur_fdm_state->get_V_calibrated_kts();
122     net->climb_rate = cur_fdm_state->get_Climb_Rate();
123
124     net->v_north = cur_fdm_state->get_V_north();
125     net->v_east = cur_fdm_state->get_V_east();
126     net->v_down = cur_fdm_state->get_V_down();
127     net->v_wind_body_north = cur_fdm_state->get_uBody();
128     net->v_wind_body_east = cur_fdm_state->get_vBody();
129     net->v_wind_body_down = cur_fdm_state->get_wBody();
130     net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
131
132     net->A_X_pilot = cur_fdm_state->get_A_X_pilot();
133     net->A_Y_pilot = cur_fdm_state->get_A_Y_pilot();
134     net->A_Z_pilot = cur_fdm_state->get_A_Z_pilot();
135
136     // Engine parameters
137     net->num_engines = FGNetFDM::FG_MAX_ENGINES;
138     for ( i = 0; i < net->num_engines; ++i ) {
139         SGPropertyNode *node = fgGetNode("engines/engine", i, true);
140         if ( node->getBoolValue( "running" ) ) {
141             net->eng_state[0] = 2;
142         } else if ( node->getBoolValue( "cranking" ) ) {
143             net->eng_state[0] = 1;
144         } else {
145             net->eng_state[0] = 0;
146         }
147         net->rpm[i] = node->getDoubleValue( "rpm" );
148         net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
149         net->EGT[i] = node->getDoubleValue( "egt-degf" );
150         // cout << "egt = " << aero->EGT << endl;
151         net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
152         net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
153     }
154
155     // Consumables
156     net->num_tanks = FGNetFDM::FG_MAX_TANKS;
157     for ( i = 0; i < net->num_tanks; ++i ) {
158         SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
159         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
160     }
161
162     // Gear and flaps
163     net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
164     for (i = 0; i < net->num_wheels; ++i ) {
165         SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
166         net->wow[i] = node->getDoubleValue("wow");
167     }
168
169     // cout << "Flap deflection = " << aero->dflap << endl;
170     net->flap_deflection = fgGetDouble("/surface-positions/flap-pos-norm" );
171
172     // the following really aren't used in this context
173     net->cur_time = globals->get_time_params()->get_cur_time();
174     net->warp = globals->get_warp();
175     net->visibility = fgGetDouble("/environment/visibility-m");
176
177     // Convert the net buffer to network format
178     net->version = htonl(net->version);
179
180     htond(net->longitude);
181     htond(net->latitude);
182     htond(net->altitude);
183     htond(net->phi);
184     htond(net->theta);
185     htond(net->psi);
186
187     htond(net->phidot);
188     htond(net->thetadot);
189     htond(net->psidot);
190     htond(net->vcas);
191     htond(net->climb_rate);
192     htond(net->v_north);
193     htond(net->v_east);
194     htond(net->v_down);
195     htond(net->v_wind_body_north);
196     htond(net->v_wind_body_east);
197     htond(net->v_wind_body_down);
198     htond(net->stall_warning);
199
200     htond(net->A_X_pilot);
201     htond(net->A_Y_pilot);
202     htond(net->A_Z_pilot);
203
204     for ( i = 0; i < net->num_engines; ++i ) {
205         htonl(net->eng_state[i]);
206         htond(net->rpm[i]);
207         htond(net->fuel_flow[i]);
208         htond(net->EGT[i]);
209         htond(net->oil_temp[i]);
210         htond(net->oil_px[i]);
211     }
212     net->num_engines = htonl(net->num_engines);
213
214     for ( i = 0; i < net->num_tanks; ++i ) {
215         htond(net->fuel_quantity[i]);
216     }
217     net->num_tanks = htonl(net->num_tanks);
218
219     for ( i = 0; i < net->num_wheels; ++i ) {
220         net->wow[i] = htonl(net->wow[i]);
221     }
222     net->num_wheels = htonl(net->num_wheels);
223     htond(net->flap_deflection);
224
225     net->cur_time = htonl( net->cur_time );
226     net->warp = htonl( net->warp );
227     htond(net->visibility);
228 }
229
230
231 void FGNetFDM2Props( FGNetFDM *net ) {
232     int i;
233
234     // Convert to the net buffer from network format
235     net->version = ntohl(net->version);
236
237     htond(net->longitude);
238     htond(net->latitude);
239     htond(net->altitude);
240     htond(net->phi);
241     htond(net->theta);
242     htond(net->psi);
243
244     htond(net->phidot);
245     htond(net->thetadot);
246     htond(net->psidot);
247     htond(net->vcas);
248     htond(net->climb_rate);
249     htond(net->v_north);
250     htond(net->v_east);
251     htond(net->v_down);
252     htond(net->v_wind_body_north);
253     htond(net->v_wind_body_east);
254     htond(net->v_wind_body_down);
255     htond(net->stall_warning);
256
257     htond(net->A_X_pilot);
258     htond(net->A_Y_pilot);
259     htond(net->A_Z_pilot);
260
261     net->num_engines = htonl(net->num_engines);
262     for ( i = 0; i < net->num_engines; ++i ) {
263         htonl(net->eng_state[i]);
264         htond(net->rpm[i]);
265         htond(net->fuel_flow[i]);
266         htond(net->EGT[i]);
267         htond(net->oil_temp[i]);
268         htond(net->oil_px[i]);
269     }
270
271     net->num_tanks = htonl(net->num_tanks);
272     for ( i = 0; i < net->num_tanks; ++i ) {
273         htond(net->fuel_quantity[i]);
274     }
275
276     net->num_wheels = htonl(net->num_wheels);
277     // I don't need to convert the Wow flags, since they are one byte in size
278     htond(net->flap_deflection);
279
280     net->cur_time = ntohl(net->cur_time);
281     net->warp = ntohl(net->warp);
282     htond(net->visibility);
283
284     if ( net->version == FG_NET_FDM_VERSION ) {
285         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
286         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
287         //      << endl;
288         cur_fdm_state->_updateGeodeticPosition( net->latitude,
289                                                 net->longitude,
290                                                 net->altitude
291                                                   * SG_METER_TO_FEET );
292         cur_fdm_state->_set_Euler_Angles( net->phi,
293                                           net->theta,
294                                           net->psi );
295         cur_fdm_state->_set_Euler_Rates( net->phidot,
296                                          net->thetadot,
297                                          net->psidot );
298         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
299         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
300         cur_fdm_state->_set_Velocities_Local( net->v_north,
301                                               net->v_east,
302                                               net->v_down );
303         cur_fdm_state->_set_Velocities_Wind_Body( net->v_wind_body_north,
304                                                   net->v_wind_body_east,
305                                                   net->v_wind_body_down );
306
307         fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
308         cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
309                                                net->A_Y_pilot,
310                                                net->A_Z_pilot );
311
312         for ( i = 0; i < net->num_engines; ++i ) {
313             SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
314             
315             // node->setBoolValue("running", t->isRunning());
316             // node->setBoolValue("cranking", t->isCranking());
317             
318             // cout << net->eng_state[i] << endl;
319             if ( net->eng_state[i] == 0 ) {
320                 node->setBoolValue( "cranking", false );
321                 node->setBoolValue( "running", false );
322             } else if ( net->eng_state[i] == 1 ) {
323                 node->setBoolValue( "cranking", true );
324                 node->setBoolValue( "running", false );
325             } else if ( net->eng_state[i] == 2 ) {
326                 node->setBoolValue( "cranking", false );
327                 node->setBoolValue( "running", true );
328             }
329
330             node->setDoubleValue( "rpm", net->rpm[i] );
331             node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
332             node->setDoubleValue( "egt-degf", net->EGT[i] );
333             node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
334             node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );         
335         }
336
337         for (i = 0; i < net->num_tanks; ++i ) {
338             SGPropertyNode * node
339                 = fgGetNode("/consumables/fuel/tank", i, true);
340             node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
341         }
342
343         for (i = 0; i < net->num_wheels; ++i ) {
344             SGPropertyNode * node
345                 = fgGetNode("/gear/gear", i, true);
346             node->setDoubleValue("wow", net->wow[i] );
347         }
348
349         fgSetDouble("/surface-positions/flap-pos-norm", net->flap_deflection);
350         SGPropertyNode * node = fgGetNode("/controls", true);
351         fgSetDouble("/surface-positions/elevator-pos-norm", 
352                     node->getDoubleValue( "elevator" ));
353         fgSetDouble("/surface-positions/rudder-pos-norm", 
354                     node->getDoubleValue( "rudder" ));
355         fgSetDouble("/surface-positions/left-aileron-pos-norm", 
356                     node->getDoubleValue( "aileron" ));
357         fgSetDouble("/surface-positions/right-aileron-pos-norm", 
358                     -node->getDoubleValue( "aileron" ));
359
360         /* these are ignored for now  ... */
361         /*
362         if ( net->cur_time ) {
363             fgSetLong("/sim/time/cur-time-override", net->cur_time);
364         }
365
366         globals->set_warp( net->warp );
367         last_warp = net->warp;
368         */
369     } else {
370         SG_LOG( SG_IO, SG_ALERT,
371                 "Error: version mismatch in FGNetFDM2Props()" );
372         SG_LOG( SG_IO, SG_ALERT,
373                 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
374         SG_LOG( SG_IO, SG_ALERT,
375                 "\tNeeds to upgrade net_fdm.hxx and recompile." );
376     }
377 }
378
379
380 // process work for this port
381 bool FGNativeFDM::process() {
382     SGIOChannel *io = get_io_channel();
383     int length = sizeof(buf);
384
385     if ( get_direction() == SG_IO_OUT ) {
386         // cout << "size of cur_fdm_state = " << length << endl;
387         FGProps2NetFDM( &buf );
388         if ( ! io->write( (char *)(& buf), length ) ) {
389             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
390             return false;
391         }
392     } else if ( get_direction() == SG_IO_IN ) {
393         if ( io->get_type() == sgFileType ) {
394             if ( io->read( (char *)(& buf), length ) == length ) {
395                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
396                 FGNetFDM2Props( &buf );
397             }
398         } else {
399             while ( io->read( (char *)(& buf), length ) == length ) {
400                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
401                 FGNetFDM2Props( &buf );
402             }
403         }
404     }
405
406     return true;
407 }
408
409
410 // close the channel
411 bool FGNativeFDM::close() {
412     SGIOChannel *io = get_io_channel();
413
414     set_enabled( false );
415
416     if ( ! io->close() ) {
417         return false;
418     }
419
420     return true;
421 }