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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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/flightProperties.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Main/globals.hxx>
36 #include <Scenery/scenery.hxx>
38 #include "native_fdm.hxx"
40 // FreeBSD works better with this included last ... (?)
41 #if defined( _MSC_VER )
43 #elif defined( __MINGW32__ )
44 # include <winsock2.h>
46 # include <netinet/in.h> // htonl() ntohl()
50 // The function htond is defined this way due to the way some
51 // processors and OSes treat floating point values. Some will raise
52 // an exception whenever a "bad" floating point value is loaded into a
53 // floating point register. Solaris is notorious for this, but then
54 // so is LynxOS on the PowerPC. By translating the data in place,
55 // there is no need to load a FP register with the "corruped" floating
56 // point value. By doing the BIG_ENDIAN test, I can optimize the
57 // routine for big-endian processors so it can be as efficient as
59 static void htond (double &x)
61 if ( sgIsLittleEndian() ) {
65 Double_Overlay = (int *) &x;
66 Holding_Buffer = Double_Overlay [0];
68 Double_Overlay [0] = htonl (Double_Overlay [1]);
69 Double_Overlay [1] = htonl (Holding_Buffer);
76 static void htonf (float &x)
78 if ( sgIsLittleEndian() ) {
82 Float_Overlay = (int *) &x;
83 Holding_Buffer = Float_Overlay [0];
85 Float_Overlay [0] = htonl (Holding_Buffer);
92 FGNativeFDM::FGNativeFDM() {
95 FGNativeFDM::~FGNativeFDM() {
99 // open hailing frequencies
100 bool FGNativeFDM::open() {
101 if ( is_enabled() ) {
102 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
103 << "is already in use, ignoring" );
107 SGIOChannel *io = get_io_channel();
109 if ( ! io->open( get_direction() ) ) {
110 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
116 // Is this really needed here ????
117 fgSetDouble("/position/sea-level-radius-ft", SG_EQUATORIAL_RADIUS_FT);
125 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
128 FlightProperties fdm_state;
130 // Version sanity checking
131 net->version = FG_NET_FDM_VERSION;
134 net->longitude = fdm_state.get_Longitude();
135 net->latitude = fdm_state.get_Latitude();
136 net->altitude = fdm_state.get_Altitude() * SG_FEET_TO_METER;
137 net->agl = fdm_state.get_Altitude_AGL() * SG_FEET_TO_METER;
138 net->phi = fdm_state.get_Phi();
139 net->theta = fdm_state.get_Theta();
140 net->psi = fdm_state.get_Psi();
141 net->alpha = fdm_state.get_Alpha();
142 net->beta = fdm_state.get_Beta();
143 net->phidot = fdm_state.get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
144 net->thetadot = fdm_state.get_Theta_dot_degps()
145 * SG_DEGREES_TO_RADIANS;
146 net->psidot = fdm_state.get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
148 net->vcas = fdm_state.get_V_calibrated_kts();
149 net->climb_rate = fdm_state.get_Climb_Rate();
151 net->v_north = fdm_state.get_V_north();
152 net->v_east = fdm_state.get_V_east();
153 net->v_down = fdm_state.get_V_down();
154 net->v_wind_body_north = fdm_state.get_uBody();
155 net->v_wind_body_east = fdm_state.get_vBody();
156 net->v_wind_body_down = fdm_state.get_wBody();
158 net->A_X_pilot = fdm_state.get_A_X_pilot();
159 net->A_Y_pilot = fdm_state.get_A_Y_pilot();
160 net->A_Z_pilot = fdm_state.get_A_Z_pilot();
162 net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
164 = fgGetDouble("/instrumentation/slip-skid-ball/indicated-slip-skid");
167 net->num_engines = FGNetFDM::FG_MAX_ENGINES;
168 for ( i = 0; i < net->num_engines; ++i ) {
169 SGPropertyNode *node = fgGetNode("engines/engine", i, true);
170 if ( node->getBoolValue( "running" ) ) {
171 net->eng_state[i] = 2;
172 } else if ( node->getBoolValue( "cranking" ) ) {
173 net->eng_state[i] = 1;
175 net->eng_state[i] = 0;
177 net->rpm[i] = node->getDoubleValue( "rpm" );
178 net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
179 net->fuel_px[i] = node->getDoubleValue( "fuel-px-psi" );
180 net->egt[i] = node->getDoubleValue( "egt-degf" );
181 // cout << "egt = " << aero->EGT << endl;
182 net->cht[i] = node->getDoubleValue( "cht-degf" );
183 net->mp_osi[i] = node->getDoubleValue( "mp-osi" );
184 net->tit[i] = node->getDoubleValue( "tit" );
185 net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
186 net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
190 net->num_tanks = FGNetFDM::FG_MAX_TANKS;
191 for ( i = 0; i < net->num_tanks; ++i ) {
192 SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
193 net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
197 net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
198 for (i = 0; i < net->num_wheels; ++i ) {
199 SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
200 net->wow[i] = node->getIntValue("wow");
201 net->gear_pos[i] = node->getDoubleValue("position-norm");
202 net->gear_steer[i] = node->getDoubleValue("steering-norm");
203 net->gear_compression[i] = node->getDoubleValue("compression-norm");
206 // the following really aren't used in this context
207 net->cur_time = globals->get_time_params()->get_cur_time();
208 net->warp = globals->get_warp();
209 net->visibility = fgGetDouble("/environment/visibility-m");
211 // Control surface positions
212 SGPropertyNode *node = fgGetNode("/surface-positions", true);
213 net->elevator = node->getDoubleValue( "elevator-pos-norm" );
214 net->elevator_trim_tab
215 = node->getDoubleValue( "elevator-trim-tab-pos-norm" );
216 // FIXME: CLO 10/28/04 - This really should be separated out into 2 values
217 net->left_flap = node->getDoubleValue( "flap-pos-norm" );
218 net->right_flap = node->getDoubleValue( "flap-pos-norm" );
219 net->left_aileron = node->getDoubleValue( "left-aileron-pos-norm" );
220 net->right_aileron = node->getDoubleValue( "right-aileron-pos-norm" );
221 net->rudder = node->getDoubleValue( "rudder-pos-norm" );
222 net->nose_wheel = node->getDoubleValue( "nose-wheel-pos-norm" );
223 net->speedbrake = node->getDoubleValue( "speedbrake-pos-norm" );
224 net->spoilers = node->getDoubleValue( "spoilers-pos-norm" );
226 if ( net_byte_order ) {
227 // Convert the net buffer to network format
228 net->version = htonl(net->version);
230 htond(net->longitude);
231 htond(net->latitude);
232 htond(net->altitude);
241 htonf(net->thetadot);
244 htonf(net->climb_rate);
248 htonf(net->v_wind_body_north);
249 htonf(net->v_wind_body_east);
250 htonf(net->v_wind_body_down);
252 htonf(net->A_X_pilot);
253 htonf(net->A_Y_pilot);
254 htonf(net->A_Z_pilot);
256 htonf(net->stall_warning);
257 htonf(net->slip_deg);
259 for ( i = 0; i < net->num_engines; ++i ) {
260 net->eng_state[i] = htonl(net->eng_state[i]);
262 htonf(net->fuel_flow[i]);
263 htonf(net->fuel_px[i]);
266 htonf(net->mp_osi[i]);
268 htonf(net->oil_temp[i]);
269 htonf(net->oil_px[i]);
271 net->num_engines = htonl(net->num_engines);
273 for ( i = 0; i < net->num_tanks; ++i ) {
274 htonf(net->fuel_quantity[i]);
276 net->num_tanks = htonl(net->num_tanks);
278 for ( i = 0; i < net->num_wheels; ++i ) {
279 net->wow[i] = htonl(net->wow[i]);
280 htonf(net->gear_pos[i]);
281 htonf(net->gear_steer[i]);
282 htonf(net->gear_compression[i]);
284 net->num_wheels = htonl(net->num_wheels);
286 net->cur_time = htonl( net->cur_time );
287 net->warp = htonl( net->warp );
288 htonf(net->visibility);
290 htonf(net->elevator);
291 htonf(net->elevator_trim_tab);
292 htonf(net->left_flap);
293 htonf(net->right_flap);
294 htonf(net->left_aileron);
295 htonf(net->right_aileron);
297 htonf(net->nose_wheel);
298 htonf(net->speedbrake);
299 htonf(net->spoilers);
304 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
306 FlightProperties fdm_state;
308 if ( net_byte_order ) {
309 // Convert to the net buffer from network format
310 net->version = ntohl(net->version);
312 htond(net->longitude);
313 htond(net->latitude);
314 htond(net->altitude);
323 htonf(net->thetadot);
326 htonf(net->climb_rate);
330 htonf(net->v_wind_body_north);
331 htonf(net->v_wind_body_east);
332 htonf(net->v_wind_body_down);
334 htonf(net->A_X_pilot);
335 htonf(net->A_Y_pilot);
336 htonf(net->A_Z_pilot);
338 htonf(net->stall_warning);
339 htonf(net->slip_deg);
341 net->num_engines = htonl(net->num_engines);
342 for ( i = 0; i < net->num_engines; ++i ) {
343 net->eng_state[i] = htonl(net->eng_state[i]);
345 htonf(net->fuel_flow[i]);
346 htonf(net->fuel_px[i]);
349 htonf(net->mp_osi[i]);
351 htonf(net->oil_temp[i]);
352 htonf(net->oil_px[i]);
355 net->num_tanks = htonl(net->num_tanks);
356 for ( i = 0; i < net->num_tanks; ++i ) {
357 htonf(net->fuel_quantity[i]);
360 net->num_wheels = htonl(net->num_wheels);
361 for ( i = 0; i < net->num_wheels; ++i ) {
362 net->wow[i] = htonl(net->wow[i]);
363 htonf(net->gear_pos[i]);
364 htonf(net->gear_steer[i]);
365 htonf(net->gear_compression[i]);
368 net->cur_time = htonl(net->cur_time);
369 net->warp = ntohl(net->warp);
370 htonf(net->visibility);
372 htonf(net->elevator);
373 htonf(net->elevator_trim_tab);
374 htonf(net->left_flap);
375 htonf(net->right_flap);
376 htonf(net->left_aileron);
377 htonf(net->right_aileron);
379 htonf(net->nose_wheel);
380 htonf(net->speedbrake);
381 htonf(net->spoilers);
384 if ( net->version == FG_NET_FDM_VERSION ) {
385 // cout << "pos = " << net->longitude << " " << net->latitude << endl;
386 // cout << "sea level rad = " << fdm_state.get_Sea_level_radius()
389 fdm_state.set_Latitude(net->latitude);
390 fdm_state.set_Longitude(net->longitude);
391 fdm_state.set_Altitude(net->altitude * SG_METER_TO_FEET);
393 if ( net->agl > -9000 ) {
394 fdm_state.set_Altitude_AGL( net->agl * SG_METER_TO_FEET );
396 double agl_m = net->altitude
397 - fdm_state.get_Runway_altitude_m();
398 fdm_state.set_Altitude_AGL( agl_m * SG_METER_TO_FEET );
400 fdm_state.set_Euler_Angles( net->phi,
403 fdm_state.set_Alpha( net->alpha );
404 fdm_state.set_Beta( net->beta );
405 fdm_state.set_Euler_Rates( net->phidot,
408 fdm_state.set_V_calibrated_kts( net->vcas );
409 fdm_state.set_Climb_Rate( net->climb_rate );
410 fdm_state.set_Velocities_Local( net->v_north,
413 fdm_state.set_Velocities_Wind_Body( net->v_wind_body_north,
414 net->v_wind_body_east,
415 net->v_wind_body_down );
417 fdm_state.set_Accels_Pilot_Body( net->A_X_pilot,
421 fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
422 fgSetDouble( "/instrumentation/slip-skid-ball/indicated-slip-skid",
424 fgSetBool( "/instrumentation/slip-skid-ball/override", true );
426 for ( i = 0; i < net->num_engines; ++i ) {
427 SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
429 // node->setBoolValue("running", t->isRunning());
430 // node->setBoolValue("cranking", t->isCranking());
432 // cout << net->eng_state[i] << endl;
433 if ( net->eng_state[i] == 0 ) {
434 node->setBoolValue( "cranking", false );
435 node->setBoolValue( "running", false );
436 } else if ( net->eng_state[i] == 1 ) {
437 node->setBoolValue( "cranking", true );
438 node->setBoolValue( "running", false );
439 } else if ( net->eng_state[i] == 2 ) {
440 node->setBoolValue( "cranking", false );
441 node->setBoolValue( "running", true );
444 node->setDoubleValue( "rpm", net->rpm[i] );
445 node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
446 node->setDoubleValue( "fuel-px-psi", net->fuel_px[i] );
447 node->setDoubleValue( "egt-degf", net->egt[i] );
448 node->setDoubleValue( "cht-degf", net->cht[i] );
449 node->setDoubleValue( "mp-osi", net->mp_osi[i] );
450 node->setDoubleValue( "tit", net->tit[i] );
451 node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
452 node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );
455 for (i = 0; i < net->num_tanks; ++i ) {
456 SGPropertyNode * node
457 = fgGetNode("/consumables/fuel/tank", i, true);
458 node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
461 for (i = 0; i < net->num_wheels; ++i ) {
462 SGPropertyNode * node = fgGetNode("/gear/gear", i, true);
463 node->setDoubleValue("wow", net->wow[i] );
464 node->setDoubleValue("position-norm", net->gear_pos[i] );
465 node->setDoubleValue("steering-norm", net->gear_steer[i] );
466 node->setDoubleValue("compression-norm", net->gear_compression[i] );
469 /* these are ignored for now ... */
471 if ( net->cur_time ) {
472 fgSetLong("/sim/time/cur-time-override", net->cur_time);
475 globals->set_warp( net->warp );
476 last_warp = net->warp;
479 SGPropertyNode *node = fgGetNode("/surface-positions", true);
480 node->setDoubleValue("elevator-pos-norm", net->elevator);
481 node->setDoubleValue("elevator-trim-tab-pos-norm",
482 net->elevator_trim_tab);
483 // FIXME: CLO 10/28/04 - This really should be separated out
485 node->setDoubleValue("flap-pos-norm", net->left_flap);
486 node->setDoubleValue("flap-pos-norm", net->right_flap);
487 node->setDoubleValue("left-aileron-pos-norm", net->left_aileron);
488 node->setDoubleValue("right-aileron-pos-norm", net->right_aileron);
489 node->setDoubleValue("rudder-pos-norm", net->rudder);
490 node->setDoubleValue("nose-wheel-pos-norm", net->nose_wheel);
491 node->setDoubleValue("speedbrake-pos-norm", net->speedbrake);
492 node->setDoubleValue("spoilers-pos-norm", net->spoilers);
494 SG_LOG( SG_IO, SG_ALERT,
495 "Error: version mismatch in FGNetFDM2Props()" );
496 SG_LOG( SG_IO, SG_ALERT,
497 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
498 SG_LOG( SG_IO, SG_ALERT,
499 "\tNeeds to upgrade net_fdm.hxx and recompile." );
504 // process work for this port
505 bool FGNativeFDM::process() {
506 SGIOChannel *io = get_io_channel();
507 int length = sizeof(buf);
509 if ( get_direction() == SG_IO_OUT ) {
511 FGProps2NetFDM( &buf );
512 if ( ! io->write( (char *)(& buf), length ) ) {
513 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
516 } else if ( get_direction() == SG_IO_IN ) {
517 if ( io->get_type() == sgFileType ) {
518 if ( io->read( (char *)(& buf), length ) == length ) {
519 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
520 FGNetFDM2Props( &buf );
524 result = io->read( (char *)(& buf), length );
525 while ( result == length ) {
526 if ( result == length ) {
527 SG_LOG( SG_IO, SG_DEBUG, " Success reading data." );
528 FGNetFDM2Props( &buf );
530 result = io->read( (char *)(& buf), length );
540 bool FGNativeFDM::close() {
541 SGIOChannel *io = get_io_channel();
543 set_enabled( false );
545 if ( ! io->close() ) {