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 - 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/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 *tempnode;
109 node = fgGetNode("/controls/flight", true);
110 net->version = FG_NET_CTRLS_VERSION;
111 net->aileron = node->getDoubleValue( "aileron" );
112 net->elevator = node->getDoubleValue( "elevator" );
113 net->elevator_trim = node->getDoubleValue( "elevator-trim" );
114 net->rudder = node->getDoubleValue( "rudder" );
115 net->flaps = node->getDoubleValue( "flaps" );
117 = fgGetDouble( "/systems/electrical/outputs/flaps", 1.0 ) >= 1.0;
118 net->flap_motor_ok = node->getBoolValue( "flaps-serviceable" );
120 net->num_engines = FGNetCtrls::FG_MAX_ENGINES;
121 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
123 node = fgGetNode("/controls/engines/engine", i );
124 net->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
125 net->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
126 net->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
127 net->magnetos[i] = node->getIntValue( "magnetos", 0 );
129 // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
132 // cout << "Starter -> " << node->getIntValue( "starter", false )
135 net->fuel_pump_power[i]
136 = node->getDoubleValue( "/systems/electrical/outputs/fuel-pump",
139 net->starter_power[i]
140 = node->getDoubleValue( "/systems/electrical/outputs/starter",
144 SGPropertyNode *faults = node->getChild( "faults", 0, true );
145 net->engine_ok[i] = faults->getBoolValue( "serviceable", true );
147 = faults->getBoolValue( "left-magneto-serviceable", true );
149 = faults->getBoolValue( "right-magneto-serviceable", true);
150 net->spark_plugs_ok[i]
151 = faults->getBoolValue( "spark-plugs-serviceable", true );
152 net->oil_press_status[i]
153 = faults->getIntValue( "oil-pressure-status", 0 );
155 = faults->getBoolValue( "fuel-pump-serviceable", true );
157 net->num_tanks = FGNetCtrls::FG_MAX_TANKS;
158 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
159 node = fgGetNode("/controls/fuel/tank", i);
160 if ( node->getChild("fuel_selector") != 0 ) {
161 net->fuel_selector[i]
162 = node->getChild("fuel_selector")->getBoolValue();
164 net->fuel_selector[i] = false;
167 net->num_wheels = FGNetCtrls::FG_MAX_WHEELS;
168 tempnode = fgGetNode("/controls/gear", true);
169 for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
170 node = fgGetNode("/controls/gear/wheel", i);
171 if ( node != NULL && node->getChild("brake") != NULL ) {
172 if ( tempnode->getChild("parking-brake")->getDoubleValue() > 0.0 ) {
176 = node->getChild("brake")->getDoubleValue();
183 node = fgGetNode("/controls/switches", true);
184 tempnode = node->getChild("master-bat");
185 if ( tempnode != NULL ) {
186 net->master_bat = tempnode->getBoolValue();
188 tempnode = node->getChild("master-alt");
189 if ( tempnode != NULL ) {
190 net->master_alt = tempnode->getBoolValue();
192 tempnode = node->getChild("master-avionics");
193 if ( tempnode != NULL ) {
194 net->master_avionics = tempnode->getBoolValue();
197 net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
198 net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
199 net->turbulence_norm =
200 fgGetDouble("/environment/turbulence/magnitude-norm");
202 net->temp_c = fgGetDouble("/environment/temperature-degc");
203 net->press_inhg = fgGetDouble("/environment/pressure-sea-level-inhg");
205 // cur_fdm_state->get_ground_elev_ft() is what we want ... this
206 // reports the altitude of the aircraft.
207 // "/environment/ground-elevation-m" reports the ground elevation
208 // of the current view point which could change substantially if
209 // the user is switching views.
210 net->hground = cur_fdm_state->get_ground_elev_ft() * SG_FEET_TO_METER;
211 net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
212 net->speedup = fgGetInt("/sim/speed-up");
214 if ( honor_freezes ) {
215 if ( fgGetBool("/sim/freeze/master") ) {
218 if ( fgGetBool("/sim/freeze/position") ) {
221 if ( fgGetBool("/sim/freeze/fuel") ) {
226 if ( net_byte_order ) {
227 // convert to network byte order
228 net->version = htonl(net->version);
230 htond(net->elevator);
231 htond(net->elevator_trim);
234 net->flaps_power = htonl(net->flaps_power);
235 net->flap_motor_ok = htonl(net->flap_motor_ok);
236 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
237 net->magnetos[i] = htonl(net->magnetos[i]);
238 net->starter_power[i] = htonl(net->starter_power[i]);
239 htond(net->throttle[i]);
240 htond(net->mixture[i]);
241 net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
242 htond(net->prop_advance[i]);
243 net->engine_ok[i] = htonl(net->engine_ok[i]);
244 net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
245 net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
246 net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
247 net->oil_press_status[i] = htonl(net->oil_press_status[i]);
248 net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
250 net->num_engines = htonl(net->num_engines);
251 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
252 net->fuel_selector[i] = htonl(net->fuel_selector[i]);
254 net->num_tanks = htonl(net->num_tanks);
255 for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
256 htond(net->brake[i]);
258 net->num_wheels = htonl(net->num_wheels);
259 net->gear_handle = htonl(net->gear_handle);
260 net->master_bat = htonl(net->master_bat);
261 net->master_alt = htonl(net->master_alt);
262 net->master_avionics = htonl(net->master_avionics);
263 htond(net->wind_speed_kt);
264 htond(net->wind_dir_deg);
265 htond(net->turbulence_norm);
267 htond(net->press_inhg);
270 net->speedup = htonl(net->speedup);
271 net->freeze = htonl(net->freeze);
276 // Update the property tree from the FGNetCtrls structure.
277 void FGNetCtrls2Props( FGNetCtrls *net, bool honor_freezes,
278 bool net_byte_order )
282 SGPropertyNode * node;
284 if ( net_byte_order ) {
285 // convert from network byte order
286 net->version = htonl(net->version);
288 htond(net->elevator);
289 htond(net->elevator_trim);
292 net->flaps_power = htonl(net->flaps_power);
293 net->flap_motor_ok = htonl(net->flap_motor_ok);
294 net->num_engines = htonl(net->num_engines);
295 for ( i = 0; i < net->num_engines; ++i ) {
296 net->magnetos[i] = htonl(net->magnetos[i]);
297 net->starter_power[i] = htonl(net->starter_power[i]);
298 htond(net->throttle[i]);
299 htond(net->mixture[i]);
300 net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
301 htond(net->prop_advance[i]);
302 net->engine_ok[i] = htonl(net->engine_ok[i]);
303 net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
304 net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
305 net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
306 net->oil_press_status[i] = htonl(net->oil_press_status[i]);
307 net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
309 net->num_tanks = htonl(net->num_tanks);
310 for ( i = 0; i < net->num_tanks; ++i ) {
311 net->fuel_selector[i] = htonl(net->fuel_selector[i]);
313 net->num_wheels = htonl(net->num_wheels);
314 for ( i = 0; i < net->num_wheels; ++i ) {
315 htond(net->brake[i]);
317 net->gear_handle = htonl(net->gear_handle);
318 net->master_bat = htonl(net->master_bat);
319 net->master_alt = htonl(net->master_alt);
320 net->master_avionics = htonl(net->master_avionics);
321 htond(net->wind_speed_kt);
322 htond(net->wind_dir_deg);
323 htond(net->turbulence_norm);
325 htond(net->press_inhg);
328 net->speedup = htonl(net->speedup);
329 net->freeze = htonl(net->freeze);
332 if ( net->version != FG_NET_CTRLS_VERSION ) {
333 SG_LOG( SG_IO, SG_ALERT,
334 "Version mismatch with raw controls packet format." );
335 SG_LOG( SG_IO, SG_ALERT,
336 "FlightGear needs version = " << FG_NET_CTRLS_VERSION
337 << " but is receiving version = " << net->version );
339 node = fgGetNode("/controls/flight", true);
340 node->setDoubleValue( "aileron", net->aileron );
341 node->setDoubleValue( "elevator", net->elevator );
342 node->setDoubleValue( "elevator-trim", net->elevator_trim );
343 node->setDoubleValue( "rudder", net->rudder );
344 node->setDoubleValue( "flaps", net->flaps );
345 fgSetBool( "/systems/electrical/outputs/flaps", net->flaps_power );
346 node->setBoolValue( "flaps-serviceable", net->flap_motor_ok );
348 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
350 node = fgGetNode("/controls/engines/engine", i);
351 node->getChild( "throttle" )->setDoubleValue( net->throttle[i] );
352 node->getChild( "mixture" )->setDoubleValue( net->mixture[i] );
353 node->getChild( "propeller-pitch" )
354 ->setDoubleValue( net->prop_advance[i] );
355 node->getChild( "magnetos" )->setDoubleValue( net->magnetos[i] );
358 SGPropertyNode *faults = node->getNode( "faults", true );
359 faults->setBoolValue( "serviceable", net->engine_ok[i] );
360 faults->setBoolValue( "left-magneto-serviceable",
361 net->mag_left_ok[i] );
362 faults->setBoolValue( "right-magneto-serviceable",
363 net->mag_right_ok[i]);
364 faults->setBoolValue( "spark-plugs-serviceable",
365 net->spark_plugs_ok[i] );
366 faults->setIntValue( "oil-pressure-status", net->oil_press_status[i] );
367 faults->setBoolValue( "fuel-pump-serviceable", net->fuel_pump_ok[i] );
370 fgSetBool( "/systems/electrical/outputs/fuel-pump",
371 net->fuel_pump_power[0] );
372 fgSetBool( "/systems/electrical/outputs/starter",
373 net->starter_power[0] );
375 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
376 node = fgGetNode( "/controls/fuel/tank", i );
377 node->getChild( "fuel_selector" )
378 ->setBoolValue( net->fuel_selector[i] );
380 for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
381 node = fgGetNode( "/controls/gear/wheel", i );
382 if ( node != NULL ) {
383 node->getChild( "brake" )->setDoubleValue( net->brake[i] );
387 node = fgGetNode( "/controls/gear", true );
388 node->setBoolValue( "gear-down", net->gear_handle );
390 node = fgGetNode( "/controls/switches", true );
391 node->setBoolValue( "master-bat", net->master_bat );
392 node->setBoolValue( "master-alt", net->master_alt );
393 node->setBoolValue( "master-avionics", net->master_avionics );
395 node = fgGetNode( "/environment", true );
396 node->setDoubleValue( "wind-speed-kt", net->wind_speed_kt );
397 node->setDoubleValue( "wind-from-heading-deg", net->wind_dir_deg );
398 node->setDoubleValue( "turbulence/magnitude-norm", net->turbulence_norm );
399 node->setBoolValue( "magnetic-variation-deg", net->magvar );
401 node->setDoubleValue( "/environment/temperature-degc",
403 node->setDoubleValue( "/environment/pressure-sea-level-inhg",
406 // ground elevation ???
408 fgSetInt( "/sim/speed-up", net->speedup );
410 if ( honor_freezes ) {
411 node = fgGetNode( "/sim/freeze", true );
412 node->setBoolValue( "master", net->freeze & 0x01 );
413 node->setBoolValue( "position", net->freeze & 0x02 );
414 node->setBoolValue( "fuel", net->freeze & 0x04 );
419 // process work for this port
420 bool FGNativeCtrls::process() {
421 SGIOChannel *io = get_io_channel();
422 int length = sizeof(FGNetCtrls);
424 if ( get_direction() == SG_IO_OUT ) {
425 // cout << "size of cur_fdm_state = " << length << endl;
427 FGProps2NetCtrls( &net_ctrls, true, true );
429 if ( ! io->write( (char *)(& net_ctrls), length ) ) {
430 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
433 } else if ( get_direction() == SG_IO_IN ) {
434 if ( io->get_type() == sgFileType ) {
435 if ( io->read( (char *)(& net_ctrls), length ) == length ) {
436 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
437 FGNetCtrls2Props( &net_ctrls, true, true );
440 while ( io->read( (char *)(& net_ctrls), length ) == length ) {
441 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
442 FGNetCtrls2Props( &net_ctrls, true, true );
452 bool FGNativeCtrls::close() {
453 SGIOChannel *io = get_io_channel();
455 set_enabled( false );
457 if ( ! io->close() ) {