1 // garmin.cxx -- Garmin protocal class
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 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 <FDM/flightProperties.hxx>
37 #include <Main/fg_props.hxx>
38 #include <Main/globals.hxx>
44 FGGarmin::FGGarmin() {
45 fdm = new FlightProperties;
48 FGGarmin::~FGGarmin() {
53 // calculate the garmin check sum
54 static char calc_nmea_cksum(char *sentence) {
55 unsigned char sum = 0;
58 // cout << sentence << endl;
60 len = strlen(sentence);
62 for ( i = 1; i < len; i++ ) {
63 // cout << sentence[i];
68 // printf("sum = %02x\n", sum);
73 // generate Garmin message
74 bool FGGarmin::gen_message() {
75 // cout << "generating garmin message" << endl;
77 char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256], gsa[256];
82 SGTime *t = globals->get_time_params();
85 sprintf( utc, "%02d%02d%02d",
86 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
89 double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
97 min = (latd - (double)deg) * 60.0;
98 sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
101 double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
109 min = (lond - (double)deg) * 60.0;
110 sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
113 sprintf( speed, "%05.1f", fdm->get_V_equiv_kts() );
116 sprintf( heading, "%05.1f", fdm->get_Psi() * SGD_RADIANS_TO_DEGREES );
119 sprintf( altitude_m, "%02d",
120 (int)(fdm->get_Altitude() * SG_FEET_TO_METER) );
123 int year = t->getGmt()->tm_year;
124 while ( year >= 100 ) { year -= 100; }
125 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
126 t->getGmt()->tm_mon+1, year );
129 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
130 if ( magdeg < 0.0 ) {
136 sprintf( magvar, "%05.1f,%c", magdeg, dir );
138 // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
139 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s",
140 utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
141 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
143 // sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
144 // utc, lat, lon, altitude_m );
146 sprintf( rmz, "PGRMZ,%s,M,3", altitude_m );
147 sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
150 "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
152 SG_LOG( SG_IO, SG_DEBUG, rmc );
153 SG_LOG( SG_IO, SG_DEBUG, rmz );
154 SG_LOG( SG_IO, SG_DEBUG, gsa );
156 string garmin_sentence;
159 garmin_sentence = "$";
160 garmin_sentence += rmc;
161 garmin_sentence += "*";
162 garmin_sentence += rmc_sum;
163 garmin_sentence += "\r\n";
165 // RMZ sentence (garmin proprietary)
166 garmin_sentence += "$";
167 garmin_sentence += rmz;
168 garmin_sentence += "*";
169 garmin_sentence += rmz_sum;
170 garmin_sentence += "\r\n";
172 // GSA sentence (totally faked)
173 garmin_sentence += gsa;
174 garmin_sentence += "\r\n";
176 std::cout << garmin_sentence;
178 length = garmin_sentence.length();
179 strncpy( buf, garmin_sentence.c_str(), length );
185 // parse Garmin message
186 bool FGGarmin::parse_message() {
187 SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
190 msg = msg.substr( 0, length );
191 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
193 string::size_type begin_line, end_line, begin, end;
194 begin_line = begin = 0;
196 // extract out each line
197 end_line = msg.find("\n", begin_line);
198 while ( end_line != string::npos ) {
199 string line = msg.substr(begin_line, end_line - begin_line);
200 begin_line = end_line + 1;
201 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
204 string start = msg.substr(begin, 1);
206 SG_LOG( SG_IO, SG_INFO, " start = " << start );
209 end = msg.find(",", begin);
210 if ( end == string::npos ) {
214 string sentence = msg.substr(begin, end - begin);
216 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
218 double lon_deg, lon_min, lat_deg, lat_min;
219 double lon, lat, speed, heading, altitude;
221 if ( sentence == "GPRMC" ) {
223 end = msg.find(",", begin);
224 if ( end == string::npos ) {
228 string utc = msg.substr(begin, end - begin);
230 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
233 end = msg.find(",", begin);
234 if ( end == string::npos ) {
238 string junk = msg.substr(begin, end - begin);
240 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
243 end = msg.find(",", begin);
244 if ( end == string::npos ) {
248 string lat_str = msg.substr(begin, end - begin);
251 lat_deg = atof( lat_str.substr(0, 2).c_str() );
252 lat_min = atof( lat_str.substr(2).c_str() );
255 end = msg.find(",", begin);
256 if ( end == string::npos ) {
260 string lat_dir = msg.substr(begin, end - begin);
263 lat = lat_deg + ( lat_min / 60.0 );
264 if ( lat_dir == "S" ) {
268 fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
269 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
272 end = msg.find(",", begin);
273 if ( end == string::npos ) {
277 string lon_str = msg.substr(begin, end - begin);
280 lon_deg = atof( lon_str.substr(0, 3).c_str() );
281 lon_min = atof( lon_str.substr(3).c_str() );
284 end = msg.find(",", begin);
285 if ( end == string::npos ) {
289 string lon_dir = msg.substr(begin, end - begin);
292 lon = lon_deg + ( lon_min / 60.0 );
293 if ( lon_dir == "W" ) {
297 fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
298 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
301 double sl_radius, lat_geoc;
302 sgGeodToGeoc( fdm->get_Latitude(),
304 &sl_radius, &lat_geoc );
305 fdm->set_Geocentric_Position( lat_geoc,
306 fdm->get_Longitude(),
307 sl_radius + fdm->get_Altitude() );
311 end = msg.find(",", begin);
312 if ( end == string::npos ) {
316 string speed_str = msg.substr(begin, end - begin);
318 speed = atof( speed_str.c_str() );
319 fdm->set_V_calibrated_kts( speed );
320 // fdm->set_V_ground_speed( speed );
321 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
324 end = msg.find(",", begin);
325 if ( end == string::npos ) {
329 string hdg_str = msg.substr(begin, end - begin);
331 heading = atof( hdg_str.c_str() );
332 fdm->set_Euler_Angles( fdm->get_Phi(),
334 heading * SGD_DEGREES_TO_RADIANS );
335 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
336 } else if ( sentence == "PGRMZ" ) {
338 end = msg.find(",", begin);
339 if ( end == string::npos ) {
343 string alt_str = msg.substr(begin, end - begin);
344 altitude = atof( alt_str.c_str() );
348 end = msg.find(",", begin);
349 if ( end == string::npos ) {
353 string alt_units = msg.substr(begin, end - begin);
356 if ( alt_units != "F" && alt_units != "f" ) {
357 altitude *= SG_METER_TO_FEET;
360 fdm->set_Altitude( altitude );
362 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
366 // printf("%.8f %.8f\n", lon, lat);
369 end_line = msg.find("\n", begin_line);
376 // open hailing frequencies
377 bool FGGarmin::open() {
378 if ( is_enabled() ) {
379 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
380 << "is already in use, ignoring" );
384 SGIOChannel *io = get_io_channel();
386 if ( ! io->open( get_direction() ) ) {
387 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
397 // process work for this port
398 bool FGGarmin::process() {
399 SGIOChannel *io = get_io_channel();
401 if ( get_direction() == SG_IO_OUT ) {
403 if ( ! io->write( buf, length ) ) {
404 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
407 } else if ( get_direction() == SG_IO_IN ) {
408 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
409 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
410 if ( parse_message() ) {
411 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
413 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
416 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
419 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
420 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
421 if ( parse_message() ) {
422 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
424 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
427 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
437 bool FGGarmin::close() {
438 SGIOChannel *io = get_io_channel();
440 set_enabled( false );
442 if ( ! io->close() ) {