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