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 SG_LOG( SG_IO, SG_INFO, "Closing up the ExternalPipe." );
94 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
97 result = close( pd1 );
99 SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
102 result = close( pd2 );
103 if ( result == -1 ) {
104 SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
111 // Initialize the ExternalPipe flight model, dt is the time increment
112 // for each subsequent iteration through the EOM
113 void FGExternalPipe::init() {
114 // Explicitly call the superclass's
115 // init method first.
118 double lon = fgGetDouble( "/sim/presets/longitude-deg" );
119 double lat = fgGetDouble( "/sim/presets/latitude-deg" );
120 double alt = fgGetDouble( "/sim/presets/altitude-ft" );
121 double ground = fgGetDouble( "/environment/ground-elevation-m" );
122 double heading = fgGetDouble("/sim/presets/heading-deg");
123 double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
125 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
130 sprintf( cmd, "1longitude-deg=%.8f", lon );
131 result = write( pd1, cmd, strlen(cmd) );
132 if ( result == -1 ) {
133 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
136 sprintf( cmd, "1latitude-deg=%.8f", lat );
137 result = ::write( pd1, cmd, strlen(cmd) );
138 if ( result == -1 ) {
139 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
142 sprintf( cmd, "1altitude-ft=%.8f", alt );
143 result = write( pd1, cmd, strlen(cmd) );
144 if ( result == -1 ) {
145 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
148 sprintf( cmd, "1ground-m=%.8f", ground );
149 result = write( pd1, cmd, strlen(cmd) );
150 if ( result == -1 ) {
151 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
154 sprintf( cmd, "1speed-kts=%.8f", speed );
155 result = write( pd1, cmd, strlen(cmd) );
156 if ( result == -1 ) {
157 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
160 sprintf( cmd, "1heading-deg=%.8f", heading );
161 result = write( pd1, cmd, strlen(cmd) );
162 if ( result == -1 ) {
163 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
166 SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
168 if( fgGetBool("/sim/presets/onground") ) {
169 sprintf( cmd, "1reset=ground" );
171 sprintf( cmd, "1reset=air" );
173 result = write( pd1, cmd, strlen(cmd) );
174 if ( result == -1 ) {
175 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
178 SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
183 // Run an iteration of the EOM.
184 void FGExternalPipe::update( double dt ) {
185 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
186 // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
191 if ( is_suspended() ) {
195 int iterations = _calc_multiloop(dt);
197 // Send control positions to remote fdm
198 length = sizeof(ctrls);
199 FGProps2NetCtrls( &ctrls, false );
203 *((int *)ptr) = iterations;
205 memcpy( ptr, (char *)(&ctrls), length );
206 result = write( pd1, buf, length + 1 );
207 if ( result == -1 ) {
208 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: "
211 // cout << " wrote to pipe" << endl;
213 length = sizeof(fdm);
214 result = read( pd2, (char *)(& fdm), length );
215 if ( result == -1 ) {
216 SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
219 FGNetFDM2Props( &fdm, false );
220 // cout << "read from pipe" << endl;
222 // SG_LOG( SG_IO, SG_INFO, " End FGExternalPipe::udpate()" );