1 // nmea.cxx -- NMEA protocal class
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
31 #include <simgear/io/iochannel.hxx>
32 #include <simgear/timing/sg_time.hxx>
34 #include <FDM/flightProperties.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
41 fdm = new FlightProperties();
49 // calculate the nmea check sum
50 static char calc_nmea_cksum(char *sentence) {
51 unsigned char sum = 0;
54 // cout << sentence << endl;
56 len = strlen(sentence);
58 for ( i = 1; i < len; i++ ) {
59 // cout << sentence[i];
64 // printf("sum = %02x\n", sum);
69 // generate NMEA message
70 bool FGNMEA::gen_message() {
71 // cout << "generating nmea message" << endl;
73 char rmc[256], gga[256], gsa[256];
74 char rmc_sum[10], gga_sum[10];
79 SGTime *t = globals->get_time_params();
82 sprintf( utc, "%02d%02d%02d",
83 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
85 char gga_lat[20], rmc_lat[20];
86 double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
94 min = (latd - (double)deg) * 60.0;
95 sprintf( gga_lat, "%02d%07.4f,%c", abs(deg), min, dir);
96 sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
98 char gga_lon[20], rmc_lon[20];
99 double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
107 min = (lond - (double)deg) * 60.0;
108 sprintf( gga_lon, "%03d%07.4f,%c", abs(deg), min, dir);
109 sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
111 double vn = fgGetDouble( "/velocities/speed-north-fps" );
112 double ve = fgGetDouble( "/velocities/speed-east-fps" );
113 double fps = sqrt( vn*vn + ve*ve );
114 double mps = fps * SG_FEET_TO_METER;
115 double kts = mps * SG_METER_TO_NM * 3600;
117 sprintf( speed, "%.1f", kts );
119 double hdg_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
120 if ( hdg_true < 0 ) {
124 sprintf( heading, "%.1f", hdg_true );
127 sprintf( altitude_m, "%.1f",
128 fdm->get_Altitude() * SG_FEET_TO_METER );
131 int year = t->getGmt()->tm_year;
132 while ( year >= 100 ) { year -= 100; }
133 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
134 t->getGmt()->tm_mon+1, year );
137 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
138 if ( magdeg < 0.0 ) {
144 sprintf( magvar, "%.1f,%c", magdeg, dir );
146 // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E,A*XX
147 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s,A",
148 utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
149 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
151 // $GPGGA,HHMMSS,DDMM.MMMM,N,DDDMM.MMMM,W,1,NN,H.H,AAAA.A,M,GG.G,M,,*XX
152 sprintf( gga, "GPGGA,%s,%s,%s,1,08,0.9,%s,M,0.0,M,,",
153 utc, gga_lat, gga_lon, altitude_m );
154 sprintf( gga_sum, "%02X", calc_nmea_cksum(gga) );
156 "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
158 SG_LOG( SG_IO, SG_DEBUG, rmc );
159 SG_LOG( SG_IO, SG_DEBUG, gga );
160 SG_LOG( SG_IO, SG_DEBUG, gsa );
162 string nmea_sentence;
166 nmea_sentence += rmc;
167 nmea_sentence += "*";
168 nmea_sentence += rmc_sum;
169 nmea_sentence += "\n";
172 nmea_sentence += "$";
173 nmea_sentence += gga;
174 nmea_sentence += "*";
175 nmea_sentence += gga_sum;
176 nmea_sentence += "\n";
178 // GSA sentence (totally faked)
179 nmea_sentence += gsa;
180 nmea_sentence += "\n";
182 // cout << nmea_sentence;
184 length = nmea_sentence.length();
185 strncpy( buf, nmea_sentence.c_str(), length );
191 // parse NMEA message. messages will look something like the
194 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
195 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
197 bool FGNMEA::parse_message() {
198 SG_LOG( SG_IO, SG_INFO, "parse nmea message" );
201 msg = msg.substr( 0, length );
202 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
204 string::size_type begin_line, end_line, begin, end;
205 begin_line = begin = 0;
207 // extract out each line
208 end_line = msg.find("\n", begin_line);
209 while ( end_line != string::npos ) {
210 string line = msg.substr(begin_line, end_line - begin_line);
211 begin_line = end_line + 1;
212 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
215 string start = msg.substr(begin, 1);
217 SG_LOG( SG_IO, SG_INFO, " start = " << start );
220 end = msg.find(",", begin);
221 if ( end == string::npos ) {
225 string sentence = msg.substr(begin, end - begin);
227 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
229 double lon_deg, lon_min, lat_deg, lat_min;
230 double lon, lat, speed, heading, altitude;
232 if ( sentence == "GPRMC" ) {
234 end = msg.find(",", begin);
235 if ( end == string::npos ) {
239 string utc = msg.substr(begin, end - begin);
241 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
244 end = msg.find(",", begin);
245 if ( end == string::npos ) {
249 string junk = msg.substr(begin, end - begin);
251 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
254 end = msg.find(",", begin);
255 if ( end == string::npos ) {
259 string lat_str = msg.substr(begin, end - begin);
262 lat_deg = atof( lat_str.substr(0, 2).c_str() );
263 lat_min = atof( lat_str.substr(2).c_str() );
266 end = msg.find(",", begin);
267 if ( end == string::npos ) {
271 string lat_dir = msg.substr(begin, end - begin);
274 lat = lat_deg + ( lat_min / 60.0 );
275 if ( lat_dir == "S" ) {
279 fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
280 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
283 end = msg.find(",", begin);
284 if ( end == string::npos ) {
288 string lon_str = msg.substr(begin, end - begin);
291 lon_deg = atof( lon_str.substr(0, 3).c_str() );
292 lon_min = atof( lon_str.substr(3).c_str() );
295 end = msg.find(",", begin);
296 if ( end == string::npos ) {
300 string lon_dir = msg.substr(begin, end - begin);
303 lon = lon_deg + ( lon_min / 60.0 );
304 if ( lon_dir == "W" ) {
308 fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
309 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
312 double sl_radius, lat_geoc;
313 sgGeodToGeoc( fdm->get_Latitude(),
315 &sl_radius, &lat_geoc );
316 fdm->set_Geocentric_Position( lat_geoc,
317 fdm->get_Longitude(),
318 sl_radius + fdm->get_Altitude() );
322 end = msg.find(",", begin);
323 if ( end == string::npos ) {
327 string speed_str = msg.substr(begin, end - begin);
329 speed = atof( speed_str.c_str() );
330 fdm->set_V_calibrated_kts( speed );
331 // fdm->set_V_ground_speed( speed );
332 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
335 end = msg.find(",", begin);
336 if ( end == string::npos ) {
340 string hdg_str = msg.substr(begin, end - begin);
342 heading = atof( hdg_str.c_str() );
343 fdm->set_Euler_Angles( fdm->get_Phi(),
345 heading * SGD_DEGREES_TO_RADIANS );
346 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
347 } else if ( sentence == "GPGGA" ) {
349 end = msg.find(",", begin);
350 if ( end == string::npos ) {
354 string utc = msg.substr(begin, end - begin);
356 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
359 end = msg.find(",", begin);
360 if ( end == string::npos ) {
364 string lat_str = msg.substr(begin, end - begin);
367 lat_deg = atof( lat_str.substr(0, 2).c_str() );
368 lat_min = atof( lat_str.substr(2).c_str() );
371 end = msg.find(",", begin);
372 if ( end == string::npos ) {
376 string lat_dir = msg.substr(begin, end - begin);
379 lat = lat_deg + ( lat_min / 60.0 );
380 if ( lat_dir == "S" ) {
384 // fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
385 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
388 end = msg.find(",", begin);
389 if ( end == string::npos ) {
393 string lon_str = msg.substr(begin, end - begin);
396 lon_deg = atof( lon_str.substr(0, 3).c_str() );
397 lon_min = atof( lon_str.substr(3).c_str() );
400 end = msg.find(",", begin);
401 if ( end == string::npos ) {
405 string lon_dir = msg.substr(begin, end - begin);
408 lon = lon_deg + ( lon_min / 60.0 );
409 if ( lon_dir == "W" ) {
413 // fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
414 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
417 end = msg.find(",", begin);
418 if ( end == string::npos ) {
422 string junk = msg.substr(begin, end - begin);
424 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
427 end = msg.find(",", begin);
428 if ( end == string::npos ) {
432 junk = msg.substr(begin, end - begin);
434 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
437 end = msg.find(",", begin);
438 if ( end == string::npos ) {
442 junk = msg.substr(begin, end - begin);
444 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
447 end = msg.find(",", begin);
448 if ( end == string::npos ) {
452 string alt_str = msg.substr(begin, end - begin);
453 altitude = atof( alt_str.c_str() );
457 end = msg.find(",", begin);
458 if ( end == string::npos ) {
462 string alt_units = msg.substr(begin, end - begin);
465 if ( alt_units != "F" ) {
466 altitude *= SG_METER_TO_FEET;
469 fdm->set_Altitude( altitude );
471 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
475 // printf("%.8f %.8f\n", lon, lat);
478 end_line = msg.find("\n", begin_line);
485 // open hailing frequencies
486 bool FGNMEA::open() {
487 if ( is_enabled() ) {
488 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
489 << "is already in use, ignoring" );
493 SGIOChannel *io = get_io_channel();
495 if ( ! io->open( get_direction() ) ) {
496 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
506 // process work for this port
507 bool FGNMEA::process() {
508 SGIOChannel *io = get_io_channel();
510 if ( get_direction() == SG_IO_OUT ) {
512 if ( ! io->write( buf, length ) ) {
513 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
516 } else if ( get_direction() == SG_IO_IN ) {
517 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
520 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
523 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
526 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
536 bool FGNMEA::close() {
537 SGIOChannel *io = get_io_channel();
539 set_enabled( false );
541 if ( ! io->close() ) {