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/fg_props.hxx>
31 #include <Main/globals.hxx>
35 SG_USING_NAMESPACE(std);
45 // calculate the nmea check sum
46 static char calc_nmea_cksum(char *sentence) {
47 unsigned char sum = 0;
50 // cout << sentence << endl;
52 len = strlen(sentence);
54 for ( i = 1; i < len; i++ ) {
55 // cout << sentence[i];
60 // printf("sum = %02x\n", sum);
65 // generate NMEA message
66 bool FGNMEA::gen_message() {
67 // cout << "generating nmea message" << endl;
69 char rmc[256], gga[256], gsa[256];
70 char rmc_sum[10], gga_sum[10];
75 SGTime *t = globals->get_time_params();
78 sprintf( utc, "%02d%02d%02d",
79 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
81 char gga_lat[20], rmc_lat[20];
82 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
90 min = (latd - (double)deg) * 60.0;
91 sprintf( gga_lat, "%02d%06.3f,%c", abs(deg), min, dir);
92 sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
94 char gga_lon[20], rmc_lon[20];
95 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
103 min = (lond - (double)deg) * 60.0;
104 sprintf( gga_lon, "%03d%06.3f,%c", abs(deg), min, dir);
105 sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
108 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
111 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
114 sprintf( altitude_m, "%02d",
115 (int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
118 int year = t->getGmt()->tm_year;
119 while ( year >= 100 ) { year -= 100; }
120 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
121 t->getGmt()->tm_mon+1, year );
124 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
125 if ( magdeg < 0.0 ) {
131 sprintf( magvar, "%05.1f,%c", magdeg, dir );
133 // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
134 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s",
135 utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
136 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
138 sprintf( gga, "GPGGA,%s,%s,%s,1,08,0.9,%s,M, , ",
139 utc, gga_lat, gga_lon, altitude_m );
140 sprintf( gga_sum, "%02X", calc_nmea_cksum(gga) );
142 "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
144 SG_LOG( SG_IO, SG_DEBUG, rmc );
145 SG_LOG( SG_IO, SG_DEBUG, gga );
146 SG_LOG( SG_IO, SG_DEBUG, gsa );
148 string nmea_sentence;
152 nmea_sentence += rmc;
153 nmea_sentence += "*";
154 nmea_sentence += rmc_sum;
155 nmea_sentence += "\r\n";
158 nmea_sentence += "$";
159 nmea_sentence += gga;
160 nmea_sentence += "*";
161 nmea_sentence += gga_sum;
162 nmea_sentence += "\r\n";
164 // GSA sentence (totally faked)
165 nmea_sentence += gsa;
166 nmea_sentence += "\r\n";
168 cout << nmea_sentence;
170 length = nmea_sentence.length();
171 strncpy( buf, nmea_sentence.c_str(), length );
177 // parse NMEA message. messages will look something like the
180 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
181 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
183 bool FGNMEA::parse_message() {
184 SG_LOG( SG_IO, SG_INFO, "parse nmea message" );
187 msg = msg.substr( 0, length );
188 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
190 string::size_type begin_line, end_line, begin, end;
191 begin_line = begin = 0;
193 // extract out each line
194 end_line = msg.find("\n", begin_line);
195 while ( end_line != string::npos ) {
196 string line = msg.substr(begin_line, end_line - begin_line);
197 begin_line = end_line + 1;
198 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
201 string start = msg.substr(begin, 1);
203 SG_LOG( SG_IO, SG_INFO, " start = " << start );
206 end = msg.find(",", begin);
207 if ( end == string::npos ) {
211 string sentence = msg.substr(begin, end - begin);
213 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
215 double lon_deg, lon_min, lat_deg, lat_min;
216 double lon, lat, speed, heading, altitude;
218 if ( sentence == "GPRMC" ) {
220 end = msg.find(",", begin);
221 if ( end == string::npos ) {
225 string utc = msg.substr(begin, end - begin);
227 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
230 end = msg.find(",", begin);
231 if ( end == string::npos ) {
235 string junk = msg.substr(begin, end - begin);
237 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
240 end = msg.find(",", begin);
241 if ( end == string::npos ) {
245 string lat_str = msg.substr(begin, end - begin);
248 lat_deg = atof( lat_str.substr(0, 2).c_str() );
249 lat_min = atof( lat_str.substr(2).c_str() );
252 end = msg.find(",", begin);
253 if ( end == string::npos ) {
257 string lat_dir = msg.substr(begin, end - begin);
260 lat = lat_deg + ( lat_min / 60.0 );
261 if ( lat_dir == "S" ) {
265 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
266 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
269 end = msg.find(",", begin);
270 if ( end == string::npos ) {
274 string lon_str = msg.substr(begin, end - begin);
277 lon_deg = atof( lon_str.substr(0, 3).c_str() );
278 lon_min = atof( lon_str.substr(3).c_str() );
281 end = msg.find(",", begin);
282 if ( end == string::npos ) {
286 string lon_dir = msg.substr(begin, end - begin);
289 lon = lon_deg + ( lon_min / 60.0 );
290 if ( lon_dir == "W" ) {
294 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
295 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
298 double sl_radius, lat_geoc;
299 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
300 cur_fdm_state->get_Altitude(),
301 &sl_radius, &lat_geoc );
302 cur_fdm_state->set_Geocentric_Position( lat_geoc,
303 cur_fdm_state->get_Longitude(),
304 sl_radius + cur_fdm_state->get_Altitude() );
308 end = msg.find(",", begin);
309 if ( end == string::npos ) {
313 string speed_str = msg.substr(begin, end - begin);
315 speed = atof( speed_str.c_str() );
316 cur_fdm_state->set_V_calibrated_kts( speed );
317 // cur_fdm_state->set_V_ground_speed( speed );
318 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
321 end = msg.find(",", begin);
322 if ( end == string::npos ) {
326 string hdg_str = msg.substr(begin, end - begin);
328 heading = atof( hdg_str.c_str() );
329 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
330 cur_fdm_state->get_Theta(),
331 heading * SGD_DEGREES_TO_RADIANS );
332 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
333 } else if ( sentence == "GPGGA" ) {
335 end = msg.find(",", begin);
336 if ( end == string::npos ) {
340 string utc = msg.substr(begin, end - begin);
342 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
345 end = msg.find(",", begin);
346 if ( end == string::npos ) {
350 string lat_str = msg.substr(begin, end - begin);
353 lat_deg = atof( lat_str.substr(0, 2).c_str() );
354 lat_min = atof( lat_str.substr(2).c_str() );
357 end = msg.find(",", begin);
358 if ( end == string::npos ) {
362 string lat_dir = msg.substr(begin, end - begin);
365 lat = lat_deg + ( lat_min / 60.0 );
366 if ( lat_dir == "S" ) {
370 // cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
371 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
374 end = msg.find(",", begin);
375 if ( end == string::npos ) {
379 string lon_str = msg.substr(begin, end - begin);
382 lon_deg = atof( lon_str.substr(0, 3).c_str() );
383 lon_min = atof( lon_str.substr(3).c_str() );
386 end = msg.find(",", begin);
387 if ( end == string::npos ) {
391 string lon_dir = msg.substr(begin, end - begin);
394 lon = lon_deg + ( lon_min / 60.0 );
395 if ( lon_dir == "W" ) {
399 // cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
400 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
403 end = msg.find(",", begin);
404 if ( end == string::npos ) {
408 string junk = msg.substr(begin, end - begin);
410 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
413 end = msg.find(",", begin);
414 if ( end == string::npos ) {
418 junk = msg.substr(begin, end - begin);
420 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
423 end = msg.find(",", begin);
424 if ( end == string::npos ) {
428 junk = msg.substr(begin, end - begin);
430 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
433 end = msg.find(",", begin);
434 if ( end == string::npos ) {
438 string alt_str = msg.substr(begin, end - begin);
439 altitude = atof( alt_str.c_str() );
443 end = msg.find(",", begin);
444 if ( end == string::npos ) {
448 string alt_units = msg.substr(begin, end - begin);
451 if ( alt_units != "F" ) {
452 altitude *= SG_METER_TO_FEET;
455 cur_fdm_state->set_Altitude( altitude );
457 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
461 // printf("%.8f %.8f\n", lon, lat);
464 end_line = msg.find("\n", begin_line);
471 // open hailing frequencies
472 bool FGNMEA::open() {
473 if ( is_enabled() ) {
474 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
475 << "is already in use, ignoring" );
479 SGIOChannel *io = get_io_channel();
481 if ( ! io->open( get_direction() ) ) {
482 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
492 // process work for this port
493 bool FGNMEA::process() {
494 SGIOChannel *io = get_io_channel();
496 if ( get_direction() == SG_IO_OUT ) {
498 if ( ! io->write( buf, length ) ) {
499 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
502 } else if ( get_direction() == SG_IO_IN ) {
503 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
506 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
509 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
512 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
522 bool FGNMEA::close() {
523 SGIOChannel *io = get_io_channel();
525 set_enabled( false );
527 if ( ! io->close() ) {