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 net_byte_order ) {
103 SGPropertyNode * node;
104 SGPropertyNode * tempnode;
107 node = fgGetNode("/controls/flight", true);
108 net->version = FG_NET_CTRLS_VERSION;
109 net->aileron = node->getDoubleValue( "aileron" );
110 net->elevator = node->getDoubleValue( "elevator" );
111 net->elevator_trim = node->getDoubleValue( "elevator-trim" );
112 net->rudder = node->getDoubleValue( "rudder" );
113 net->flaps = node->getDoubleValue( "flaps" );
114 node = fgGetNode("/controls", true);
116 = node->getDoubleValue( "/systems/electrical/outputs/flaps",
118 net->num_engines = FGNetCtrls::FG_MAX_ENGINES;
119 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
120 node = fgGetNode("/controls/engines/engine", i );
121 net->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
122 net->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
123 net->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
124 net->magnetos[i] = node->getIntValue( "magnetos", 0 );
126 // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
129 // cout << "Starter -> " << node->getIntValue( "starter", false )
132 node = fgGetNode("/controls", true);
133 net->fuel_pump_power[i]
134 = node->getDoubleValue( "/systems/electrical/outputs/fuel-pump",
137 net->starter_power[i]
138 = node->getDoubleValue( "/systems/electrical/outputs/starter",
141 net->num_tanks = FGNetCtrls::FG_MAX_TANKS;
142 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
143 node = fgGetNode("/controls/fuel/tank", i);
144 if ( node->getChild("fuel_selector") != 0 ) {
145 net->fuel_selector[i]
146 = node->getChild("fuel_selector")->getBoolValue();
148 net->fuel_selector[i] = false;
151 net->num_wheels = FGNetCtrls::FG_MAX_WHEELS;
152 tempnode = fgGetNode("/controls/gear", true);
153 for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
154 node = fgGetNode("/controls/gear/wheel", i);
155 if ( node->getChild("brake") != NULL ) {
156 if ( tempnode->getChild("parking-brake")->getDoubleValue() > 0.0 ) {
160 = node->getChild("brake")->getDoubleValue();
167 node = fgGetNode("/controls/switches", true);
168 tempnode = node->getChild("master-bat");
169 if ( tempnode != NULL ) {
170 net->master_bat = tempnode->getBoolValue();
172 tempnode = node->getChild("master-alt");
173 if ( tempnode != NULL ) {
174 net->master_alt = tempnode->getBoolValue();
176 tempnode = node->getChild("master-avionics");
177 if ( tempnode != NULL ) {
178 net->master_avionics = tempnode->getBoolValue();
181 net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
182 net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
183 net->turbulence_norm =
184 fgGetDouble("/environment/turbulence/magnitude-norm");
186 // cur_fdm_state->get_ground_elev_ft() is what we want ... this
187 // reports the altitude of the aircraft.
188 // "/environment/ground-elevation-m" reports the ground elevation
189 // of the current view point which could change substantially if
190 // the user is switching views.
191 net->hground = cur_fdm_state->get_ground_elev_ft() * SG_FEET_TO_METER;
192 net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
193 net->speedup = fgGetInt("/sim/speed-up");
195 if ( fgGetBool("/sim/freeze/master") ) {
198 if ( fgGetBool("/sim/freeze/position") ) {
201 if ( fgGetBool("/sim/freeze/fuel") ) {
205 if ( net_byte_order ) {
206 // convert to network byte order
207 net->version = htonl(net->version);
209 htond(net->elevator);
210 htond(net->elevator_trim);
213 net->flaps_power = htonl(net->flaps_power);
214 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
215 htond(net->throttle[i]);
216 htond(net->mixture[i]);
217 net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
218 htond(net->prop_advance[i]);
219 net->magnetos[i] = htonl(net->magnetos[i]);
220 net->starter_power[i] = htonl(net->starter_power[i]);
222 net->num_engines = htonl(net->num_engines);
223 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
224 net->fuel_selector[i] = htonl(net->fuel_selector[i]);
226 net->num_tanks = htonl(net->num_tanks);
227 for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
228 htond(net->brake[i]);
230 net->num_wheels = htonl(net->num_wheels);
231 net->gear_handle = htonl(net->gear_handle);
232 net->master_bat = htonl(net->master_bat);
233 net->master_alt = htonl(net->master_alt);
234 net->master_avionics = htonl(net->master_avionics);
235 htond(net->wind_speed_kt);
236 htond(net->wind_dir_deg);
237 htond(net->turbulence_norm);
240 net->speedup = htonl(net->speedup);
241 net->freeze = htonl(net->freeze);
246 // Update the property tree from the FGNetCtrls structure.
247 void FGNetCtrls2Props( FGNetCtrls *net, bool net_byte_order ) {
250 SGPropertyNode * node;
252 if ( net_byte_order ) {
253 // convert from network byte order
254 net->version = htonl(net->version);
256 htond(net->elevator);
257 htond(net->elevator_trim);
260 net->flaps_power = htonl(net->flaps_power);
261 net->num_engines = htonl(net->num_engines);
262 for ( i = 0; i < net->num_engines; ++i ) {
263 net->magnetos[i] = htonl(net->magnetos[i]);
264 net->starter_power[i] = htonl(net->starter_power[i]);
265 htond(net->throttle[i]);
266 htond(net->mixture[i]);
267 net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
268 htond(net->prop_advance[i]);
270 net->num_tanks = htonl(net->num_tanks);
271 for ( i = 0; i < net->num_tanks; ++i ) {
272 net->fuel_selector[i] = htonl(net->fuel_selector[i]);
274 net->num_wheels = htonl(net->num_wheels);
275 for ( i = 0; i < net->num_wheels; ++i ) {
276 htond(net->brake[i]);
278 net->gear_handle = htonl(net->gear_handle);
279 net->master_bat = htonl(net->master_bat);
280 net->master_alt = htonl(net->master_alt);
281 net->master_avionics = htonl(net->master_avionics);
282 htond(net->wind_speed_kt);
283 htond(net->wind_dir_deg);
284 htond(net->turbulence_norm);
287 net->speedup = htonl(net->speedup);
288 net->freeze = htonl(net->freeze);
291 if ( net->version != FG_NET_CTRLS_VERSION ) {
292 SG_LOG( SG_IO, SG_ALERT,
293 "Version mismatch with raw controls packet format." );
294 SG_LOG( SG_IO, SG_ALERT,
295 "FlightGear needs version = " << FG_NET_CTRLS_VERSION
296 << " but is receiving version = " << net->version );
298 node = fgGetNode("/controls/flight", true);
299 node->setDoubleValue( "aileron", net->aileron );
300 node->setDoubleValue( "elevator", net->elevator );
301 node->setDoubleValue( "elevator-trim", net->elevator_trim );
302 node->setDoubleValue( "rudder", net->rudder );
303 node->setDoubleValue( "flaps", net->flaps );
305 fgSetBool( "/systems/electrical/outputs/flaps", net->flaps_power );
307 for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
308 node = fgGetNode("/controls/engines/engine", i);
309 node->getChild( "throttle" )->setDoubleValue( net->throttle[i] );
310 node->getChild( "mixture" )->setDoubleValue( net->mixture[i] );
311 node->getChild( "propeller-pitch" )
312 ->setDoubleValue( net->prop_advance[i] );
313 node->getChild( "magnetos" )->setDoubleValue( net->magnetos[i] );
316 fgSetBool( "/systems/electrical/outputs/fuel-pump",
317 net->fuel_pump_power[0] );
318 fgSetBool( "/systems/electrical/outputs/starter",
319 net->starter_power[0] );
321 for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
322 node = fgGetNode( "/controls/fuel/tank", i );
323 node->getChild( "fuel_selector" )
324 ->setBoolValue( net->fuel_selector[i] );
326 for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
327 node = fgGetNode( "/controls/gear/wheel", i );
328 node->getChild( "brake" )->setDoubleValue( net->brake[i] );
331 node = fgGetNode( "/controls/gear", true );
332 node->setBoolValue( "gear-down", net->gear_handle );
334 node = fgGetNode( "/controls/switches", true );
335 node->setBoolValue( "master-bat", net->master_bat );
336 node->setBoolValue( "master-alt", net->master_alt );
337 node->setBoolValue( "master-avionics", net->master_avionics );
339 node = fgGetNode( "/environment", true );
340 node->setDoubleValue( "wind-speed-kt", net->wind_speed_kt );
341 node->setDoubleValue( "wind-from-heading-deg", net->wind_dir_deg );
342 node->setDoubleValue( "turbulence/magnitude-norm", net->turbulence_norm );
343 node->setBoolValue( "magnetic-variation-deg", net->magvar );
345 // ground elevation ???
347 fgSetInt( "/sim/speed-up", net->speedup );
349 node = fgGetNode( "/sim/freeze", true );
350 node->setBoolValue( "master", net->freeze & 0x01 );
351 node->setBoolValue( "position", net->freeze & 0x02 );
352 node->setBoolValue( "fuel", net->freeze & 0x04 );
356 // process work for this port
357 bool FGNativeCtrls::process() {
358 SGIOChannel *io = get_io_channel();
359 int length = sizeof(FGNetCtrls);
361 if ( get_direction() == SG_IO_OUT ) {
362 // cout << "size of cur_fdm_state = " << length << endl;
364 FGProps2NetCtrls( &net_ctrls );
366 if ( ! io->write( (char *)(& net_ctrls), length ) ) {
367 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
370 } else if ( get_direction() == SG_IO_IN ) {
371 if ( io->get_type() == sgFileType ) {
372 if ( io->read( (char *)(& net_ctrls), length ) == length ) {
373 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
374 FGNetCtrls2Props( &net_ctrls );
377 while ( io->read( (char *)(& net_ctrls), length ) == length ) {
378 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
379 FGNetCtrls2Props( &net_ctrls );
389 bool FGNativeCtrls::close() {
390 SGIOChannel *io = get_io_channel();
392 set_enabled( false );
394 if ( ! io->close() ) {