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 buf = new char[sizeof(char) + sizeof(int) + sizeof(ctrls)];
50 fifo_name_1 = name + "1";
51 fifo_name_2 = name + "2";
53 SG_LOG( SG_IO, SG_ALERT, "ExternalPipe Inited with " << name );
55 // Make the named pipe
58 result = mkfifo( fifo_name_1.c_str(), 0644 );
60 SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
64 result = mkfifo( fifo_name_2.c_str(), 0644 );
66 SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
71 pd1 = open( fifo_name_1.c_str(), O_RDWR );
73 SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_1 );
76 pd2 = open( fifo_name_2.c_str(), O_RDWR );
78 SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_2 );
85 FGExternalPipe::~FGExternalPipe() {
88 SG_LOG( SG_IO, SG_INFO, "Closing up the ExternalPipe." );
93 result = close( pd1 );
95 SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
98 result = close( pd2 );
100 SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
107 // Initialize the ExternalPipe flight model, dt is the time increment
108 // for each subsequent iteration through the EOM
109 void FGExternalPipe::init() {
110 // Explicitly call the superclass's
111 // init method first.
114 double lon = fgGetDouble( "/sim/presets/longitude-deg" );
115 double lat = fgGetDouble( "/sim/presets/latitude-deg" );
116 double alt = fgGetDouble( "/sim/presets/altitude-ft" );
117 double ground = fgGetDouble( "/environment/ground-elevation-m" );
118 double heading = fgGetDouble("/sim/presets/heading-deg");
119 double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
120 double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
121 double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" );
128 sprintf( cmd, "1longitude-deg=%.8f", lon );
129 result = write( pd1, cmd, strlen(cmd) );
130 if ( result == -1 ) {
131 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
134 sprintf( cmd, "1latitude-deg=%.8f", lat );
135 result = ::write( pd1, cmd, strlen(cmd) );
136 if ( result == -1 ) {
137 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
140 sprintf( cmd, "1altitude-ft=%.8f", alt );
141 result = 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, "1ground-m=%.8f", ground );
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, "1speed-kts=%.8f", speed );
153 result = 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, "1heading-deg=%.8f", heading );
159 result = 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 if ( weight > 1000.0 ) {
165 sprintf( cmd, "1aircraft-weight-lbs=%.2f", weight );
166 result = write( pd1, cmd, strlen(cmd) );
167 if ( result == -1 ) {
168 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
172 if ( cg_offset > -5.0 || cg_offset < 5.0 ) {
173 sprintf( cmd, "1aircraft-cg-offset-inches=%.2f", cg_offset );
174 result = write( pd1, cmd, strlen(cmd) );
175 if ( result == -1 ) {
176 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
180 SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
182 if( fgGetBool("/sim/presets/onground") ) {
183 sprintf( cmd, "1reset=ground" );
185 sprintf( cmd, "1reset=air" );
187 result = write( pd1, cmd, strlen(cmd) );
188 if ( result == -1 ) {
189 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
192 SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
197 // Run an iteration of the EOM.
198 void FGExternalPipe::update( double dt ) {
200 // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
205 if ( is_suspended() ) {
209 int iterations = _calc_multiloop(dt);
211 double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
212 static double last_weight = 0.0;
213 if ( fabs( weight - last_weight ) > 0.01 ) {
215 sprintf( cmd, "1aircraft-weight-lbs=%.2f", weight );
216 result = write( pd1, cmd, strlen(cmd) );
217 if ( result == -1 ) {
218 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
221 last_weight = weight;
223 double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" );
224 static double last_cg_offset = -9999.9;
225 if ( fabs( cg_offset - last_cg_offset ) > 0.01 ) {
227 sprintf( cmd, "1aircraft-cg-offset-inches=%.2f", cg_offset );
228 result = write( pd1, cmd, strlen(cmd) );
229 if ( result == -1 ) {
230 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
233 last_cg_offset = cg_offset;
235 // Send control positions to remote fdm
236 length = sizeof(ctrls);
237 FGProps2NetCtrls( &ctrls, true, false );
241 *((int *)ptr) = iterations;
243 memcpy( ptr, (char *)(&ctrls), length );
244 result = write( pd1, buf, length + 1 );
245 if ( result == -1 ) {
246 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: "
251 length = sizeof(fdm);
252 result = read( pd2, (char *)(& fdm), length );
253 if ( result == -1 ) {
254 SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
257 FGNetFDM2Props( &fdm, false );