]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
550fb49bbee69234934a48aafaa7bb6c387b6a93
[flightgear.git] / src / Network / native_fdm.cxx
1 // native_fdm.cxx -- FGFS "Native" flight dynamics protocal class
2 //
3 // Written by Curtis Olson, started September 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/lowlevel.hxx> // endian tests
30 #include <simgear/io/iochannel.hxx>
31 #include <simgear/timing/sg_time.hxx>
32
33 #include <FDM/flight.hxx>
34 #include <Time/tmp.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
37 #include <Scenery/scenery.hxx>
38
39 #include "native_fdm.hxx"
40
41 // FreeBSD works better with this included last ... (?)
42 #if defined(WIN32) && !defined(__CYGWIN__)
43 #  include <windows.h>
44 #else
45 #  include <netinet/in.h>       // htonl() ntohl()
46 #endif
47
48
49 // The function htond is defined this way due to the way some
50 // processors and OSes treat floating point values.  Some will raise
51 // an exception whenever a "bad" floating point value is loaded into a
52 // floating point register.  Solaris is notorious for this, but then
53 // so is LynxOS on the PowerPC.  By translating the data in place,
54 // there is no need to load a FP register with the "corruped" floating
55 // point value.  By doing the BIG_ENDIAN test, I can optimize the
56 // routine for big-endian processors so it can be as efficient as
57 // possible
58 static void htond (double &x)   
59 {
60     if ( sgIsLittleEndian() ) {
61         int    *Double_Overlay;
62         int     Holding_Buffer;
63     
64         Double_Overlay = (int *) &x;
65         Holding_Buffer = Double_Overlay [0];
66     
67         Double_Overlay [0] = htonl (Double_Overlay [1]);
68         Double_Overlay [1] = htonl (Holding_Buffer);
69     } else {
70         return;
71     }
72 }
73
74 // Float version
75 static void htonf (float &x)    
76 {
77     if ( sgIsLittleEndian() ) {
78         int    *Float_Overlay;
79         int     Holding_Buffer;
80     
81         Float_Overlay = (int *) &x;
82         Holding_Buffer = Float_Overlay [0];
83     
84         Float_Overlay [0] = htonl (Holding_Buffer);
85     } else {
86         return;
87     }
88 }
89
90
91 FGNativeFDM::FGNativeFDM() {
92 }
93
94 FGNativeFDM::~FGNativeFDM() {
95 }
96
97
98 // open hailing frequencies
99 bool FGNativeFDM::open() {
100     if ( is_enabled() ) {
101         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
102                 << "is already in use, ignoring" );
103         return false;
104     }
105
106     SGIOChannel *io = get_io_channel();
107
108     if ( ! io->open( get_direction() ) ) {
109         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
110         return false;
111     }
112
113     set_enabled( true );
114
115     // Is this really needed here ????
116     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
117
118     return true;
119 }
120
121
122 void FGProps2NetFDM( FGNetFDM *net, bool net_byte_order ) {
123     int i;
124
125     // Version sanity checking
126     net->version = FG_NET_FDM_VERSION;
127
128     // Aero parameters
129     net->longitude = cur_fdm_state->get_Longitude();
130     net->latitude = cur_fdm_state->get_Latitude();
131     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
132     net->agl = cur_fdm_state->get_Altitude_AGL() * SG_FEET_TO_METER;
133     net->phi = cur_fdm_state->get_Phi();
134     net->theta = cur_fdm_state->get_Theta();
135     net->psi = cur_fdm_state->get_Psi();
136     net->alpha = cur_fdm_state->get_Alpha();
137     net->beta = cur_fdm_state->get_Beta();
138     net->phidot = cur_fdm_state->get_Phi_dot_degps() * SG_DEGREES_TO_RADIANS;
139     net->thetadot = cur_fdm_state->get_Theta_dot_degps()
140         * SG_DEGREES_TO_RADIANS;
141     net->psidot = cur_fdm_state->get_Psi_dot_degps() * SG_DEGREES_TO_RADIANS;
142
143     net->vcas = cur_fdm_state->get_V_calibrated_kts();
144     net->climb_rate = cur_fdm_state->get_Climb_Rate();
145
146     net->v_north = cur_fdm_state->get_V_north();
147     net->v_east = cur_fdm_state->get_V_east();
148     net->v_down = cur_fdm_state->get_V_down();
149     net->v_wind_body_north = cur_fdm_state->get_uBody();
150     net->v_wind_body_east = cur_fdm_state->get_vBody();
151     net->v_wind_body_down = cur_fdm_state->get_wBody();
152
153     net->A_X_pilot = cur_fdm_state->get_A_X_pilot();
154     net->A_Y_pilot = cur_fdm_state->get_A_Y_pilot();
155     net->A_Z_pilot = cur_fdm_state->get_A_Z_pilot();
156
157     net->stall_warning = fgGetDouble("/sim/alarms/stall-warning", 0.0);
158     net->slip_deg
159       = fgGetDouble("/instrumentation/slip-skid-ball/indicated-slip-skid");
160
161     // Engine parameters
162     net->num_engines = FGNetFDM::FG_MAX_ENGINES;
163     for ( i = 0; i < net->num_engines; ++i ) {
164         SGPropertyNode *node = fgGetNode("engines/engine", i, true);
165         if ( node->getBoolValue( "running" ) ) {
166             net->eng_state[0] = 2;
167         } else if ( node->getBoolValue( "cranking" ) ) {
168             net->eng_state[0] = 1;
169         } else {
170             net->eng_state[0] = 0;
171         }
172         net->rpm[i] = node->getDoubleValue( "rpm" );
173         net->fuel_flow[i] = node->getDoubleValue( "fuel-flow-gph" );
174         net->EGT[i] = node->getDoubleValue( "egt-degf" );
175         // cout << "egt = " << aero->EGT << endl;
176         net->oil_temp[i] = node->getDoubleValue( "oil-temperature-degf" );
177         net->oil_px[i] = node->getDoubleValue( "oil-pressure-psi" );
178     }
179
180     // Consumables
181     net->num_tanks = FGNetFDM::FG_MAX_TANKS;
182     for ( i = 0; i < net->num_tanks; ++i ) {
183         SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
184         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
185     }
186
187     // Gear and flaps
188     net->num_wheels = FGNetFDM::FG_MAX_WHEELS;
189     for (i = 0; i < net->num_wheels; ++i ) {
190         SGPropertyNode *node = fgGetNode("/gear/gear", i, true);
191         net->wow[i] = node->getDoubleValue("wow");
192         net->gear_pos[i] = node->getDoubleValue("position-norm");
193         net->gear_steer[i] = node->getDoubleValue("steering-norm");
194         net->gear_compression[i] = node->getDoubleValue("compression-norm");
195     }
196
197     // the following really aren't used in this context
198     net->cur_time = globals->get_time_params()->get_cur_time();
199     net->warp = globals->get_warp();
200     net->visibility = fgGetDouble("/environment/visibility-m");
201
202     // Control surface positions
203     SGPropertyNode *node = fgGetNode("/surface-positions", true);
204     net->elevator = node->getDoubleValue( "elevator-pos-norm" );
205     net->flaps = node->getDoubleValue( "flap-pos-norm" );
206     net->left_aileron = node->getDoubleValue( "left-aileron-pos-norm" );
207     net->right_aileron = node->getDoubleValue( "right-aileron-pos-norm" );
208     net->rudder = node->getDoubleValue( "rudder-pos-norm" );
209     net->rudder = node->getDoubleValue( "nose-wheel-pos-norm" );
210     net->speedbrake = node->getDoubleValue( "speedbrake-pos-norm" );
211     net->spoilers = node->getDoubleValue( "spoilers-pos-norm" );
212
213     if ( net_byte_order ) {
214         // Convert the net buffer to network format
215         net->version = htonl(net->version);
216
217         htond(net->longitude);
218         htond(net->latitude);
219         htond(net->altitude);
220         htonf(net->agl);
221         htonf(net->phi);
222         htonf(net->theta);
223         htonf(net->psi);
224         htonf(net->alpha);
225         htonf(net->beta);
226
227         htonf(net->phidot);
228         htonf(net->thetadot);
229         htonf(net->psidot);
230         htonf(net->vcas);
231         htonf(net->climb_rate);
232         htonf(net->v_north);
233         htonf(net->v_east);
234         htonf(net->v_down);
235         htonf(net->v_wind_body_north);
236         htonf(net->v_wind_body_east);
237         htonf(net->v_wind_body_down);
238
239         htonf(net->A_X_pilot);
240         htonf(net->A_Y_pilot);
241         htonf(net->A_Z_pilot);
242
243         htonf(net->stall_warning);
244         htonf(net->slip_deg);
245
246         for ( i = 0; i < net->num_engines; ++i ) {
247             net->eng_state[i] = htonl(net->eng_state[i]);
248             htonf(net->rpm[i]);
249             htonf(net->fuel_flow[i]);
250             htonf(net->EGT[i]);
251             htonf(net->oil_temp[i]);
252             htonf(net->oil_px[i]);
253         }
254         net->num_engines = htonl(net->num_engines);
255
256         for ( i = 0; i < net->num_tanks; ++i ) {
257             htonf(net->fuel_quantity[i]);
258         }
259         net->num_tanks = htonl(net->num_tanks);
260
261         for ( i = 0; i < net->num_wheels; ++i ) {
262             net->wow[i] = htonl(net->wow[i]);
263             htonf(net->gear_pos[i]);
264             htonf(net->gear_steer[i]);
265             htonf(net->gear_compression[i]);
266         }
267         net->num_wheels = htonl(net->num_wheels);
268
269         net->cur_time = htonl( net->cur_time );
270         net->warp = htonl( net->warp );
271         htonf(net->visibility);
272
273         htonf(net->elevator);
274         htonf(net->flaps);
275         htonf(net->left_aileron);
276         htonf(net->right_aileron);
277         htonf(net->rudder);
278         htonf(net->nose_wheel);
279         htonf(net->speedbrake);
280         htonf(net->spoilers);
281     }
282 }
283
284
285 void FGNetFDM2Props( FGNetFDM *net, bool net_byte_order ) {
286     int i;
287
288     if ( net_byte_order ) {
289         // Convert to the net buffer from network format
290         net->version = ntohl(net->version);
291
292         htond(net->longitude);
293         htond(net->latitude);
294         htond(net->altitude);
295         htonf(net->agl);
296         htonf(net->phi);
297         htonf(net->theta);
298         htonf(net->psi);
299         htonf(net->alpha);
300         htonf(net->beta);
301
302         htonf(net->phidot);
303         htonf(net->thetadot);
304         htonf(net->psidot);
305         htonf(net->vcas);
306         htonf(net->climb_rate);
307         htonf(net->v_north);
308         htonf(net->v_east);
309         htonf(net->v_down);
310         htonf(net->v_wind_body_north);
311         htonf(net->v_wind_body_east);
312         htonf(net->v_wind_body_down);
313
314         htonf(net->A_X_pilot);
315         htonf(net->A_Y_pilot);
316         htonf(net->A_Z_pilot);
317
318         htonf(net->stall_warning);
319         htonf(net->slip_deg);
320
321         net->num_engines = htonl(net->num_engines);
322         for ( i = 0; i < net->num_engines; ++i ) {
323             net->eng_state[i] = htonl(net->eng_state[i]);
324             htonf(net->rpm[i]);
325             htonf(net->fuel_flow[i]);
326             htonf(net->EGT[i]);
327             htonf(net->oil_temp[i]);
328             htonf(net->oil_px[i]);
329         }
330
331         net->num_tanks = htonl(net->num_tanks);
332         for ( i = 0; i < net->num_tanks; ++i ) {
333             htonf(net->fuel_quantity[i]);
334         }
335
336         net->num_wheels = htonl(net->num_wheels);
337         for ( i = 0; i < net->num_wheels; ++i ) {
338             net->wow[i] = htonl(net->wow[i]);
339             htonf(net->gear_pos[i]);
340             htonf(net->gear_steer[i]);
341             htonf(net->gear_compression[i]);
342         }
343
344         net->cur_time = ntohl(net->cur_time);
345         net->warp = ntohl(net->warp);
346         htonf(net->visibility);
347
348         htonf(net->elevator);
349         htonf(net->flaps);
350         htonf(net->left_aileron);
351         htonf(net->right_aileron);
352         htonf(net->rudder);
353         htonf(net->nose_wheel);
354         htonf(net->speedbrake);
355         htonf(net->spoilers);
356     }
357
358     if ( net->version == FG_NET_FDM_VERSION ) {
359         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
360         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
361         //      << endl;
362         cur_fdm_state->_updateGeodeticPosition( net->latitude,
363                                                 net->longitude,
364                                                 net->altitude
365                                                   * SG_METER_TO_FEET );
366         if ( net->agl > -9000 ) {
367             cur_fdm_state->_set_Altitude_AGL( net->agl * SG_METER_TO_FEET );
368         } else {
369             double agl_m
370               = net->altitude - globals->get_scenery()->get_cur_elev();
371             cur_fdm_state->_set_Altitude_AGL( agl_m * SG_METER_TO_FEET );
372         }
373         cur_fdm_state->_set_Euler_Angles( net->phi,
374                                           net->theta,
375                                           net->psi );
376         cur_fdm_state->_set_Alpha( net->alpha );
377         cur_fdm_state->_set_Beta( net->beta );
378         cur_fdm_state->_set_Euler_Rates( net->phidot,
379                                          net->thetadot,
380                                          net->psidot );
381         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
382         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
383         cur_fdm_state->_set_Velocities_Local( net->v_north,
384                                               net->v_east,
385                                               net->v_down );
386         cur_fdm_state->_set_Velocities_Wind_Body( net->v_wind_body_north,
387                                                   net->v_wind_body_east,
388                                                   net->v_wind_body_down );
389
390         cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
391                                                net->A_Y_pilot,
392                                                net->A_Z_pilot );
393
394         fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
395         fgSetDouble( "/instrumentation/slip-skid-ball/indicated-slip-skid",
396                      net->slip_deg );
397         fgSetBool( "/instrumentation/slip-skid-ball/override", true );
398
399         for ( i = 0; i < net->num_engines; ++i ) {
400             SGPropertyNode *node = fgGetNode( "engines/engine", i, true );
401             
402             // node->setBoolValue("running", t->isRunning());
403             // node->setBoolValue("cranking", t->isCranking());
404             
405             // cout << net->eng_state[i] << endl;
406             if ( net->eng_state[i] == 0 ) {
407                 node->setBoolValue( "cranking", false );
408                 node->setBoolValue( "running", false );
409             } else if ( net->eng_state[i] == 1 ) {
410                 node->setBoolValue( "cranking", true );
411                 node->setBoolValue( "running", false );
412             } else if ( net->eng_state[i] == 2 ) {
413                 node->setBoolValue( "cranking", false );
414                 node->setBoolValue( "running", true );
415             }
416
417             node->setDoubleValue( "rpm", net->rpm[i] );
418             node->setDoubleValue( "fuel-flow-gph", net->fuel_flow[i] );
419             node->setDoubleValue( "egt-degf", net->EGT[i] );
420             node->setDoubleValue( "oil-temperature-degf", net->oil_temp[i] );
421             node->setDoubleValue( "oil-pressure-psi", net->oil_px[i] );         
422         }
423
424         for (i = 0; i < net->num_tanks; ++i ) {
425             SGPropertyNode * node
426                 = fgGetNode("/consumables/fuel/tank", i, true);
427             node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
428         }
429
430         for (i = 0; i < net->num_wheels; ++i ) {
431             SGPropertyNode * node = fgGetNode("/gear/gear", i, true);
432             node->setDoubleValue("wow", net->wow[i] );
433             node->setDoubleValue("position-norm", net->gear_pos[i] );
434             node->setDoubleValue("steering-norm", net->gear_steer[i] );
435             node->setDoubleValue("compression-norm", net->gear_compression[i] );
436         }
437
438         /* these are ignored for now  ... */
439         /*
440         if ( net->cur_time ) {
441             fgSetLong("/sim/time/cur-time-override", net->cur_time);
442         }
443
444         globals->set_warp( net->warp );
445         last_warp = net->warp;
446         */
447
448         SGPropertyNode *node = fgGetNode("/surface-positions", true);
449         node->setDoubleValue("elevator-pos-norm", net->elevator);
450         node->setDoubleValue("flap-pos-norm", net->flaps);
451         node->setDoubleValue("left-aileron-pos-norm", net->left_aileron);
452         node->setDoubleValue("right-aileron-pos-norm", net->right_aileron);
453         node->setDoubleValue("rudder-pos-norm", net->rudder);
454         node->setDoubleValue("nose-wheel-pos-norm", net->nose_wheel);
455         node->setDoubleValue("speedbrake-pos-norm", net->speedbrake);
456         node->setDoubleValue("spoilers-pos-norm", net->spoilers);
457     } else {
458         SG_LOG( SG_IO, SG_ALERT,
459                 "Error: version mismatch in FGNetFDM2Props()" );
460         SG_LOG( SG_IO, SG_ALERT,
461                 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
462         SG_LOG( SG_IO, SG_ALERT,
463                 "\tNeeds to upgrade net_fdm.hxx and recompile." );
464     }
465 }
466
467
468 // process work for this port
469 bool FGNativeFDM::process() {
470     SGIOChannel *io = get_io_channel();
471     int length = sizeof(buf);
472
473     if ( get_direction() == SG_IO_OUT ) {
474         // cout << "size of cur_fdm_state = " << length << endl;
475         FGProps2NetFDM( &buf );
476         if ( ! io->write( (char *)(& buf), length ) ) {
477             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
478             return false;
479         }
480     } else if ( get_direction() == SG_IO_IN ) {
481         if ( io->get_type() == sgFileType ) {
482             if ( io->read( (char *)(& buf), length ) == length ) {
483                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
484                 FGNetFDM2Props( &buf );
485             }
486         } else {
487             int result;
488             result = io->read( (char *)(& buf), length );
489             while ( result == length ) {
490                 if ( result == length ) {
491                     SG_LOG( SG_IO, SG_DEBUG, "  Success reading data." );
492                     FGNetFDM2Props( &buf );
493                 }
494                 result = io->read( (char *)(& buf), length );
495             }
496         }
497     }
498
499     return true;
500 }
501
502
503 // close the channel
504 bool FGNativeFDM::close() {
505     SGIOChannel *io = get_io_channel();
506
507     set_enabled( false );
508
509     if ( ! io->close() ) {
510         return false;
511     }
512
513     return true;
514 }