]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_ctrls.cxx
Clean up a couple bugs in the multiengine handling of the net_fdm.hxx
[flightgear.git] / src / Network / native_ctrls.cxx
1 // native_ctrls.cxx -- FGFS "Native" controls I/O class
2 //
3 // Written by Curtis Olson, started July 2001.
4 //
5 // Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/io/iochannel.hxx>
30 #include <simgear/io/lowlevel.hxx> // endian tests
31
32 #include <FDM/flight.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Scenery/scenery.hxx>  // ground elevation
35
36 #include "native_ctrls.hxx"
37
38 // FreeBSD works better with this included last ... (?)
39 #if defined(WIN32) && !defined(__CYGWIN__)
40 #  include <windows.h>
41 #else
42 #  include <netinet/in.h>       // htonl() ntohl()
43 #endif
44
45
46 FGNativeCtrls::FGNativeCtrls() {
47 }
48
49 FGNativeCtrls::~FGNativeCtrls() {
50 }
51
52
53 // open hailing frequencies
54 bool FGNativeCtrls::open() {
55     if ( is_enabled() ) {
56         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
57                 << "is already in use, ignoring" );
58         return false;
59     }
60
61     SGIOChannel *io = get_io_channel();
62
63     if ( ! io->open( get_direction() ) ) {
64         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
65         return false;
66     }
67
68     set_enabled( true );
69
70     return true;
71 }
72
73
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
82 // possible
83 static void htond (double &x)   
84 {
85     if ( sgIsLittleEndian() ) {
86         int    *Double_Overlay;
87         int     Holding_Buffer;
88     
89         Double_Overlay = (int *) &x;
90         Holding_Buffer = Double_Overlay [0];
91     
92         Double_Overlay [0] = htonl (Double_Overlay [1]);
93         Double_Overlay [1] = htonl (Holding_Buffer);
94     } else {
95         return;
96     }
97 }
98
99
100 // Populate the FGNetCtrls structure from the property tree.
101 void FGProps2NetCtrls( FGNetCtrls *net, bool honor_freezes,
102                        bool net_byte_order )
103 {
104     int i;
105     SGPropertyNode *node;
106     SGPropertyNode *starter;
107     SGPropertyNode *fuelpump;
108     SGPropertyNode *tempnode;
109
110     // fill in values
111     node  = fgGetNode("/controls/flight", true);
112     net->version = FG_NET_CTRLS_VERSION;
113     net->aileron = node->getDoubleValue( "aileron" );
114     net->elevator = node->getDoubleValue( "elevator" );
115     net->elevator_trim = node->getDoubleValue( "elevator-trim" );
116     net->rudder = node->getDoubleValue( "rudder" );
117     net->flaps = node->getDoubleValue( "flaps" );
118     net->flaps_power
119         = fgGetDouble( "/systems/electrical/outputs/flaps", 1.0 ) >= 1.0;
120     net->flap_motor_ok = node->getBoolValue( "flaps-serviceable" );
121
122     net->num_engines = FGNetCtrls::FG_MAX_ENGINES;
123     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
124         // Controls
125         node = fgGetNode("/controls/engines/engine", i );
126         starter = fgGetNode("/systems/electrical/outputs/starter", i );
127         fuelpump = fgGetNode("/systems/electrical/outputs/fuel-pump", i );
128         net->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
129         net->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
130         net->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
131         net->magnetos[i] = node->getIntValue( "magnetos", 0 );
132         if ( i == 0 ) {
133           // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
134         }
135         if ( i == 0 ) {
136           // cout << "Starter -> " << node->getIntValue( "starter", false )
137           //      << endl;
138         }
139
140         if ( fuelpump != NULL ) {
141             net->fuel_pump_power[i] = ( fuelpump->getDoubleValue() >= 1.0 );
142         } else {
143             net->fuel_pump_power[i] = 0.0;
144         }
145
146         if ( starter != NULL ) {
147             net->starter_power[i] = ( starter->getDoubleValue() >= 1.0 );
148         } else {
149             net->starter_power[i] = 0.0;
150         }
151
152         // Faults
153         SGPropertyNode *faults = node->getChild( "faults", 0, true );
154         net->engine_ok[i] = faults->getBoolValue( "serviceable", true );
155         net->mag_left_ok[i]
156           = faults->getBoolValue( "left-magneto-serviceable", true );
157         net->mag_right_ok[i]
158           = faults->getBoolValue( "right-magneto-serviceable", true);
159         net->spark_plugs_ok[i]
160           = faults->getBoolValue( "spark-plugs-serviceable", true );
161         net->oil_press_status[i]
162           = faults->getIntValue( "oil-pressure-status", 0 );
163         net->fuel_pump_ok[i]
164           = faults->getBoolValue( "fuel-pump-serviceable", true );
165     }
166     net->num_tanks = FGNetCtrls::FG_MAX_TANKS;
167     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
168         node = fgGetNode("/controls/fuel/tank", i);
169         if ( node->getChild("fuel_selector") != 0 ) {
170             net->fuel_selector[i]
171                 = node->getChild("fuel_selector")->getBoolValue();
172         } else {
173             net->fuel_selector[i] = false;
174         }
175     }
176     node = fgGetNode("/controls/gear", true);
177     net->brake_left = node->getChild("brake-left")->getDoubleValue();
178     net->brake_right = node->getChild("brake-right")->getDoubleValue();
179     net->brake_parking = node->getChild("brake-parking")->getDoubleValue();
180
181     node = fgGetNode("/controls/switches", true);
182     tempnode = node->getChild("master-bat");
183     if ( tempnode != NULL ) {
184         net->master_bat = tempnode->getBoolValue();
185     }
186     tempnode = node->getChild("master-alt");
187     if ( tempnode != NULL ) {
188         net->master_alt = tempnode->getBoolValue();
189     }
190     tempnode = node->getChild("master-avionics");
191     if ( tempnode != NULL ) {
192         net->master_avionics = tempnode->getBoolValue();
193     }
194
195     net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
196     net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
197     net->turbulence_norm =
198         fgGetDouble("/environment/turbulence/magnitude-norm");
199
200     net->temp_c = fgGetDouble("/environment/temperature-degc");
201     net->press_inhg = fgGetDouble("/environment/pressure-sea-level-inhg");
202
203     // cur_fdm_state->get_ground_elev_ft() is what we want ... this
204     // reports the altitude of the aircraft.
205     // "/environment/ground-elevation-m" reports the ground elevation
206     // of the current view point which could change substantially if
207     // the user is switching views.
208     net->hground = cur_fdm_state->get_ground_elev_ft() * SG_FEET_TO_METER;
209     net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
210     net->speedup = fgGetInt("/sim/speed-up");
211     net->freeze = 0;
212     if ( honor_freezes ) {
213         if ( fgGetBool("/sim/freeze/master") ) {
214             net->freeze |= 0x01;
215         }
216         if ( fgGetBool("/sim/freeze/position") ) {
217             net->freeze |= 0x02;
218         }
219         if ( fgGetBool("/sim/freeze/fuel") ) {
220             net->freeze |= 0x04;
221         }
222     }
223
224     if ( net_byte_order ) {
225         // convert to network byte order
226         net->version = htonl(net->version);
227         htond(net->aileron);
228         htond(net->elevator);
229         htond(net->elevator_trim);
230         htond(net->rudder);
231         htond(net->flaps);
232         net->flaps_power = htonl(net->flaps_power);
233         net->flap_motor_ok = htonl(net->flap_motor_ok);
234         for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
235             net->magnetos[i] = htonl(net->magnetos[i]);
236             net->starter_power[i] = htonl(net->starter_power[i]);
237             htond(net->throttle[i]);
238             htond(net->mixture[i]);
239             net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
240             htond(net->prop_advance[i]);
241             net->engine_ok[i] = htonl(net->engine_ok[i]);
242             net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
243             net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
244             net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
245             net->oil_press_status[i] = htonl(net->oil_press_status[i]);
246             net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
247         }
248         net->num_engines = htonl(net->num_engines);
249         for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
250             net->fuel_selector[i] = htonl(net->fuel_selector[i]);
251         }
252         net->num_tanks = htonl(net->num_tanks);
253         htond(net->brake_left);
254         htond(net->brake_right);
255         htond(net->brake_parking);
256         net->gear_handle = htonl(net->gear_handle);
257         net->master_bat = htonl(net->master_bat);
258         net->master_alt = htonl(net->master_alt);
259         net->master_avionics = htonl(net->master_avionics);
260         htond(net->wind_speed_kt);
261         htond(net->wind_dir_deg);
262         htond(net->turbulence_norm);
263         htond(net->temp_c);
264         htond(net->press_inhg);
265         htond(net->hground);
266         htond(net->magvar);
267         net->speedup = htonl(net->speedup);
268         net->freeze = htonl(net->freeze);
269     }
270 }
271
272
273 // Update the property tree from the FGNetCtrls structure.
274 void FGNetCtrls2Props( FGNetCtrls *net, bool honor_freezes,
275                        bool net_byte_order )
276 {
277     int i;
278
279     SGPropertyNode * node;
280
281     if ( net_byte_order ) {
282         // convert from network byte order
283         net->version = htonl(net->version);
284         htond(net->aileron);
285         htond(net->elevator);
286         htond(net->elevator_trim);
287         htond(net->rudder);
288         htond(net->flaps);
289         net->flaps_power = htonl(net->flaps_power);
290         net->flap_motor_ok = htonl(net->flap_motor_ok);
291         net->num_engines = htonl(net->num_engines);
292         for ( i = 0; i < net->num_engines; ++i ) {
293             net->magnetos[i] = htonl(net->magnetos[i]);
294             net->starter_power[i] = htonl(net->starter_power[i]);
295             htond(net->throttle[i]);
296             htond(net->mixture[i]);
297             net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
298             htond(net->prop_advance[i]);
299             net->engine_ok[i] = htonl(net->engine_ok[i]);
300             net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
301             net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
302             net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
303             net->oil_press_status[i] = htonl(net->oil_press_status[i]);
304             net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
305         }
306         net->num_tanks = htonl(net->num_tanks);
307         for ( i = 0; i < net->num_tanks; ++i ) {
308             net->fuel_selector[i] = htonl(net->fuel_selector[i]);
309         }
310         htond(net->brake_left);
311         htond(net->brake_right);
312         htond(net->brake_parking);
313         net->gear_handle = htonl(net->gear_handle);
314         net->master_bat = htonl(net->master_bat);
315         net->master_alt = htonl(net->master_alt);
316         net->master_avionics = htonl(net->master_avionics);
317         htond(net->wind_speed_kt);
318         htond(net->wind_dir_deg);
319         htond(net->turbulence_norm);
320         htond(net->temp_c);
321         htond(net->press_inhg);
322         htond(net->hground);
323         htond(net->magvar);
324         net->speedup = htonl(net->speedup);
325         net->freeze = htonl(net->freeze);
326     }
327
328     if ( net->version != FG_NET_CTRLS_VERSION ) {
329         SG_LOG( SG_IO, SG_ALERT,
330                 "Version mismatch with raw controls packet format." );
331         SG_LOG( SG_IO, SG_ALERT,
332                 "FlightGear needs version = " << FG_NET_CTRLS_VERSION
333                 << " but is receiving version = "  << net->version );
334     }
335     node = fgGetNode("/controls/flight", true);
336     node->setDoubleValue( "aileron", net->aileron );
337     node->setDoubleValue( "elevator", net->elevator );
338     node->setDoubleValue( "elevator-trim", net->elevator_trim );
339     node->setDoubleValue( "rudder", net->rudder );
340     node->setDoubleValue( "flaps", net->flaps );
341     fgSetBool( "/systems/electrical/outputs/flaps", net->flaps_power );
342     node->setBoolValue( "flaps-serviceable", net->flap_motor_ok );
343
344     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
345         // Controls
346         node = fgGetNode("/controls/engines/engine", i);
347         node->getChild( "throttle" )->setDoubleValue( net->throttle[i] );
348         node->getChild( "mixture" )->setDoubleValue( net->mixture[i] );
349         node->getChild( "propeller-pitch" )
350             ->setDoubleValue( net->prop_advance[i] );
351         node->getChild( "magnetos" )->setDoubleValue( net->magnetos[i] );
352
353         // Faults
354         SGPropertyNode *faults = node->getNode( "faults", true );
355         faults->setBoolValue( "serviceable", net->engine_ok[i] );
356         faults->setBoolValue( "left-magneto-serviceable",
357                               net->mag_left_ok[i] );
358         faults->setBoolValue( "right-magneto-serviceable",
359                               net->mag_right_ok[i]);
360         faults->setBoolValue( "spark-plugs-serviceable",
361                               net->spark_plugs_ok[i] );
362         faults->setIntValue( "oil-pressure-status", net->oil_press_status[i] );
363         faults->setBoolValue( "fuel-pump-serviceable", net->fuel_pump_ok[i] );
364     }
365
366     fgSetBool( "/systems/electrical/outputs/fuel-pump",
367                net->fuel_pump_power[0] );
368     fgSetBool( "/systems/electrical/outputs/starter",
369                net->starter_power[0] );
370
371     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
372         node = fgGetNode( "/controls/fuel/tank", i );
373         node->getChild( "fuel_selector" )
374             ->setBoolValue( net->fuel_selector[i] );
375     }
376     node = fgGetNode( "/controls/gear" );
377     if ( node != NULL ) {
378         node->getChild( "brake-left" )->setDoubleValue( net->brake_left );
379         node->getChild( "brake-right" )->setDoubleValue( net->brake_right );
380         node->getChild( "brake-parking" )->setDoubleValue( net->brake_parking );
381     }
382
383     node = fgGetNode( "/controls/gear", true );
384     node->setBoolValue( "gear-down", net->gear_handle );
385
386     node = fgGetNode( "/controls/switches", true );
387     node->setBoolValue( "master-bat", net->master_bat );
388     node->setBoolValue( "master-alt", net->master_alt );
389     node->setBoolValue( "master-avionics", net->master_avionics );
390     
391     node = fgGetNode( "/environment", true );
392     node->setDoubleValue( "wind-speed-kt", net->wind_speed_kt );
393     node->setDoubleValue( "wind-from-heading-deg", net->wind_dir_deg );
394     node->setDoubleValue( "turbulence/magnitude-norm", net->turbulence_norm );
395     node->setBoolValue( "magnetic-variation-deg", net->magvar );
396
397     node->setDoubleValue( "/environment/temperature-degc",
398                           net->temp_c );
399     node->setDoubleValue( "/environment/pressure-sea-level-inhg",
400                           net->press_inhg );
401
402     // ground elevation ???
403
404     fgSetInt( "/sim/speed-up", net->speedup );
405
406     if ( honor_freezes ) {
407         node = fgGetNode( "/sim/freeze", true );
408         node->setBoolValue( "master", net->freeze & 0x01 );
409         node->setBoolValue( "position", net->freeze & 0x02 );
410         node->setBoolValue( "fuel", net->freeze & 0x04 );
411     }
412 }
413
414
415 // process work for this port
416 bool FGNativeCtrls::process() {
417     SGIOChannel *io = get_io_channel();
418     int length = sizeof(FGNetCtrls);
419
420     if ( get_direction() == SG_IO_OUT ) {
421         // cout << "size of cur_fdm_state = " << length << endl;
422
423         FGProps2NetCtrls( &net_ctrls, true, true );
424
425         if ( ! io->write( (char *)(& net_ctrls), length ) ) {
426             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
427             return false;
428         }
429     } else if ( get_direction() == SG_IO_IN ) {
430         if ( io->get_type() == sgFileType ) {
431             if ( io->read( (char *)(& net_ctrls), length ) == length ) {
432                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
433                 FGNetCtrls2Props( &net_ctrls, true, true );
434             }
435         } else {
436             while ( io->read( (char *)(& net_ctrls), length ) == length ) {
437                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
438                 FGNetCtrls2Props( &net_ctrls, true, true );
439             }
440         }
441     }
442
443     return true;
444 }
445
446
447 // close the channel
448 bool FGNativeCtrls::close() {
449     SGIOChannel *io = get_io_channel();
450
451     set_enabled( false );
452
453     if ( ! io->close() ) {
454         return false;
455     }
456
457     return true;
458 }