1 // AV400.cxx -- Garmin 400 series protocal class
3 // Written by Curtis Olson, started August 2006.
5 // Copyright (C) 2006 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
33 #include <simgear/io/iochannel.hxx>
34 #include <simgear/timing/sg_time.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.hxx>
49 // calculate the garmin check sum
50 static char calc_nmea_cksum(char *sentence) {
51 unsigned char sum = 0;
54 // cout << sentence << endl;
56 len = strlen(sentence);
58 for ( i = 1; i < len; i++ ) {
59 // cout << sentence[i];
64 // printf("sum = %02x\n", sum);
70 // generate AV400 message
71 bool FGAV400::gen_message() {
72 // cout << "generating garmin message" << endl;
74 char msg_z[32], msg_A[32], msg_B[32], msg_C[32], msg_D[32];
75 char msg_Q[32], msg_T[32], msg_type2[256];
76 // the following could be implemented, but currently are unused
77 // char msg_E[32], msg_G[32], msg_I[32], msg_K[32], msg_L[32], msg_S[32];
85 sprintf( msg_z, "z%05.0f\r\n", fdm.get_Altitude() );
90 double latd = fdm.get_Latitude() * SGD_RADIANS_TO_DEGREES;
98 min = (latd - (double)deg) * 60.0 * 100.0;
99 sprintf( msg_A, "A%c %02d %04.0f\r\n", dir, deg, min);
102 double lond = fdm.get_Longitude() * SGD_RADIANS_TO_DEGREES;
110 min = (lond - (double)deg) * 60.0 * 100.0;
111 sprintf( msg_B, "B%c %03d %04.0f\r\n", dir, deg, min);
114 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
115 double vn = fgGetDouble( "/velocities/speed-north-fps" );
116 double ve = fgGetDouble( "/velocities/speed-east-fps" );
117 double gnd_trk_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
118 double gnd_trk_mag = gnd_trk_true - magdeg;
119 if ( gnd_trk_mag < 0.0 ) { gnd_trk_mag += 360.0; }
120 if ( gnd_trk_mag >= 360.0 ) { gnd_trk_mag -= 360.0; }
121 sprintf( msg_C, "C%03.0f\r\n", gnd_trk_mag);
124 double speed_kt = sqrt( vn*vn + ve*ve ) * SG_FPS_TO_KT;
125 if ( speed_kt > 999.0 ) {
128 sprintf( msg_D, "D%03.0f\r\n", speed_kt);
130 // create msg_E (not implemented)
131 // create msg_G (not implemented)
132 // create msg_I (not implemented)
133 // create msg_K (not implemented)
134 // create msg_L (not implemented)
137 if ( magdeg < 0.0 ) {
143 sprintf( msg_Q, "Q%c%03.0f\r\n", dir, magdeg * 10.0 );
145 // create msg_S (not implemented)
148 sprintf( msg_T, "T---------\r\n" );
150 // create msg_l (not implemented)
153 sprintf( msg_type2, "w01%c\r\n", (char)65 );
157 sentence += '\002'; // STX
158 sentence += msg_z; // altitude
159 sentence += msg_A; // latitude
160 sentence += msg_B; // longitude
161 sentence += msg_C; // ground track
162 sentence += msg_D; // ground speed (kt)
163 // sentence += "E-----\r\n";
164 // sentence += "G-----\r\n";
165 // sentence += "I----\r\n";
166 // sentence += "K-----\r\n";
167 // sentence += "L----\r\n";
168 sentence += msg_Q; // magvar
169 // sentence += "S-----\r\n";
170 sentence += msg_T; // end of type 1 messages (must be sent)
171 sentence += msg_type2; // type2 message
172 // sentence += "l------\r\n";
173 sentence += '\003'; // ETX
176 length = sentence.length();
177 // cout << endl << "length = " << length << endl;
178 strncpy( buf, sentence.c_str(), length );
184 // parse AV400 message
185 bool FGAV400::parse_message() {
186 SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
189 msg = msg.substr( 0, length );
190 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
192 string::size_type begin_line, end_line, begin, end;
193 begin_line = begin = 0;
195 // extract out each line
196 end_line = msg.find("\n", begin_line);
197 while ( end_line != string::npos ) {
198 string line = msg.substr(begin_line, end_line - begin_line);
199 begin_line = end_line + 1;
200 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
203 string start = msg.substr(begin, 1);
205 SG_LOG( SG_IO, SG_INFO, " start = " << start );
208 end = msg.find(",", begin);
209 if ( end == string::npos ) {
213 string sentence = msg.substr(begin, end - begin);
215 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
217 double lon_deg, lon_min, lat_deg, lat_min;
218 double lon, lat, speed, heading, altitude;
220 if ( sentence == "GPRMC" ) {
222 end = msg.find(",", begin);
223 if ( end == string::npos ) {
227 string utc = msg.substr(begin, end - begin);
229 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
232 end = msg.find(",", begin);
233 if ( end == string::npos ) {
237 string junk = msg.substr(begin, end - begin);
239 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
242 end = msg.find(",", begin);
243 if ( end == string::npos ) {
247 string lat_str = msg.substr(begin, end - begin);
250 lat_deg = atof( lat_str.substr(0, 2).c_str() );
251 lat_min = atof( lat_str.substr(2).c_str() );
254 end = msg.find(",", begin);
255 if ( end == string::npos ) {
259 string lat_dir = msg.substr(begin, end - begin);
262 lat = lat_deg + ( lat_min / 60.0 );
263 if ( lat_dir == "S" ) {
267 fdm.set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
268 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
271 end = msg.find(",", begin);
272 if ( end == string::npos ) {
276 string lon_str = msg.substr(begin, end - begin);
279 lon_deg = atof( lon_str.substr(0, 3).c_str() );
280 lon_min = atof( lon_str.substr(3).c_str() );
283 end = msg.find(",", begin);
284 if ( end == string::npos ) {
288 string lon_dir = msg.substr(begin, end - begin);
291 lon = lon_deg + ( lon_min / 60.0 );
292 if ( lon_dir == "W" ) {
296 fdm.set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
297 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
300 double sl_radius, lat_geoc;
301 sgGeodToGeoc( fdm.get_Latitude(),
303 &sl_radius, &lat_geoc );
304 fdm.set_Geocentric_Position( lat_geoc,
306 sl_radius + fdm.get_Altitude() );
310 end = msg.find(",", begin);
311 if ( end == string::npos ) {
315 string speed_str = msg.substr(begin, end - begin);
317 speed = atof( speed_str.c_str() );
318 fdm.set_V_calibrated_kts( speed );
319 // fdm.set_V_ground_speed( speed );
320 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
323 end = msg.find(",", begin);
324 if ( end == string::npos ) {
328 string hdg_str = msg.substr(begin, end - begin);
330 heading = atof( hdg_str.c_str() );
331 fdm.set_Euler_Angles( fdm.get_Phi(),
333 heading * SGD_DEGREES_TO_RADIANS );
334 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
335 } else if ( sentence == "PGRMZ" ) {
337 end = msg.find(",", begin);
338 if ( end == string::npos ) {
342 string alt_str = msg.substr(begin, end - begin);
343 altitude = atof( alt_str.c_str() );
347 end = msg.find(",", begin);
348 if ( end == string::npos ) {
352 string alt_units = msg.substr(begin, end - begin);
355 if ( alt_units != "F" && alt_units != "f" ) {
356 altitude *= SG_METER_TO_FEET;
359 fdm.set_Altitude( altitude );
361 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
365 // printf("%.8f %.8f\n", lon, lat);
368 end_line = msg.find("\n", begin_line);
375 // open hailing frequencies
376 bool FGAV400::open() {
377 if ( is_enabled() ) {
378 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
379 << "is already in use, ignoring" );
383 SGIOChannel *io = get_io_channel();
385 if ( ! io->open( get_direction() ) ) {
386 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
396 // process work for this port
397 bool FGAV400::process() {
398 SGIOChannel *io = get_io_channel();
400 if ( get_direction() == SG_IO_OUT ) {
402 if ( ! io->write( buf, length ) ) {
403 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
406 } else if ( get_direction() == SG_IO_IN ) {
407 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
408 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
409 if ( parse_message() ) {
410 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
412 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
415 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
418 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
419 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
420 if ( parse_message() ) {
421 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
423 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
426 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
436 bool FGAV400::close() {
437 SGIOChannel *io = get_io_channel();
439 set_enabled( false );
441 if ( ! io->close() ) {