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( _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 cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
124 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
127 // Version sanity checking
128 net->version = FG_NET_FDM_VERSION;
131 net->longitude = cur_fdm_state->get_Longitude();
132 net->latitude = cur_fdm_state->get_Latitude();
133 net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
134 net->agl = cur_fdm_state->get_Altitude_AGL() * SG_FEET_TO_METER;
135 net->phi = cur_fdm_state->get_Phi();
136 net->theta = cur_fdm_state->get_Theta();
137 net->psi = cur_fdm_state->get_Psi();
138 net->alpha = cur_fdm_state->get_Alpha();
139 net->beta = cur_fdm_state->get_Beta();
140 net->phidot = cur_fdm_state->get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
141 net->thetadot = cur_fdm_state->get_Theta_dot_degps()
142 * SG_DEGREES_TO_RADIANS;
143 net->psidot = cur_fdm_state->get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
145 net->vcas = cur_fdm_state->get_V_calibrated_kts();
146 net->climb_rate = cur_fdm_state->get_Climb_Rate();
148 net->v_north = cur_fdm_state->get_V_north();
149 net->v_east = cur_fdm_state->get_V_east();
150 net->v_down = cur_fdm_state->get_V_down();
151 net->v_wind_body_north = cur_fdm_state->get_uBody();
152 net->v_wind_body_east = cur_fdm_state->get_vBody();
153 net->v_wind_body_down = cur_fdm_state->get_wBody();
155 net->A_X_pilot = cur_fdm_state->get_A_X_pilot();
156 net->A_Y_pilot = cur_fdm_state->get_A_Y_pilot();
157 net->A_Z_pilot = cur_fdm_state->get_A_Z_pilot();
159 net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
161 = fgGetDouble("/instrumentation/slip-skid-ball/indicated-slip-skid");
164 net->num_engines = FGNetFDM::FG_MAX_ENGINES;
165 for ( i = 0; i < net->num_engines; ++i ) {
166 SGPropertyNode *node = fgGetNode("engines/engine", i, true);
167 if ( node->getBoolValue( "running" ) ) {
168 net->eng_state[i] = 2;
169 } else if ( node->getBoolValue( "cranking" ) ) {
170 net->eng_state[i] = 1;
172 net->eng_state[i] = 0;
174 net->rpm[i] = node->getDoubleValue( "rpm" );
175 net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
176 net->fuel_px[i] = node->getDoubleValue( "fuel-px-psi" );
177 net->egt[i] = node->getDoubleValue( "egt-degf" );
178 // cout << "egt = " << aero->EGT << endl;
179 net->cht[i] = node->getDoubleValue( "cht-degf" );
180 net->mp_osi[i] = node->getDoubleValue( "mp-osi" );
181 net->tit[i] = node->getDoubleValue( "tit" );
182 net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
183 net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
187 net->num_tanks = FGNetFDM::FG_MAX_TANKS;
188 for ( i = 0; i < net->num_tanks; ++i ) {
189 SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
190 net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
194 net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
195 for (i = 0; i < net->num_wheels; ++i ) {
196 SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
197 net->wow[i] = node->getIntValue("wow");
198 net->gear_pos[i] = node->getDoubleValue("position-norm");
199 net->gear_steer[i] = node->getDoubleValue("steering-norm");
200 net->gear_compression[i] = node->getDoubleValue("compression-norm");
203 // the following really aren't used in this context
204 net->cur_time = globals->get_time_params()->get_cur_time();
205 net->warp = globals->get_warp();
206 net->visibility = fgGetDouble("/environment/visibility-m");
208 // Control surface positions
209 SGPropertyNode *node = fgGetNode("/surface-positions", true);
210 net->elevator = node->getDoubleValue( "elevator-pos-norm" );
211 net->elevator_trim_tab
212 = node->getDoubleValue( "elevator-trim-tab-pos-norm" );
213 // FIXME: CLO 10/28/04 - This really should be separated out into 2 values
214 net->left_flap = node->getDoubleValue( "flap-pos-norm" );
215 net->right_flap = node->getDoubleValue( "flap-pos-norm" );
216 net->left_aileron = node->getDoubleValue( "left-aileron-pos-norm" );
217 net->right_aileron = node->getDoubleValue( "right-aileron-pos-norm" );
218 net->rudder = node->getDoubleValue( "rudder-pos-norm" );
219 net->nose_wheel = node->getDoubleValue( "nose-wheel-pos-norm" );
220 net->speedbrake = node->getDoubleValue( "speedbrake-pos-norm" );
221 net->spoilers = node->getDoubleValue( "spoilers-pos-norm" );
223 if ( net_byte_order ) {
224 // Convert the net buffer to network format
225 net->version = htonl(net->version);
227 htond(net->longitude);
228 htond(net->latitude);
229 htond(net->altitude);
238 htonf(net->thetadot);
241 htonf(net->climb_rate);
245 htonf(net->v_wind_body_north);
246 htonf(net->v_wind_body_east);
247 htonf(net->v_wind_body_down);
249 htonf(net->A_X_pilot);
250 htonf(net->A_Y_pilot);
251 htonf(net->A_Z_pilot);
253 htonf(net->stall_warning);
254 htonf(net->slip_deg);
256 for ( i = 0; i < net->num_engines; ++i ) {
257 net->eng_state[i] = htonl(net->eng_state[i]);
259 htonf(net->fuel_flow[i]);
260 htonf(net->fuel_px[i]);
263 htonf(net->mp_osi[i]);
265 htonf(net->oil_temp[i]);
266 htonf(net->oil_px[i]);
268 net->num_engines = htonl(net->num_engines);
270 for ( i = 0; i < net->num_tanks; ++i ) {
271 htonf(net->fuel_quantity[i]);
273 net->num_tanks = htonl(net->num_tanks);
275 for ( i = 0; i < net->num_wheels; ++i ) {
276 net->wow[i] = htonl(net->wow[i]);
277 htonf(net->gear_pos[i]);
278 htonf(net->gear_steer[i]);
279 htonf(net->gear_compression[i]);
281 net->num_wheels = htonl(net->num_wheels);
283 net->cur_time = htonl( net->cur_time );
284 net->warp = htonl( net->warp );
285 htonf(net->visibility);
287 htonf(net->elevator);
288 htonf(net->elevator_trim_tab);
289 htonf(net->left_flap);
290 htonf(net->right_flap);
291 htonf(net->left_aileron);
292 htonf(net->right_aileron);
294 htonf(net->nose_wheel);
295 htonf(net->speedbrake);
296 htonf(net->spoilers);
301 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
304 if ( net_byte_order ) {
305 // Convert to the net buffer from network format
306 net->version = ntohl(net->version);
308 htond(net->longitude);
309 htond(net->latitude);
310 htond(net->altitude);
319 htonf(net->thetadot);
322 htonf(net->climb_rate);
326 htonf(net->v_wind_body_north);
327 htonf(net->v_wind_body_east);
328 htonf(net->v_wind_body_down);
330 htonf(net->A_X_pilot);
331 htonf(net->A_Y_pilot);
332 htonf(net->A_Z_pilot);
334 htonf(net->stall_warning);
335 htonf(net->slip_deg);
337 net->num_engines = htonl(net->num_engines);
338 for ( i = 0; i < net->num_engines; ++i ) {
339 net->eng_state[i] = htonl(net->eng_state[i]);
341 htonf(net->fuel_flow[i]);
342 htonf(net->fuel_px[i]);
345 htonf(net->mp_osi[i]);
347 htonf(net->oil_temp[i]);
348 htonf(net->oil_px[i]);
351 net->num_tanks = htonl(net->num_tanks);
352 for ( i = 0; i < net->num_tanks; ++i ) {
353 htonf(net->fuel_quantity[i]);
356 net->num_wheels = htonl(net->num_wheels);
357 for ( i = 0; i < net->num_wheels; ++i ) {
358 net->wow[i] = htonl(net->wow[i]);
359 htonf(net->gear_pos[i]);
360 htonf(net->gear_steer[i]);
361 htonf(net->gear_compression[i]);
364 net->cur_time = htonl(net->cur_time);
365 net->warp = ntohl(net->warp);
366 htonf(net->visibility);
368 htonf(net->elevator);
369 htonf(net->elevator_trim_tab);
370 htonf(net->left_flap);
371 htonf(net->right_flap);
372 htonf(net->left_aileron);
373 htonf(net->right_aileron);
375 htonf(net->nose_wheel);
376 htonf(net->speedbrake);
377 htonf(net->spoilers);
380 if ( net->version == FG_NET_FDM_VERSION ) {
381 // cout << "pos = " << net->longitude << " " << net->latitude << endl;
382 // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
384 cur_fdm_state->_updateGeodeticPosition( net->latitude,
387 * SG_METER_TO_FEET );
388 if ( net->agl > -9000 ) {
389 cur_fdm_state->_set_Altitude_AGL( net->agl * SG_METER_TO_FEET );
391 double agl_m = net->altitude
392 - cur_fdm_state->get_Runway_altitude_m();
393 cur_fdm_state->_set_Altitude_AGL( agl_m * SG_METER_TO_FEET );
395 cur_fdm_state->_set_Euler_Angles( net->phi,
398 cur_fdm_state->_set_Alpha( net->alpha );
399 cur_fdm_state->_set_Beta( net->beta );
400 cur_fdm_state->_set_Euler_Rates( net->phidot,
403 cur_fdm_state->_set_V_calibrated_kts( net->vcas );
404 cur_fdm_state->_set_Climb_Rate( net->climb_rate );
405 cur_fdm_state->_set_Velocities_Local( net->v_north,
408 cur_fdm_state->_set_Velocities_Wind_Body( net->v_wind_body_north,
409 net->v_wind_body_east,
410 net->v_wind_body_down );
412 cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
416 fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
417 fgSetDouble( "/instrumentation/slip-skid-ball/indicated-slip-skid",
419 fgSetBool( "/instrumentation/slip-skid-ball/override", true );
421 for ( i = 0; i < net->num_engines; ++i ) {
422 SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
424 // node->setBoolValue("running", t->isRunning());
425 // node->setBoolValue("cranking", t->isCranking());
427 // cout << net->eng_state[i] << endl;
428 if ( net->eng_state[i] == 0 ) {
429 node->setBoolValue( "cranking", false );
430 node->setBoolValue( "running", false );
431 } else if ( net->eng_state[i] == 1 ) {
432 node->setBoolValue( "cranking", true );
433 node->setBoolValue( "running", false );
434 } else if ( net->eng_state[i] == 2 ) {
435 node->setBoolValue( "cranking", false );
436 node->setBoolValue( "running", true );
439 node->setDoubleValue( "rpm", net->rpm[i] );
440 node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
441 node->setDoubleValue( "fuel-px-psi", net->fuel_px[i] );
442 node->setDoubleValue( "egt-degf", net->egt[i] );
443 node->setDoubleValue( "cht-degf", net->cht[i] );
444 node->setDoubleValue( "mp-osi", net->mp_osi[i] );
445 node->setDoubleValue( "tit", net->tit[i] );
446 node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
447 node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );
450 for (i = 0; i < net->num_tanks; ++i ) {
451 SGPropertyNode * node
452 = fgGetNode("/consumables/fuel/tank", i, true);
453 node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
456 for (i = 0; i < net->num_wheels; ++i ) {
457 SGPropertyNode * node = fgGetNode("/gear/gear", i, true);
458 node->setDoubleValue("wow", net->wow[i] );
459 node->setDoubleValue("position-norm", net->gear_pos[i] );
460 node->setDoubleValue("steering-norm", net->gear_steer[i] );
461 node->setDoubleValue("compression-norm", net->gear_compression[i] );
464 /* these are ignored for now ... */
466 if ( net->cur_time ) {
467 fgSetLong("/sim/time/cur-time-override", net->cur_time);
470 globals->set_warp( net->warp );
471 last_warp = net->warp;
474 SGPropertyNode *node = fgGetNode("/surface-positions", true);
475 node->setDoubleValue("elevator-pos-norm", net->elevator);
476 node->setDoubleValue("elevator-trim-tab-pos-norm",
477 net->elevator_trim_tab);
478 // FIXME: CLO 10/28/04 - This really should be separated out
480 node->setDoubleValue("flap-pos-norm", net->left_flap);
481 node->setDoubleValue("flap-pos-norm", net->right_flap);
482 node->setDoubleValue("left-aileron-pos-norm", net->left_aileron);
483 node->setDoubleValue("right-aileron-pos-norm", net->right_aileron);
484 node->setDoubleValue("rudder-pos-norm", net->rudder);
485 node->setDoubleValue("nose-wheel-pos-norm", net->nose_wheel);
486 node->setDoubleValue("speedbrake-pos-norm", net->speedbrake);
487 node->setDoubleValue("spoilers-pos-norm", net->spoilers);
489 SG_LOG( SG_IO, SG_ALERT,
490 "Error: version mismatch in FGNetFDM2Props()" );
491 SG_LOG( SG_IO, SG_ALERT,
492 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
493 SG_LOG( SG_IO, SG_ALERT,
494 "\tNeeds to upgrade net_fdm.hxx and recompile." );
499 // process work for this port
500 bool FGNativeFDM::process() {
501 SGIOChannel *io = get_io_channel();
502 int length = sizeof(buf);
504 if ( get_direction() == SG_IO_OUT ) {
505 // cout << "size of cur_fdm_state = " << length << endl;
506 FGProps2NetFDM( &buf );
507 if ( ! io->write( (char *)(& buf), length ) ) {
508 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
511 } else if ( get_direction() == SG_IO_IN ) {
512 if ( io->get_type() == sgFileType ) {
513 if ( io->read( (char *)(& buf), length ) == length ) {
514 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
515 FGNetFDM2Props( &buf );
519 result = io->read( (char *)(& buf), length );
520 while ( result == length ) {
521 if ( result == length ) {
522 SG_LOG( SG_IO, SG_DEBUG, " Success reading data." );
523 FGNetFDM2Props( &buf );
525 result = io->read( (char *)(& buf), length );
535 bool FGNativeFDM::close() {
536 SGIOChannel *io = get_io_channel();
538 set_enabled( false );
540 if ( ! io->close() ) {