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.
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/io/iochannel.hxx>
27 #include <FDM/flight.hxx>
29 #include "native_fdm.hxx"
32 FGNativeFDM::FGNativeFDM() {
35 FGNativeFDM::~FGNativeFDM() {
39 // open hailing frequencies
40 bool FGNativeFDM::open() {
42 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
43 << "is already in use, ignoring" );
47 SGIOChannel *io = get_io_channel();
49 if ( ! io->open( get_direction() ) ) {
50 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
56 cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
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();
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,
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." );
89 // process work for this port
90 bool FGNativeFDM::process() {
91 SGIOChannel *io = get_io_channel();
92 int length = sizeof(buf);
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." );
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 );
108 while ( io->read( (char *)(& buf), length ) == length ) {
109 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
110 raw2global( &buf, cur_fdm_state );
120 bool FGNativeFDM::close() {
121 SGIOChannel *io = get_io_channel();
123 set_enabled( false );
125 if ( ! io->close() ) {