1 // ExternalPipe.cxx -- 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 <errno.h> // perror()
31 # include <fcntl.h> // open()
32 # include <unistd.h> // unlink()
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/io/lowlevel.hxx> // endian tests
38 #include <Main/fg_props.hxx>
39 #include <Network/native_ctrls.hxx>
40 #include <Network/native_fdm.hxx>
42 #include "ExternalPipe.hxx"
45 static const int MAX_BUF = 32768;
47 FGExternalPipe::FGExternalPipe( double dt, string name ) {
50 last_cg_offset = -9999.9;
52 buf = new char[MAX_BUF];
55 fifo_name_1 = name + "1";
56 fifo_name_2 = name + "2";
58 SG_LOG( SG_IO, SG_ALERT, "ExternalPipe Inited with " << name );
60 // Make the named pipe
63 result = mkfifo( fifo_name_1.c_str(), 0644 );
65 SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
67 perror( "ExternalPipe()" );
69 result = mkfifo( fifo_name_2.c_str(), 0644 );
71 SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
73 perror( "ExternalPipe()" );
76 pd1 = open( fifo_name_1.c_str(), O_RDWR );
78 SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_1 );
81 pd2 = open( fifo_name_2.c_str(), O_RDWR );
83 SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_2 );
90 FGExternalPipe::~FGExternalPipe() {
93 SG_LOG( SG_IO, SG_INFO, "Closing up the ExternalPipe." );
98 result = close( pd1 );
100 SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
102 perror( "~FGExternalPipe()" );
104 result = close( pd2 );
105 if ( result == -1 ) {
106 SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
108 perror( "~FGExternalPipe()" );
114 static int write_fifo( char cmd_type, int pd, char *cmd, int len ) {
116 char *buf = new char[len + 3];
118 // write 2 byte command length + command type + command
119 char hi = (len + 1) / 256;
120 char lo = (len + 1) - (hi * 256);
126 // strncpy( buf + 3, cmd, len );
127 memcpy( buf + 3, cmd, len );
129 if ( cmd_type == '1' ) {
130 // cout << "writing '" << cmd << "'" << endl;
131 } else if ( cmd_type == '2' ) {
132 // cout << "writing controls packet" << endl;
134 // cout << "writing unknown command?" << endl;
137 // for ( int i = 0; i < len + 3; ++i ) {
138 // cout << " " << (int)buf[i];
142 int result = ::write( pd, buf, len + 3 );
143 if ( result == -1 ) {
144 perror( "write_fifo()" );
145 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << pd );
147 // cout << "wrote " << len + 3 << " bytes." << endl;
158 // Initialize the ExternalPipe flight model, dt is the time increment
159 // for each subsequent iteration through the EOM
160 void FGExternalPipe::init() {
161 // Explicitly call the superclass's
162 // init method first.
165 double lon = fgGetDouble( "/sim/presets/longitude-deg" );
166 double lat = fgGetDouble( "/sim/presets/latitude-deg" );
167 double alt = fgGetDouble( "/sim/presets/altitude-ft" );
168 double ground = fgGetDouble( "/environment/ground-elevation-m" );
169 double heading = fgGetDouble("/sim/presets/heading-deg");
170 double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
171 double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
172 double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" );
179 sprintf( cmd, "longitude-deg=%.8f", lon );
180 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
182 sprintf( cmd, "latitude-deg=%.8f", lat );
183 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
185 sprintf( cmd, "altitude-ft=%.8f", alt );
186 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
188 sprintf( cmd, "ground-m=%.8f", ground );
189 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
191 sprintf( cmd, "speed-kts=%.8f", speed );
192 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
194 sprintf( cmd, "heading-deg=%.8f", heading );
195 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
197 if ( weight > 1000.0 ) {
198 sprintf( cmd, "aircraft-weight-lbs=%.2f", weight );
199 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
201 last_weight = weight;
203 if ( cg_offset > -5.0 || cg_offset < 5.0 ) {
204 sprintf( cmd, "aircraft-cg-offset-inches=%.2f", cg_offset );
205 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
207 last_cg_offset = cg_offset;
209 SG_LOG( SG_IO, SG_ALERT, "before sending reset command." );
211 if( fgGetBool("/sim/presets/onground") ) {
212 sprintf( cmd, "reset=ground" );
214 sprintf( cmd, "reset=air" );
216 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
218 SG_LOG( SG_IO, SG_ALERT, "Remote FDM init() finished." );
223 // Run an iteration of the EOM.
224 void FGExternalPipe::update( double dt ) {
226 // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
231 if ( is_suspended() ) {
235 int iterations = _calc_multiloop(dt);
237 double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
238 static double last_weight = 0.0;
239 if ( fabs( weight - last_weight ) > 0.01 ) {
241 sprintf( cmd, "aircraft-weight-lbs=%.2f", weight );
242 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
244 last_weight = weight;
246 double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" );
247 if ( fabs( cg_offset - last_cg_offset ) > 0.01 ) {
249 sprintf( cmd, "aircraft-cg-offset-inches=%.2f", cg_offset );
250 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
252 last_cg_offset = cg_offset;
254 // Send control positions to remote fdm
255 length = sizeof(ctrls);
256 FGProps2NetCtrls( &ctrls, true, false );
258 *((int *)ptr) = iterations;
259 // cout << "iterations = " << iterations << endl;
261 memcpy( ptr, (char *)(&ctrls), length );
262 // cout << "writing control structure, size = "
263 // << length + sizeof(int) << endl;
265 result = write_fifo( '2', pd1, buf, length + sizeof(int) );
268 length = sizeof(fdm);
269 // cout << "about to read fdm data from remote fdm." << endl;
270 result = read( pd2, (char *)(& fdm), length );
271 if ( result == -1 ) {
272 SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
275 // cout << " read successful." << endl;
277 FGNetFDM2Props( &fdm, false );