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;
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();
73 raw->vcas = cur_fdm_state->get_V_calibrated_kts();
74 raw->climb_rate = cur_fdm_state->get_Climb_Rate();
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,
86 cur_fdm_state->_set_Euler_Angles( raw->phi,
89 cur_fdm_state->_set_V_calibrated_kts( raw->vcas );
90 cur_fdm_state->_set_Climb_Rate( raw->climb_rate );
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." );
99 // process work for this port
100 bool FGNativeFDM::process() {
101 SGIOChannel *io = get_io_channel();
102 int length = sizeof(buf);
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." );
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 );
118 while ( io->read( (char *)(& buf), length ) == length ) {
119 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
120 raw2global( &buf, cur_fdm_state );
130 bool FGNativeFDM::close() {
131 SGIOChannel *io = get_io_channel();
133 set_enabled( false );
135 if ( ! io->close() ) {