]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_ctrls.cxx
YASim now supports the new fuel.nas fuel management system. It
[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 *tempnode;
107
108     // fill in values
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" );
116     net->flaps_power
117         = fgGetDouble( "/systems/electrical/outputs/flaps", 1.0 ) >= 1.0;
118     net->flap_motor_ok = node->getBoolValue( "flaps-serviceable" );
119
120     net->num_engines = FGNetCtrls::FG_MAX_ENGINES;
121     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
122         // Controls
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 );
128         if ( i == 0 ) {
129           // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
130         }
131         if ( i == 0 ) {
132           // cout << "Starter -> " << node->getIntValue( "starter", false )
133           //      << endl;
134         }
135         net->fuel_pump_power[i]
136             = node->getDoubleValue( "/systems/electrical/outputs/fuel-pump",
137                                     1.0 ) >= 1.0;
138
139         net->starter_power[i]
140             = node->getDoubleValue( "/systems/electrical/outputs/starter",
141                                     1.0 ) >= 1.0;
142
143         // Faults
144         SGPropertyNode *faults = node->getChild( "faults", 0, true );
145         net->engine_ok[i] = faults->getBoolValue( "serviceable", true );
146         net->mag_left_ok[i]
147           = faults->getBoolValue( "left-magneto-serviceable", true );
148         net->mag_right_ok[i]
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 );
154         net->fuel_pump_ok[i]
155           = faults->getBoolValue( "fuel-pump-serviceable", true );
156     }
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();
163         } else {
164             net->fuel_selector[i] = false;
165         }
166     }
167     node = fgGetNode("/controls/gear", true);
168     net->brake_left = node->getChild("brake-left")->getDoubleValue();
169     net->brake_right = node->getChild("brake-right")->getDoubleValue();
170     net->brake_parking = node->getChild("brake-parking")->getDoubleValue();
171
172     node = fgGetNode("/controls/switches", true);
173     tempnode = node->getChild("master-bat");
174     if ( tempnode != NULL ) {
175         net->master_bat = tempnode->getBoolValue();
176     }
177     tempnode = node->getChild("master-alt");
178     if ( tempnode != NULL ) {
179         net->master_alt = tempnode->getBoolValue();
180     }
181     tempnode = node->getChild("master-avionics");
182     if ( tempnode != NULL ) {
183         net->master_avionics = tempnode->getBoolValue();
184     }
185
186     net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
187     net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
188     net->turbulence_norm =
189         fgGetDouble("/environment/turbulence/magnitude-norm");
190
191     net->temp_c = fgGetDouble("/environment/temperature-degc");
192     net->press_inhg = fgGetDouble("/environment/pressure-sea-level-inhg");
193
194     // cur_fdm_state->get_ground_elev_ft() is what we want ... this
195     // reports the altitude of the aircraft.
196     // "/environment/ground-elevation-m" reports the ground elevation
197     // of the current view point which could change substantially if
198     // the user is switching views.
199     net->hground = cur_fdm_state->get_ground_elev_ft() * SG_FEET_TO_METER;
200     net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
201     net->speedup = fgGetInt("/sim/speed-up");
202     net->freeze = 0;
203     if ( honor_freezes ) {
204         if ( fgGetBool("/sim/freeze/master") ) {
205             net->freeze |= 0x01;
206         }
207         if ( fgGetBool("/sim/freeze/position") ) {
208             net->freeze |= 0x02;
209         }
210         if ( fgGetBool("/sim/freeze/fuel") ) {
211             net->freeze |= 0x04;
212         }
213     }
214
215     if ( net_byte_order ) {
216         // convert to network byte order
217         net->version = htonl(net->version);
218         htond(net->aileron);
219         htond(net->elevator);
220         htond(net->elevator_trim);
221         htond(net->rudder);
222         htond(net->flaps);
223         net->flaps_power = htonl(net->flaps_power);
224         net->flap_motor_ok = htonl(net->flap_motor_ok);
225         for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
226             net->magnetos[i] = htonl(net->magnetos[i]);
227             net->starter_power[i] = htonl(net->starter_power[i]);
228             htond(net->throttle[i]);
229             htond(net->mixture[i]);
230             net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
231             htond(net->prop_advance[i]);
232             net->engine_ok[i] = htonl(net->engine_ok[i]);
233             net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
234             net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
235             net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
236             net->oil_press_status[i] = htonl(net->oil_press_status[i]);
237             net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
238         }
239         net->num_engines = htonl(net->num_engines);
240         for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
241             net->fuel_selector[i] = htonl(net->fuel_selector[i]);
242         }
243         net->num_tanks = htonl(net->num_tanks);
244         htond(net->brake_left);
245         htond(net->brake_right);
246         htond(net->brake_parking);
247         net->gear_handle = htonl(net->gear_handle);
248         net->master_bat = htonl(net->master_bat);
249         net->master_alt = htonl(net->master_alt);
250         net->master_avionics = htonl(net->master_avionics);
251         htond(net->wind_speed_kt);
252         htond(net->wind_dir_deg);
253         htond(net->turbulence_norm);
254         htond(net->temp_c);
255         htond(net->press_inhg);
256         htond(net->hground);
257         htond(net->magvar);
258         net->speedup = htonl(net->speedup);
259         net->freeze = htonl(net->freeze);
260     }
261 }
262
263
264 // Update the property tree from the FGNetCtrls structure.
265 void FGNetCtrls2Props( FGNetCtrls *net, bool honor_freezes,
266                        bool net_byte_order )
267 {
268     int i;
269
270     SGPropertyNode * node;
271
272     if ( net_byte_order ) {
273         // convert from network byte order
274         net->version = htonl(net->version);
275         htond(net->aileron);
276         htond(net->elevator);
277         htond(net->elevator_trim);
278         htond(net->rudder);
279         htond(net->flaps);
280         net->flaps_power = htonl(net->flaps_power);
281         net->flap_motor_ok = htonl(net->flap_motor_ok);
282         net->num_engines = htonl(net->num_engines);
283         for ( i = 0; i < net->num_engines; ++i ) {
284             net->magnetos[i] = htonl(net->magnetos[i]);
285             net->starter_power[i] = htonl(net->starter_power[i]);
286             htond(net->throttle[i]);
287             htond(net->mixture[i]);
288             net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
289             htond(net->prop_advance[i]);
290             net->engine_ok[i] = htonl(net->engine_ok[i]);
291             net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
292             net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
293             net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
294             net->oil_press_status[i] = htonl(net->oil_press_status[i]);
295             net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
296         }
297         net->num_tanks = htonl(net->num_tanks);
298         for ( i = 0; i < net->num_tanks; ++i ) {
299             net->fuel_selector[i] = htonl(net->fuel_selector[i]);
300         }
301         htond(net->brake_left);
302         htond(net->brake_right);
303         htond(net->brake_parking);
304         net->gear_handle = htonl(net->gear_handle);
305         net->master_bat = htonl(net->master_bat);
306         net->master_alt = htonl(net->master_alt);
307         net->master_avionics = htonl(net->master_avionics);
308         htond(net->wind_speed_kt);
309         htond(net->wind_dir_deg);
310         htond(net->turbulence_norm);
311         htond(net->temp_c);
312         htond(net->press_inhg);
313         htond(net->hground);
314         htond(net->magvar);
315         net->speedup = htonl(net->speedup);
316         net->freeze = htonl(net->freeze);
317     }
318
319     if ( net->version != FG_NET_CTRLS_VERSION ) {
320         SG_LOG( SG_IO, SG_ALERT,
321                 "Version mismatch with raw controls packet format." );
322         SG_LOG( SG_IO, SG_ALERT,
323                 "FlightGear needs version = " << FG_NET_CTRLS_VERSION
324                 << " but is receiving version = "  << net->version );
325     }
326     node = fgGetNode("/controls/flight", true);
327     node->setDoubleValue( "aileron", net->aileron );
328     node->setDoubleValue( "elevator", net->elevator );
329     node->setDoubleValue( "elevator-trim", net->elevator_trim );
330     node->setDoubleValue( "rudder", net->rudder );
331     node->setDoubleValue( "flaps", net->flaps );
332     fgSetBool( "/systems/electrical/outputs/flaps", net->flaps_power );
333     node->setBoolValue( "flaps-serviceable", net->flap_motor_ok );
334
335     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
336         // Controls
337         node = fgGetNode("/controls/engines/engine", i);
338         node->getChild( "throttle" )->setDoubleValue( net->throttle[i] );
339         node->getChild( "mixture" )->setDoubleValue( net->mixture[i] );
340         node->getChild( "propeller-pitch" )
341             ->setDoubleValue( net->prop_advance[i] );
342         node->getChild( "magnetos" )->setDoubleValue( net->magnetos[i] );
343
344         // Faults
345         SGPropertyNode *faults = node->getNode( "faults", true );
346         faults->setBoolValue( "serviceable", net->engine_ok[i] );
347         faults->setBoolValue( "left-magneto-serviceable",
348                               net->mag_left_ok[i] );
349         faults->setBoolValue( "right-magneto-serviceable",
350                               net->mag_right_ok[i]);
351         faults->setBoolValue( "spark-plugs-serviceable",
352                               net->spark_plugs_ok[i] );
353         faults->setIntValue( "oil-pressure-status", net->oil_press_status[i] );
354         faults->setBoolValue( "fuel-pump-serviceable", net->fuel_pump_ok[i] );
355     }
356
357     fgSetBool( "/systems/electrical/outputs/fuel-pump",
358                net->fuel_pump_power[0] );
359     fgSetBool( "/systems/electrical/outputs/starter",
360                net->starter_power[0] );
361
362     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
363         node = fgGetNode( "/controls/fuel/tank", i );
364         node->getChild( "fuel_selector" )
365             ->setBoolValue( net->fuel_selector[i] );
366     }
367     node = fgGetNode( "/controls/gear" );
368     if ( node != NULL ) {
369         node->getChild( "brake-left" )->setDoubleValue( net->brake_left );
370         node->getChild( "brake-right" )->setDoubleValue( net->brake_right );
371         node->getChild( "brake-parking" )->setDoubleValue( net->brake_parking );
372     }
373
374     node = fgGetNode( "/controls/gear", true );
375     node->setBoolValue( "gear-down", net->gear_handle );
376
377     node = fgGetNode( "/controls/switches", true );
378     node->setBoolValue( "master-bat", net->master_bat );
379     node->setBoolValue( "master-alt", net->master_alt );
380     node->setBoolValue( "master-avionics", net->master_avionics );
381     
382     node = fgGetNode( "/environment", true );
383     node->setDoubleValue( "wind-speed-kt", net->wind_speed_kt );
384     node->setDoubleValue( "wind-from-heading-deg", net->wind_dir_deg );
385     node->setDoubleValue( "turbulence/magnitude-norm", net->turbulence_norm );
386     node->setBoolValue( "magnetic-variation-deg", net->magvar );
387
388     node->setDoubleValue( "/environment/temperature-degc",
389                           net->temp_c );
390     node->setDoubleValue( "/environment/pressure-sea-level-inhg",
391                           net->press_inhg );
392
393     // ground elevation ???
394
395     fgSetInt( "/sim/speed-up", net->speedup );
396
397     if ( honor_freezes ) {
398         node = fgGetNode( "/sim/freeze", true );
399         node->setBoolValue( "master", net->freeze & 0x01 );
400         node->setBoolValue( "position", net->freeze & 0x02 );
401         node->setBoolValue( "fuel", net->freeze & 0x04 );
402     }
403 }
404
405
406 // process work for this port
407 bool FGNativeCtrls::process() {
408     SGIOChannel *io = get_io_channel();
409     int length = sizeof(FGNetCtrls);
410
411     if ( get_direction() == SG_IO_OUT ) {
412         // cout << "size of cur_fdm_state = " << length << endl;
413
414         FGProps2NetCtrls( &net_ctrls, true, true );
415
416         if ( ! io->write( (char *)(& net_ctrls), length ) ) {
417             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
418             return false;
419         }
420     } else if ( get_direction() == SG_IO_IN ) {
421         if ( io->get_type() == sgFileType ) {
422             if ( io->read( (char *)(& net_ctrls), length ) == length ) {
423                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
424                 FGNetCtrls2Props( &net_ctrls, true, true );
425             }
426         } else {
427             while ( io->read( (char *)(& net_ctrls), length ) == length ) {
428                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
429                 FGNetCtrls2Props( &net_ctrls, true, true );
430             }
431         }
432     }
433
434     return true;
435 }
436
437
438 // close the channel
439 bool FGNativeCtrls::close() {
440     SGIOChannel *io = get_io_channel();
441
442     set_enabled( false );
443
444     if ( ! io->close() ) {
445         return false;
446     }
447
448     return true;
449 }