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