]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
Revert to pre-wind-sticking ground reaction forces until they can be debugged.
[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 <simgear/debug/logstream.hxx>
25 #include <simgear/io/iochannel.hxx>
26
27 #include <FDM/flight.hxx>
28
29 #include "native_fdm.hxx"
30
31
32 FGNativeFDM::FGNativeFDM() {
33 }
34
35 FGNativeFDM::~FGNativeFDM() {
36 }
37
38
39 // open hailing frequencies
40 bool FGNativeFDM::open() {
41     if ( is_enabled() ) {
42         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
43                 << "is already in use, ignoring" );
44         return false;
45     }
46
47     SGIOChannel *io = get_io_channel();
48
49     if ( ! io->open( get_direction() ) ) {
50         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
51         return false;
52     }
53
54     set_enabled( true );
55
56     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
57     return true;
58 }
59
60
61 static void global2raw( const FGInterface *global, FGRawFDM *raw ) {
62     raw->version = FG_RAW_FDM_VERSION;
63
64     // positions
65     raw->longitude = cur_fdm_state->get_Longitude();
66     raw->latitude = cur_fdm_state->get_Latitude();
67     raw->altitude = cur_fdm_state->get_Altitude();
68     raw->phi = cur_fdm_state->get_Phi();
69     raw->theta = cur_fdm_state->get_Theta();
70     raw->psi = cur_fdm_state->get_Psi();
71
72     // velocities
73     raw->vcas = cur_fdm_state->get_V_calibrated_kts();
74     raw->climb_rate = cur_fdm_state->get_Climb_Rate();
75 }
76
77
78 static void raw2global( const FGRawFDM *raw, FGInterface *global ) {
79     if ( raw->version == FG_RAW_FDM_VERSION ) {
80         // cout << "pos = " << raw->longitude << " " << raw->latitude << endl;
81         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius() << endl;
82         cur_fdm_state->_updatePosition( raw->latitude, raw->longitude,
83                                         raw->altitude * SG_METER_TO_FEET );
84         cur_fdm_state->_set_Euler_Angles( raw->phi,
85                                           raw->theta,
86                                           raw->psi );
87         cur_fdm_state->_set_V_calibrated_kts( raw->vcas );
88         cur_fdm_state->_set_Climb_Rate( raw->climb_rate );
89     } else {
90         SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in raw2global()" );
91         SG_LOG( SG_IO, SG_ALERT,
92                 "\tsomeone needs to upgrade raw_fdm.hxx and recompile." );
93     }
94 }
95
96
97 // process work for this port
98 bool FGNativeFDM::process() {
99     SGIOChannel *io = get_io_channel();
100     int length = sizeof(buf);
101
102     if ( get_direction() == SG_IO_OUT ) {
103         // cout << "size of cur_fdm_state = " << length << endl;
104         global2raw( cur_fdm_state, &buf );
105         if ( ! io->write( (char *)(& buf), length ) ) {
106             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
107             return false;
108         }
109     } else if ( get_direction() == SG_IO_IN ) {
110         if ( io->get_type() == sgFileType ) {
111             if ( io->read( (char *)(& buf), length ) == length ) {
112                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
113                 raw2global( &buf, cur_fdm_state );
114             }
115         } else {
116             while ( io->read( (char *)(& buf), length ) == length ) {
117                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
118                 raw2global( &buf, cur_fdm_state );
119             }
120         }
121     }
122
123     return true;
124 }
125
126
127 // close the channel
128 bool FGNativeFDM::close() {
129     SGIOChannel *io = get_io_channel();
130
131     set_enabled( false );
132
133     if ( ! io->close() ) {
134         return false;
135     }
136
137     return true;
138 }