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