1 // garmin.cxx -- Garmin protocal class
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <Debug/logstream.hxx>
25 #include <FDM/flight.hxx>
26 #include <Math/fg_geodesy.hxx>
27 #include <Time/fg_time.hxx>
29 #include "iochannel.hxx"
33 FGGarmin::FGGarmin() {
36 FGGarmin::~FGGarmin() {
40 // calculate the garmin check sum
41 static char calc_nmea_cksum(char *sentence) {
42 unsigned char sum = 0;
45 // cout << sentence << endl;
47 len = strlen(sentence);
49 for ( i = 1; i < len; i++ ) {
50 // cout << sentence[i];
55 // printf("sum = %02x\n", sum);
60 // generate Garmin message
61 bool FGGarmin::gen_message() {
62 // cout << "generating garmin message" << endl;
64 char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256];
69 FGTime *t = FGTime::cur_time_params;
72 sprintf( utc, "%02d%02d%02d",
73 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
76 double latd = cur_fdm_state->get_Latitude() * RAD_TO_DEG;
84 min = (latd - (double)deg) * 60.0;
85 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
88 double lond = cur_fdm_state->get_Longitude() * RAD_TO_DEG;
96 min = (lond - (double)deg) * 60.0;
97 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
100 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
103 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * RAD_TO_DEG );
106 sprintf( altitude_m, "%02d",
107 (int)(cur_fdm_state->get_Altitude() * FEET_TO_METER) );
109 char altitude_ft[10];
110 sprintf( altitude_ft, "%02d", (int)cur_fdm_state->get_Altitude() );
113 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
114 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
116 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
117 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,000.0,E",
118 utc, lat, lon, speed, heading, date );
119 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
121 // sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
122 // utc, lat, lon, altitude_m );
124 sprintf( rmz, "PGRMZ,%s,f,3", altitude_ft );
125 sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
127 FG_LOG( FG_IO, FG_DEBUG, rmc );
128 FG_LOG( FG_IO, FG_DEBUG, rmz );
130 string garmin_sentence;
133 garmin_sentence = "$";
134 garmin_sentence += rmc;
135 garmin_sentence += "*";
136 garmin_sentence += rmc_sum;
137 garmin_sentence += "\n";
139 // RMZ sentence (garmin proprietary)
140 garmin_sentence += "$";
141 garmin_sentence += rmz;
142 garmin_sentence += "*";
143 garmin_sentence += rmz_sum;
144 garmin_sentence += "\n";
146 cout << garmin_sentence;
148 length = garmin_sentence.length();
149 strncpy( buf, garmin_sentence.c_str(), length );
155 // parse Garmin message
156 bool FGGarmin::parse_message() {
157 FG_LOG( FG_IO, FG_INFO, "parse garmin message" );
160 msg = msg.substr( 0, length );
161 FG_LOG( FG_IO, FG_INFO, "entire message = " << msg );
163 string::size_type begin_line, end_line, begin, end;
164 begin_line = begin = 0;
166 // extract out each line
167 end_line = msg.find("\n", begin_line);
168 while ( end_line != string::npos ) {
169 string line = msg.substr(begin_line, end_line - begin_line);
170 begin_line = end_line + 1;
171 FG_LOG( FG_IO, FG_INFO, " input line = " << line );
174 string start = msg.substr(begin, 1);
176 FG_LOG( FG_IO, FG_INFO, " start = " << start );
179 end = msg.find(",", begin);
180 if ( end == string::npos ) {
184 string sentence = msg.substr(begin, end - begin);
186 FG_LOG( FG_IO, FG_INFO, " sentence = " << sentence );
188 double lon_deg, lon_min, lat_deg, lat_min;
189 double lon, lat, speed, heading, altitude;
191 if ( sentence == "GPRMC" ) {
193 end = msg.find(",", begin);
194 if ( end == string::npos ) {
198 string utc = msg.substr(begin, end - begin);
200 FG_LOG( FG_IO, FG_INFO, " utc = " << utc );
203 end = msg.find(",", begin);
204 if ( end == string::npos ) {
208 string junk = msg.substr(begin, end - begin);
210 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
213 end = msg.find(",", begin);
214 if ( end == string::npos ) {
218 string lat_str = msg.substr(begin, end - begin);
221 lat_deg = atof( lat_str.substr(0, 2).c_str() );
222 lat_min = atof( lat_str.substr(2).c_str() );
225 end = msg.find(",", begin);
226 if ( end == string::npos ) {
230 string lat_dir = msg.substr(begin, end - begin);
233 lat = lat_deg + ( lat_min / 60.0 );
234 if ( lat_dir == "S" ) {
238 cur_fdm_state->set_Latitude( lat * DEG_TO_RAD );
239 FG_LOG( FG_IO, FG_INFO, " lat = " << lat );
242 end = msg.find(",", begin);
243 if ( end == string::npos ) {
247 string lon_str = msg.substr(begin, end - begin);
250 lon_deg = atof( lon_str.substr(0, 3).c_str() );
251 lon_min = atof( lon_str.substr(3).c_str() );
254 end = msg.find(",", begin);
255 if ( end == string::npos ) {
259 string lon_dir = msg.substr(begin, end - begin);
262 lon = lon_deg + ( lon_min / 60.0 );
263 if ( lon_dir == "W" ) {
267 cur_fdm_state->set_Longitude( lon * DEG_TO_RAD );
268 FG_LOG( FG_IO, FG_INFO, " lon = " << lon );
270 double sl_radius, lat_geoc;
271 fgGeodToGeoc( cur_fdm_state->get_Latitude(),
272 cur_fdm_state->get_Altitude(),
273 &sl_radius, &lat_geoc );
274 cur_fdm_state->set_Geocentric_Position( lat_geoc,
275 cur_fdm_state->get_Longitude(),
276 sl_radius + cur_fdm_state->get_Altitude() );
279 end = msg.find(",", begin);
280 if ( end == string::npos ) {
284 string speed_str = msg.substr(begin, end - begin);
286 speed = atof( speed_str.c_str() );
287 cur_fdm_state->set_V_equiv_kts( speed );
288 cur_fdm_state->set_V_ground_speed( speed );
289 FG_LOG( FG_IO, FG_INFO, " speed = " << speed );
292 end = msg.find(",", begin);
293 if ( end == string::npos ) {
297 string hdg_str = msg.substr(begin, end - begin);
299 heading = atof( hdg_str.c_str() );
300 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
301 cur_fdm_state->get_Theta(),
302 heading * DEG_TO_RAD );
303 FG_LOG( FG_IO, FG_INFO, " heading = " << heading );
304 } else if ( sentence == "PGRMZ" ) {
306 end = msg.find(",", begin);
307 if ( end == string::npos ) {
311 string alt_str = msg.substr(begin, end - begin);
312 altitude = atof( alt_str.c_str() );
316 end = msg.find(",", begin);
317 if ( end == string::npos ) {
321 string alt_units = msg.substr(begin, end - begin);
324 if ( alt_units != "F" && alt_units != "f" ) {
325 altitude *= METER_TO_FEET;
328 cur_fdm_state->set_Altitude( altitude );
330 FG_LOG( FG_IO, FG_INFO, " altitude = " << altitude );
334 // printf("%.8f %.8f\n", lon, lat);
337 end_line = msg.find("\n", begin_line);
344 // open hailing frequencies
345 bool FGGarmin::open() {
346 if ( is_enabled() ) {
347 FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel "
348 << "is already in use, ignoring" );
352 FGIOChannel *io = get_io_channel();
354 if ( ! io->open( get_direction() ) ) {
355 FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
365 // process work for this port
366 bool FGGarmin::process() {
367 FGIOChannel *io = get_io_channel();
369 if ( get_direction() == out ) {
371 if ( ! io->write( buf, length ) ) {
372 FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
375 } else if ( get_direction() == in ) {
376 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
377 FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
378 if ( parse_message() ) {
379 FG_LOG( FG_IO, FG_ALERT, "Success parsing data." );
381 FG_LOG( FG_IO, FG_ALERT, "Error parsing data." );
384 FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
387 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
388 FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
389 if ( parse_message() ) {
390 FG_LOG( FG_IO, FG_ALERT, "Success parsing data." );
392 FG_LOG( FG_IO, FG_ALERT, "Error parsing data." );
395 FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
405 bool FGGarmin::close() {
406 FGIOChannel *io = get_io_channel();
408 set_enabled( false );
410 if ( ! io->close() ) {