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 static void htonf (float &x)
76 if ( sgIsLittleEndian() ) {
80 Float_Overlay = (int *) &x;
81 Holding_Buffer = Float_Overlay [0];
83 Float_Overlay [0] = htonl (Holding_Buffer);
90 FGNativeFDM::FGNativeFDM() {
93 FGNativeFDM::~FGNativeFDM() {
97 // open hailing frequencies
98 bool FGNativeFDM::open() {
100 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
101 << "is already in use, ignoring" );
105 SGIOChannel *io = get_io_channel();
107 if ( ! io->open( get_direction() ) ) {
108 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
114 // Is this really needed here ????
115 cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
121 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
124 // Version sanity checking
125 net->version = FG_NET_FDM_VERSION;
128 net->longitude = cur_fdm_state->get_Longitude();
129 net->latitude = cur_fdm_state->get_Latitude();
130 net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
131 net->phi = cur_fdm_state->get_Phi();
132 net->theta = cur_fdm_state->get_Theta();
133 net->psi = cur_fdm_state->get_Psi();
134 net->phidot = cur_fdm_state->get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
135 net->thetadot = cur_fdm_state->get_Theta_dot_degps()
136 * SG_DEGREES_TO_RADIANS;
137 net->psidot = cur_fdm_state->get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
139 net->vcas = cur_fdm_state->get_V_calibrated_kts();
140 net->climb_rate = cur_fdm_state->get_Climb_Rate();
142 net->v_north = cur_fdm_state->get_V_north();
143 net->v_east = cur_fdm_state->get_V_east();
144 net->v_down = cur_fdm_state->get_V_down();
145 net->v_wind_body_north = cur_fdm_state->get_uBody();
146 net->v_wind_body_east = cur_fdm_state->get_vBody();
147 net->v_wind_body_down = cur_fdm_state->get_wBody();
148 net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
150 net->A_X_pilot = cur_fdm_state->get_A_X_pilot();
151 net->A_Y_pilot = cur_fdm_state->get_A_Y_pilot();
152 net->A_Z_pilot = cur_fdm_state->get_A_Z_pilot();
155 net->num_engines = FGNetFDM::FG_MAX_ENGINES;
156 for ( i = 0; i < net->num_engines; ++i ) {
157 SGPropertyNode *node = fgGetNode("engines/engine", i, true);
158 if ( node->getBoolValue( "running" ) ) {
159 net->eng_state[0] = 2;
160 } else if ( node->getBoolValue( "cranking" ) ) {
161 net->eng_state[0] = 1;
163 net->eng_state[0] = 0;
165 net->rpm[i] = node->getDoubleValue( "rpm" );
166 net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
167 net->EGT[i] = node->getDoubleValue( "egt-degf" );
168 // cout << "egt = " << aero->EGT << endl;
169 net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
170 net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
174 net->num_tanks = FGNetFDM::FG_MAX_TANKS;
175 for ( i = 0; i < net->num_tanks; ++i ) {
176 SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
177 net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
181 net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
182 for (i = 0; i < net->num_wheels; ++i ) {
183 SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
184 net->wow[i] = node->getDoubleValue("wow");
187 // the following really aren't used in this context
188 net->cur_time = globals->get_time_params()->get_cur_time();
189 net->warp = globals->get_warp();
190 net->visibility = fgGetDouble("/environment/visibility-m");
192 // Control surface positions
193 SGPropertyNode *node = fgGetNode("/surface-positions", true);
194 net->elevator = node->getDoubleValue( "elevator-pos-norm" );
195 net->flaps = node->getDoubleValue( "flap-pos-norm" );
196 net->left_aileron = node->getDoubleValue( "left-aileron-pos-norm" );
197 net->right_aileron = node->getDoubleValue( "right-aileron-pos-norm" );
198 net->rudder = node->getDoubleValue( "rudder-pos-norm" );
199 net->speedbrake = node->getDoubleValue( "speedbrake-pos-norm" );
200 net->spoilers = node->getDoubleValue( "spoilers-pos-norm" );
202 if ( net_byte_order ) {
203 // Convert the net buffer to network format
204 net->version = htonl(net->version);
206 htond(net->longitude);
207 htond(net->latitude);
208 htond(net->altitude);
215 htonf(net->thetadot);
218 htonf(net->climb_rate);
222 htonf(net->v_wind_body_north);
223 htonf(net->v_wind_body_east);
224 htonf(net->v_wind_body_down);
225 htonf(net->stall_warning);
227 htonf(net->A_X_pilot);
228 htonf(net->A_Y_pilot);
229 htonf(net->A_Z_pilot);
231 for ( i = 0; i < net->num_engines; ++i ) {
232 htonl(net->eng_state[i]);
234 htonf(net->fuel_flow[i]);
236 htonf(net->oil_temp[i]);
237 htonf(net->oil_px[i]);
239 net->num_engines = htonl(net->num_engines);
241 for ( i = 0; i < net->num_tanks; ++i ) {
242 htonf(net->fuel_quantity[i]);
244 net->num_tanks = htonl(net->num_tanks);
246 for ( i = 0; i < net->num_wheels; ++i ) {
247 net->wow[i] = htonl(net->wow[i]);
249 net->num_wheels = htonl(net->num_wheels);
251 net->cur_time = htonl( net->cur_time );
252 net->warp = htonl( net->warp );
253 htonf(net->visibility);
255 htonf(net->elevator);
257 htonf(net->left_aileron);
258 htonf(net->right_aileron);
260 htonf(net->speedbrake);
261 htonf(net->spoilers);
266 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
269 if ( net_byte_order ) {
270 // Convert to the net buffer from network format
271 net->version = ntohl(net->version);
273 htond(net->longitude);
274 htond(net->latitude);
275 htond(net->altitude);
282 htonf(net->thetadot);
285 htonf(net->climb_rate);
289 htonf(net->v_wind_body_north);
290 htonf(net->v_wind_body_east);
291 htonf(net->v_wind_body_down);
292 htonf(net->stall_warning);
294 htonf(net->A_X_pilot);
295 htonf(net->A_Y_pilot);
296 htonf(net->A_Z_pilot);
298 net->num_engines = htonl(net->num_engines);
299 for ( i = 0; i < net->num_engines; ++i ) {
300 htonl(net->eng_state[i]);
302 htonf(net->fuel_flow[i]);
304 htonf(net->oil_temp[i]);
305 htonf(net->oil_px[i]);
308 net->num_tanks = htonl(net->num_tanks);
309 for ( i = 0; i < net->num_tanks; ++i ) {
310 htonf(net->fuel_quantity[i]);
313 net->num_wheels = htonl(net->num_wheels);
314 // I don't need to convert the Wow flags, since they are one
317 net->cur_time = ntohl(net->cur_time);
318 net->warp = ntohl(net->warp);
319 htonf(net->visibility);
321 htonf(net->elevator);
323 htonf(net->left_aileron);
324 htonf(net->right_aileron);
326 htonf(net->speedbrake);
327 htonf(net->spoilers);
330 if ( net->version == FG_NET_FDM_VERSION ) {
331 // cout << "pos = " << net->longitude << " " << net->latitude << endl;
332 // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
334 cur_fdm_state->_updateGeodeticPosition( net->latitude,
337 * SG_METER_TO_FEET );
338 cur_fdm_state->_set_Euler_Angles( net->phi,
341 cur_fdm_state->_set_Euler_Rates( net->phidot,
344 cur_fdm_state->_set_V_calibrated_kts( net->vcas );
345 cur_fdm_state->_set_Climb_Rate( net->climb_rate );
346 cur_fdm_state->_set_Velocities_Local( net->v_north,
349 cur_fdm_state->_set_Velocities_Wind_Body( net->v_wind_body_north,
350 net->v_wind_body_east,
351 net->v_wind_body_down );
353 fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
354 cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
358 for ( i = 0; i < net->num_engines; ++i ) {
359 SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
361 // node->setBoolValue("running", t->isRunning());
362 // node->setBoolValue("cranking", t->isCranking());
364 // cout << net->eng_state[i] << endl;
365 if ( net->eng_state[i] == 0 ) {
366 node->setBoolValue( "cranking", false );
367 node->setBoolValue( "running", false );
368 } else if ( net->eng_state[i] == 1 ) {
369 node->setBoolValue( "cranking", true );
370 node->setBoolValue( "running", false );
371 } else if ( net->eng_state[i] == 2 ) {
372 node->setBoolValue( "cranking", false );
373 node->setBoolValue( "running", true );
376 node->setDoubleValue( "rpm", net->rpm[i] );
377 node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
378 node->setDoubleValue( "egt-degf", net->EGT[i] );
379 node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
380 node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );
383 for (i = 0; i < net->num_tanks; ++i ) {
384 SGPropertyNode * node
385 = fgGetNode("/consumables/fuel/tank", i, true);
386 node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
389 for (i = 0; i < net->num_wheels; ++i ) {
390 SGPropertyNode * node
391 = fgGetNode("/gear/gear", i, true);
392 node->setDoubleValue("wow", net->wow[i] );
395 /* these are ignored for now ... */
397 if ( net->cur_time ) {
398 fgSetLong("/sim/time/cur-time-override", net->cur_time);
401 globals->set_warp( net->warp );
402 last_warp = net->warp;
405 SGPropertyNode *node = fgGetNode("/surface-positions", true);
406 node->setDoubleValue("elevator-pos-norm", net->elevator);
407 node->setDoubleValue("flap-pos-norm", net->flaps);
408 node->setDoubleValue("left-aileron-pos-norm", net->left_aileron);
409 node->setDoubleValue("right-aileron-pos-norm", net->right_aileron);
410 node->setDoubleValue("rudder-pos-norm", net->rudder);
411 node->setDoubleValue("speedbrake-pos-norm", net->speedbrake);
412 node->setDoubleValue("spoilers-pos-norm", net->spoilers);
414 SG_LOG( SG_IO, SG_ALERT,
415 "Error: version mismatch in FGNetFDM2Props()" );
416 SG_LOG( SG_IO, SG_ALERT,
417 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
418 SG_LOG( SG_IO, SG_ALERT,
419 "\tNeeds to upgrade net_fdm.hxx and recompile." );
424 // process work for this port
425 bool FGNativeFDM::process() {
426 SGIOChannel *io = get_io_channel();
427 int length = sizeof(buf);
429 if ( get_direction() == SG_IO_OUT ) {
430 // cout << "size of cur_fdm_state = " << length << endl;
431 FGProps2NetFDM( &buf );
432 if ( ! io->write( (char *)(& buf), length ) ) {
433 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
436 } else if ( get_direction() == SG_IO_IN ) {
437 if ( io->get_type() == sgFileType ) {
438 if ( io->read( (char *)(& buf), length ) == length ) {
439 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
440 FGNetFDM2Props( &buf );
444 result = io->read( (char *)(& buf), length );
445 while ( result == length ) {
446 if ( result == length ) {
447 SG_LOG( SG_IO, SG_DEBUG, " Success reading data." );
448 FGNetFDM2Props( &buf );
450 result = io->read( (char *)(& buf), length );
460 bool FGNativeFDM::close() {
461 SGIOChannel *io = get_io_channel();
463 set_enabled( false );
465 if ( ! io->close() ) {