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>
27 #include <simgear/timing/sg_time.hxx>
29 #include <FDM/flight.hxx>
30 #include <Main/globals.hxx>
35 FGGarmin::FGGarmin() {
38 FGGarmin::~FGGarmin() {
42 // calculate the garmin check sum
43 static char calc_nmea_cksum(char *sentence) {
44 unsigned char sum = 0;
47 // cout << sentence << endl;
49 len = strlen(sentence);
51 for ( i = 1; i < len; i++ ) {
52 // cout << sentence[i];
57 // printf("sum = %02x\n", sum);
62 // generate Garmin message
63 bool FGGarmin::gen_message() {
64 // cout << "generating garmin message" << endl;
66 char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256];
71 SGTime *t = globals->get_time_params();
74 sprintf( utc, "%02d%02d%02d",
75 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
78 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
86 min = (latd - (double)deg) * 60.0;
87 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
90 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
98 min = (lond - (double)deg) * 60.0;
99 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
102 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
105 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
108 sprintf( altitude_m, "%02d",
109 (int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
111 char altitude_ft[10];
112 sprintf( altitude_ft, "%02d", (int)cur_fdm_state->get_Altitude() );
115 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
116 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
118 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
119 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,000.0,E",
120 utc, lat, lon, speed, heading, date );
121 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
123 // sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
124 // utc, lat, lon, altitude_m );
126 sprintf( rmz, "PGRMZ,%s,f,3", altitude_ft );
127 sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
129 SG_LOG( SG_IO, SG_DEBUG, rmc );
130 SG_LOG( SG_IO, SG_DEBUG, rmz );
132 string garmin_sentence;
135 garmin_sentence = "$";
136 garmin_sentence += rmc;
137 garmin_sentence += "*";
138 garmin_sentence += rmc_sum;
139 garmin_sentence += "\n";
141 // RMZ sentence (garmin proprietary)
142 garmin_sentence += "$";
143 garmin_sentence += rmz;
144 garmin_sentence += "*";
145 garmin_sentence += rmz_sum;
146 garmin_sentence += "\n";
148 std::cout << garmin_sentence;
150 length = garmin_sentence.length();
151 strncpy( buf, garmin_sentence.c_str(), length );
157 // parse Garmin message
158 bool FGGarmin::parse_message() {
159 SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
162 msg = msg.substr( 0, length );
163 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
165 string::size_type begin_line, end_line, begin, end;
166 begin_line = begin = 0;
168 // extract out each line
169 end_line = msg.find("\n", begin_line);
170 while ( end_line != string::npos ) {
171 string line = msg.substr(begin_line, end_line - begin_line);
172 begin_line = end_line + 1;
173 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
176 string start = msg.substr(begin, 1);
178 SG_LOG( SG_IO, SG_INFO, " start = " << start );
181 end = msg.find(",", begin);
182 if ( end == string::npos ) {
186 string sentence = msg.substr(begin, end - begin);
188 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
190 double lon_deg, lon_min, lat_deg, lat_min;
191 double lon, lat, speed, heading, altitude;
193 if ( sentence == "GPRMC" ) {
195 end = msg.find(",", begin);
196 if ( end == string::npos ) {
200 string utc = msg.substr(begin, end - begin);
202 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
205 end = msg.find(",", begin);
206 if ( end == string::npos ) {
210 string junk = msg.substr(begin, end - begin);
212 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
215 end = msg.find(",", begin);
216 if ( end == string::npos ) {
220 string lat_str = msg.substr(begin, end - begin);
223 lat_deg = atof( lat_str.substr(0, 2).c_str() );
224 lat_min = atof( lat_str.substr(2).c_str() );
227 end = msg.find(",", begin);
228 if ( end == string::npos ) {
232 string lat_dir = msg.substr(begin, end - begin);
235 lat = lat_deg + ( lat_min / 60.0 );
236 if ( lat_dir == "S" ) {
240 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
241 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
244 end = msg.find(",", begin);
245 if ( end == string::npos ) {
249 string lon_str = msg.substr(begin, end - begin);
252 lon_deg = atof( lon_str.substr(0, 3).c_str() );
253 lon_min = atof( lon_str.substr(3).c_str() );
256 end = msg.find(",", begin);
257 if ( end == string::npos ) {
261 string lon_dir = msg.substr(begin, end - begin);
264 lon = lon_deg + ( lon_min / 60.0 );
265 if ( lon_dir == "W" ) {
269 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
270 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
273 double sl_radius, lat_geoc;
274 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
275 cur_fdm_state->get_Altitude(),
276 &sl_radius, &lat_geoc );
277 cur_fdm_state->set_Geocentric_Position( lat_geoc,
278 cur_fdm_state->get_Longitude(),
279 sl_radius + cur_fdm_state->get_Altitude() );
283 end = msg.find(",", begin);
284 if ( end == string::npos ) {
288 string speed_str = msg.substr(begin, end - begin);
290 speed = atof( speed_str.c_str() );
291 cur_fdm_state->set_V_calibrated_kts( speed );
292 // cur_fdm_state->set_V_ground_speed( speed );
293 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
296 end = msg.find(",", begin);
297 if ( end == string::npos ) {
301 string hdg_str = msg.substr(begin, end - begin);
303 heading = atof( hdg_str.c_str() );
304 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
305 cur_fdm_state->get_Theta(),
306 heading * SGD_DEGREES_TO_RADIANS );
307 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
308 } else if ( sentence == "PGRMZ" ) {
310 end = msg.find(",", begin);
311 if ( end == string::npos ) {
315 string alt_str = msg.substr(begin, end - begin);
316 altitude = atof( alt_str.c_str() );
320 end = msg.find(",", begin);
321 if ( end == string::npos ) {
325 string alt_units = msg.substr(begin, end - begin);
328 if ( alt_units != (string)"F" && alt_units != (string)"f" ) {
329 altitude *= SG_METER_TO_FEET;
332 cur_fdm_state->set_Altitude( altitude );
334 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
338 // printf("%.8f %.8f\n", lon, lat);
341 end_line = msg.find("\n", begin_line);
348 // open hailing frequencies
349 bool FGGarmin::open() {
350 if ( is_enabled() ) {
351 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
352 << "is already in use, ignoring" );
356 SGIOChannel *io = get_io_channel();
358 if ( ! io->open( get_direction() ) ) {
359 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
369 // process work for this port
370 bool FGGarmin::process() {
371 SGIOChannel *io = get_io_channel();
373 if ( get_direction() == SG_IO_OUT ) {
375 if ( ! io->write( buf, length ) ) {
376 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
379 } else if ( get_direction() == SG_IO_IN ) {
380 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
381 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
382 if ( parse_message() ) {
383 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
385 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
388 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
391 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
392 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
393 if ( parse_message() ) {
394 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
396 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
399 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
409 bool FGGarmin::close() {
410 SGIOChannel *io = get_io_channel();
412 set_enabled( false );
414 if ( ! io->close() ) {