1 // native_fdm.cxx -- FGFS "Native" flight dynamics protocal class
3 // Written by Curtis Olson, started September 2001.
5 // Copyright (C) 2001 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
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>
33 #include <FDM/flight.hxx>
34 #include <Time/tmp.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
38 #include "native_fdm.hxx"
40 // FreeBSD works better with this included last ... (?)
41 #if defined(WIN32) && !defined(__CYGWIN__)
44 # include <netinet/in.h> // htonl() ntohl()
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
57 static void htond (double &x)
59 if ( sgIsLittleEndian() ) {
63 Double_Overlay = (int *) &x;
64 Holding_Buffer = Double_Overlay [0];
66 Double_Overlay [0] = htonl (Double_Overlay [1]);
67 Double_Overlay [1] = htonl (Holding_Buffer);
74 FGNativeFDM::FGNativeFDM() {
77 FGNativeFDM::~FGNativeFDM() {
81 // open hailing frequencies
82 bool FGNativeFDM::open() {
84 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
85 << "is already in use, ignoring" );
89 SGIOChannel *io = get_io_channel();
91 if ( ! io->open( get_direction() ) ) {
92 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
98 // initialize time stamp to something reasonably current
101 // Is this really needed here ????
102 cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
108 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
111 // Version sanity checking
112 net->version = FG_NET_FDM_VERSION;
115 net->longitude = cur_fdm_state->get_Longitude();
116 net->latitude = cur_fdm_state->get_Latitude();
117 net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
118 net->phi = cur_fdm_state->get_Phi();
119 net->theta = cur_fdm_state->get_Theta();
120 net->psi = cur_fdm_state->get_Psi();
121 net->phidot = cur_fdm_state->get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
122 net->thetadot = cur_fdm_state->get_Theta_dot_degps()
123 * SG_DEGREES_TO_RADIANS;
124 net->psidot = cur_fdm_state->get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
126 net->vcas = cur_fdm_state->get_V_calibrated_kts();
127 net->climb_rate = cur_fdm_state->get_Climb_Rate();
129 net->v_north = cur_fdm_state->get_V_north();
130 net->v_east = cur_fdm_state->get_V_east();
131 net->v_down = cur_fdm_state->get_V_down();
132 net->v_wind_body_north = cur_fdm_state->get_uBody();
133 net->v_wind_body_east = cur_fdm_state->get_vBody();
134 net->v_wind_body_down = cur_fdm_state->get_wBody();
135 net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
137 net->A_X_pilot = cur_fdm_state->get_A_X_pilot();
138 net->A_Y_pilot = cur_fdm_state->get_A_Y_pilot();
139 net->A_Z_pilot = cur_fdm_state->get_A_Z_pilot();
142 net->num_engines = FGNetFDM::FG_MAX_ENGINES;
143 for ( i = 0; i < net->num_engines; ++i ) {
144 SGPropertyNode *node = fgGetNode("engines/engine", i, true);
145 if ( node->getBoolValue( "running" ) ) {
146 net->eng_state[0] = 2;
147 } else if ( node->getBoolValue( "cranking" ) ) {
148 net->eng_state[0] = 1;
150 net->eng_state[0] = 0;
152 net->rpm[i] = node->getDoubleValue( "rpm" );
153 net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
154 net->EGT[i] = node->getDoubleValue( "egt-degf" );
155 // cout << "egt = " << aero->EGT << endl;
156 net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
157 net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
161 net->num_tanks = FGNetFDM::FG_MAX_TANKS;
162 for ( i = 0; i < net->num_tanks; ++i ) {
163 SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
164 net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
168 net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
169 for (i = 0; i < net->num_wheels; ++i ) {
170 SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
171 net->wow[i] = node->getDoubleValue("wow");
174 // cout << "Flap deflection = " << aero->dflap << endl;
175 net->flap_deflection = fgGetDouble("/surface-positions/flap-pos-norm" );
177 // the following really aren't used in this context
178 net->cur_time = globals->get_time_params()->get_cur_time();
179 net->warp = globals->get_warp();
180 net->visibility = fgGetDouble("/environment/visibility-m");
182 if ( net_byte_order ) {
183 // Convert the net buffer to network format
184 net->version = htonl(net->version);
186 htond(net->longitude);
187 htond(net->latitude);
188 htond(net->altitude);
194 htond(net->thetadot);
197 htond(net->climb_rate);
201 htond(net->v_wind_body_north);
202 htond(net->v_wind_body_east);
203 htond(net->v_wind_body_down);
204 htond(net->stall_warning);
206 htond(net->A_X_pilot);
207 htond(net->A_Y_pilot);
208 htond(net->A_Z_pilot);
210 for ( i = 0; i < net->num_engines; ++i ) {
211 htonl(net->eng_state[i]);
213 htond(net->fuel_flow[i]);
215 htond(net->oil_temp[i]);
216 htond(net->oil_px[i]);
218 net->num_engines = htonl(net->num_engines);
220 for ( i = 0; i < net->num_tanks; ++i ) {
221 htond(net->fuel_quantity[i]);
223 net->num_tanks = htonl(net->num_tanks);
225 for ( i = 0; i < net->num_wheels; ++i ) {
226 net->wow[i] = htonl(net->wow[i]);
228 net->num_wheels = htonl(net->num_wheels);
229 htond(net->flap_deflection);
231 net->cur_time = htonl( net->cur_time );
232 net->warp = htonl( net->warp );
233 htond(net->visibility);
238 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
241 if ( net_byte_order ) {
242 // Convert to the net buffer from network format
243 net->version = ntohl(net->version);
245 htond(net->longitude);
246 htond(net->latitude);
247 htond(net->altitude);
253 htond(net->thetadot);
256 htond(net->climb_rate);
260 htond(net->v_wind_body_north);
261 htond(net->v_wind_body_east);
262 htond(net->v_wind_body_down);
263 htond(net->stall_warning);
265 htond(net->A_X_pilot);
266 htond(net->A_Y_pilot);
267 htond(net->A_Z_pilot);
269 net->num_engines = htonl(net->num_engines);
270 for ( i = 0; i < net->num_engines; ++i ) {
271 htonl(net->eng_state[i]);
273 htond(net->fuel_flow[i]);
275 htond(net->oil_temp[i]);
276 htond(net->oil_px[i]);
279 net->num_tanks = htonl(net->num_tanks);
280 for ( i = 0; i < net->num_tanks; ++i ) {
281 htond(net->fuel_quantity[i]);
284 net->num_wheels = htonl(net->num_wheels);
285 // I don't need to convert the Wow flags, since they are one
287 htond(net->flap_deflection);
289 net->cur_time = ntohl(net->cur_time);
290 net->warp = ntohl(net->warp);
291 htond(net->visibility);
294 if ( net->version == FG_NET_FDM_VERSION ) {
295 // cout << "pos = " << net->longitude << " " << net->latitude << endl;
296 // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
298 cur_fdm_state->_updateGeodeticPosition( net->latitude,
301 * SG_METER_TO_FEET );
302 cur_fdm_state->_set_Euler_Angles( net->phi,
305 cur_fdm_state->_set_Euler_Rates( net->phidot,
308 cur_fdm_state->_set_V_calibrated_kts( net->vcas );
309 cur_fdm_state->_set_Climb_Rate( net->climb_rate );
310 cur_fdm_state->_set_Velocities_Local( net->v_north,
313 cur_fdm_state->_set_Velocities_Wind_Body( net->v_wind_body_north,
314 net->v_wind_body_east,
315 net->v_wind_body_down );
317 fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
318 cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
322 for ( i = 0; i < net->num_engines; ++i ) {
323 SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
325 // node->setBoolValue("running", t->isRunning());
326 // node->setBoolValue("cranking", t->isCranking());
328 // cout << net->eng_state[i] << endl;
329 if ( net->eng_state[i] == 0 ) {
330 node->setBoolValue( "cranking", false );
331 node->setBoolValue( "running", false );
332 } else if ( net->eng_state[i] == 1 ) {
333 node->setBoolValue( "cranking", true );
334 node->setBoolValue( "running", false );
335 } else if ( net->eng_state[i] == 2 ) {
336 node->setBoolValue( "cranking", false );
337 node->setBoolValue( "running", true );
340 node->setDoubleValue( "rpm", net->rpm[i] );
341 node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
342 node->setDoubleValue( "egt-degf", net->EGT[i] );
343 node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
344 node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );
347 for (i = 0; i < net->num_tanks; ++i ) {
348 SGPropertyNode * node
349 = fgGetNode("/consumables/fuel/tank", i, true);
350 node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
353 for (i = 0; i < net->num_wheels; ++i ) {
354 SGPropertyNode * node
355 = fgGetNode("/gear/gear", i, true);
356 node->setDoubleValue("wow", net->wow[i] );
359 fgSetDouble("/surface-positions/flap-pos-norm", net->flap_deflection);
360 SGPropertyNode * node = fgGetNode("/controls", true);
361 fgSetDouble("/surface-positions/elevator-pos-norm",
362 node->getDoubleValue( "elevator" ));
363 fgSetDouble("/surface-positions/rudder-pos-norm",
364 node->getDoubleValue( "rudder" ));
365 fgSetDouble("/surface-positions/left-aileron-pos-norm",
366 node->getDoubleValue( "aileron" ));
367 fgSetDouble("/surface-positions/right-aileron-pos-norm",
368 -node->getDoubleValue( "aileron" ));
370 /* these are ignored for now ... */
372 if ( net->cur_time ) {
373 fgSetLong("/sim/time/cur-time-override", net->cur_time);
376 globals->set_warp( net->warp );
377 last_warp = net->warp;
380 SG_LOG( SG_IO, SG_ALERT,
381 "Error: version mismatch in FGNetFDM2Props()" );
382 SG_LOG( SG_IO, SG_ALERT,
383 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
384 SG_LOG( SG_IO, SG_ALERT,
385 "\tNeeds to upgrade net_fdm.hxx and recompile." );
390 // process work for this port
391 bool FGNativeFDM::process() {
392 SGIOChannel *io = get_io_channel();
393 int length = sizeof(buf);
395 if ( get_direction() == SG_IO_OUT ) {
396 // cout << "size of cur_fdm_state = " << length << endl;
397 FGProps2NetFDM( &buf );
398 if ( ! io->write( (char *)(& buf), length ) ) {
399 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
402 } else if ( get_direction() == SG_IO_IN ) {
403 if ( io->get_type() == sgFileType ) {
404 if ( io->read( (char *)(& buf), length ) == length ) {
405 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
406 FGNetFDM2Props( &buf );
409 // double dt = 1000000.0 / 30.0;
410 // SGTimeStamp current; current.stamp();
412 result = io->read( (char *)(& buf), length );
413 if ( result == length ) {
414 SG_LOG( SG_IO, SG_DEBUG, " Success reading data." );
415 FGNetFDM2Props( &buf );
417 while ( result == length /* || current - last_time < dt */ ) {
418 result = io->read( (char *)(& buf), length );
419 if ( result == length ) {
420 SG_LOG( SG_IO, SG_DEBUG, " Success reading data." );
421 FGNetFDM2Props( &buf );
425 // last_time = current;
434 bool FGNativeFDM::close() {
435 SGIOChannel *io = get_io_channel();
437 set_enabled( false );
439 if ( ! io->close() ) {