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/fg_geodesy.hxx>
26 #include <simgear/timing/sg_time.hxx>
28 #include <FDM/flight.hxx>
30 #include "iochannel.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 = SGTime::cur_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() * RAD_TO_DEG;
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() * RAD_TO_DEG;
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() * RAD_TO_DEG );
107 sprintf( altitude_m, "%02d",
108 (int)(cur_fdm_state->get_Altitude() * 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 FG_LOG( FG_IO, FG_DEBUG, rmc );
129 FG_LOG( FG_IO, FG_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 FG_LOG( FG_IO, FG_INFO, "parse garmin message" );
161 msg = msg.substr( 0, length );
162 FG_LOG( FG_IO, FG_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 FG_LOG( FG_IO, FG_INFO, " input line = " << line );
175 string start = msg.substr(begin, 1);
177 FG_LOG( FG_IO, FG_INFO, " start = " << start );
180 end = msg.find(",", begin);
181 if ( end == string::npos ) {
185 string sentence = msg.substr(begin, end - begin);
187 FG_LOG( FG_IO, FG_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 FG_LOG( FG_IO, FG_INFO, " utc = " << utc );
204 end = msg.find(",", begin);
205 if ( end == string::npos ) {
209 string junk = msg.substr(begin, end - begin);
211 FG_LOG( FG_IO, FG_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 * DEG_TO_RAD );
240 FG_LOG( FG_IO, FG_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 * DEG_TO_RAD );
269 FG_LOG( FG_IO, FG_INFO, " lon = " << lon );
271 double sl_radius, lat_geoc;
272 fgGeodToGeoc( cur_fdm_state->get_Latitude(),
273 cur_fdm_state->get_Altitude(),
274 &sl_radius, &lat_geoc );
275 cur_fdm_state->set_Geocentric_Position( lat_geoc,
276 cur_fdm_state->get_Longitude(),
277 sl_radius + cur_fdm_state->get_Altitude() );
280 end = msg.find(",", begin);
281 if ( end == string::npos ) {
285 string speed_str = msg.substr(begin, end - begin);
287 speed = atof( speed_str.c_str() );
288 cur_fdm_state->set_V_equiv_kts( speed );
289 cur_fdm_state->set_V_ground_speed( speed );
290 FG_LOG( FG_IO, FG_INFO, " speed = " << speed );
293 end = msg.find(",", begin);
294 if ( end == string::npos ) {
298 string hdg_str = msg.substr(begin, end - begin);
300 heading = atof( hdg_str.c_str() );
301 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
302 cur_fdm_state->get_Theta(),
303 heading * DEG_TO_RAD );
304 FG_LOG( FG_IO, FG_INFO, " heading = " << heading );
305 } else if ( sentence == "PGRMZ" ) {
307 end = msg.find(",", begin);
308 if ( end == string::npos ) {
312 string alt_str = msg.substr(begin, end - begin);
313 altitude = atof( alt_str.c_str() );
317 end = msg.find(",", begin);
318 if ( end == string::npos ) {
322 string alt_units = msg.substr(begin, end - begin);
325 if ( alt_units != "F" && alt_units != "f" ) {
326 altitude *= METER_TO_FEET;
329 cur_fdm_state->set_Altitude( altitude );
331 FG_LOG( FG_IO, FG_INFO, " altitude = " << altitude );
335 // printf("%.8f %.8f\n", lon, lat);
338 end_line = msg.find("\n", begin_line);
345 // open hailing frequencies
346 bool FGGarmin::open() {
347 if ( is_enabled() ) {
348 FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel "
349 << "is already in use, ignoring" );
353 FGIOChannel *io = get_io_channel();
355 if ( ! io->open( get_direction() ) ) {
356 FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
366 // process work for this port
367 bool FGGarmin::process() {
368 FGIOChannel *io = get_io_channel();
370 if ( get_direction() == out ) {
372 if ( ! io->write( buf, length ) ) {
373 FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
376 } else if ( get_direction() == in ) {
377 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
378 FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
379 if ( parse_message() ) {
380 FG_LOG( FG_IO, FG_ALERT, "Success parsing data." );
382 FG_LOG( FG_IO, FG_ALERT, "Error parsing data." );
385 FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
388 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
389 FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
390 if ( parse_message() ) {
391 FG_LOG( FG_IO, FG_ALERT, "Success parsing data." );
393 FG_LOG( FG_IO, FG_ALERT, "Error parsing data." );
396 FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
406 bool FGGarmin::close() {
407 FGIOChannel *io = get_io_channel();
409 set_enabled( false );
411 if ( ! io->close() ) {