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