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/flight.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(WIN32) && !defined(__CYGWIN__)
45 # include <netinet/in.h> // htonl() ntohl()
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
58 static void htond (double &x)
60 if ( sgIsLittleEndian() ) {
64 Double_Overlay = (int *) &x;
65 Holding_Buffer = Double_Overlay [0];
67 Double_Overlay [0] = htonl (Double_Overlay [1]);
68 Double_Overlay [1] = htonl (Holding_Buffer);
75 static void htonf (float &x)
77 if ( sgIsLittleEndian() ) {
81 Float_Overlay = (int *) &x;
82 Holding_Buffer = Float_Overlay [0];
84 Float_Overlay [0] = htonl (Holding_Buffer);
91 FGNativeFDM::FGNativeFDM() {
94 FGNativeFDM::~FGNativeFDM() {
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" );
106 SGIOChannel *io = get_io_channel();
108 if ( ! io->open( get_direction() ) ) {
109 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
115 // Is this really needed here ????
116 cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
122 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
125 // Version sanity checking
126 net->version = FG_NET_FDM_VERSION;
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->alpha = cur_fdm_state->get_Alpha();
137 net->beta = cur_fdm_state->get_Beta();
138 net->phidot = cur_fdm_state->get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
139 net->thetadot = cur_fdm_state->get_Theta_dot_degps()
140 * SG_DEGREES_TO_RADIANS;
141 net->psidot = cur_fdm_state->get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
143 net->vcas = cur_fdm_state->get_V_calibrated_kts();
144 net->climb_rate = cur_fdm_state->get_Climb_Rate();
146 net->v_north = cur_fdm_state->get_V_north();
147 net->v_east = cur_fdm_state->get_V_east();
148 net->v_down = cur_fdm_state->get_V_down();
149 net->v_wind_body_north = cur_fdm_state->get_uBody();
150 net->v_wind_body_east = cur_fdm_state->get_vBody();
151 net->v_wind_body_down = cur_fdm_state->get_wBody();
153 net->A_X_pilot = cur_fdm_state->get_A_X_pilot();
154 net->A_Y_pilot = cur_fdm_state->get_A_Y_pilot();
155 net->A_Z_pilot = cur_fdm_state->get_A_Z_pilot();
157 net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
159 = fgGetDouble("/instrumentation/slip-skid-ball/indicated-slip-skid");
162 net->num_engines = FGNetFDM::FG_MAX_ENGINES;
163 for ( i = 0; i < net->num_engines; ++i ) {
164 SGPropertyNode *node = fgGetNode("engines/engine", i, true);
165 if ( node->getBoolValue( "running" ) ) {
166 net->eng_state[i] = 2;
167 } else if ( node->getBoolValue( "cranking" ) ) {
168 net->eng_state[i] = 1;
170 net->eng_state[i] = 0;
172 net->rpm[i] = node->getDoubleValue( "rpm" );
173 net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
174 net->fuel_px[i] = node->getDoubleValue( "fuel-px-psi" );
175 net->egt[i] = node->getDoubleValue( "egt-degf" );
176 // cout << "egt = " << aero->EGT << endl;
177 net->cht[i] = node->getDoubleValue( "cht-degf" );
178 net->mp_osi[i] = node->getDoubleValue( "mp-osi" );
179 net->tit[i] = node->getDoubleValue( "tit" );
180 net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
181 net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
185 net->num_tanks = FGNetFDM::FG_MAX_TANKS;
186 for ( i = 0; i < net->num_tanks; ++i ) {
187 SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
188 net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
192 net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
193 for (i = 0; i < net->num_wheels; ++i ) {
194 SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
195 net->wow[i] = node->getIntValue("wow");
196 net->gear_pos[i] = node->getDoubleValue("position-norm");
197 net->gear_steer[i] = node->getDoubleValue("steering-norm");
198 net->gear_compression[i] = node->getDoubleValue("compression-norm");
201 // the following really aren't used in this context
202 net->cur_time = globals->get_time_params()->get_cur_time();
203 net->warp = globals->get_warp();
204 net->visibility = fgGetDouble("/environment/visibility-m");
206 // Control surface positions
207 SGPropertyNode *node = fgGetNode("/surface-positions", true);
208 net->elevator = node->getDoubleValue( "elevator-pos-norm" );
209 net->elevator_trim_tab
210 = node->getDoubleValue( "elevator-trim-tab-pos-norm" );
211 // FIXME: CLO 10/28/04 - This really should be separated out into 2 values
212 net->left_flap = node->getDoubleValue( "flap-pos-norm" );
213 net->right_flap = node->getDoubleValue( "flap-pos-norm" );
214 net->left_aileron = node->getDoubleValue( "left-aileron-pos-norm" );
215 net->right_aileron = node->getDoubleValue( "right-aileron-pos-norm" );
216 net->rudder = node->getDoubleValue( "rudder-pos-norm" );
217 net->nose_wheel = node->getDoubleValue( "nose-wheel-pos-norm" );
218 net->speedbrake = node->getDoubleValue( "speedbrake-pos-norm" );
219 net->spoilers = node->getDoubleValue( "spoilers-pos-norm" );
221 if ( net_byte_order ) {
222 // Convert the net buffer to network format
223 net->version = htonl(net->version);
225 htond(net->longitude);
226 htond(net->latitude);
227 htond(net->altitude);
236 htonf(net->thetadot);
239 htonf(net->climb_rate);
243 htonf(net->v_wind_body_north);
244 htonf(net->v_wind_body_east);
245 htonf(net->v_wind_body_down);
247 htonf(net->A_X_pilot);
248 htonf(net->A_Y_pilot);
249 htonf(net->A_Z_pilot);
251 htonf(net->stall_warning);
252 htonf(net->slip_deg);
254 for ( i = 0; i < net->num_engines; ++i ) {
255 net->eng_state[i] = htonl(net->eng_state[i]);
257 htonf(net->fuel_flow[i]);
258 htonf(net->fuel_px[i]);
261 htonf(net->mp_osi[i]);
263 htonf(net->oil_temp[i]);
264 htonf(net->oil_px[i]);
266 net->num_engines = htonl(net->num_engines);
268 for ( i = 0; i < net->num_tanks; ++i ) {
269 htonf(net->fuel_quantity[i]);
271 net->num_tanks = htonl(net->num_tanks);
273 for ( i = 0; i < net->num_wheels; ++i ) {
274 net->wow[i] = htonl(net->wow[i]);
275 htonf(net->gear_pos[i]);
276 htonf(net->gear_steer[i]);
277 htonf(net->gear_compression[i]);
279 net->num_wheels = htonl(net->num_wheels);
281 net->cur_time = htonl( net->cur_time );
282 net->warp = htonl( net->warp );
283 htonf(net->visibility);
285 htonf(net->elevator);
286 htonf(net->elevator_trim_tab);
287 htonf(net->left_flap);
288 htonf(net->right_flap);
289 htonf(net->left_aileron);
290 htonf(net->right_aileron);
292 htonf(net->nose_wheel);
293 htonf(net->speedbrake);
294 htonf(net->spoilers);
299 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
302 if ( net_byte_order ) {
303 // Convert to the net buffer from network format
304 net->version = ntohl(net->version);
306 htond(net->longitude);
307 htond(net->latitude);
308 htond(net->altitude);
317 htonf(net->thetadot);
320 htonf(net->climb_rate);
324 htonf(net->v_wind_body_north);
325 htonf(net->v_wind_body_east);
326 htonf(net->v_wind_body_down);
328 htonf(net->A_X_pilot);
329 htonf(net->A_Y_pilot);
330 htonf(net->A_Z_pilot);
332 htonf(net->stall_warning);
333 htonf(net->slip_deg);
335 net->num_engines = htonl(net->num_engines);
336 for ( i = 0; i < net->num_engines; ++i ) {
337 net->eng_state[i] = htonl(net->eng_state[i]);
339 htonf(net->fuel_flow[i]);
340 htonf(net->fuel_px[i]);
343 htonf(net->mp_osi[i]);
345 htonf(net->oil_temp[i]);
346 htonf(net->oil_px[i]);
349 net->num_tanks = htonl(net->num_tanks);
350 for ( i = 0; i < net->num_tanks; ++i ) {
351 htonf(net->fuel_quantity[i]);
354 net->num_wheels = htonl(net->num_wheels);
355 for ( i = 0; i < net->num_wheels; ++i ) {
356 net->wow[i] = htonl(net->wow[i]);
357 htonf(net->gear_pos[i]);
358 htonf(net->gear_steer[i]);
359 htonf(net->gear_compression[i]);
362 net->cur_time = htonl(net->cur_time);
363 net->warp = ntohl(net->warp);
364 htonf(net->visibility);
366 htonf(net->elevator);
367 htonf(net->elevator_trim_tab);
368 htonf(net->left_flap);
369 htonf(net->right_flap);
370 htonf(net->left_aileron);
371 htonf(net->right_aileron);
373 htonf(net->nose_wheel);
374 htonf(net->speedbrake);
375 htonf(net->spoilers);
378 if ( net->version == FG_NET_FDM_VERSION ) {
379 // cout << "pos = " << net->longitude << " " << net->latitude << endl;
380 // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
382 cur_fdm_state->_updateGeodeticPosition( net->latitude,
385 * SG_METER_TO_FEET );
386 if ( net->agl > -9000 ) {
387 cur_fdm_state->_set_Altitude_AGL( net->agl * SG_METER_TO_FEET );
389 double agl_m = net->altitude
390 - cur_fdm_state->get_Runway_altitude_m();
391 cur_fdm_state->_set_Altitude_AGL( agl_m * SG_METER_TO_FEET );
393 cur_fdm_state->_set_Euler_Angles( net->phi,
396 cur_fdm_state->_set_Alpha( net->alpha );
397 cur_fdm_state->_set_Beta( net->beta );
398 cur_fdm_state->_set_Euler_Rates( net->phidot,
401 cur_fdm_state->_set_V_calibrated_kts( net->vcas );
402 cur_fdm_state->_set_Climb_Rate( net->climb_rate );
403 cur_fdm_state->_set_Velocities_Local( net->v_north,
406 cur_fdm_state->_set_Velocities_Wind_Body( net->v_wind_body_north,
407 net->v_wind_body_east,
408 net->v_wind_body_down );
410 cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
414 fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
415 fgSetDouble( "/instrumentation/slip-skid-ball/indicated-slip-skid",
417 fgSetBool( "/instrumentation/slip-skid-ball/override", true );
419 for ( i = 0; i < net->num_engines; ++i ) {
420 SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
422 // node->setBoolValue("running", t->isRunning());
423 // node->setBoolValue("cranking", t->isCranking());
425 // cout << net->eng_state[i] << endl;
426 if ( net->eng_state[i] == 0 ) {
427 node->setBoolValue( "cranking", false );
428 node->setBoolValue( "running", false );
429 } else if ( net->eng_state[i] == 1 ) {
430 node->setBoolValue( "cranking", true );
431 node->setBoolValue( "running", false );
432 } else if ( net->eng_state[i] == 2 ) {
433 node->setBoolValue( "cranking", false );
434 node->setBoolValue( "running", true );
437 node->setDoubleValue( "rpm", net->rpm[i] );
438 node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
439 node->setDoubleValue( "fuel-px-psi", net->fuel_px[i] );
440 node->setDoubleValue( "egt-degf", net->egt[i] );
441 node->setDoubleValue( "cht-degf", net->cht[i] );
442 node->setDoubleValue( "mp-osi", net->mp_osi[i] );
443 node->setDoubleValue( "tit", net->tit[i] );
444 node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
445 node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );
448 for (i = 0; i < net->num_tanks; ++i ) {
449 SGPropertyNode * node
450 = fgGetNode("/consumables/fuel/tank", i, true);
451 node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
454 for (i = 0; i < net->num_wheels; ++i ) {
455 SGPropertyNode * node = fgGetNode("/gear/gear", i, true);
456 node->setDoubleValue("wow", net->wow[i] );
457 node->setDoubleValue("position-norm", net->gear_pos[i] );
458 node->setDoubleValue("steering-norm", net->gear_steer[i] );
459 node->setDoubleValue("compression-norm", net->gear_compression[i] );
462 /* these are ignored for now ... */
464 if ( net->cur_time ) {
465 fgSetLong("/sim/time/cur-time-override", net->cur_time);
468 globals->set_warp( net->warp );
469 last_warp = net->warp;
472 SGPropertyNode *node = fgGetNode("/surface-positions", true);
473 node->setDoubleValue("elevator-pos-norm", net->elevator);
474 node->setDoubleValue("elevator-trim-tab-pos-norm",
475 net->elevator_trim_tab);
476 // FIXME: CLO 10/28/04 - This really should be separated out
478 node->setDoubleValue("flap-pos-norm", net->left_flap);
479 node->setDoubleValue("flap-pos-norm", net->right_flap);
480 node->setDoubleValue("left-aileron-pos-norm", net->left_aileron);
481 node->setDoubleValue("right-aileron-pos-norm", net->right_aileron);
482 node->setDoubleValue("rudder-pos-norm", net->rudder);
483 node->setDoubleValue("nose-wheel-pos-norm", net->nose_wheel);
484 node->setDoubleValue("speedbrake-pos-norm", net->speedbrake);
485 node->setDoubleValue("spoilers-pos-norm", net->spoilers);
487 SG_LOG( SG_IO, SG_ALERT,
488 "Error: version mismatch in FGNetFDM2Props()" );
489 SG_LOG( SG_IO, SG_ALERT,
490 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
491 SG_LOG( SG_IO, SG_ALERT,
492 "\tNeeds to upgrade net_fdm.hxx and recompile." );
497 // process work for this port
498 bool FGNativeFDM::process() {
499 SGIOChannel *io = get_io_channel();
500 int length = sizeof(buf);
502 if ( get_direction() == SG_IO_OUT ) {
503 // cout << "size of cur_fdm_state = " << length << endl;
504 FGProps2NetFDM( &buf );
505 if ( ! io->write( (char *)(& buf), length ) ) {
506 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
509 } else if ( get_direction() == SG_IO_IN ) {
510 if ( io->get_type() == sgFileType ) {
511 if ( io->read( (char *)(& buf), length ) == length ) {
512 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
513 FGNetFDM2Props( &buf );
517 result = io->read( (char *)(& buf), length );
518 while ( result == length ) {
519 if ( result == length ) {
520 SG_LOG( SG_IO, SG_DEBUG, " Success reading data." );
521 FGNetFDM2Props( &buf );
523 result = io->read( (char *)(& buf), length );
533 bool FGNativeFDM::close() {
534 SGIOChannel *io = get_io_channel();
536 set_enabled( false );
538 if ( ! io->close() ) {