1 // native_fdm.cxx -- FGFS "Native" flight dynamics protocal class
3 // Written by Curtis Olson, started September 2001.
5 // Copyright (C) 2001 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/io/lowlevel.hxx> // endian tests
30 #include <simgear/io/iochannel.hxx>
32 #include <FDM/flight.hxx>
33 #include <Time/tmp.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Main/globals.hxx>
37 #include "native_fdm.hxx"
39 // FreeBSD works better with this included last ... (?)
40 #if defined(WIN32) && !defined(__CYGWIN__)
43 # include <netinet/in.h> // htonl() ntohl()
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
56 static void htond (double &x)
58 if ( sgIsLittleEndian() ) {
62 Double_Overlay = (int *) &x;
63 Holding_Buffer = Double_Overlay [0];
65 Double_Overlay [0] = htonl (Double_Overlay [1]);
66 Double_Overlay [1] = htonl (Holding_Buffer);
73 FGNativeFDM::FGNativeFDM() {
76 FGNativeFDM::~FGNativeFDM() {
80 // open hailing frequencies
81 bool FGNativeFDM::open() {
83 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
84 << "is already in use, ignoring" );
88 SGIOChannel *io = get_io_channel();
90 if ( ! io->open( get_direction() ) ) {
91 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
97 cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
102 static void global2net( const FGInterface *global, FGNetFDM *net ) {
103 static const SGPropertyNode *visibility
104 = fgGetNode("/environment/visibility-m");
106 net->version = FG_NET_FDM_VERSION;
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();
117 net->vcas = cur_fdm_state->get_V_calibrated_kts();
118 net->climb_rate = cur_fdm_state->get_Climb_Rate();
121 net->cur_time = globals->get_time_params()->get_cur_time();
122 net->warp = globals->get_warp();
123 net->visibility = visibility->getDoubleValue();
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);
134 htond(net->climb_rate);
135 net->cur_time = htonl( net->cur_time );
136 net->warp = htonl( net->warp );
137 htond(net->visibility);
141 static void net2global( FGNetFDM *net, FGInterface *global ) {
142 static long int last_warp = 0;
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);
153 htond(net->climb_rate);
154 net->cur_time = ntohl(net->cur_time);
155 net->warp = ntohl(net->warp);
156 htond(net->visibility);
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,
164 * SG_METER_TO_FEET );
165 cur_fdm_state->_set_Euler_Angles( net->phi,
168 cur_fdm_state->_set_V_calibrated_kts( net->vcas );
169 cur_fdm_state->_set_Climb_Rate( net->climb_rate );
171 globals->set_warp( net->warp );
172 if ( net->warp != last_warp ) {
173 fgUpdateSkyAndLightingParams();
175 last_warp = net->warp;
177 fgSetDouble( "/environment/visibility-m", net->visibility );
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." );
188 // process work for this port
189 bool FGNativeFDM::process() {
190 SGIOChannel *io = get_io_channel();
191 int length = sizeof(buf);
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." );
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 );
207 while ( io->read( (char *)(& buf), length ) == length ) {
208 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
209 net2global( &buf, cur_fdm_state );
219 bool FGNativeFDM::close() {
220 SGIOChannel *io = get_io_channel();
222 set_enabled( false );
224 if ( ! io->close() ) {