]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ExternalPipe/ExternalPipe.cxx
Encapsulate the interpolstion version of FGEnvironment and fix some bugs
[flightgear.git] / src / FDM / ExternalPipe / ExternalPipe.cxx
1 // ExternalPipe.cxx -- a "pipe" interface to an external flight dynamics model
2 //
3 // Written by Curtis Olson, started March 2003.
4 //
5 // Copyright (C) 2003  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #ifdef HAVE_MKFIFO
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()
32 #endif
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/io/lowlevel.hxx> // endian tests
36
37 #include <Main/fg_props.hxx>
38 #include <Network/native_ctrls.hxx>
39 #include <Network/native_fdm.hxx>
40
41 #include "ExternalPipe.hxx"
42
43
44 FGExternalPipe::FGExternalPipe( double dt, string name ) {
45     valid = true;
46
47     buf = new char[sizeof(char) + sizeof(int) + sizeof(ctrls)];
48
49 #ifdef HAVE_MKFIFO
50     fifo_name_1 = name + "1";
51     fifo_name_2 = name + "2";
52
53     SG_LOG( SG_IO, SG_ALERT, "ExternalPipe Inited with " << name );
54
55     // Make the named pipe
56     umask(0);
57     int result;
58     result = mkfifo( fifo_name_1.c_str(), 0644 );
59     if ( result == -1 ) {
60         SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
61                 << fifo_name_1 );
62         valid = false;
63     }
64     result = mkfifo( fifo_name_2.c_str(), 0644 );
65     if ( result == -1 ) {
66         SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
67                 << fifo_name_2 );
68         valid = false;
69     }
70
71     pd1 = open( fifo_name_1.c_str(), O_RDWR );
72     if ( pd1 == -1 ) {
73         SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_1 );
74         valid = false;
75     }
76     pd2 = open( fifo_name_2.c_str(), O_RDWR );
77     if ( pd2 == -1 ) {
78         SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_2 );
79         valid = false;
80     }
81 #endif
82 }
83
84
85 FGExternalPipe::~FGExternalPipe() {
86     delete [] buf;
87
88     SG_LOG( SG_IO, SG_INFO, "Closing up the ExternalPipe." );
89     
90 #ifdef HAVE_MKFIFO
91     // close
92     int result;
93     result = close( pd1 );
94     if ( result == -1 ) {
95         SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
96                 << fifo_name_1 );
97     }
98     result = close( pd2 );
99     if ( result == -1 ) {
100         SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
101                 << fifo_name_2 );
102     }
103 #endif
104 }
105
106
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.
112     common_init();
113
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" );
122
123 #ifdef HAVE_MKFIFO
124
125     char cmd[256];
126     int result;
127
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 );
132     }
133
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 );
138     }
139
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 );
144     }
145
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 );
150     }
151
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 );
156     }
157
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 );
162     }
163
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 );
169       }
170     }
171
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 );
177       }
178     }
179
180     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
181
182     if( fgGetBool("/sim/presets/onground") ) {
183       sprintf( cmd, "1reset=ground" );
184     } else {
185       sprintf( cmd, "1reset=air" );
186     }
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 );
190     }
191
192     SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
193 #endif
194 }
195
196
197 // Run an iteration of the EOM.
198 void FGExternalPipe::update( double dt ) {
199 #ifdef HAVE_MKFIFO
200     // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
201
202     int length;
203     int result;
204     
205     if ( is_suspended() ) {
206         return;
207     }
208
209     int iterations = _calc_multiloop(dt);
210
211     double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
212     static double last_weight = 0.0;
213     if ( fabs( weight - last_weight ) > 0.01 ) {
214       char cmd[256];
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 );
219       }
220     }
221     last_weight = weight;
222
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 ) {
226       char cmd[256];
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 );
231       }
232     }
233     last_cg_offset = cg_offset;
234
235     // Send control positions to remote fdm
236     length = sizeof(ctrls);
237     FGProps2NetCtrls( &ctrls, true, false );
238     char *ptr = buf;
239     *ptr = '2';
240     ptr++;
241     *((int *)ptr) = iterations;
242     ptr += sizeof(int);
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: "
247                 << fifo_name_1 );
248     }
249
250     // Read fdm values
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: "
255                 << fifo_name_2 );
256     }
257     FGNetFDM2Props( &fdm, false );
258 #endif
259 }