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" );
127 sprintf( cmd, "1longitude-deg=%.8f", lon );
128 result = write( pd1, cmd, strlen(cmd) );
129 if ( result == -1 ) {
130 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
133 sprintf( cmd, "1latitude-deg=%.8f", lat );
134 result = ::write( pd1, cmd, strlen(cmd) );
135 if ( result == -1 ) {
136 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
139 sprintf( cmd, "1altitude-ft=%.8f", alt );
140 result = write( pd1, cmd, strlen(cmd) );
141 if ( result == -1 ) {
142 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
145 sprintf( cmd, "1ground-m=%.8f", ground );
146 result = write( pd1, cmd, strlen(cmd) );
147 if ( result == -1 ) {
148 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
151 sprintf( cmd, "1speed-kts=%.8f", speed );
152 result = write( pd1, cmd, strlen(cmd) );
153 if ( result == -1 ) {
154 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
157 sprintf( cmd, "1heading-deg=%.8f", heading );
158 result = write( pd1, cmd, strlen(cmd) );
159 if ( result == -1 ) {
160 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
163 if ( weight > 1000.0 ) {
164 sprintf( cmd, "1aircraft-weight-lbs=%.2f", weight );
165 result = write( pd1, cmd, strlen(cmd) );
166 if ( result == -1 ) {
167 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
171 SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
173 if( fgGetBool("/sim/presets/onground") ) {
174 sprintf( cmd, "1reset=ground" );
176 sprintf( cmd, "1reset=air" );
178 result = write( pd1, cmd, strlen(cmd) );
179 if ( result == -1 ) {
180 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
183 SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
188 // Run an iteration of the EOM.
189 void FGExternalPipe::update( double dt ) {
191 // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
196 if ( is_suspended() ) {
200 int iterations = _calc_multiloop(dt);
202 double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
203 static double last_weight = 0.0;
204 if ( fabs( weight - last_weight ) > 0.01 ) {
206 sprintf( cmd, "1aircraft-weight-lbs=%.2f", weight );
207 result = write( pd1, cmd, strlen(cmd) );
208 if ( result == -1 ) {
209 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
212 last_weight = weight;
214 // Send control positions to remote fdm
215 length = sizeof(ctrls);
216 FGProps2NetCtrls( &ctrls, true, false );
220 *((int *)ptr) = iterations;
222 memcpy( ptr, (char *)(&ctrls), length );
223 result = write( pd1, buf, length + 1 );
224 if ( result == -1 ) {
225 SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: "
230 length = sizeof(fdm);
231 result = read( pd2, (char *)(& fdm), length );
232 if ( result == -1 ) {
233 SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
236 FGNetFDM2Props( &fdm, false );