1 // nmea.cxx -- NMEA 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"
40 // calculate the nmea 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 NMEA message
61 bool FGNMEA::gen_message() {
62 // cout << "generating nmea message" << endl;
64 char rmc[256], gga[256];
65 char rmc_sum[10], gga_sum[10];
70 FGTime *t = FGTime::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,0.000,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,,,%s,F,,,,",
123 utc, lat, lon, altitude_ft );
124 sprintf( gga_sum, "%02X", calc_nmea_cksum(gga) );
127 FG_LOG( FG_IO, FG_DEBUG, rmc );
128 FG_LOG( FG_IO, FG_DEBUG, gga );
130 string nmea_sentence;
134 nmea_sentence += rmc;
135 nmea_sentence += "*";
136 nmea_sentence += rmc_sum;
137 nmea_sentence += "\n";
140 nmea_sentence += "$";
141 nmea_sentence += gga;
142 nmea_sentence += "*";
143 nmea_sentence += gga_sum;
144 nmea_sentence += "\n";
146 cout << nmea_sentence;
148 length = nmea_sentence.length();
149 strncpy( buf, nmea_sentence.c_str(), length );
155 // parse NMEA message. messages will look something like the
158 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
159 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
161 bool FGNMEA::parse_message() {
162 FG_LOG( FG_IO, FG_INFO, "parse nmea message" );
165 msg = msg.substr( 0, length );
166 FG_LOG( FG_IO, FG_INFO, "entire message = " << msg );
168 string::size_type begin_line, end_line, begin, end;
169 begin_line = begin = 0;
171 // extract out each line
172 end_line = msg.find("\n", begin_line);
173 while ( end_line != string::npos ) {
174 string line = msg.substr(begin_line, end_line - begin_line);
175 begin_line = end_line + 1;
176 FG_LOG( FG_IO, FG_INFO, " input line = " << line );
179 string start = msg.substr(begin, 1);
181 FG_LOG( FG_IO, FG_INFO, " start = " << start );
184 end = msg.find(",", begin);
185 if ( end == string::npos ) {
189 string sentence = msg.substr(begin, end - begin);
191 FG_LOG( FG_IO, FG_INFO, " sentence = " << sentence );
193 double lon_deg, lon_min, lat_deg, lat_min;
194 double lon, lat, speed, heading, altitude;
196 if ( sentence == "GPRMC" ) {
198 end = msg.find(",", begin);
199 if ( end == string::npos ) {
203 string utc = msg.substr(begin, end - begin);
205 FG_LOG( FG_IO, FG_INFO, " utc = " << utc );
208 end = msg.find(",", begin);
209 if ( end == string::npos ) {
213 string junk = msg.substr(begin, end - begin);
215 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
218 end = msg.find(",", begin);
219 if ( end == string::npos ) {
223 string lat_str = msg.substr(begin, end - begin);
226 lat_deg = atof( lat_str.substr(0, 2).c_str() );
227 lat_min = atof( lat_str.substr(2).c_str() );
230 end = msg.find(",", begin);
231 if ( end == string::npos ) {
235 string lat_dir = msg.substr(begin, end - begin);
238 lat = lat_deg + ( lat_min / 60.0 );
239 if ( lat_dir == "S" ) {
243 cur_fdm_state->set_Latitude( lat * DEG_TO_RAD );
244 FG_LOG( FG_IO, FG_INFO, " lat = " << lat );
247 end = msg.find(",", begin);
248 if ( end == string::npos ) {
252 string lon_str = msg.substr(begin, end - begin);
255 lon_deg = atof( lon_str.substr(0, 3).c_str() );
256 lon_min = atof( lon_str.substr(3).c_str() );
259 end = msg.find(",", begin);
260 if ( end == string::npos ) {
264 string lon_dir = msg.substr(begin, end - begin);
267 lon = lon_deg + ( lon_min / 60.0 );
268 if ( lon_dir == "W" ) {
272 cur_fdm_state->set_Longitude( lon * DEG_TO_RAD );
273 FG_LOG( FG_IO, FG_INFO, " lon = " << lon );
275 double sl_radius, lat_geoc;
276 fgGeodToGeoc( cur_fdm_state->get_Latitude(),
277 cur_fdm_state->get_Altitude(),
278 &sl_radius, &lat_geoc );
279 cur_fdm_state->set_Geocentric_Position( lat_geoc,
280 cur_fdm_state->get_Longitude(),
281 sl_radius + cur_fdm_state->get_Altitude() );
284 end = msg.find(",", begin);
285 if ( end == string::npos ) {
289 string speed_str = msg.substr(begin, end - begin);
291 speed = atof( speed_str.c_str() );
292 cur_fdm_state->set_V_equiv_kts( speed );
293 cur_fdm_state->set_V_ground_speed( speed );
294 FG_LOG( FG_IO, FG_INFO, " speed = " << speed );
297 end = msg.find(",", begin);
298 if ( end == string::npos ) {
302 string hdg_str = msg.substr(begin, end - begin);
304 heading = atof( hdg_str.c_str() );
305 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
306 cur_fdm_state->get_Theta(),
307 heading * DEG_TO_RAD );
308 FG_LOG( FG_IO, FG_INFO, " heading = " << heading );
309 } else if ( sentence == "GPGGA" ) {
311 end = msg.find(",", begin);
312 if ( end == string::npos ) {
316 string utc = msg.substr(begin, end - begin);
318 FG_LOG( FG_IO, FG_INFO, " utc = " << utc );
321 end = msg.find(",", begin);
322 if ( end == string::npos ) {
326 string lat_str = msg.substr(begin, end - begin);
329 lat_deg = atof( lat_str.substr(0, 2).c_str() );
330 lat_min = atof( lat_str.substr(2).c_str() );
333 end = msg.find(",", begin);
334 if ( end == string::npos ) {
338 string lat_dir = msg.substr(begin, end - begin);
341 lat = lat_deg + ( lat_min / 60.0 );
342 if ( lat_dir == "S" ) {
346 // cur_fdm_state->set_Latitude( lat * DEG_TO_RAD );
347 FG_LOG( FG_IO, FG_INFO, " lat = " << lat );
350 end = msg.find(",", begin);
351 if ( end == string::npos ) {
355 string lon_str = msg.substr(begin, end - begin);
358 lon_deg = atof( lon_str.substr(0, 3).c_str() );
359 lon_min = atof( lon_str.substr(3).c_str() );
362 end = msg.find(",", begin);
363 if ( end == string::npos ) {
367 string lon_dir = msg.substr(begin, end - begin);
370 lon = lon_deg + ( lon_min / 60.0 );
371 if ( lon_dir == "W" ) {
375 // cur_fdm_state->set_Longitude( lon * DEG_TO_RAD );
376 FG_LOG( FG_IO, FG_INFO, " lon = " << lon );
379 end = msg.find(",", begin);
380 if ( end == string::npos ) {
384 string junk = msg.substr(begin, end - begin);
386 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
389 end = msg.find(",", begin);
390 if ( end == string::npos ) {
394 junk = msg.substr(begin, end - begin);
396 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
399 end = msg.find(",", begin);
400 if ( end == string::npos ) {
404 junk = msg.substr(begin, end - begin);
406 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
409 end = msg.find(",", begin);
410 if ( end == string::npos ) {
414 string alt_str = msg.substr(begin, end - begin);
415 altitude = atof( alt_str.c_str() );
419 end = msg.find(",", begin);
420 if ( end == string::npos ) {
424 string alt_units = msg.substr(begin, end - begin);
427 if ( alt_units != "F" ) {
428 altitude *= METER_TO_FEET;
431 cur_fdm_state->set_Altitude( altitude );
433 FG_LOG( FG_IO, FG_INFO, " altitude = " << altitude );
437 // printf("%.8f %.8f\n", lon, lat);
440 end_line = msg.find("\n", begin_line);
447 // open hailing frequencies
448 bool FGNMEA::open() {
449 if ( is_enabled() ) {
450 FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel "
451 << "is already in use, ignoring" );
455 FGIOChannel *io = get_io_channel();
457 if ( ! io->open( get_direction() ) ) {
458 FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
468 // process work for this port
469 bool FGNMEA::process() {
470 FGIOChannel *io = get_io_channel();
472 if ( get_direction() == out ) {
474 if ( ! io->write( buf, length ) ) {
475 FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
478 } else if ( get_direction() == in ) {
479 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
482 FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
485 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
488 FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
498 bool FGNMEA::close() {
499 FGIOChannel *io = get_io_channel();
501 set_enabled( false );
503 if ( ! io->close() ) {