1 // ExternalPipe.hxx -- a "pipe" interface to an external flight dynamics model
3 // Written by Curtis Olson, started March 2003.
5 // Copyright (C) 2003 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.
28 # include <sys/types.h> // mkfifo() open() umask()
29 # include <sys/stat.h> // mkfifo() open() umask()
30 # include <fcntl.h> // open()
31 # include <unistd.h> // unlink()
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/io/lowlevel.hxx> // endian tests
37 #include <Main/fg_props.hxx>
38 #include <Network/native_ctrls.hxx>
39 #include <Network/native_fdm.hxx>
41 #include "ExternalPipe.hxx"
44 FGExternalPipe::FGExternalPipe( double dt, string name ) {
47 buf = new char[sizeof(ctrls) + 1];
50 fifo_name_1 = name + "1";
51 fifo_name_2 = name + "2";
53 SG_LOG( SG_IO, SG_ALERT, "ExternalPipe Inited with " << name );
55 // Make the named pipe
58 result = mkfifo( fifo_name_1.c_str(), 0644 );
60 SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
64 result = mkfifo( fifo_name_2.c_str(), 0644 );
66 SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
71 pd1 = open( fifo_name_1.c_str(), O_RDWR );
73 SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_1 );
76 pd2 = open( fifo_name_2.c_str(), O_RDWR );
78 SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_2 );
85 FGExternalPipe::~FGExternalPipe() {
88 SG_LOG( SG_IO, SG_INFO, "Closing up the ExternalPipe." );
93 result = close( pd1 );
95 SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
98 result = close( pd2 );
100 SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
107 // Initialize the ExternalPipe flight model, dt is the time increment
108 // for each subsequent iteration through the EOM
109 void FGExternalPipe::init() {
110 // Explicitly call the superclass's
111 // init method first.
114 double lon = fgGetDouble( "/sim/presets/longitude-deg" );
115 double lat = fgGetDouble( "/sim/presets/latitude-deg" );
116 double alt = fgGetDouble( "/sim/presets/altitude-ft" );
117 double ground = fgGetDouble( "/environment/ground-elevation-m" );
118 double heading = fgGetDouble("/sim/presets/heading-deg");
119 double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
126 sprintf( cmd, "1longitude-deg=%.8f", lon );
127 result = write( pd1, cmd, strlen(cmd) );
128 if ( result == -1 ) {
129 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
132 sprintf( cmd, "1latitude-deg=%.8f", lat );
133 result = ::write( pd1, cmd, strlen(cmd) );
134 if ( result == -1 ) {
135 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
138 sprintf( cmd, "1altitude-ft=%.8f", alt );
139 result = write( pd1, cmd, strlen(cmd) );
140 if ( result == -1 ) {
141 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
144 sprintf( cmd, "1ground-m=%.8f", ground );
145 result = write( pd1, cmd, strlen(cmd) );
146 if ( result == -1 ) {
147 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
150 sprintf( cmd, "1speed-kts=%.8f", speed );
151 result = write( pd1, cmd, strlen(cmd) );
152 if ( result == -1 ) {
153 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
156 sprintf( cmd, "1heading-deg=%.8f", heading );
157 result = write( pd1, cmd, strlen(cmd) );
158 if ( result == -1 ) {
159 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
162 SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
164 if( fgGetBool("/sim/presets/onground") ) {
165 sprintf( cmd, "1reset=ground" );
167 sprintf( cmd, "1reset=air" );
169 result = write( pd1, cmd, strlen(cmd) );
170 if ( result == -1 ) {
171 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
174 SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
179 // Run an iteration of the EOM.
180 void FGExternalPipe::update( double dt ) {
182 // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
187 if ( is_suspended() ) {
191 int iterations = _calc_multiloop(dt);
193 // Send control positions to remote fdm
194 length = sizeof(ctrls);
195 FGProps2NetCtrls( &ctrls, false );
199 *((int *)ptr) = iterations;
201 memcpy( ptr, (char *)(&ctrls), length );
202 result = write( pd1, buf, length + 1 );
203 if ( result == -1 ) {
204 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: "
208 length = sizeof(fdm);
209 result = read( pd2, (char *)(& fdm), length );
210 if ( result == -1 ) {
211 SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
214 FGNetFDM2Props( &fdm, false );