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 <simgear/debug/logstream.hxx>
25 #include <simgear/math/sg_geodesy.hxx>
26 #include <simgear/io/iochannel.hxx>
28 #include <FDM/flight.hxx>
29 #include <Main/globals.hxx>
34 FGGarmin::FGGarmin() {
37 FGGarmin::~FGGarmin() {
41 // calculate the garmin check sum
42 static char calc_nmea_cksum(char *sentence) {
43 unsigned char sum = 0;
46 // cout << sentence << endl;
48 len = strlen(sentence);
50 for ( i = 1; i < len; i++ ) {
51 // cout << sentence[i];
56 // printf("sum = %02x\n", sum);
61 // generate Garmin message
62 bool FGGarmin::gen_message() {
63 // cout << "generating garmin message" << endl;
65 char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256];
70 SGTime *t = globals->get_time_params();
73 sprintf( utc, "%02d%02d%02d",
74 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
77 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
85 min = (latd - (double)deg) * 60.0;
86 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
89 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
97 min = (lond - (double)deg) * 60.0;
98 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
101 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
104 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
107 sprintf( altitude_m, "%02d",
108 (int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
110 char altitude_ft[10];
111 sprintf( altitude_ft, "%02d", (int)cur_fdm_state->get_Altitude() );
114 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
115 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
117 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
118 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,000.0,E",
119 utc, lat, lon, speed, heading, date );
120 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
122 // sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
123 // utc, lat, lon, altitude_m );
125 sprintf( rmz, "PGRMZ,%s,f,3", altitude_ft );
126 sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
128 SG_LOG( SG_IO, SG_DEBUG, rmc );
129 SG_LOG( SG_IO, SG_DEBUG, rmz );
131 string garmin_sentence;
134 garmin_sentence = "$";
135 garmin_sentence += rmc;
136 garmin_sentence += "*";
137 garmin_sentence += rmc_sum;
138 garmin_sentence += "\n";
140 // RMZ sentence (garmin proprietary)
141 garmin_sentence += "$";
142 garmin_sentence += rmz;
143 garmin_sentence += "*";
144 garmin_sentence += rmz_sum;
145 garmin_sentence += "\n";
147 cout << garmin_sentence;
149 length = garmin_sentence.length();
150 strncpy( buf, garmin_sentence.c_str(), length );
156 // parse Garmin message
157 bool FGGarmin::parse_message() {
158 SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
161 msg = msg.substr( 0, length );
162 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
164 string::size_type begin_line, end_line, begin, end;
165 begin_line = begin = 0;
167 // extract out each line
168 end_line = msg.find("\n", begin_line);
169 while ( end_line != string::npos ) {
170 string line = msg.substr(begin_line, end_line - begin_line);
171 begin_line = end_line + 1;
172 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
175 string start = msg.substr(begin, 1);
177 SG_LOG( SG_IO, SG_INFO, " start = " << start );
180 end = msg.find(",", begin);
181 if ( end == string::npos ) {
185 string sentence = msg.substr(begin, end - begin);
187 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
189 double lon_deg, lon_min, lat_deg, lat_min;
190 double lon, lat, speed, heading, altitude;
192 if ( sentence == "GPRMC" ) {
194 end = msg.find(",", begin);
195 if ( end == string::npos ) {
199 string utc = msg.substr(begin, end - begin);
201 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
204 end = msg.find(",", begin);
205 if ( end == string::npos ) {
209 string junk = msg.substr(begin, end - begin);
211 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
214 end = msg.find(",", begin);
215 if ( end == string::npos ) {
219 string lat_str = msg.substr(begin, end - begin);
222 lat_deg = atof( lat_str.substr(0, 2).c_str() );
223 lat_min = atof( lat_str.substr(2).c_str() );
226 end = msg.find(",", begin);
227 if ( end == string::npos ) {
231 string lat_dir = msg.substr(begin, end - begin);
234 lat = lat_deg + ( lat_min / 60.0 );
235 if ( lat_dir == "S" ) {
239 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
240 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
243 end = msg.find(",", begin);
244 if ( end == string::npos ) {
248 string lon_str = msg.substr(begin, end - begin);
251 lon_deg = atof( lon_str.substr(0, 3).c_str() );
252 lon_min = atof( lon_str.substr(3).c_str() );
255 end = msg.find(",", begin);
256 if ( end == string::npos ) {
260 string lon_dir = msg.substr(begin, end - begin);
263 lon = lon_deg + ( lon_min / 60.0 );
264 if ( lon_dir == "W" ) {
268 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
269 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
272 double sl_radius, lat_geoc;
273 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
274 cur_fdm_state->get_Altitude(),
275 &sl_radius, &lat_geoc );
276 cur_fdm_state->set_Geocentric_Position( lat_geoc,
277 cur_fdm_state->get_Longitude(),
278 sl_radius + cur_fdm_state->get_Altitude() );
282 end = msg.find(",", begin);
283 if ( end == string::npos ) {
287 string speed_str = msg.substr(begin, end - begin);
289 speed = atof( speed_str.c_str() );
290 cur_fdm_state->set_V_calibrated_kts( speed );
291 // cur_fdm_state->set_V_ground_speed( speed );
292 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
295 end = msg.find(",", begin);
296 if ( end == string::npos ) {
300 string hdg_str = msg.substr(begin, end - begin);
302 heading = atof( hdg_str.c_str() );
303 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
304 cur_fdm_state->get_Theta(),
305 heading * SGD_DEGREES_TO_RADIANS );
306 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
307 } else if ( sentence == "PGRMZ" ) {
309 end = msg.find(",", begin);
310 if ( end == string::npos ) {
314 string alt_str = msg.substr(begin, end - begin);
315 altitude = atof( alt_str.c_str() );
319 end = msg.find(",", begin);
320 if ( end == string::npos ) {
324 string alt_units = msg.substr(begin, end - begin);
327 if ( alt_units != (string)"F" && alt_units != (string)"f" ) {
328 altitude *= SG_METER_TO_FEET;
331 cur_fdm_state->set_Altitude( altitude );
333 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
337 // printf("%.8f %.8f\n", lon, lat);
340 end_line = msg.find("\n", begin_line);
347 // open hailing frequencies
348 bool FGGarmin::open() {
349 if ( is_enabled() ) {
350 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
351 << "is already in use, ignoring" );
355 SGIOChannel *io = get_io_channel();
357 if ( ! io->open( get_direction() ) ) {
358 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
368 // process work for this port
369 bool FGGarmin::process() {
370 SGIOChannel *io = get_io_channel();
372 if ( get_direction() == SG_IO_OUT ) {
374 if ( ! io->write( buf, length ) ) {
375 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
378 } else if ( get_direction() == SG_IO_IN ) {
379 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
380 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
381 if ( parse_message() ) {
382 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
384 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
387 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
390 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
391 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
392 if ( parse_message() ) {
393 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
395 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
398 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
408 bool FGGarmin::close() {
409 SGIOChannel *io = get_io_channel();
411 set_enabled( false );
413 if ( ! io->close() ) {