2001-09-04 14:39:12 +00:00
|
|
|
// native_fdm.cxx -- FGFS "Native" flight dynamics protocal class
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started September 2001.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2001 Curtis L. Olson - curt@flightgear.org
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
2002-02-06 04:31:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-04 14:39:12 +00:00
|
|
|
#include <simgear/debug/logstream.hxx>
|
2002-02-04 22:54:02 +00:00
|
|
|
#include <simgear/io/lowlevel.hxx> // endian tests
|
2001-09-04 14:39:12 +00:00
|
|
|
#include <simgear/io/iochannel.hxx>
|
2002-03-16 00:18:38 +00:00
|
|
|
#include <simgear/timing/sg_time.hxx>
|
2001-09-04 14:39:12 +00:00
|
|
|
|
|
|
|
#include <FDM/flight.hxx>
|
2002-02-08 23:03:54 +00:00
|
|
|
#include <Time/tmp.hxx>
|
|
|
|
#include <Main/fg_props.hxx>
|
2002-02-04 22:54:02 +00:00
|
|
|
#include <Main/globals.hxx>
|
2001-09-04 14:39:12 +00:00
|
|
|
|
|
|
|
#include "native_fdm.hxx"
|
|
|
|
|
2002-02-07 15:38:32 +00:00
|
|
|
// FreeBSD works better with this included last ... (?)
|
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
# include <windows.h>
|
|
|
|
#else
|
|
|
|
# include <netinet/in.h> // htonl() ntohl()
|
|
|
|
#endif
|
2001-09-04 14:39:12 +00:00
|
|
|
|
2002-02-04 22:54:02 +00:00
|
|
|
|
|
|
|
// The function htond is defined this way due to the way some
|
|
|
|
// processors and OSes treat floating point values. Some will raise
|
|
|
|
// an exception whenever a "bad" floating point value is loaded into a
|
|
|
|
// floating point register. Solaris is notorious for this, but then
|
|
|
|
// so is LynxOS on the PowerPC. By translating the data in place,
|
|
|
|
// there is no need to load a FP register with the "corruped" floating
|
|
|
|
// point value. By doing the BIG_ENDIAN test, I can optimize the
|
|
|
|
// routine for big-endian processors so it can be as efficient as
|
|
|
|
// possible
|
|
|
|
static void htond (double &x)
|
|
|
|
{
|
|
|
|
if ( sgIsLittleEndian() ) {
|
|
|
|
int *Double_Overlay;
|
|
|
|
int Holding_Buffer;
|
|
|
|
|
|
|
|
Double_Overlay = (int *) &x;
|
|
|
|
Holding_Buffer = Double_Overlay [0];
|
|
|
|
|
|
|
|
Double_Overlay [0] = htonl (Double_Overlay [1]);
|
|
|
|
Double_Overlay [1] = htonl (Holding_Buffer);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-04 14:39:12 +00:00
|
|
|
FGNativeFDM::FGNativeFDM() {
|
|
|
|
}
|
|
|
|
|
|
|
|
FGNativeFDM::~FGNativeFDM() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// open hailing frequencies
|
|
|
|
bool FGNativeFDM::open() {
|
|
|
|
if ( is_enabled() ) {
|
|
|
|
SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
|
|
|
|
<< "is already in use, ignoring" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SGIOChannel *io = get_io_channel();
|
|
|
|
|
|
|
|
if ( ! io->open( get_direction() ) ) {
|
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_enabled( true );
|
|
|
|
|
|
|
|
cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-04 22:54:02 +00:00
|
|
|
static void global2net( const FGInterface *global, FGNetFDM *net ) {
|
2002-02-08 23:03:54 +00:00
|
|
|
static const SGPropertyNode *visibility
|
|
|
|
= fgGetNode("/environment/visibility-m");
|
|
|
|
|
2002-02-04 22:54:02 +00:00
|
|
|
net->version = FG_NET_FDM_VERSION;
|
2001-10-26 05:42:08 +00:00
|
|
|
|
|
|
|
// positions
|
2002-02-04 22:54:02 +00:00
|
|
|
net->longitude = cur_fdm_state->get_Longitude();
|
|
|
|
net->latitude = cur_fdm_state->get_Latitude();
|
|
|
|
net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
|
|
|
|
net->phi = cur_fdm_state->get_Phi();
|
|
|
|
net->theta = cur_fdm_state->get_Theta();
|
|
|
|
net->psi = cur_fdm_state->get_Psi();
|
2001-10-26 05:42:08 +00:00
|
|
|
|
|
|
|
// velocities
|
2002-02-04 22:54:02 +00:00
|
|
|
net->vcas = cur_fdm_state->get_V_calibrated_kts();
|
|
|
|
net->climb_rate = cur_fdm_state->get_Climb_Rate();
|
|
|
|
|
2002-02-08 23:03:54 +00:00
|
|
|
// environment
|
2002-02-04 22:54:02 +00:00
|
|
|
net->cur_time = globals->get_time_params()->get_cur_time();
|
|
|
|
net->warp = globals->get_warp();
|
2002-02-08 23:03:54 +00:00
|
|
|
net->visibility = visibility->getDoubleValue();
|
2002-02-04 22:54:02 +00:00
|
|
|
|
|
|
|
// Convert the net buffer to network format
|
|
|
|
net->version = htonl(net->version);
|
|
|
|
htond(net->longitude);
|
|
|
|
htond(net->latitude);
|
|
|
|
htond(net->altitude);
|
|
|
|
htond(net->phi);
|
|
|
|
htond(net->theta);
|
|
|
|
htond(net->psi);
|
|
|
|
htond(net->vcas);
|
|
|
|
htond(net->climb_rate);
|
|
|
|
net->cur_time = htonl( net->cur_time );
|
|
|
|
net->warp = htonl( net->warp );
|
2002-02-08 23:03:54 +00:00
|
|
|
htond(net->visibility);
|
2001-09-04 14:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-04 22:54:02 +00:00
|
|
|
static void net2global( FGNetFDM *net, FGInterface *global ) {
|
2002-02-08 23:03:54 +00:00
|
|
|
static long int last_warp = 0;
|
2002-02-04 22:54:02 +00:00
|
|
|
|
|
|
|
// Convert to the net buffer from network format
|
2002-02-08 23:03:54 +00:00
|
|
|
net->version = ntohl(net->version);
|
2002-02-04 22:54:02 +00:00
|
|
|
htond(net->longitude);
|
|
|
|
htond(net->latitude);
|
|
|
|
htond(net->altitude);
|
|
|
|
htond(net->phi);
|
|
|
|
htond(net->theta);
|
|
|
|
htond(net->psi);
|
|
|
|
htond(net->vcas);
|
|
|
|
htond(net->climb_rate);
|
2002-02-08 23:03:54 +00:00
|
|
|
net->cur_time = ntohl(net->cur_time);
|
|
|
|
net->warp = ntohl(net->warp);
|
|
|
|
htond(net->visibility);
|
2002-02-04 22:54:02 +00:00
|
|
|
|
|
|
|
if ( net->version == FG_NET_FDM_VERSION ) {
|
2002-02-08 23:03:54 +00:00
|
|
|
// cout << "pos = " << net->longitude << " " << net->latitude << endl;
|
|
|
|
// cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius() << endl;
|
|
|
|
cur_fdm_state->_updateGeodeticPosition( net->latitude,
|
|
|
|
net->longitude,
|
|
|
|
net->altitude
|
|
|
|
* SG_METER_TO_FEET );
|
|
|
|
cur_fdm_state->_set_Euler_Angles( net->phi,
|
|
|
|
net->theta,
|
|
|
|
net->psi );
|
|
|
|
cur_fdm_state->_set_V_calibrated_kts( net->vcas );
|
|
|
|
cur_fdm_state->_set_Climb_Rate( net->climb_rate );
|
2002-02-04 22:54:02 +00:00
|
|
|
|
2002-02-13 17:43:01 +00:00
|
|
|
if ( net->cur_time ) {
|
2002-02-10 04:18:10 +00:00
|
|
|
fgSetLong("/sim/time/cur-time-override", net->cur_time);
|
|
|
|
}
|
|
|
|
|
2002-02-04 22:54:02 +00:00
|
|
|
globals->set_warp( net->warp );
|
2002-02-08 23:03:54 +00:00
|
|
|
if ( net->warp != last_warp ) {
|
|
|
|
fgUpdateSkyAndLightingParams();
|
|
|
|
}
|
|
|
|
last_warp = net->warp;
|
|
|
|
|
|
|
|
fgSetDouble( "/environment/visibility-m", net->visibility );
|
2001-09-04 14:39:12 +00:00
|
|
|
} else {
|
2002-02-04 22:54:02 +00:00
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in net2global()" );
|
2002-02-08 23:03:54 +00:00
|
|
|
SG_LOG( SG_IO, SG_ALERT,
|
|
|
|
"\tread " << net->version << " need " << FG_NET_FDM_VERSION );
|
2001-09-04 14:39:12 +00:00
|
|
|
SG_LOG( SG_IO, SG_ALERT,
|
2002-02-04 22:54:02 +00:00
|
|
|
"\tsomeone needs to upgrade net_fdm.hxx and recompile." );
|
2001-09-04 14:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// process work for this port
|
|
|
|
bool FGNativeFDM::process() {
|
|
|
|
SGIOChannel *io = get_io_channel();
|
|
|
|
int length = sizeof(buf);
|
|
|
|
|
|
|
|
if ( get_direction() == SG_IO_OUT ) {
|
|
|
|
// cout << "size of cur_fdm_state = " << length << endl;
|
2002-02-04 22:54:02 +00:00
|
|
|
global2net( cur_fdm_state, &buf );
|
2001-09-04 14:39:12 +00:00
|
|
|
if ( ! io->write( (char *)(& buf), length ) ) {
|
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if ( get_direction() == SG_IO_IN ) {
|
|
|
|
if ( io->get_type() == sgFileType ) {
|
|
|
|
if ( io->read( (char *)(& buf), length ) == length ) {
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
|
2002-02-04 22:54:02 +00:00
|
|
|
net2global( &buf, cur_fdm_state );
|
2001-09-04 14:39:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while ( io->read( (char *)(& buf), length ) == length ) {
|
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
|
2002-02-04 22:54:02 +00:00
|
|
|
net2global( &buf, cur_fdm_state );
|
2001-09-04 14:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// close the channel
|
|
|
|
bool FGNativeFDM::close() {
|
|
|
|
SGIOChannel *io = get_io_channel();
|
|
|
|
|
|
|
|
set_enabled( false );
|
|
|
|
|
|
|
|
if ( ! io->close() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|