1 // native_ctrls.cxx -- FGFS "Native" controls I/O class
3 // Written by Curtis Olson, started July 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/iochannel.hxx>
30 #include <simgear/io/lowlevel.hxx> // endian tests
32 #include <FDM/flight.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Scenery/scenery.hxx> // ground elevation
36 #include "native_ctrls.hxx"
38 // FreeBSD works better with this included last ... (?)
39 #if defined(WIN32) && !defined(__CYGWIN__)
42 # include <netinet/in.h> // htonl() ntohl()
46 FGNativeCtrls::FGNativeCtrls() {
49 FGNativeCtrls::~FGNativeCtrls() {
53 // open hailing frequencies
54 bool FGNativeCtrls::open() {
56 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
57 << "is already in use, ignoring" );
61 SGIOChannel *io = get_io_channel();
63 if ( ! io->open( get_direction() ) ) {
64 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
74 // The function htond is defined this way due to the way some
75 // processors and OSes treat floating point values. Some will raise
76 // an exception whenever a "bad" floating point value is loaded into a
77 // floating point register. Solaris is notorious for this, but then
78 // so is LynxOS on the PowerPC. By translating the data in place,
79 // there is no need to load a FP register with the "corruped" floating
80 // point value. By doing the BIG_ENDIAN test, I can optimize the
81 // routine for big-endian processors so it can be as efficient as
83 static void htond (double &x)
85 if ( sgIsLittleEndian() ) {
89 Double_Overlay = (int *) &x;
90 Holding_Buffer = Double_Overlay [0];
92 Double_Overlay [0] = htonl (Double_Overlay [1]);
93 Double_Overlay [1] = htonl (Holding_Buffer);
100 // Populate the FGNetCtrls structure from the property tree.
101 void FGProps2NetCtrls( FGNetCtrls *net, bool honor_freezes,
102 bool net_byte_order )
105 SGPropertyNode *node;
106 SGPropertyNode *fuelpump;
107 SGPropertyNode *tempnode;
110 node = fgGetNode("/controls/flight", true);
111 net->version = FG_NET_CTRLS_VERSION;
112 net->aileron = node->getDoubleValue( "aileron" );
113 net->elevator = node->getDoubleValue( "elevator" );
114 net->rudder = node->getDoubleValue( "rudder" );
115 net->aileron_trim = node->getDoubleValue( "aileron-trim" );
116 net->elevator_trim = node->getDoubleValue( "elevator-trim" );
117 net->rudder_trim = node->getDoubleValue( "rudder-trim" );
118 net->flaps = node->getDoubleValue( "flaps" );
120 = fgGetDouble( "/systems/electrical/outputs/flaps", 1.0 ) >= 1.0;
121 net->flap_motor_ok = node->getBoolValue( "flaps-serviceable" );
123 net->num_engines = FGNetCtrls::FG_MAX_ENGINES;
124 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
126 node = fgGetNode("/controls/engines/engine", i );
127 fuelpump = fgGetNode("/systems/electrical/outputs/fuel-pump", i );
129 tempnode = node->getChild("starter");
130 if ( tempnode != NULL ) {
131 net->starter_power[i] = ( tempnode->getDoubleValue() >= 1.0 );
133 tempnode = node->getChild("master-bat");
134 if ( tempnode != NULL ) {
135 net->master_bat[i] = tempnode->getBoolValue();
137 tempnode = node->getChild("master-alt");
138 if ( tempnode != NULL ) {
139 net->master_alt[i] = tempnode->getBoolValue();
142 net->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
143 net->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
144 net->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
145 net->condition[i] = node->getDoubleValue( "condition", 0.0 );
146 net->magnetos[i] = node->getIntValue( "magnetos", 0 );
148 // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
151 // cout << "Starter -> " << node->getIntValue( "starter", false )
155 if ( fuelpump != NULL ) {
156 net->fuel_pump_power[i] = ( fuelpump->getDoubleValue() >= 1.0 );
158 net->fuel_pump_power[i] = 0;
162 SGPropertyNode *faults = node->getChild( "faults", 0, true );
163 net->engine_ok[i] = faults->getBoolValue( "serviceable", true );
165 = faults->getBoolValue( "left-magneto-serviceable", true );
167 = faults->getBoolValue( "right-magneto-serviceable", true);
168 net->spark_plugs_ok[i]
169 = faults->getBoolValue( "spark-plugs-serviceable", true );
170 net->oil_press_status[i]
171 = faults->getIntValue( "oil-pressure-status", 0 );
173 = faults->getBoolValue( "fuel-pump-serviceable", true );
175 net->num_tanks = FGNetCtrls::FG_MAX_TANKS;
176 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
177 node = fgGetNode("/controls/fuel/tank", i);
178 if ( node->getChild("fuel_selector") != 0 ) {
179 net->fuel_selector[i]
180 = node->getChild("fuel_selector")->getBoolValue();
182 net->fuel_selector[i] = false;
185 node = fgGetNode("/controls/gear", true);
186 net->brake_left = node->getChild("brake-left")->getDoubleValue();
187 net->brake_right = node->getChild("brake-right")->getDoubleValue();
188 net->copilot_brake_left
189 = node->getChild("copilot-brake-left")->getDoubleValue();
190 net->copilot_brake_right
191 = node->getChild("copilot-brake-right")->getDoubleValue();
192 net->brake_parking = node->getChild("brake-parking")->getDoubleValue();
194 net->gear_handle = fgGetBool( "/controls/gear/gear-down" );
196 net->master_avionics = fgGetBool("/controls/switches/master-avionics");
198 net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
199 net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
200 net->turbulence_norm =
201 fgGetDouble("/environment/turbulence/magnitude-norm");
203 net->temp_c = fgGetDouble("/environment/temperature-degc");
204 net->press_inhg = fgGetDouble("/environment/pressure-sea-level-inhg");
206 // cur_fdm_state->get_ground_elev_ft() is what we want ... this
207 // reports the altitude of the aircraft.
208 // "/environment/ground-elevation-m" reports the ground elevation
209 // of the current view point which could change substantially if
210 // the user is switching views.
211 net->hground = cur_fdm_state->get_ground_elev_ft() * SG_FEET_TO_METER;
212 net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
214 net->icing = fgGetBool("/hazards/icing/wing");
216 net->speedup = fgGetInt("/sim/speed-up");
218 if ( honor_freezes ) {
219 if ( fgGetBool("/sim/freeze/master") ) {
222 if ( fgGetBool("/sim/freeze/position") ) {
225 if ( fgGetBool("/sim/freeze/fuel") ) {
230 if ( net_byte_order ) {
231 // convert to network byte order
232 net->version = htonl(net->version);
234 htond(net->elevator);
236 htond(net->aileron_trim);
237 htond(net->elevator_trim);
238 htond(net->rudder_trim);
240 net->flaps_power = htonl(net->flaps_power);
241 net->flap_motor_ok = htonl(net->flap_motor_ok);
243 net->num_engines = htonl(net->num_engines);
244 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
245 net->master_bat[i] = htonl(net->master_bat[i]);
246 net->master_alt[i] = htonl(net->master_alt[i]);
247 net->magnetos[i] = htonl(net->magnetos[i]);
248 net->starter_power[i] = htonl(net->starter_power[i]);
249 htond(net->throttle[i]);
250 htond(net->mixture[i]);
251 net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
252 htond(net->prop_advance[i]);
253 htond(net->condition[i]);
254 net->engine_ok[i] = htonl(net->engine_ok[i]);
255 net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
256 net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
257 net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
258 net->oil_press_status[i] = htonl(net->oil_press_status[i]);
259 net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
262 net->num_tanks = htonl(net->num_tanks);
263 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
264 net->fuel_selector[i] = htonl(net->fuel_selector[i]);
267 net->cross_feed = htonl(net->cross_feed);
268 htond(net->brake_left);
269 htond(net->brake_right);
270 htond(net->copilot_brake_left);
271 htond(net->copilot_brake_right);
272 htond(net->brake_parking);
273 net->gear_handle = htonl(net->gear_handle);
274 net->master_avionics = htonl(net->master_avionics);
275 htond(net->wind_speed_kt);
276 htond(net->wind_dir_deg);
277 htond(net->turbulence_norm);
279 htond(net->press_inhg);
282 net->icing = htonl(net->icing);
283 net->speedup = htonl(net->speedup);
284 net->freeze = htonl(net->freeze);
289 // Update the property tree from the FGNetCtrls structure.
290 void FGNetCtrls2Props( FGNetCtrls *net, bool honor_freezes,
291 bool net_byte_order )
295 SGPropertyNode * node;
297 if ( net_byte_order ) {
298 // convert from network byte order
299 net->version = htonl(net->version);
301 htond(net->elevator);
303 htond(net->aileron_trim);
304 htond(net->elevator_trim);
305 htond(net->rudder_trim);
307 net->flaps_power = htonl(net->flaps_power);
308 net->flap_motor_ok = htonl(net->flap_motor_ok);
310 net->num_engines = htonl(net->num_engines);
311 for ( i = 0; i < (int)net->num_engines; ++i ) {
312 net->master_bat[i] = htonl(net->master_bat[i]);
313 net->master_alt[i] = htonl(net->master_alt[i]);
314 net->magnetos[i] = htonl(net->magnetos[i]);
315 net->starter_power[i] = htonl(net->starter_power[i]);
316 htond(net->throttle[i]);
317 htond(net->mixture[i]);
318 net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
319 htond(net->prop_advance[i]);
320 htond(net->condition[i]);
321 net->engine_ok[i] = htonl(net->engine_ok[i]);
322 net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
323 net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
324 net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
325 net->oil_press_status[i] = htonl(net->oil_press_status[i]);
326 net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
329 net->num_tanks = htonl(net->num_tanks);
330 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
331 net->fuel_selector[i] = htonl(net->fuel_selector[i]);
334 net->cross_feed = htonl(net->cross_feed);
335 htond(net->brake_left);
336 htond(net->brake_right);
337 htond(net->copilot_brake_left);
338 htond(net->copilot_brake_right);
339 htond(net->brake_parking);
340 net->gear_handle = htonl(net->gear_handle);
341 net->master_avionics = htonl(net->master_avionics);
342 htond(net->wind_speed_kt);
343 htond(net->wind_dir_deg);
344 htond(net->turbulence_norm);
346 htond(net->press_inhg);
349 net->icing = htonl(net->icing);
350 net->speedup = htonl(net->speedup);
351 net->freeze = htonl(net->freeze);
354 if ( net->version != FG_NET_CTRLS_VERSION ) {
355 SG_LOG( SG_IO, SG_ALERT,
356 "Version mismatch with raw controls packet format." );
357 SG_LOG( SG_IO, SG_ALERT,
358 "FlightGear needs version = " << FG_NET_CTRLS_VERSION
359 << " but is receiving version = " << net->version );
361 node = fgGetNode("/controls/flight", true);
362 node->setDoubleValue( "aileron", net->aileron );
363 node->setDoubleValue( "elevator", net->elevator );
364 node->setDoubleValue( "rudder", net->rudder );
365 node->setDoubleValue( "aileron-trim", net->aileron_trim );
366 node->setDoubleValue( "elevator-trim", net->elevator_trim );
367 node->setDoubleValue( "rudder-trim", net->rudder_trim );
368 node->setDoubleValue( "flaps", net->flaps );
369 node->setDoubleValue( "speedbrake", net->speedbrake ); //JWW
371 node->setDoubleValue( "spoilers", net->spoilers ); //JWW
372 // cout << "NET->Spoilers: " << net->spoilers << endl;
373 fgSetBool( "/systems/electrical/outputs/flaps", net->flaps_power );
374 node->setBoolValue( "flaps-serviceable", net->flap_motor_ok );
376 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
378 node = fgGetNode("/controls/engines/engine", i);
379 node->getChild( "throttle" )->setDoubleValue( net->throttle[i] );
380 node->getChild( "mixture" )->setDoubleValue( net->mixture[i] );
381 node->getChild( "propeller-pitch" )
382 ->setDoubleValue( net->prop_advance[i] );
383 node->getChild( "condition" )
384 ->setDoubleValue( net->condition[i] );
385 node->getChild( "magnetos" )->setDoubleValue( net->magnetos[i] );
386 node->getChild( "starter" )->setDoubleValue( net->starter_power[i] );
387 node->getChild( "feed_tank" )->setIntValue( net->feed_tank_to[i] );
388 node->getChild( "reverser" )->setBoolValue( net->reverse[i] );
390 SGPropertyNode *faults = node->getNode( "faults", true );
391 faults->setBoolValue( "serviceable", net->engine_ok[i] );
392 faults->setBoolValue( "left-magneto-serviceable",
393 net->mag_left_ok[i] );
394 faults->setBoolValue( "right-magneto-serviceable",
395 net->mag_right_ok[i]);
396 faults->setBoolValue( "spark-plugs-serviceable",
397 net->spark_plugs_ok[i] );
398 faults->setIntValue( "oil-pressure-status", net->oil_press_status[i] );
399 faults->setBoolValue( "fuel-pump-serviceable", net->fuel_pump_ok[i] );
402 fgSetBool( "/systems/electrical/outputs/fuel-pump",
403 net->fuel_pump_power[0] );
405 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
406 node = fgGetNode( "/controls/fuel/tank", i );
407 node->getChild( "fuel_selector" )
408 ->setBoolValue( net->fuel_selector[i] );
409 // node->getChild( "to_tank" )->xfer_tank( i, net->xfer_to[i] );
411 node = fgGetNode( "/controls/gear" );
412 if ( node != NULL ) {
413 node->getChild( "brake-left" )->setDoubleValue( net->brake_left );
414 node->getChild( "brake-right" )->setDoubleValue( net->brake_right );
415 node->getChild( "copilot-brake-left" )
416 ->setDoubleValue( net->copilot_brake_left );
417 node->getChild( "copilot-brake-right" )
418 ->setDoubleValue( net->copilot_brake_right );
419 node->getChild( "brake-parking" )->setDoubleValue( net->brake_parking );
422 node = fgGetNode( "/controls/gear", true );
423 node->setBoolValue( "gear-down", net->gear_handle );
424 // node->setDoubleValue( "brake-parking", net->brake_parking );
425 // node->setDoubleValue( net->brake_left );
426 // node->setDoubleValue( net->brake_right );
428 node = fgGetNode( "/controls/switches", true );
429 node->setBoolValue( "master-bat", net->master_bat );
430 node->setBoolValue( "master-alt", net->master_alt );
431 node->setBoolValue( "master-avionics", net->master_avionics );
433 node = fgGetNode( "/environment", true );
434 node->setDoubleValue( "wind-speed-kt", net->wind_speed_kt );
435 node->setDoubleValue( "wind-from-heading-deg", net->wind_dir_deg );
436 node->setDoubleValue( "turbulence/magnitude-norm", net->turbulence_norm );
437 node->setBoolValue( "magnetic-variation-deg", net->magvar );
439 node->setDoubleValue( "/environment/temperature-degc",
441 node->setDoubleValue( "/environment/pressure-sea-level-inhg",
444 // ground elevation ???
446 fgSetDouble("/hazards/icing/wing", net->icing);
448 node = fgGetNode( "/radios", true );
449 node->setDoubleValue( "comm/frequencies/selected-mhz[0]", net->comm_1 );
450 node->setDoubleValue( "nav/frequencies/selected-mhz[0]", net->nav_1 );
451 node->setDoubleValue( "nav[1]/frequencies/selected-mhz[0]", net->nav_2 );
453 fgSetInt( "/sim/speed-up", net->speedup );
455 if ( honor_freezes ) {
456 node = fgGetNode( "/sim/freeze", true );
457 node->setBoolValue( "master", net->freeze & 0x01 );
458 node->setBoolValue( "position", net->freeze & 0x02 );
459 node->setBoolValue( "fuel", net->freeze & 0x04 );
464 // process work for this port
465 bool FGNativeCtrls::process() {
466 SGIOChannel *io = get_io_channel();
467 int length = sizeof(FGNetCtrls);
469 if ( get_direction() == SG_IO_OUT ) {
470 // cout << "size of cur_fdm_state = " << length << endl;
472 FGProps2NetCtrls( &net_ctrls, true, true );
474 if ( ! io->write( (char *)(& net_ctrls), length ) ) {
475 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
478 } else if ( get_direction() == SG_IO_IN ) {
479 if ( io->get_type() == sgFileType ) {
480 if ( io->read( (char *)(& net_ctrls), length ) == length ) {
481 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
482 FGNetCtrls2Props( &net_ctrls, true, true );
485 while ( io->read( (char *)(& net_ctrls), length ) == length ) {
486 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
487 FGNetCtrls2Props( &net_ctrls, true, true );
497 bool FGNativeCtrls::close() {
498 SGIOChannel *io = get_io_channel();
500 set_enabled( false );
502 if ( ! io->close() ) {