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 - http://www.flightgear.org/~curt
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 unsigned char hi = (len + 1) / 256;
120 unsigned char lo = (len + 1) - (hi * 256);
122 // cout << "len = " << len << " hi = " << (int)hi << " lo = " << (int)lo << endl;
128 // strncpy( buf + 3, cmd, len );
129 memcpy( buf + 3, cmd, len );
131 if ( cmd_type == '1' ) {
132 // cout << "writing '" << cmd << "'" << endl;
133 } else if ( cmd_type == '2' ) {
134 // cout << "writing controls packet" << endl;
136 // cout << "writing unknown command?" << endl;
139 // for ( int i = 0; i < len + 3; ++i ) {
140 // cout << " " << (int)buf[i];
144 int result = ::write( pd, buf, len + 3 );
145 if ( result == -1 ) {
146 perror( "write_fifo()" );
147 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << pd );
149 // cout << "wrote " << len + 3 << " bytes." << endl;
160 // Initialize the ExternalPipe flight model, dt is the time increment
161 // for each subsequent iteration through the EOM
162 void FGExternalPipe::init() {
163 // Explicitly call the superclass's
164 // init method first.
167 double lon = fgGetDouble( "/sim/presets/longitude-deg" );
168 double lat = fgGetDouble( "/sim/presets/latitude-deg" );
169 double alt = fgGetDouble( "/sim/presets/altitude-ft" );
170 double ground = fgGetDouble( "/environment/ground-elevation-m" );
171 double heading = fgGetDouble("/sim/presets/heading-deg");
172 double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
173 double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
174 double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" );
181 sprintf( cmd, "longitude-deg=%.8f", lon );
182 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
184 sprintf( cmd, "latitude-deg=%.8f", lat );
185 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
187 sprintf( cmd, "altitude-ft=%.8f", alt );
188 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
190 sprintf( cmd, "ground-m=%.8f", ground );
191 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
193 sprintf( cmd, "speed-kts=%.8f", speed );
194 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
196 sprintf( cmd, "heading-deg=%.8f", heading );
197 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
199 if ( weight > 1000.0 ) {
200 sprintf( cmd, "aircraft-weight-lbs=%.2f", weight );
201 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
203 last_weight = weight;
205 if ( cg_offset > -5.0 || cg_offset < 5.0 ) {
206 sprintf( cmd, "aircraft-cg-offset-inches=%.2f", cg_offset );
207 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
209 last_cg_offset = cg_offset;
211 SG_LOG( SG_IO, SG_ALERT, "before sending reset command." );
213 if( fgGetBool("/sim/presets/onground") ) {
214 sprintf( cmd, "reset=ground" );
216 sprintf( cmd, "reset=air" );
218 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
220 SG_LOG( SG_IO, SG_ALERT, "Remote FDM init() finished." );
225 // Run an iteration of the EOM.
226 void FGExternalPipe::update( double dt ) {
228 // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
233 if ( is_suspended() ) {
237 int iterations = _calc_multiloop(dt);
239 double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
240 static double last_weight = 0.0;
241 if ( fabs( weight - last_weight ) > 0.01 ) {
243 sprintf( cmd, "aircraft-weight-lbs=%.2f", weight );
244 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
246 last_weight = weight;
248 double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" );
249 if ( fabs( cg_offset - last_cg_offset ) > 0.01 ) {
251 sprintf( cmd, "aircraft-cg-offset-inches=%.2f", cg_offset );
252 result = write_fifo( '1', pd1, cmd, strlen(cmd) );
254 last_cg_offset = cg_offset;
256 // Send control positions to remote fdm
257 length = sizeof(ctrls);
258 FGProps2NetCtrls( &ctrls, true, false );
260 *((int *)ptr) = iterations;
261 // cout << "iterations = " << iterations << endl;
263 memcpy( ptr, (char *)(&ctrls), length );
264 // cout << "writing control structure, size = "
265 // << length + sizeof(int) << endl;
267 result = write_fifo( '2', pd1, buf, length + sizeof(int) );
270 length = sizeof(fdm);
271 // cout << "about to read fdm data from remote fdm." << endl;
272 result = read( pd2, (char *)(& fdm), length );
273 if ( result == -1 ) {
274 SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
277 // cout << " read successful." << endl;
279 FGNetFDM2Props( &fdm, false );