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 );
277 double sl_radius, lat_geoc;
278 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
279 cur_fdm_state->get_Altitude(),
280 &sl_radius, &lat_geoc );
281 cur_fdm_state->set_Geocentric_Position( lat_geoc,
282 cur_fdm_state->get_Longitude(),
283 sl_radius + cur_fdm_state->get_Altitude() );
287 end = msg.find(",", begin);
288 if ( end == string::npos ) {
292 string speed_str = msg.substr(begin, end - begin);
294 speed = atof( speed_str.c_str() );
295 cur_fdm_state->set_V_calibrated_kts( speed );
296 // cur_fdm_state->set_V_ground_speed( speed );
297 FG_LOG( FG_IO, FG_INFO, " speed = " << speed );
300 end = msg.find(",", begin);
301 if ( end == string::npos ) {
305 string hdg_str = msg.substr(begin, end - begin);
307 heading = atof( hdg_str.c_str() );
308 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
309 cur_fdm_state->get_Theta(),
310 heading * DEG_TO_RAD );
311 FG_LOG( FG_IO, FG_INFO, " heading = " << heading );
312 } else if ( sentence == "GPGGA" ) {
314 end = msg.find(",", begin);
315 if ( end == string::npos ) {
319 string utc = msg.substr(begin, end - begin);
321 FG_LOG( FG_IO, FG_INFO, " utc = " << utc );
324 end = msg.find(",", begin);
325 if ( end == string::npos ) {
329 string lat_str = msg.substr(begin, end - begin);
332 lat_deg = atof( lat_str.substr(0, 2).c_str() );
333 lat_min = atof( lat_str.substr(2).c_str() );
336 end = msg.find(",", begin);
337 if ( end == string::npos ) {
341 string lat_dir = msg.substr(begin, end - begin);
344 lat = lat_deg + ( lat_min / 60.0 );
345 if ( lat_dir == "S" ) {
349 // cur_fdm_state->set_Latitude( lat * DEG_TO_RAD );
350 FG_LOG( FG_IO, FG_INFO, " lat = " << lat );
353 end = msg.find(",", begin);
354 if ( end == string::npos ) {
358 string lon_str = msg.substr(begin, end - begin);
361 lon_deg = atof( lon_str.substr(0, 3).c_str() );
362 lon_min = atof( lon_str.substr(3).c_str() );
365 end = msg.find(",", begin);
366 if ( end == string::npos ) {
370 string lon_dir = msg.substr(begin, end - begin);
373 lon = lon_deg + ( lon_min / 60.0 );
374 if ( lon_dir == "W" ) {
378 // cur_fdm_state->set_Longitude( lon * DEG_TO_RAD );
379 FG_LOG( FG_IO, FG_INFO, " lon = " << lon );
382 end = msg.find(",", begin);
383 if ( end == string::npos ) {
387 string junk = msg.substr(begin, end - begin);
389 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
392 end = msg.find(",", begin);
393 if ( end == string::npos ) {
397 junk = msg.substr(begin, end - begin);
399 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
402 end = msg.find(",", begin);
403 if ( end == string::npos ) {
407 junk = msg.substr(begin, end - begin);
409 FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
412 end = msg.find(",", begin);
413 if ( end == string::npos ) {
417 string alt_str = msg.substr(begin, end - begin);
418 altitude = atof( alt_str.c_str() );
422 end = msg.find(",", begin);
423 if ( end == string::npos ) {
427 string alt_units = msg.substr(begin, end - begin);
430 if ( alt_units != "F" ) {
431 altitude *= METER_TO_FEET;
434 cur_fdm_state->set_Altitude( altitude );
436 FG_LOG( FG_IO, FG_INFO, " altitude = " << altitude );
440 // printf("%.8f %.8f\n", lon, lat);
443 end_line = msg.find("\n", begin_line);
450 // open hailing frequencies
451 bool FGNMEA::open() {
452 if ( is_enabled() ) {
453 FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel "
454 << "is already in use, ignoring" );
458 SGIOChannel *io = get_io_channel();
460 if ( ! io->open( get_direction() ) ) {
461 FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
471 // process work for this port
472 bool FGNMEA::process() {
473 SGIOChannel *io = get_io_channel();
475 if ( get_direction() == SG_IO_OUT ) {
477 if ( ! io->write( buf, length ) ) {
478 FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
481 } else if ( get_direction() == SG_IO_IN ) {
482 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
485 FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
488 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
491 FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
501 bool FGNMEA::close() {
502 SGIOChannel *io = get_io_channel();
504 set_enabled( false );
506 if ( ! io->close() ) {