]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ExternalPipe/ExternalPipe.cxx
Change the message passing structure just a bit in order to remove a possible
[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 <errno.h>            // perror()
31 #  include <fcntl.h>            // open()
32 #  include <unistd.h>           // unlink()
33 #endif
34
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/io/lowlevel.hxx> // endian tests
37
38 #include <Main/fg_props.hxx>
39 #include <Network/native_ctrls.hxx>
40 #include <Network/native_fdm.hxx>
41
42 #include "ExternalPipe.hxx"
43
44
45 static const int MAX_BUF = 32768;
46
47 FGExternalPipe::FGExternalPipe( double dt, string name ) {
48     valid = true;
49     last_weight = 0.0;
50     last_cg_offset = -9999.9;
51
52     buf = new char[MAX_BUF];
53
54 #ifdef HAVE_MKFIFO
55     fifo_name_1 = name + "1";
56     fifo_name_2 = name + "2";
57
58     SG_LOG( SG_IO, SG_ALERT, "ExternalPipe Inited with " << name );
59
60     // Make the named pipe
61     umask(0);
62     int result;
63     result = mkfifo( fifo_name_1.c_str(), 0644 );
64     if ( result == -1 ) {
65         SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
66                 << fifo_name_1 );
67         perror( "ExternalPipe()" );
68     }
69     result = mkfifo( fifo_name_2.c_str(), 0644 );
70     if ( result == -1 ) {
71         SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
72                 << fifo_name_2 );
73         perror( "ExternalPipe()" );
74     }
75
76     pd1 = open( fifo_name_1.c_str(), O_RDWR );
77     if ( pd1 == -1 ) {
78         SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_1 );
79         valid = false;
80     }
81     pd2 = open( fifo_name_2.c_str(), O_RDWR );
82     if ( pd2 == -1 ) {
83         SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_2 );
84         valid = false;
85     }
86 #endif
87 }
88
89
90 FGExternalPipe::~FGExternalPipe() {
91     delete [] buf;
92
93     SG_LOG( SG_IO, SG_INFO, "Closing up the ExternalPipe." );
94     
95 #ifdef HAVE_MKFIFO
96     // close
97     int result;
98     result = close( pd1 );
99     if ( result == -1 ) {
100         SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
101                 << fifo_name_1 );
102         perror( "~FGExternalPipe()" );
103     }
104     result = close( pd2 );
105     if ( result == -1 ) {
106         SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
107                 << fifo_name_2 );
108         perror( "~FGExternalPipe()" );
109     }
110 #endif
111 }
112
113
114 static int write_fifo( char cmd_type, int pd, char *cmd, int len ) {
115 #ifdef HAVE_MKFIFO
116     char *buf = new char[len + 3];
117
118     // write 2 byte command length + command type + command
119     char hi = (len + 1) / 256;
120     char lo = (len + 1) - (hi * 256);
121
122     buf[0] = hi;
123     buf[1] = lo;
124     buf[2] = cmd_type;
125
126     // strncpy( buf + 3, cmd, len );
127     memcpy( buf + 3, cmd, len );
128
129     if ( cmd_type == '1' ) {
130         // cout << "writing '" << cmd << "'" << endl;
131     } else if ( cmd_type == '2' ) {
132         // cout << "writing controls packet" << endl;
133     } else {
134         // cout << "writing unknown command?" << endl;
135     }
136
137     // for ( int i = 0; i < len + 3; ++i ) {
138     //     cout << " " << (int)buf[i];
139     // }
140     // cout << endl;
141
142     int result = ::write( pd, buf, len + 3 );
143     if ( result == -1 ) {
144         perror( "write_fifo()" );
145         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << pd );
146     }
147     // cout << "wrote " << len + 3 << " bytes." << endl;
148
149     delete [] buf;
150
151     return result;
152 #else
153     return 0;
154 #endif
155 }
156
157
158 // Initialize the ExternalPipe flight model, dt is the time increment
159 // for each subsequent iteration through the EOM
160 void FGExternalPipe::init() {
161     // Explicitly call the superclass's
162     // init method first.
163     common_init();
164
165     double lon = fgGetDouble( "/sim/presets/longitude-deg" );
166     double lat = fgGetDouble( "/sim/presets/latitude-deg" );
167     double alt = fgGetDouble( "/sim/presets/altitude-ft" );
168     double ground = fgGetDouble( "/environment/ground-elevation-m" );
169     double heading = fgGetDouble("/sim/presets/heading-deg");
170     double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
171     double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
172     double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" );
173
174 #ifdef HAVE_MKFIFO
175
176     char cmd[256];
177     int result;
178
179     sprintf( cmd, "longitude-deg=%.8f", lon );
180     result = write_fifo( '1', pd1, cmd, strlen(cmd) );
181
182     sprintf( cmd, "latitude-deg=%.8f", lat );
183     result = write_fifo( '1', pd1, cmd, strlen(cmd) );
184
185     sprintf( cmd, "altitude-ft=%.8f", alt );
186     result = write_fifo( '1', pd1, cmd, strlen(cmd) );
187
188     sprintf( cmd, "ground-m=%.8f", ground );
189     result = write_fifo( '1', pd1, cmd, strlen(cmd) );
190
191     sprintf( cmd, "speed-kts=%.8f", speed );
192     result = write_fifo( '1', pd1, cmd, strlen(cmd) );
193
194     sprintf( cmd, "heading-deg=%.8f", heading );
195     result = write_fifo( '1', pd1, cmd, strlen(cmd) );
196
197     if ( weight > 1000.0 ) {
198         sprintf( cmd, "aircraft-weight-lbs=%.2f", weight );
199         result = write_fifo( '1', pd1, cmd, strlen(cmd) );
200     }
201     last_weight = weight;
202
203     if ( cg_offset > -5.0 || cg_offset < 5.0 ) {
204         sprintf( cmd, "aircraft-cg-offset-inches=%.2f", cg_offset );
205         result = write_fifo( '1', pd1, cmd, strlen(cmd) );
206     }
207     last_cg_offset = cg_offset;
208
209     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
210
211     if( fgGetBool("/sim/presets/onground") ) {
212         sprintf( cmd, "reset=ground" );
213     } else {
214         sprintf( cmd, "reset=air" );
215     }
216     result = write_fifo( '1', pd1, cmd, strlen(cmd) );
217
218     SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
219 #endif
220 }
221
222
223 // Run an iteration of the EOM.
224 void FGExternalPipe::update( double dt ) {
225 #ifdef HAVE_MKFIFO
226     // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
227
228     int length;
229     int result;
230     
231     if ( is_suspended() ) {
232         return;
233     }
234
235     int iterations = _calc_multiloop(dt);
236
237     double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
238     static double last_weight = 0.0;
239     if ( fabs( weight - last_weight ) > 0.01 ) {
240         char cmd[256];
241         sprintf( cmd, "aircraft-weight-lbs=%.2f", weight );
242         result = write_fifo( '1', pd1, cmd, strlen(cmd) );
243     }
244     last_weight = weight;
245
246     double cg_offset = fgGetDouble( "/sim/aircraft-cg-offset-inches" );
247     if ( fabs( cg_offset - last_cg_offset ) > 0.01 ) {
248         char cmd[256];
249         sprintf( cmd, "aircraft-cg-offset-inches=%.2f", cg_offset );
250         result = write_fifo( '1', pd1, cmd, strlen(cmd) );
251     }
252     last_cg_offset = cg_offset;
253
254     // Send control positions to remote fdm
255     length = sizeof(ctrls);
256     FGProps2NetCtrls( &ctrls, true, false );
257     char *ptr = buf;
258     *((int *)ptr) = iterations;
259     // cout << "iterations = " << iterations << endl;
260     ptr += sizeof(int);
261     memcpy( ptr, (char *)(&ctrls), length );
262     // cout << "writing control structure, size = "
263     //      << length + sizeof(int) << endl;
264
265     result = write_fifo( '2', pd1, buf, length + sizeof(int) );
266
267     // Read fdm values
268     length = sizeof(fdm);
269     // cout << "about to read fdm data from remote fdm." << endl;
270     result = read( pd2, (char *)(& fdm), length );
271     if ( result == -1 ) {
272         SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
273                 << fifo_name_2 );
274     } else {
275         // cout << "  read successful." << endl;
276     }
277     FGNetFDM2Props( &fdm, false );
278 #endif
279 }