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>
27 #include <simgear/timing/sg_time.hxx>
29 #include <FDM/flight.hxx>
30 #include <Main/globals.hxx>
34 SG_USING_NAMESPACE(std);
44 // calculate the nmea check sum
45 static char calc_nmea_cksum(char *sentence) {
46 unsigned char sum = 0;
49 // cout << sentence << endl;
51 len = strlen(sentence);
53 for ( i = 1; i < len; i++ ) {
54 // cout << sentence[i];
59 // printf("sum = %02x\n", sum);
64 // generate NMEA message
65 bool FGNMEA::gen_message() {
66 // cout << "generating nmea message" << endl;
68 char rmc[256], gga[256];
69 char rmc_sum[10], gga_sum[10];
74 SGTime *t = globals->get_time_params();
77 sprintf( utc, "%02d%02d%02d",
78 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
81 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
89 min = (latd - (double)deg) * 60.0;
90 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
93 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
101 min = (lond - (double)deg) * 60.0;
102 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
105 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
108 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
111 sprintf( altitude_m, "%02d",
112 (int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
114 char altitude_ft[10];
115 sprintf( altitude_ft, "%02d", (int)cur_fdm_state->get_Altitude() );
118 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
119 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
121 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
122 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,0.000,E",
123 utc, lat, lon, speed, heading, date );
124 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
126 sprintf( gga, "GPGGA,%s,%s,%s,1,,,%s,F,,,,",
127 utc, lat, lon, altitude_ft );
128 sprintf( gga_sum, "%02X", calc_nmea_cksum(gga) );
131 SG_LOG( SG_IO, SG_DEBUG, rmc );
132 SG_LOG( SG_IO, SG_DEBUG, gga );
134 string nmea_sentence;
138 nmea_sentence += rmc;
139 nmea_sentence += "*";
140 nmea_sentence += rmc_sum;
141 nmea_sentence += "\n";
144 nmea_sentence += "$";
145 nmea_sentence += gga;
146 nmea_sentence += "*";
147 nmea_sentence += gga_sum;
148 nmea_sentence += "\n";
150 cout << nmea_sentence;
152 length = nmea_sentence.length();
153 strncpy( buf, nmea_sentence.c_str(), length );
159 // parse NMEA message. messages will look something like the
162 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
163 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
165 bool FGNMEA::parse_message() {
166 SG_LOG( SG_IO, SG_INFO, "parse nmea message" );
169 msg = msg.substr( 0, length );
170 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
172 string::size_type begin_line, end_line, begin, end;
173 begin_line = begin = 0;
175 // extract out each line
176 end_line = msg.find("\n", begin_line);
177 while ( end_line != string::npos ) {
178 string line = msg.substr(begin_line, end_line - begin_line);
179 begin_line = end_line + 1;
180 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
183 string start = msg.substr(begin, 1);
185 SG_LOG( SG_IO, SG_INFO, " start = " << start );
188 end = msg.find(",", begin);
189 if ( end == string::npos ) {
193 string sentence = msg.substr(begin, end - begin);
195 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
197 double lon_deg, lon_min, lat_deg, lat_min;
198 double lon, lat, speed, heading, altitude;
200 if ( sentence == "GPRMC" ) {
202 end = msg.find(",", begin);
203 if ( end == string::npos ) {
207 string utc = msg.substr(begin, end - begin);
209 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
212 end = msg.find(",", begin);
213 if ( end == string::npos ) {
217 string junk = msg.substr(begin, end - begin);
219 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
222 end = msg.find(",", begin);
223 if ( end == string::npos ) {
227 string lat_str = msg.substr(begin, end - begin);
230 lat_deg = atof( lat_str.substr(0, 2).c_str() );
231 lat_min = atof( lat_str.substr(2).c_str() );
234 end = msg.find(",", begin);
235 if ( end == string::npos ) {
239 string lat_dir = msg.substr(begin, end - begin);
242 lat = lat_deg + ( lat_min / 60.0 );
243 if ( lat_dir == "S" ) {
247 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
248 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
251 end = msg.find(",", begin);
252 if ( end == string::npos ) {
256 string lon_str = msg.substr(begin, end - begin);
259 lon_deg = atof( lon_str.substr(0, 3).c_str() );
260 lon_min = atof( lon_str.substr(3).c_str() );
263 end = msg.find(",", begin);
264 if ( end == string::npos ) {
268 string lon_dir = msg.substr(begin, end - begin);
271 lon = lon_deg + ( lon_min / 60.0 );
272 if ( lon_dir == "W" ) {
276 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
277 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
280 double sl_radius, lat_geoc;
281 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
282 cur_fdm_state->get_Altitude(),
283 &sl_radius, &lat_geoc );
284 cur_fdm_state->set_Geocentric_Position( lat_geoc,
285 cur_fdm_state->get_Longitude(),
286 sl_radius + cur_fdm_state->get_Altitude() );
290 end = msg.find(",", begin);
291 if ( end == string::npos ) {
295 string speed_str = msg.substr(begin, end - begin);
297 speed = atof( speed_str.c_str() );
298 cur_fdm_state->set_V_calibrated_kts( speed );
299 // cur_fdm_state->set_V_ground_speed( speed );
300 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
303 end = msg.find(",", begin);
304 if ( end == string::npos ) {
308 string hdg_str = msg.substr(begin, end - begin);
310 heading = atof( hdg_str.c_str() );
311 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
312 cur_fdm_state->get_Theta(),
313 heading * SGD_DEGREES_TO_RADIANS );
314 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
315 } else if ( sentence == "GPGGA" ) {
317 end = msg.find(",", begin);
318 if ( end == string::npos ) {
322 string utc = msg.substr(begin, end - begin);
324 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
327 end = msg.find(",", begin);
328 if ( end == string::npos ) {
332 string lat_str = msg.substr(begin, end - begin);
335 lat_deg = atof( lat_str.substr(0, 2).c_str() );
336 lat_min = atof( lat_str.substr(2).c_str() );
339 end = msg.find(",", begin);
340 if ( end == string::npos ) {
344 string lat_dir = msg.substr(begin, end - begin);
347 lat = lat_deg + ( lat_min / 60.0 );
348 if ( lat_dir == "S" ) {
352 // cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
353 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
356 end = msg.find(",", begin);
357 if ( end == string::npos ) {
361 string lon_str = msg.substr(begin, end - begin);
364 lon_deg = atof( lon_str.substr(0, 3).c_str() );
365 lon_min = atof( lon_str.substr(3).c_str() );
368 end = msg.find(",", begin);
369 if ( end == string::npos ) {
373 string lon_dir = msg.substr(begin, end - begin);
376 lon = lon_deg + ( lon_min / 60.0 );
377 if ( lon_dir == "W" ) {
381 // cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
382 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
385 end = msg.find(",", begin);
386 if ( end == string::npos ) {
390 string junk = msg.substr(begin, end - begin);
392 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
395 end = msg.find(",", begin);
396 if ( end == string::npos ) {
400 junk = msg.substr(begin, end - begin);
402 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
405 end = msg.find(",", begin);
406 if ( end == string::npos ) {
410 junk = msg.substr(begin, end - begin);
412 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
415 end = msg.find(",", begin);
416 if ( end == string::npos ) {
420 string alt_str = msg.substr(begin, end - begin);
421 altitude = atof( alt_str.c_str() );
425 end = msg.find(",", begin);
426 if ( end == string::npos ) {
430 string alt_units = msg.substr(begin, end - begin);
433 if ( alt_units != (string)"F" ) {
434 altitude *= SG_METER_TO_FEET;
437 cur_fdm_state->set_Altitude( altitude );
439 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
443 // printf("%.8f %.8f\n", lon, lat);
446 end_line = msg.find("\n", begin_line);
453 // open hailing frequencies
454 bool FGNMEA::open() {
455 if ( is_enabled() ) {
456 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
457 << "is already in use, ignoring" );
461 SGIOChannel *io = get_io_channel();
463 if ( ! io->open( get_direction() ) ) {
464 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
474 // process work for this port
475 bool FGNMEA::process() {
476 SGIOChannel *io = get_io_channel();
478 if ( get_direction() == SG_IO_OUT ) {
480 if ( ! io->write( buf, length ) ) {
481 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
484 } else if ( get_direction() == SG_IO_IN ) {
485 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
488 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
491 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
494 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
504 bool FGNMEA::close() {
505 SGIOChannel *io = get_io_channel();
507 set_enabled( false );
509 if ( ! io->close() ) {