]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
90e54b44b2da75cf77be6deec77d39237e16b705
[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
32 #include <FDM/flight.hxx>
33 #include <Time/tmp.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Main/globals.hxx>
36
37 #include "native_fdm.hxx"
38
39 // FreeBSD works better with this included last ... (?)
40 #if defined(WIN32) && !defined(__CYGWIN__)
41 #  include <windows.h>
42 #else
43 #  include <netinet/in.h>       // htonl() ntohl()
44 #endif
45
46
47 // The function htond is defined this way due to the way some
48 // processors and OSes treat floating point values.  Some will raise
49 // an exception whenever a "bad" floating point value is loaded into a
50 // floating point register.  Solaris is notorious for this, but then
51 // so is LynxOS on the PowerPC.  By translating the data in place,
52 // there is no need to load a FP register with the "corruped" floating
53 // point value.  By doing the BIG_ENDIAN test, I can optimize the
54 // routine for big-endian processors so it can be as efficient as
55 // possible
56 static void htond (double &x)   
57 {
58     if ( sgIsLittleEndian() ) {
59         int    *Double_Overlay;
60         int     Holding_Buffer;
61     
62         Double_Overlay = (int *) &x;
63         Holding_Buffer = Double_Overlay [0];
64     
65         Double_Overlay [0] = htonl (Double_Overlay [1]);
66         Double_Overlay [1] = htonl (Holding_Buffer);
67     } else {
68         return;
69     }
70 }
71
72
73 FGNativeFDM::FGNativeFDM() {
74 }
75
76 FGNativeFDM::~FGNativeFDM() {
77 }
78
79
80 // open hailing frequencies
81 bool FGNativeFDM::open() {
82     if ( is_enabled() ) {
83         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
84                 << "is already in use, ignoring" );
85         return false;
86     }
87
88     SGIOChannel *io = get_io_channel();
89
90     if ( ! io->open( get_direction() ) ) {
91         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
92         return false;
93     }
94
95     set_enabled( true );
96
97     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
98     return true;
99 }
100
101
102 static void global2net( const FGInterface *global, FGNetFDM *net ) {
103     static const SGPropertyNode *visibility
104         = fgGetNode("/environment/visibility-m");
105
106     net->version = FG_NET_FDM_VERSION;
107
108     // positions
109     net->longitude = cur_fdm_state->get_Longitude();
110     net->latitude = cur_fdm_state->get_Latitude();
111     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
112     net->phi = cur_fdm_state->get_Phi();
113     net->theta = cur_fdm_state->get_Theta();
114     net->psi = cur_fdm_state->get_Psi();
115
116     // velocities
117     net->vcas = cur_fdm_state->get_V_calibrated_kts();
118     net->climb_rate = cur_fdm_state->get_Climb_Rate();
119
120     // environment
121     net->cur_time = globals->get_time_params()->get_cur_time();
122     net->warp = globals->get_warp();
123     net->visibility = visibility->getDoubleValue();
124
125     // Convert the net buffer to network format
126     net->version = htonl(net->version);
127     htond(net->longitude);
128     htond(net->latitude);
129     htond(net->altitude);
130     htond(net->phi);
131     htond(net->theta);
132     htond(net->psi);
133     htond(net->vcas);
134     htond(net->climb_rate);
135     net->cur_time = htonl( net->cur_time );
136     net->warp = htonl( net->warp );
137     htond(net->visibility);
138 }
139
140
141 static void net2global( FGNetFDM *net, FGInterface *global ) {
142     static long int last_warp = 0;
143
144     // Convert to the net buffer from network format
145     net->version = ntohl(net->version);
146     htond(net->longitude);
147     htond(net->latitude);
148     htond(net->altitude);
149     htond(net->phi);
150     htond(net->theta);
151     htond(net->psi);
152     htond(net->vcas);
153     htond(net->climb_rate);
154     net->cur_time = ntohl(net->cur_time);
155     net->warp = ntohl(net->warp);
156     htond(net->visibility);
157
158     if ( net->version == FG_NET_FDM_VERSION ) {
159         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
160         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius() << endl;
161         cur_fdm_state->_updateGeodeticPosition( net->latitude,
162                                                 net->longitude,
163                                                 net->altitude
164                                                   * SG_METER_TO_FEET );
165         cur_fdm_state->_set_Euler_Angles( net->phi,
166                                           net->theta,
167                                           net->psi );
168         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
169         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
170
171         globals->set_warp( net->warp );
172         if ( net->warp != last_warp ) {
173             fgUpdateSkyAndLightingParams();
174         }
175         last_warp = net->warp;
176
177         fgSetDouble( "/environment/visibility-m", net->visibility );
178     } else {
179         SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in net2global()" );
180         SG_LOG( SG_IO, SG_ALERT,
181                 "\tread " << net->version << " need " << FG_NET_FDM_VERSION );
182         SG_LOG( SG_IO, SG_ALERT,
183                 "\tsomeone needs to upgrade net_fdm.hxx and recompile." );
184     }
185 }
186
187
188 // process work for this port
189 bool FGNativeFDM::process() {
190     SGIOChannel *io = get_io_channel();
191     int length = sizeof(buf);
192
193     if ( get_direction() == SG_IO_OUT ) {
194         // cout << "size of cur_fdm_state = " << length << endl;
195         global2net( cur_fdm_state, &buf );
196         if ( ! io->write( (char *)(& buf), length ) ) {
197             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
198             return false;
199         }
200     } else if ( get_direction() == SG_IO_IN ) {
201         if ( io->get_type() == sgFileType ) {
202             if ( io->read( (char *)(& buf), length ) == length ) {
203                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
204                 net2global( &buf, cur_fdm_state );
205             }
206         } else {
207             while ( io->read( (char *)(& buf), length ) == length ) {
208                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
209                 net2global( &buf, cur_fdm_state );
210             }
211         }
212     }
213
214     return true;
215 }
216
217
218 // close the channel
219 bool FGNativeFDM::close() {
220     SGIOChannel *io = get_io_channel();
221
222     set_enabled( false );
223
224     if ( ! io->close() ) {
225         return false;
226     }
227
228     return true;
229 }