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 <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>
41 // calculate the nmea 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 NMEA message
62 bool FGNMEA::gen_message() {
63 // cout << "generating nmea message" << endl;
65 char rmc[256], gga[256];
66 char rmc_sum[10], gga_sum[10];
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() * RAD_TO_DEG;
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() * RAD_TO_DEG;
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() * RAD_TO_DEG );
108 sprintf( altitude_m, "%02d",
109 (int)(cur_fdm_state->get_Altitude() * 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,0.000,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,,,%s,F,,,,",
124 utc, lat, lon, altitude_ft );
125 sprintf( gga_sum, "%02X", calc_nmea_cksum(gga) );
128 FG_LOG( FG_IO, FG_DEBUG, rmc );
129 FG_LOG( FG_IO, FG_DEBUG, gga );
131 string nmea_sentence;
135 nmea_sentence += rmc;
136 nmea_sentence += "*";
137 nmea_sentence += rmc_sum;
138 nmea_sentence += "\n";
141 nmea_sentence += "$";
142 nmea_sentence += gga;
143 nmea_sentence += "*";
144 nmea_sentence += gga_sum;
145 nmea_sentence += "\n";
147 cout << nmea_sentence;
149 length = nmea_sentence.length();
150 strncpy( buf, nmea_sentence.c_str(), length );
156 // parse NMEA message. messages will look something like the
159 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
160 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
162 bool FGNMEA::parse_message() {
163 FG_LOG( FG_IO, FG_INFO, "parse nmea message" );
166 msg = msg.substr( 0, length );
167 FG_LOG( FG_IO, FG_INFO, "entire message = " << msg );
169 string::size_type begin_line, end_line, begin, end;
170 begin_line = begin = 0;
172 // extract out each line
173 end_line = msg.find("\n", begin_line);
174 while ( end_line != string::npos ) {
175 string line = msg.substr(begin_line, end_line - begin_line);
176 begin_line = end_line + 1;
177 FG_LOG( FG_IO, FG_INFO, " input line = " << line );
180 string start = msg.substr(begin, 1);
182 FG_LOG( FG_IO, FG_INFO, " start = " << start );
185 end = msg.find(",", begin);
186 if ( end == string::npos ) {
190 string sentence = msg.substr(begin, end - begin);
192 FG_LOG( FG_IO, FG_INFO, " sentence = " << sentence );
194 double lon_deg, lon_min, lat_deg, lat_min;
195 double lon, lat, speed, heading, altitude;
197 if ( sentence == "GPRMC" ) {
199 end = msg.find(",", begin);
200 if ( end == string::npos ) {
204 string utc = msg.substr(begin, end - begin);
206 FG_LOG( FG_IO, FG_INFO, " utc = " << utc );
209 end = msg.find(",", begin);
210 if ( end == string::npos ) {
214 string junk = msg.substr(begin, end - begin);
216 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
219 end = msg.find(",", begin);
220 if ( end == string::npos ) {
224 string lat_str = msg.substr(begin, end - begin);
227 lat_deg = atof( lat_str.substr(0, 2).c_str() );
228 lat_min = atof( lat_str.substr(2).c_str() );
231 end = msg.find(",", begin);
232 if ( end == string::npos ) {
236 string lat_dir = msg.substr(begin, end - begin);
239 lat = lat_deg + ( lat_min / 60.0 );
240 if ( lat_dir == "S" ) {
244 cur_fdm_state->set_Latitude( lat * DEG_TO_RAD );
245 FG_LOG( FG_IO, FG_INFO, " lat = " << lat );
248 end = msg.find(",", begin);
249 if ( end == string::npos ) {
253 string lon_str = msg.substr(begin, end - begin);
256 lon_deg = atof( lon_str.substr(0, 3).c_str() );
257 lon_min = atof( lon_str.substr(3).c_str() );
260 end = msg.find(",", begin);
261 if ( end == string::npos ) {
265 string lon_dir = msg.substr(begin, end - begin);
268 lon = lon_deg + ( lon_min / 60.0 );
269 if ( lon_dir == "W" ) {
273 cur_fdm_state->set_Longitude( lon * DEG_TO_RAD );
274 FG_LOG( FG_IO, FG_INFO, " lon = " << lon );
276 double sl_radius, lat_geoc;
277 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
278 cur_fdm_state->get_Altitude(),
279 &sl_radius, &lat_geoc );
280 cur_fdm_state->set_Geocentric_Position( lat_geoc,
281 cur_fdm_state->get_Longitude(),
282 sl_radius + cur_fdm_state->get_Altitude() );
285 end = msg.find(",", begin);
286 if ( end == string::npos ) {
290 string speed_str = msg.substr(begin, end - begin);
292 speed = atof( speed_str.c_str() );
293 cur_fdm_state->set_V_equiv_kts( speed );
294 cur_fdm_state->set_V_ground_speed( speed );
295 FG_LOG( FG_IO, FG_INFO, " speed = " << speed );
298 end = msg.find(",", begin);
299 if ( end == string::npos ) {
303 string hdg_str = msg.substr(begin, end - begin);
305 heading = atof( hdg_str.c_str() );
306 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
307 cur_fdm_state->get_Theta(),
308 heading * DEG_TO_RAD );
309 FG_LOG( FG_IO, FG_INFO, " heading = " << heading );
310 } else if ( sentence == "GPGGA" ) {
312 end = msg.find(",", begin);
313 if ( end == string::npos ) {
317 string utc = msg.substr(begin, end - begin);
319 FG_LOG( FG_IO, FG_INFO, " utc = " << utc );
322 end = msg.find(",", begin);
323 if ( end == string::npos ) {
327 string lat_str = msg.substr(begin, end - begin);
330 lat_deg = atof( lat_str.substr(0, 2).c_str() );
331 lat_min = atof( lat_str.substr(2).c_str() );
334 end = msg.find(",", begin);
335 if ( end == string::npos ) {
339 string lat_dir = msg.substr(begin, end - begin);
342 lat = lat_deg + ( lat_min / 60.0 );
343 if ( lat_dir == "S" ) {
347 // cur_fdm_state->set_Latitude( lat * DEG_TO_RAD );
348 FG_LOG( FG_IO, FG_INFO, " lat = " << lat );
351 end = msg.find(",", begin);
352 if ( end == string::npos ) {
356 string lon_str = msg.substr(begin, end - begin);
359 lon_deg = atof( lon_str.substr(0, 3).c_str() );
360 lon_min = atof( lon_str.substr(3).c_str() );
363 end = msg.find(",", begin);
364 if ( end == string::npos ) {
368 string lon_dir = msg.substr(begin, end - begin);
371 lon = lon_deg + ( lon_min / 60.0 );
372 if ( lon_dir == "W" ) {
376 // cur_fdm_state->set_Longitude( lon * DEG_TO_RAD );
377 FG_LOG( FG_IO, FG_INFO, " lon = " << lon );
380 end = msg.find(",", begin);
381 if ( end == string::npos ) {
385 string junk = msg.substr(begin, end - begin);
387 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
390 end = msg.find(",", begin);
391 if ( end == string::npos ) {
395 junk = msg.substr(begin, end - begin);
397 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
400 end = msg.find(",", begin);
401 if ( end == string::npos ) {
405 junk = msg.substr(begin, end - begin);
407 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
410 end = msg.find(",", begin);
411 if ( end == string::npos ) {
415 string alt_str = msg.substr(begin, end - begin);
416 altitude = atof( alt_str.c_str() );
420 end = msg.find(",", begin);
421 if ( end == string::npos ) {
425 string alt_units = msg.substr(begin, end - begin);
428 if ( alt_units != "F" ) {
429 altitude *= METER_TO_FEET;
432 cur_fdm_state->set_Altitude( altitude );
434 FG_LOG( FG_IO, FG_INFO, " altitude = " << altitude );
438 // printf("%.8f %.8f\n", lon, lat);
441 end_line = msg.find("\n", begin_line);
448 // open hailing frequencies
449 bool FGNMEA::open() {
450 if ( is_enabled() ) {
451 FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel "
452 << "is already in use, ignoring" );
456 SGIOChannel *io = get_io_channel();
458 if ( ! io->open( get_direction() ) ) {
459 FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
469 // process work for this port
470 bool FGNMEA::process() {
471 SGIOChannel *io = get_io_channel();
473 if ( get_direction() == SG_IO_OUT ) {
475 if ( ! io->write( buf, length ) ) {
476 FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
479 } else if ( get_direction() == SG_IO_IN ) {
480 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
483 FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
486 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
489 FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
499 bool FGNMEA::close() {
500 SGIOChannel *io = get_io_channel();
502 set_enabled( false );
504 if ( ! io->close() ) {