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
7 // Written by Curtis Olson, started Janauary 2009.
9 // Copyright (C) 2009 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
38 #include <simgear/io/iochannel.hxx>
39 #include <simgear/timing/sg_time.hxx>
41 #include <FDM/flightProperties.hxx>
42 #include <Main/fg_props.hxx>
43 #include <Main/globals.hxx>
45 #include "AV400Sim.hxx"
47 FGAV400Sim::FGAV400Sim() {
48 fdm = new FlightProperties;
51 FGAV400Sim::~FGAV400Sim() {
56 // generate AV400Sim message
57 bool FGAV400Sim::gen_message() {
58 // cout << "generating garmin message" << endl;
60 char msg_a[32], msg_b[32], msg_c[32], msg_d[32], msg_e[32];
61 char msg_f[32], msg_h[32], msg_i[32], msg_j[32], msg_k[32], msg_l[32], msg_r[32];
69 double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
77 min = (latd - (double)deg) * 60.0 * 100.0;
78 sprintf( msg_a, "a%c %03d %04.0f\r\n", dir, deg, min);
81 double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
89 min = (lond - (double)deg) * 60.0 * 100.0;
90 sprintf( msg_b, "b%c %03d %04.0f\r\n", dir, deg, min);
93 double alt = fdm->get_Altitude();
94 if ( alt > 99999.0 ) { alt = 99999.0; }
95 sprintf( msg_c, "c%05.0f\r\n", alt );
98 double ve_kts = fgGetDouble( "/velocities/speed-east-fps" ) * SG_FPS_TO_KT;
105 if ( ve_kts > 999.0 ) { ve_kts = 999.0; }
106 sprintf( msg_d, "d%c%03.0f\r\n", dir, ve_kts );
109 double vn_kts = fgGetDouble( "/velocities/speed-north-fps" ) * SG_FPS_TO_KT;
110 if ( vn_kts < 0.0 ) {
116 if ( vn_kts > 999.0 ) { vn_kts = 999.0; }
117 sprintf( msg_e, "e%c%03.0f\r\n", dir, vn_kts );
120 double climb_fpm = fgGetDouble( "/velocities/vertical-speed-fps" ) * 60;
121 if ( climb_fpm < 0.0 ) {
122 climb_fpm = -climb_fpm;
127 if ( climb_fpm > 9999.0 ) { climb_fpm = 9999.0; }
128 sprintf( msg_f, "f%c%04.0f\r\n", dir, climb_fpm );
131 double obs = fgGetDouble( "/instrumentation/nav[0]/radials/selected-deg" );
132 sprintf( msg_h, "h%04d\r\n", (int)(obs*10) );
135 double fuel = fgGetDouble( "/consumables/fuel/total-fuel-gals" );
136 if ( fuel > 999.9 ) { fuel = 999.9; }
137 sprintf( msg_i, "i%04.0f\r\n", fuel*10.0 );
140 double gph = fgGetDouble( "/engines/engine[0]/fuel-flow-gph" );
141 gph += fgGetDouble( "/engines/engine[1]/fuel-flow-gph" );
142 gph += fgGetDouble( "/engines/engine[2]/fuel-flow-gph" );
143 gph += fgGetDouble( "/engines/engine[3]/fuel-flow-gph" );
144 if ( gph > 999.9 ) { gph = 999.9; }
145 sprintf( msg_j, "j%04.0f\r\n", gph*10.0 );
148 sprintf( msg_k, "k%04d%02d%02d%02d%02d%02d\r\n",
149 fgGetInt( "/sim/time/utc/year"),
150 fgGetInt( "/sim/time/utc/month"),
151 fgGetInt( "/sim/time/utc/day"),
152 fgGetInt( "/sim/time/utc/hour"),
153 fgGetInt( "/sim/time/utc/minute"),
154 fgGetInt( "/sim/time/utc/second") );
157 alt = fgGetDouble( "/instrumentation/pressure-alt-ft" );
158 if ( alt > 99999.0 ) { alt = 99999.0; }
159 sprintf( msg_l, "l%05.0f\r\n", alt );
162 sprintf( msg_r, "rA\r\n" );
165 sprintf( msg_type2, "w01%c\r\n", (char)65 );
169 sentence += '\002'; // STX
170 sentence += msg_a; // latitude
171 sentence += msg_b; // longitude
172 sentence += msg_c; // gps altitude
173 sentence += msg_d; // ve kts
174 sentence += msg_e; // vn kts
175 sentence += msg_f; // climb fpm
176 sentence += msg_h; // obs heading in deg (*10)
177 sentence += msg_i; // total fuel in gal (*10)
178 sentence += msg_j; // fuel flow gph (*10)
179 sentence += msg_k; // date/time (UTC)
180 sentence += msg_l; // pressure altitude
181 sentence += msg_r; // RAIM available
182 sentence += msg_type2; // type2 message
183 sentence += '\003'; // ETX
186 length = sentence.length();
187 // cout << endl << "length = " << length << endl;
188 strncpy( buf, sentence.c_str(), length );
194 // parse AV400Sim message
195 bool FGAV400Sim::parse_message() {
196 SG_LOG( SG_IO, SG_INFO, "parse AV400Sim message" );
199 msg = msg.substr( 0, length );
200 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
202 string ident = msg.substr(0, 1);
203 if ( ident == "i" ) {
204 string side = msg.substr(1,1);
205 string num = msg.substr(2,3);
207 fgSetDouble("/instrumentation/gps/cdi-deflection", 0.0);
209 int pos = atoi(num.c_str());
213 fgSetDouble("/instrumentation/gps/cdi-deflection",
215 fgSetBool("/instrumentation/gps/has-gs", false);
217 } else if ( ident == "k" ) {
218 string ind = msg.substr(1,1);
220 fgSetBool("/instrumentation/gps/to-flag", true);
221 fgSetBool("/instrumentation/gps/from-flag", false);
222 } else if ( ind == "F" ) {
223 fgSetBool("/instrumentation/gps/to-flag", false);
224 fgSetBool("/instrumentation/gps/from-flag", true);
226 fgSetBool("/instrumentation/gps/to-flag", false);
227 fgSetBool("/instrumentation/gps/from-flag", false);
230 // SG_LOG( SG_IO, SG_ALERT, "unknown AV400Sim message = " << msg );
237 // open hailing frequencies
238 bool FGAV400Sim::open() {
239 if ( is_enabled() ) {
240 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
241 << "is already in use, ignoring" );
245 SGIOChannel *io = get_io_channel();
247 if ( ! io->open( get_direction() ) ) {
248 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
258 // process work for this port
259 bool FGAV400Sim::process() {
260 SGIOChannel *io = get_io_channel();
262 // until we have parsers/generators for the reverse direction,
263 // this is hardwired to expect that the physical GPS is slaving
266 // Send FlightGear data to the external device
268 if ( ! io->write( buf, length ) ) {
269 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
273 // read the device messages back
274 while ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
275 // SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
276 if ( parse_message() ) {
277 // SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
279 // SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
288 bool FGAV400Sim::close() {
289 SGIOChannel *io = get_io_channel();
291 set_enabled( false );
293 if ( ! io->close() ) {