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.
27 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
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];
49 cout << "dt = " << dt << endl;
51 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
52 fifo_name_1 = name + "1";
53 fifo_name_2 = name + "2";
55 SG_LOG( SG_IO, SG_ALERT, "ExternalPipe Inited with " << name );
57 // Make the named pipe
60 result = mkfifo( fifo_name_1.c_str(), 0644 );
62 SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
66 result = mkfifo( fifo_name_2.c_str(), 0644 );
68 SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
73 pd1 = open( fifo_name_1.c_str(), O_RDWR );
74 cout << "pd1 = " << pd1 << endl;
76 SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_1 );
79 pd2 = open( fifo_name_2.c_str(), O_RDWR );
80 cout << "pd2 = " << pd2 << endl;
82 SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_2 );
89 FGExternalPipe::~FGExternalPipe() {
92 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
95 result = close( pd1 );
97 SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
100 result = close( pd2 );
101 if ( result == -1 ) {
102 SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
106 // remove the file system entry
107 result = unlink( fifo_name_1.c_str() );
108 if ( result == -1 ) {
109 SG_LOG( SG_IO, SG_ALERT, "Unable to remove named pipe: "
112 result = unlink( fifo_name_2.c_str() );
113 if ( result == -1 ) {
114 SG_LOG( SG_IO, SG_ALERT, "Unable to remove named pipe: "
121 // Initialize the ExternalPipe flight model, dt is the time increment
122 // for each subsequent iteration through the EOM
123 void FGExternalPipe::init() {
124 // Explicitly call the superclass's
125 // init method first.
128 double lon = fgGetDouble( "/sim/presets/longitude-deg" );
129 double lat = fgGetDouble( "/sim/presets/latitude-deg" );
130 double alt = fgGetDouble( "/sim/presets/altitude-ft" );
131 double ground = fgGetDouble( "/environment/ground-elevation-m" );
132 double heading = fgGetDouble("/sim/presets/heading-deg");
133 double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
135 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
140 sprintf( cmd, "1longitude-deg=%.8f", lon );
141 result = std::write( pd1, cmd, strlen(cmd) );
142 if ( result == -1 ) {
143 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
146 sprintf( cmd, "1latitude-deg=%.8f", lat );
147 result = ::write( pd1, cmd, strlen(cmd) );
148 if ( result == -1 ) {
149 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
152 sprintf( cmd, "1altitude-ft=%.8f", alt );
153 result = std::write( pd1, cmd, strlen(cmd) );
154 if ( result == -1 ) {
155 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
158 sprintf( cmd, "1ground-m=%.8f", ground );
159 result = std::write( pd1, cmd, strlen(cmd) );
160 if ( result == -1 ) {
161 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
164 sprintf( cmd, "1speed-kts=%.8f", speed );
165 result = std::write( pd1, cmd, strlen(cmd) );
166 if ( result == -1 ) {
167 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
170 sprintf( cmd, "1heading-deg=%.8f", heading );
171 result = std::write( pd1, cmd, strlen(cmd) );
172 if ( result == -1 ) {
173 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
176 SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
178 if( fgGetBool("/sim/presets/onground") ) {
179 sprintf( cmd, "1reset=ground" );
181 sprintf( cmd, "1reset=air" );
183 result = std::write( pd1, cmd, strlen(cmd) );
184 if ( result == -1 ) {
185 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
188 SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
193 // Run an iteration of the EOM.
194 void FGExternalPipe::update( double dt ) {
195 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
199 if ( is_suspended() ) {
203 int iterations = _calc_multiloop(dt);
205 // Send control positions to remote fdm
206 length = sizeof(ctrls);
207 FGProps2NetCtrls( &ctrls, false );
211 *((int *)ptr) = iterations;
213 memcpy( ptr, (char *)(&ctrls), length );
214 result = std::write( pd1, buf, length + 1 );
215 if ( result == -1 ) {
216 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: "
219 // cout << "wrote to pipe" << endl;
221 // Read next set of FDM data (blocking enabled to maintain 'sync')
222 length = sizeof(fdm);
223 result = std::read( pd2, (char *)(& fdm), length );
224 if ( result == -1 ) {
225 SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
228 FGNetFDM2Props( &fdm, false );
229 // cout << "read from pipe" << endl;