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 <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 last_cg_offset = -9999.9;
49 buf = new char[sizeof(char) + sizeof(int) + sizeof(ctrls)];
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 );
75 SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_1 );
78 pd2 = open( fifo_name_2.c_str(), O_RDWR );
80 SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_2 );
87 FGExternalPipe::~FGExternalPipe() {
90 SG_LOG( SG_IO, SG_INFO, "Closing up the ExternalPipe." );
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: "
109 // Initialize the ExternalPipe flight model, dt is the time increment
110 // for each subsequent iteration through the EOM
111 void FGExternalPipe::init() {
112 // Explicitly call the superclass's
113 // init method first.
116 double lon = fgGetDouble( "/sim/presets/longitude-deg" );
117 double lat = fgGetDouble( "/sim/presets/latitude-deg" );
118 double alt = fgGetDouble( "/sim/presets/altitude-ft" );
119 double ground = fgGetDouble( "/environment/ground-elevation-m" );
120 double heading = fgGetDouble("/sim/presets/heading-deg");
121 double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
122 double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
123 double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" );
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 if ( weight > 1000.0 ) {
167 sprintf( cmd, "1aircraft-weight-lbs=%.2f", weight );
168 result = write( pd1, cmd, strlen(cmd) );
169 if ( result == -1 ) {
170 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
173 last_weight = weight;
175 if ( cg_offset > -5.0 || cg_offset < 5.0 ) {
176 sprintf( cmd, "1aircraft-cg-offset-inches=%.2f", cg_offset );
177 result = write( pd1, cmd, strlen(cmd) );
178 if ( result == -1 ) {
179 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
182 last_cg_offset = cg_offset;
184 SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
186 if( fgGetBool("/sim/presets/onground") ) {
187 sprintf( cmd, "1reset=ground" );
189 sprintf( cmd, "1reset=air" );
191 result = write( pd1, cmd, strlen(cmd) );
192 if ( result == -1 ) {
193 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
196 SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
201 // Run an iteration of the EOM.
202 void FGExternalPipe::update( double dt ) {
204 // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
209 if ( is_suspended() ) {
213 int iterations = _calc_multiloop(dt);
215 double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
216 static double last_weight = 0.0;
217 if ( fabs( weight - last_weight ) > 0.01 ) {
219 sprintf( cmd, "1aircraft-weight-lbs=%.2f", weight );
220 result = write( pd1, cmd, strlen(cmd) );
221 if ( result == -1 ) {
222 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
225 last_weight = weight;
227 double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" );
228 if ( fabs( cg_offset - last_cg_offset ) > 0.01 ) {
230 sprintf( cmd, "1aircraft-cg-offset-inches=%.2f", cg_offset );
231 result = write( pd1, cmd, strlen(cmd) );
232 if ( result == -1 ) {
233 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
236 last_cg_offset = cg_offset;
238 // Send control positions to remote fdm
239 length = sizeof(ctrls);
240 FGProps2NetCtrls( &ctrls, true, false );
244 *((int *)ptr) = iterations;
246 memcpy( ptr, (char *)(&ctrls), length );
247 // cout << "writing control structure to remote fdm." << endl;
248 result = write( pd1, buf, length + 1 );
249 if ( result == -1 ) {
250 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: "
253 // cout << " write successful = " << length + 1 << endl;
257 length = sizeof(fdm);
258 // cout << "about to read fdm data from remote fdm." << endl;
259 result = read( pd2, (char *)(& fdm), length );
260 if ( result == -1 ) {
261 SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
264 // cout << " read successful." << endl;
266 FGNetFDM2Props( &fdm, false );