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