]> git.mxchange.org Git - flightgear.git/blob - src/Network/AV400Sim.cxx
Improve timing statistics
[flightgear.git] / src / Network / AV400Sim.cxx
1 // AV400Sim.cxx -- Garmin 400 series protocal class.  This AV400Sim
2 // protocol generates the set of "simulator" commands a garmin 400
3 // series gps would expect as input in simulator mode.  The AV400
4 // protocol generates the set of commands that a garmin 400 series gps
5 // would emit.
6 //
7 // Written by Curtis Olson, started Janauary 2009.
8 //
9 // Copyright (C) 2009  Curtis L. Olson - http://www.flightgear.org/~curt
10 //
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of the
14 // License, or (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful, but
17 // WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 // General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24 //
25 // $Id$
26
27
28 #ifdef HAVE_CONFIG_H
29 #  include "config.h"
30 #endif
31
32 #include <cstring>
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/io/iochannel.hxx>
37 #include <simgear/timing/sg_time.hxx>
38
39 #include <FDM/flightProperties.hxx>
40 #include <Main/fg_props.hxx>
41 #include <Main/globals.hxx>
42
43 #include "AV400Sim.hxx"
44
45 FGAV400Sim::FGAV400Sim() {
46   fdm = new FlightProperties;
47 }
48
49 FGAV400Sim::~FGAV400Sim() {
50   delete fdm;
51 }
52
53
54 // generate AV400Sim message
55 bool FGAV400Sim::gen_message() {
56     // cout << "generating garmin message" << endl;
57
58     char msg_a[32], msg_b[32], msg_c[32], msg_d[32], msg_e[32];
59     char msg_f[32], msg_h[32], msg_i[32], msg_j[32], msg_k[32], msg_l[32], msg_r[32];
60     char msg_type2[256];
61
62     char dir;
63     int deg;
64     double min;
65
66     // create msg_a
67     double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
68     if ( latd < 0.0 ) {
69         latd = -latd;
70         dir = 'S';
71     } else {
72         dir = 'N';
73     }
74     deg = (int)latd;
75     min = (latd - (double)deg) * 60.0 * 100.0;
76     sprintf( msg_a, "a%c %03d %04.0f\r\n", dir, deg, min);
77
78     // create msg_b
79     double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
80     if ( lond < 0.0 ) {
81         lond = -lond;
82         dir = 'W';
83     } else {
84         dir = 'E';
85     }
86     deg = (int)lond;
87     min = (lond - (double)deg) * 60.0 * 100.0;
88     sprintf( msg_b, "b%c %03d %04.0f\r\n", dir, deg, min);
89
90     // create msg_c
91     double alt = fdm->get_Altitude();
92     if ( alt > 99999.0 ) { alt = 99999.0; }
93     sprintf( msg_c, "c%05.0f\r\n", alt );
94
95     // create msg_d
96     double ve_kts = fgGetDouble( "/velocities/speed-east-fps" ) * SG_FPS_TO_KT;
97     if ( ve_kts < 0.0 ) {
98         ve_kts = -ve_kts;
99         dir = 'W';
100     } else {
101         dir = 'E';
102     }
103     if ( ve_kts > 999.0 ) { ve_kts = 999.0; }
104     sprintf( msg_d, "d%c%03.0f\r\n", dir, ve_kts );
105
106     // create msg_e
107     double vn_kts = fgGetDouble( "/velocities/speed-north-fps" ) * SG_FPS_TO_KT;
108     if ( vn_kts < 0.0 ) {
109         vn_kts = -vn_kts;
110         dir = 'S';
111     } else {
112         dir = 'N';
113     }
114     if ( vn_kts > 999.0 ) { vn_kts = 999.0; }
115     sprintf( msg_e, "e%c%03.0f\r\n", dir, vn_kts );
116
117     // create msg_f
118     double climb_fpm = fgGetDouble( "/velocities/vertical-speed-fps" ) * 60;
119     if ( climb_fpm < 0.0 ) {
120         climb_fpm = -climb_fpm;
121         dir = 'D';
122     } else {
123         dir = 'U';
124     }
125     if ( climb_fpm > 9999.0 ) { climb_fpm = 9999.0; }
126     sprintf( msg_f, "f%c%04.0f\r\n", dir, climb_fpm );
127
128     // create msg_h
129     double obs = fgGetDouble( "/instrumentation/nav[0]/radials/selected-deg" );
130     sprintf( msg_h, "h%04d\r\n", (int)(obs*10) );
131
132     // create msg_i
133     double fuel = fgGetDouble( "/consumables/fuel/total-fuel-gals" );
134     if ( fuel > 999.9 ) { fuel = 999.9; }
135     sprintf( msg_i, "i%04.0f\r\n", fuel*10.0 );
136
137     // create msg_j
138     double gph = fgGetDouble( "/engines/engine[0]/fuel-flow-gph" );
139     gph += fgGetDouble( "/engines/engine[1]/fuel-flow-gph" );
140     gph += fgGetDouble( "/engines/engine[2]/fuel-flow-gph" );
141     gph += fgGetDouble( "/engines/engine[3]/fuel-flow-gph" );
142     if ( gph > 999.9 ) { gph = 999.9; }
143     sprintf( msg_j, "j%04.0f\r\n", gph*10.0 );
144
145     // create msg_k
146     sprintf( msg_k, "k%04d%02d%02d%02d%02d%02d\r\n",
147              fgGetInt( "/sim/time/utc/year"),
148              fgGetInt( "/sim/time/utc/month"),
149              fgGetInt( "/sim/time/utc/day"),
150              fgGetInt( "/sim/time/utc/hour"),
151              fgGetInt( "/sim/time/utc/minute"),
152              fgGetInt( "/sim/time/utc/second") );
153
154     // create msg_l
155     alt = fgGetDouble( "/instrumentation/pressure-alt-ft" );
156     if ( alt > 99999.0 ) { alt = 99999.0; }
157     sprintf( msg_l, "l%05.0f\r\n", alt );
158
159     // create msg_r
160     sprintf( msg_r, "rA\r\n" );
161
162     // sentence type 2
163     sprintf( msg_type2, "w01%c\r\n", (char)65 );
164
165     // assemble message
166     string sentence;
167     sentence += '\002';         // STX
168     sentence += msg_a;          // latitude
169     sentence += msg_b;          // longitude
170     sentence += msg_c;          // gps altitude
171     sentence += msg_d;          // ve kts
172     sentence += msg_e;          // vn kts
173     sentence += msg_f;          // climb fpm
174     sentence += msg_h;          // obs heading in deg (*10)
175     sentence += msg_i;          // total fuel in gal (*10)
176     sentence += msg_j;          // fuel flow gph (*10)
177     sentence += msg_k;          // date/time (UTC)
178     sentence += msg_l;          // pressure altitude
179     sentence += msg_r;          // RAIM available
180     sentence += msg_type2;      // type2 message
181     sentence += '\003';         // ETX
182
183     // cout << sentence;
184     length = sentence.length();
185     // cout << endl << "length = " << length << endl;
186     strncpy( buf, sentence.c_str(), length );
187
188     return true;
189 }
190
191
192 // parse AV400Sim message
193 bool FGAV400Sim::parse_message() {
194     SG_LOG( SG_IO, SG_INFO, "parse AV400Sim message" );
195
196     string msg = buf;
197     msg = msg.substr( 0, length );
198     SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
199
200     string ident = msg.substr(0, 1);
201     if ( ident == "i" ) {
202         string side = msg.substr(1,1);
203         string num = msg.substr(2,3);
204         if ( side == "-" ) {
205             fgSetDouble("/instrumentation/gps/cdi-deflection", 0.0);
206         } else {
207             int pos = atoi(num.c_str());
208             if ( side == "L" ) {
209                 pos *= -1;
210             }
211             fgSetDouble("/instrumentation/gps/cdi-deflection",
212                         (double)pos / 8.0);
213             fgSetBool("/instrumentation/gps/has-gs", false);
214         }
215     } else if ( ident == "k" ) {
216         string ind = msg.substr(1,1);
217         if ( ind == "T" ) {
218             fgSetBool("/instrumentation/gps/to-flag", true);
219             fgSetBool("/instrumentation/gps/from-flag", false);
220         } else if ( ind == "F" ) {
221             fgSetBool("/instrumentation/gps/to-flag", false);
222             fgSetBool("/instrumentation/gps/from-flag", true);
223         } else {
224             fgSetBool("/instrumentation/gps/to-flag", false);
225             fgSetBool("/instrumentation/gps/from-flag", false);
226         }
227     } else {
228         // SG_LOG( SG_IO, SG_ALERT, "unknown AV400Sim message = " << msg );
229     }
230
231     return true;
232 }
233
234
235 // open hailing frequencies
236 bool FGAV400Sim::open() {
237     if ( is_enabled() ) {
238         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
239                 << "is already in use, ignoring" );
240         return false;
241     }
242
243     SGIOChannel *io = get_io_channel();
244
245     if ( ! io->open( get_direction() ) ) {
246         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
247         return false;
248     }
249
250     set_enabled( true );
251
252     return true;
253 }
254
255
256 // process work for this port
257 bool FGAV400Sim::process() {
258     SGIOChannel *io = get_io_channel();
259
260     // until we have parsers/generators for the reverse direction,
261     // this is hardwired to expect that the physical GPS is slaving
262     // from FlightGear.
263
264     // Send FlightGear data to the external device
265     gen_message();
266     if ( ! io->write( buf, length ) ) {
267         SG_LOG( SG_IO, SG_WARN, "Error writing data." );
268         return false;
269     }
270
271     // read the device messages back
272     while ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
273         // SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
274         if ( parse_message() ) {
275             // SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
276         } else {
277             // SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
278         }
279     }
280
281     return true;
282 }
283
284
285 // close the channel
286 bool FGAV400Sim::close() {
287     SGIOChannel *io = get_io_channel();
288
289     set_enabled( false );
290
291     if ( ! io->close() ) {
292         return false;
293     }
294
295     return true;
296 }