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 <Time/tmp.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
37 #include <Scenery/scenery.hxx>
39 #include "native_fdm.hxx"
41 // FreeBSD works better with this included last ... (?)
42 #if defined( _MSC_VER )
44 #elif defined( __MINGW32__ )
45 # include <winsock2.h>
47 # include <netinet/in.h> // htonl() ntohl()
51 // The function htond is defined this way due to the way some
52 // processors and OSes treat floating point values. Some will raise
53 // an exception whenever a "bad" floating point value is loaded into a
54 // floating point register. Solaris is notorious for this, but then
55 // so is LynxOS on the PowerPC. By translating the data in place,
56 // there is no need to load a FP register with the "corruped" floating
57 // point value. By doing the BIG_ENDIAN test, I can optimize the
58 // routine for big-endian processors so it can be as efficient as
60 static void htond (double &x)
62 if ( sgIsLittleEndian() ) {
66 Double_Overlay = (int *) &x;
67 Holding_Buffer = Double_Overlay [0];
69 Double_Overlay [0] = htonl (Double_Overlay [1]);
70 Double_Overlay [1] = htonl (Holding_Buffer);
77 static void htonf (float &x)
79 if ( sgIsLittleEndian() ) {
83 Float_Overlay = (int *) &x;
84 Holding_Buffer = Float_Overlay [0];
86 Float_Overlay [0] = htonl (Holding_Buffer);
93 FGNativeFDM::FGNativeFDM() {
96 FGNativeFDM::~FGNativeFDM() {
100 // open hailing frequencies
101 bool FGNativeFDM::open() {
102 if ( is_enabled() ) {
103 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
104 << "is already in use, ignoring" );
108 SGIOChannel *io = get_io_channel();
110 if ( ! io->open( get_direction() ) ) {
111 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
117 // Is this really needed here ????
118 fgSetDouble("/position/sea-level-radius-ft", SG_EQUATORIAL_RADIUS_FT);
126 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
129 FlightProperties fdm_state;
131 // Version sanity checking
132 net->version = FG_NET_FDM_VERSION;
135 net->longitude = fdm_state.get_Longitude();
136 net->latitude = fdm_state.get_Latitude();
137 net->altitude = fdm_state.get_Altitude() * SG_FEET_TO_METER;
138 net->agl = fdm_state.get_Altitude_AGL() * SG_FEET_TO_METER;
139 net->phi = fdm_state.get_Phi();
140 net->theta = fdm_state.get_Theta();
141 net->psi = fdm_state.get_Psi();
142 net->alpha = fdm_state.get_Alpha();
143 net->beta = fdm_state.get_Beta();
144 net->phidot = fdm_state.get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
145 net->thetadot = fdm_state.get_Theta_dot_degps()
146 * SG_DEGREES_TO_RADIANS;
147 net->psidot = fdm_state.get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
149 net->vcas = fdm_state.get_V_calibrated_kts();
150 net->climb_rate = fdm_state.get_Climb_Rate();
152 net->v_north = fdm_state.get_V_north();
153 net->v_east = fdm_state.get_V_east();
154 net->v_down = fdm_state.get_V_down();
155 net->v_wind_body_north = fdm_state.get_uBody();
156 net->v_wind_body_east = fdm_state.get_vBody();
157 net->v_wind_body_down = fdm_state.get_wBody();
159 net->A_X_pilot = fdm_state.get_A_X_pilot();
160 net->A_Y_pilot = fdm_state.get_A_Y_pilot();
161 net->A_Z_pilot = fdm_state.get_A_Z_pilot();
163 net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
165 = fgGetDouble("/instrumentation/slip-skid-ball/indicated-slip-skid");
168 net->num_engines = FGNetFDM::FG_MAX_ENGINES;
169 for ( i = 0; i < net->num_engines; ++i ) {
170 SGPropertyNode *node = fgGetNode("engines/engine", i, true);
171 if ( node->getBoolValue( "running" ) ) {
172 net->eng_state[i] = 2;
173 } else if ( node->getBoolValue( "cranking" ) ) {
174 net->eng_state[i] = 1;
176 net->eng_state[i] = 0;
178 net->rpm[i] = node->getDoubleValue( "rpm" );
179 net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
180 net->fuel_px[i] = node->getDoubleValue( "fuel-px-psi" );
181 net->egt[i] = node->getDoubleValue( "egt-degf" );
182 // cout << "egt = " << aero->EGT << endl;
183 net->cht[i] = node->getDoubleValue( "cht-degf" );
184 net->mp_osi[i] = node->getDoubleValue( "mp-osi" );
185 net->tit[i] = node->getDoubleValue( "tit" );
186 net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
187 net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
191 net->num_tanks = FGNetFDM::FG_MAX_TANKS;
192 for ( i = 0; i < net->num_tanks; ++i ) {
193 SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
194 net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
198 net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
199 for (i = 0; i < net->num_wheels; ++i ) {
200 SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
201 net->wow[i] = node->getIntValue("wow");
202 net->gear_pos[i] = node->getDoubleValue("position-norm");
203 net->gear_steer[i] = node->getDoubleValue("steering-norm");
204 net->gear_compression[i] = node->getDoubleValue("compression-norm");
207 // the following really aren't used in this context
208 net->cur_time = globals->get_time_params()->get_cur_time();
209 net->warp = globals->get_warp();
210 net->visibility = fgGetDouble("/environment/visibility-m");
212 // Control surface positions
213 SGPropertyNode *node = fgGetNode("/surface-positions", true);
214 net->elevator = node->getDoubleValue( "elevator-pos-norm" );
215 net->elevator_trim_tab
216 = node->getDoubleValue( "elevator-trim-tab-pos-norm" );
217 // FIXME: CLO 10/28/04 - This really should be separated out into 2 values
218 net->left_flap = node->getDoubleValue( "flap-pos-norm" );
219 net->right_flap = node->getDoubleValue( "flap-pos-norm" );
220 net->left_aileron = node->getDoubleValue( "left-aileron-pos-norm" );
221 net->right_aileron = node->getDoubleValue( "right-aileron-pos-norm" );
222 net->rudder = node->getDoubleValue( "rudder-pos-norm" );
223 net->nose_wheel = node->getDoubleValue( "nose-wheel-pos-norm" );
224 net->speedbrake = node->getDoubleValue( "speedbrake-pos-norm" );
225 net->spoilers = node->getDoubleValue( "spoilers-pos-norm" );
227 if ( net_byte_order ) {
228 // Convert the net buffer to network format
229 net->version = htonl(net->version);
231 htond(net->longitude);
232 htond(net->latitude);
233 htond(net->altitude);
242 htonf(net->thetadot);
245 htonf(net->climb_rate);
249 htonf(net->v_wind_body_north);
250 htonf(net->v_wind_body_east);
251 htonf(net->v_wind_body_down);
253 htonf(net->A_X_pilot);
254 htonf(net->A_Y_pilot);
255 htonf(net->A_Z_pilot);
257 htonf(net->stall_warning);
258 htonf(net->slip_deg);
260 for ( i = 0; i < net->num_engines; ++i ) {
261 net->eng_state[i] = htonl(net->eng_state[i]);
263 htonf(net->fuel_flow[i]);
264 htonf(net->fuel_px[i]);
267 htonf(net->mp_osi[i]);
269 htonf(net->oil_temp[i]);
270 htonf(net->oil_px[i]);
272 net->num_engines = htonl(net->num_engines);
274 for ( i = 0; i < net->num_tanks; ++i ) {
275 htonf(net->fuel_quantity[i]);
277 net->num_tanks = htonl(net->num_tanks);
279 for ( i = 0; i < net->num_wheels; ++i ) {
280 net->wow[i] = htonl(net->wow[i]);
281 htonf(net->gear_pos[i]);
282 htonf(net->gear_steer[i]);
283 htonf(net->gear_compression[i]);
285 net->num_wheels = htonl(net->num_wheels);
287 net->cur_time = htonl( net->cur_time );
288 net->warp = htonl( net->warp );
289 htonf(net->visibility);
291 htonf(net->elevator);
292 htonf(net->elevator_trim_tab);
293 htonf(net->left_flap);
294 htonf(net->right_flap);
295 htonf(net->left_aileron);
296 htonf(net->right_aileron);
298 htonf(net->nose_wheel);
299 htonf(net->speedbrake);
300 htonf(net->spoilers);
305 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
307 FlightProperties fdm_state;
309 if ( net_byte_order ) {
310 // Convert to the net buffer from network format
311 net->version = ntohl(net->version);
313 htond(net->longitude);
314 htond(net->latitude);
315 htond(net->altitude);
324 htonf(net->thetadot);
327 htonf(net->climb_rate);
331 htonf(net->v_wind_body_north);
332 htonf(net->v_wind_body_east);
333 htonf(net->v_wind_body_down);
335 htonf(net->A_X_pilot);
336 htonf(net->A_Y_pilot);
337 htonf(net->A_Z_pilot);
339 htonf(net->stall_warning);
340 htonf(net->slip_deg);
342 net->num_engines = htonl(net->num_engines);
343 for ( i = 0; i < net->num_engines; ++i ) {
344 net->eng_state[i] = htonl(net->eng_state[i]);
346 htonf(net->fuel_flow[i]);
347 htonf(net->fuel_px[i]);
350 htonf(net->mp_osi[i]);
352 htonf(net->oil_temp[i]);
353 htonf(net->oil_px[i]);
356 net->num_tanks = htonl(net->num_tanks);
357 for ( i = 0; i < net->num_tanks; ++i ) {
358 htonf(net->fuel_quantity[i]);
361 net->num_wheels = htonl(net->num_wheels);
362 for ( i = 0; i < net->num_wheels; ++i ) {
363 net->wow[i] = htonl(net->wow[i]);
364 htonf(net->gear_pos[i]);
365 htonf(net->gear_steer[i]);
366 htonf(net->gear_compression[i]);
369 net->cur_time = htonl(net->cur_time);
370 net->warp = ntohl(net->warp);
371 htonf(net->visibility);
373 htonf(net->elevator);
374 htonf(net->elevator_trim_tab);
375 htonf(net->left_flap);
376 htonf(net->right_flap);
377 htonf(net->left_aileron);
378 htonf(net->right_aileron);
380 htonf(net->nose_wheel);
381 htonf(net->speedbrake);
382 htonf(net->spoilers);
385 if ( net->version == FG_NET_FDM_VERSION ) {
386 // cout << "pos = " << net->longitude << " " << net->latitude << endl;
387 // cout << "sea level rad = " << fdm_state.get_Sea_level_radius()
390 fdm_state.set_Latitude(net->latitude);
391 fdm_state.set_Longitude(net->longitude);
392 fdm_state.set_Altitude(net->altitude * SG_METER_TO_FEET);
394 if ( net->agl > -9000 ) {
395 fdm_state.set_Altitude_AGL( net->agl * SG_METER_TO_FEET );
397 double agl_m = net->altitude
398 - fdm_state.get_Runway_altitude_m();
399 fdm_state.set_Altitude_AGL( agl_m * SG_METER_TO_FEET );
401 fdm_state.set_Euler_Angles( net->phi,
404 fdm_state.set_Alpha( net->alpha );
405 fdm_state.set_Beta( net->beta );
406 fdm_state.set_Euler_Rates( net->phidot,
409 fdm_state.set_V_calibrated_kts( net->vcas );
410 fdm_state.set_Climb_Rate( net->climb_rate );
411 fdm_state.set_Velocities_Local( net->v_north,
414 fdm_state.set_Velocities_Wind_Body( net->v_wind_body_north,
415 net->v_wind_body_east,
416 net->v_wind_body_down );
418 fdm_state.set_Accels_Pilot_Body( net->A_X_pilot,
422 fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
423 fgSetDouble( "/instrumentation/slip-skid-ball/indicated-slip-skid",
425 fgSetBool( "/instrumentation/slip-skid-ball/override", true );
427 for ( i = 0; i < net->num_engines; ++i ) {
428 SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
430 // node->setBoolValue("running", t->isRunning());
431 // node->setBoolValue("cranking", t->isCranking());
433 // cout << net->eng_state[i] << endl;
434 if ( net->eng_state[i] == 0 ) {
435 node->setBoolValue( "cranking", false );
436 node->setBoolValue( "running", false );
437 } else if ( net->eng_state[i] == 1 ) {
438 node->setBoolValue( "cranking", true );
439 node->setBoolValue( "running", false );
440 } else if ( net->eng_state[i] == 2 ) {
441 node->setBoolValue( "cranking", false );
442 node->setBoolValue( "running", true );
445 node->setDoubleValue( "rpm", net->rpm[i] );
446 node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
447 node->setDoubleValue( "fuel-px-psi", net->fuel_px[i] );
448 node->setDoubleValue( "egt-degf", net->egt[i] );
449 node->setDoubleValue( "cht-degf", net->cht[i] );
450 node->setDoubleValue( "mp-osi", net->mp_osi[i] );
451 node->setDoubleValue( "tit", net->tit[i] );
452 node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
453 node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );
456 for (i = 0; i < net->num_tanks; ++i ) {
457 SGPropertyNode * node
458 = fgGetNode("/consumables/fuel/tank", i, true);
459 node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
462 for (i = 0; i < net->num_wheels; ++i ) {
463 SGPropertyNode * node = fgGetNode("/gear/gear", i, true);
464 node->setDoubleValue("wow", net->wow[i] );
465 node->setDoubleValue("position-norm", net->gear_pos[i] );
466 node->setDoubleValue("steering-norm", net->gear_steer[i] );
467 node->setDoubleValue("compression-norm", net->gear_compression[i] );
470 /* these are ignored for now ... */
472 if ( net->cur_time ) {
473 fgSetLong("/sim/time/cur-time-override", net->cur_time);
476 globals->set_warp( net->warp );
477 last_warp = net->warp;
480 SGPropertyNode *node = fgGetNode("/surface-positions", true);
481 node->setDoubleValue("elevator-pos-norm", net->elevator);
482 node->setDoubleValue("elevator-trim-tab-pos-norm",
483 net->elevator_trim_tab);
484 // FIXME: CLO 10/28/04 - This really should be separated out
486 node->setDoubleValue("flap-pos-norm", net->left_flap);
487 node->setDoubleValue("flap-pos-norm", net->right_flap);
488 node->setDoubleValue("left-aileron-pos-norm", net->left_aileron);
489 node->setDoubleValue("right-aileron-pos-norm", net->right_aileron);
490 node->setDoubleValue("rudder-pos-norm", net->rudder);
491 node->setDoubleValue("nose-wheel-pos-norm", net->nose_wheel);
492 node->setDoubleValue("speedbrake-pos-norm", net->speedbrake);
493 node->setDoubleValue("spoilers-pos-norm", net->spoilers);
495 SG_LOG( SG_IO, SG_ALERT,
496 "Error: version mismatch in FGNetFDM2Props()" );
497 SG_LOG( SG_IO, SG_ALERT,
498 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
499 SG_LOG( SG_IO, SG_ALERT,
500 "\tNeeds to upgrade net_fdm.hxx and recompile." );
505 // process work for this port
506 bool FGNativeFDM::process() {
507 SGIOChannel *io = get_io_channel();
508 int length = sizeof(buf);
510 if ( get_direction() == SG_IO_OUT ) {
512 FGProps2NetFDM( &buf );
513 if ( ! io->write( (char *)(& buf), length ) ) {
514 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
517 } else if ( get_direction() == SG_IO_IN ) {
518 if ( io->get_type() == sgFileType ) {
519 if ( io->read( (char *)(& buf), length ) == length ) {
520 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
521 FGNetFDM2Props( &buf );
525 result = io->read( (char *)(& buf), length );
526 while ( result == length ) {
527 if ( result == length ) {
528 SG_LOG( SG_IO, SG_DEBUG, " Success reading data." );
529 FGNetFDM2Props( &buf );
531 result = io->read( (char *)(& buf), length );
541 bool FGNativeFDM::close() {
542 SGIOChannel *io = get_io_channel();
544 set_enabled( false );
546 if ( ! io->close() ) {