]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
Substantial rewrite of FGNewMat, the material class. Most of the
[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() * SG_FEET_TO_METER;
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->_updateGeodeticPosition( raw->latitude,
83                                                 raw->longitude,
84                                                 raw->altitude
85                                                   * SG_METER_TO_FEET );
86         cur_fdm_state->_set_Euler_Angles( raw->phi,
87                                           raw->theta,
88                                           raw->psi );
89         cur_fdm_state->_set_V_calibrated_kts( raw->vcas );
90         cur_fdm_state->_set_Climb_Rate( raw->climb_rate );
91     } else {
92         SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in raw2global()" );
93         SG_LOG( SG_IO, SG_ALERT,
94                 "\tsomeone needs to upgrade raw_fdm.hxx and recompile." );
95     }
96 }
97
98
99 // process work for this port
100 bool FGNativeFDM::process() {
101     SGIOChannel *io = get_io_channel();
102     int length = sizeof(buf);
103
104     if ( get_direction() == SG_IO_OUT ) {
105         // cout << "size of cur_fdm_state = " << length << endl;
106         global2raw( cur_fdm_state, &buf );
107         if ( ! io->write( (char *)(& buf), length ) ) {
108             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
109             return false;
110         }
111     } else if ( get_direction() == SG_IO_IN ) {
112         if ( io->get_type() == sgFileType ) {
113             if ( io->read( (char *)(& buf), length ) == length ) {
114                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
115                 raw2global( &buf, cur_fdm_state );
116             }
117         } else {
118             while ( io->read( (char *)(& buf), length ) == length ) {
119                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
120                 raw2global( &buf, cur_fdm_state );
121             }
122         }
123     }
124
125     return true;
126 }
127
128
129 // close the channel
130 bool FGNativeFDM::close() {
131     SGIOChannel *io = get_io_channel();
132
133     set_enabled( false );
134
135     if ( ! io->close() ) {
136         return false;
137     }
138
139     return true;
140 }