]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ExternalPipe/ExternalPipe.cxx
3ebb328128de8afa0e25709a87b159823c84ea91
[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
122 #ifdef HAVE_MKFIFO
123
124     char cmd[256];
125     int result;
126
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 );
131     }
132
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 );
137     }
138
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 );
143     }
144
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 );
149     }
150
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 );
155     }
156
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 );
161     }
162
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 );
168       }
169     }
170
171     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
172
173     if( fgGetBool("/sim/presets/onground") ) {
174       sprintf( cmd, "1reset=ground" );
175     } else {
176       sprintf( cmd, "1reset=air" );
177     }
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 );
181     }
182
183     SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
184 #endif
185 }
186
187
188 // Run an iteration of the EOM.
189 void FGExternalPipe::update( double dt ) {
190 #ifdef HAVE_MKFIFO
191     // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
192
193     int length;
194     int result;
195     
196     if ( is_suspended() ) {
197         return;
198     }
199
200     int iterations = _calc_multiloop(dt);
201
202     double weight = fgGetDouble( "/sim/aircraft-weight-lbs" );
203     static double last_weight = 0.0;
204     if ( fabs( weight - last_weight ) > 0.01 ) {
205       char cmd[256];
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 );
210       }
211     }
212     last_weight = weight;
213
214     // Send control positions to remote fdm
215     length = sizeof(ctrls);
216     FGProps2NetCtrls( &ctrls, true, false );
217     char *ptr = buf;
218     *ptr = '2';
219     ptr++;
220     *((int *)ptr) = iterations;
221     ptr += sizeof(int);
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: "
226                 << fifo_name_1 );
227     }
228
229     // Read fdm values
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: "
234                 << fifo_name_2 );
235     }
236     FGNetFDM2Props( &fdm, false );
237 #endif
238 }