]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
efa46c8b8db787895bcf3af22120a981cfd1d705
[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     raw->longitude = cur_fdm_state->get_Longitude();
64     raw->latitude = cur_fdm_state->get_Latitude();
65     raw->altitude = cur_fdm_state->get_Altitude();
66     raw->phi = cur_fdm_state->get_Phi();
67     raw->theta = cur_fdm_state->get_Theta();
68     raw->psi = cur_fdm_state->get_Psi();
69 }
70
71
72 static void raw2global( const FGRawFDM *raw, FGInterface *global ) {
73     if ( raw->version == FG_RAW_FDM_VERSION ) {
74         // cout << "pos = " << raw->longitude << " " << raw->latitude << endl;
75         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius() << endl;
76         cur_fdm_state->_updatePosition( raw->latitude, raw->longitude,
77                                         raw->altitude * SG_METER_TO_FEET );
78         cur_fdm_state->_set_Euler_Angles( raw->phi,
79                                           raw->theta,
80                                           raw->psi );
81     } else {
82         SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in raw2global()" );
83         SG_LOG( SG_IO, SG_ALERT,
84                 "\tsomeone needs to upgrade raw_fdm.hxx and recompile." );
85     }
86 }
87
88
89 // process work for this port
90 bool FGNativeFDM::process() {
91     SGIOChannel *io = get_io_channel();
92     int length = sizeof(buf);
93
94     if ( get_direction() == SG_IO_OUT ) {
95         // cout << "size of cur_fdm_state = " << length << endl;
96         global2raw( cur_fdm_state, &buf );
97         if ( ! io->write( (char *)(& buf), length ) ) {
98             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
99             return false;
100         }
101     } else if ( get_direction() == SG_IO_IN ) {
102         if ( io->get_type() == sgFileType ) {
103             if ( io->read( (char *)(& buf), length ) == length ) {
104                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
105                 raw2global( &buf, cur_fdm_state );
106             }
107         } else {
108             while ( io->read( (char *)(& buf), length ) == length ) {
109                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
110                 raw2global( &buf, cur_fdm_state );
111             }
112         }
113     }
114
115     return true;
116 }
117
118
119 // close the channel
120 bool FGNativeFDM::close() {
121     SGIOChannel *io = get_io_channel();
122
123     set_enabled( false );
124
125     if ( ! io->close() ) {
126         return false;
127     }
128
129     return true;
130 }