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.
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/math/sg_geodesy.hxx>
32 #include <simgear/io/iochannel.hxx>
33 #include <simgear/timing/sg_time.hxx>
35 #include <FDM/flightProperties.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.hxx>
42 fdm = new FlightProperties();
50 // calculate the nmea check sum
51 static char calc_nmea_cksum(char *sentence) {
52 unsigned char sum = 0;
55 // cout << sentence << endl;
57 len = strlen(sentence);
59 for ( i = 1; i < len; i++ ) {
60 // cout << sentence[i];
65 // printf("sum = %02x\n", sum);
70 // generate NMEA message
71 bool FGNMEA::gen_message() {
72 // cout << "generating nmea message" << endl;
74 char rmc[256], gga[256], gsa[256];
75 char rmc_sum[10], gga_sum[10];
80 SGTime *t = globals->get_time_params();
83 sprintf( utc, "%02d%02d%02d",
84 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
86 char gga_lat[20], rmc_lat[20];
87 double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
95 min = (latd - (double)deg) * 60.0;
96 sprintf( gga_lat, "%02d%07.4f,%c", abs(deg), min, dir);
97 sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
99 char gga_lon[20], rmc_lon[20];
100 double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
108 min = (lond - (double)deg) * 60.0;
109 sprintf( gga_lon, "%03d%07.4f,%c", abs(deg), min, dir);
110 sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
112 double vn = fgGetDouble( "/velocities/speed-north-fps" );
113 double ve = fgGetDouble( "/velocities/speed-east-fps" );
114 double fps = sqrt( vn*vn + ve*ve );
115 double mps = fps * SG_FEET_TO_METER;
116 double kts = mps * SG_METER_TO_NM * 3600;
118 sprintf( speed, "%.1f", kts );
120 double hdg_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
121 if ( hdg_true < 0 ) {
125 sprintf( heading, "%.1f", hdg_true );
128 sprintf( altitude_m, "%.1f",
129 fdm->get_Altitude() * SG_FEET_TO_METER );
132 int year = t->getGmt()->tm_year;
133 while ( year >= 100 ) { year -= 100; }
134 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
135 t->getGmt()->tm_mon+1, year );
138 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
139 if ( magdeg < 0.0 ) {
145 sprintf( magvar, "%.1f,%c", magdeg, dir );
147 // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E,A*XX
148 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s,A",
149 utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
150 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
152 // $GPGGA,HHMMSS,DDMM.MMMM,N,DDDMM.MMMM,W,1,NN,H.H,AAAA.A,M,GG.G,M,,*XX
153 sprintf( gga, "GPGGA,%s,%s,%s,1,08,0.9,%s,M,0.0,M,,",
154 utc, gga_lat, gga_lon, altitude_m );
155 sprintf( gga_sum, "%02X", calc_nmea_cksum(gga) );
157 "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
159 SG_LOG( SG_IO, SG_DEBUG, rmc );
160 SG_LOG( SG_IO, SG_DEBUG, gga );
161 SG_LOG( SG_IO, SG_DEBUG, gsa );
163 string nmea_sentence;
167 nmea_sentence += rmc;
168 nmea_sentence += "*";
169 nmea_sentence += rmc_sum;
170 nmea_sentence += "\n";
173 nmea_sentence += "$";
174 nmea_sentence += gga;
175 nmea_sentence += "*";
176 nmea_sentence += gga_sum;
177 nmea_sentence += "\n";
179 // GSA sentence (totally faked)
180 nmea_sentence += gsa;
181 nmea_sentence += "\n";
183 // cout << nmea_sentence;
185 length = nmea_sentence.length();
186 strncpy( buf, nmea_sentence.c_str(), length );
192 // parse NMEA message. messages will look something like the
195 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
196 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
198 bool FGNMEA::parse_message() {
199 SG_LOG( SG_IO, SG_INFO, "parse nmea message" );
202 msg = msg.substr( 0, length );
203 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
205 string::size_type begin_line, end_line, begin, end;
206 begin_line = begin = 0;
208 // extract out each line
209 end_line = msg.find("\n", begin_line);
210 while ( end_line != string::npos ) {
211 string line = msg.substr(begin_line, end_line - begin_line);
212 begin_line = end_line + 1;
213 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
216 string start = msg.substr(begin, 1);
218 SG_LOG( SG_IO, SG_INFO, " start = " << start );
221 end = msg.find(",", begin);
222 if ( end == string::npos ) {
226 string sentence = msg.substr(begin, end - begin);
228 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
230 double lon_deg, lon_min, lat_deg, lat_min;
231 double lon, lat, speed, heading, altitude;
233 if ( sentence == "GPRMC" ) {
235 end = msg.find(",", begin);
236 if ( end == string::npos ) {
240 string utc = msg.substr(begin, end - begin);
242 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
245 end = msg.find(",", begin);
246 if ( end == string::npos ) {
250 string junk = msg.substr(begin, end - begin);
252 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
255 end = msg.find(",", begin);
256 if ( end == string::npos ) {
260 string lat_str = msg.substr(begin, end - begin);
263 lat_deg = atof( lat_str.substr(0, 2).c_str() );
264 lat_min = atof( lat_str.substr(2).c_str() );
267 end = msg.find(",", begin);
268 if ( end == string::npos ) {
272 string lat_dir = msg.substr(begin, end - begin);
275 lat = lat_deg + ( lat_min / 60.0 );
276 if ( lat_dir == "S" ) {
280 fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
281 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
284 end = msg.find(",", begin);
285 if ( end == string::npos ) {
289 string lon_str = msg.substr(begin, end - begin);
292 lon_deg = atof( lon_str.substr(0, 3).c_str() );
293 lon_min = atof( lon_str.substr(3).c_str() );
296 end = msg.find(",", begin);
297 if ( end == string::npos ) {
301 string lon_dir = msg.substr(begin, end - begin);
304 lon = lon_deg + ( lon_min / 60.0 );
305 if ( lon_dir == "W" ) {
309 fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
310 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
313 double sl_radius, lat_geoc;
314 sgGeodToGeoc( fdm->get_Latitude(),
316 &sl_radius, &lat_geoc );
317 fdm->set_Geocentric_Position( lat_geoc,
318 fdm->get_Longitude(),
319 sl_radius + fdm->get_Altitude() );
323 end = msg.find(",", begin);
324 if ( end == string::npos ) {
328 string speed_str = msg.substr(begin, end - begin);
330 speed = atof( speed_str.c_str() );
331 fdm->set_V_calibrated_kts( speed );
332 // fdm->set_V_ground_speed( speed );
333 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
336 end = msg.find(",", begin);
337 if ( end == string::npos ) {
341 string hdg_str = msg.substr(begin, end - begin);
343 heading = atof( hdg_str.c_str() );
344 fdm->set_Euler_Angles( fdm->get_Phi(),
346 heading * SGD_DEGREES_TO_RADIANS );
347 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
348 } else if ( sentence == "GPGGA" ) {
350 end = msg.find(",", begin);
351 if ( end == string::npos ) {
355 string utc = msg.substr(begin, end - begin);
357 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
360 end = msg.find(",", begin);
361 if ( end == string::npos ) {
365 string lat_str = msg.substr(begin, end - begin);
368 lat_deg = atof( lat_str.substr(0, 2).c_str() );
369 lat_min = atof( lat_str.substr(2).c_str() );
372 end = msg.find(",", begin);
373 if ( end == string::npos ) {
377 string lat_dir = msg.substr(begin, end - begin);
380 lat = lat_deg + ( lat_min / 60.0 );
381 if ( lat_dir == "S" ) {
385 // fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
386 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
389 end = msg.find(",", begin);
390 if ( end == string::npos ) {
394 string lon_str = msg.substr(begin, end - begin);
397 lon_deg = atof( lon_str.substr(0, 3).c_str() );
398 lon_min = atof( lon_str.substr(3).c_str() );
401 end = msg.find(",", begin);
402 if ( end == string::npos ) {
406 string lon_dir = msg.substr(begin, end - begin);
409 lon = lon_deg + ( lon_min / 60.0 );
410 if ( lon_dir == "W" ) {
414 // fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
415 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
418 end = msg.find(",", begin);
419 if ( end == string::npos ) {
423 string junk = msg.substr(begin, end - begin);
425 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
428 end = msg.find(",", begin);
429 if ( end == string::npos ) {
433 junk = msg.substr(begin, end - begin);
435 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
438 end = msg.find(",", begin);
439 if ( end == string::npos ) {
443 junk = msg.substr(begin, end - begin);
445 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
448 end = msg.find(",", begin);
449 if ( end == string::npos ) {
453 string alt_str = msg.substr(begin, end - begin);
454 altitude = atof( alt_str.c_str() );
458 end = msg.find(",", begin);
459 if ( end == string::npos ) {
463 string alt_units = msg.substr(begin, end - begin);
466 if ( alt_units != "F" ) {
467 altitude *= SG_METER_TO_FEET;
470 fdm->set_Altitude( altitude );
472 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
476 // printf("%.8f %.8f\n", lon, lat);
479 end_line = msg.find("\n", begin_line);
486 // open hailing frequencies
487 bool FGNMEA::open() {
488 if ( is_enabled() ) {
489 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
490 << "is already in use, ignoring" );
494 SGIOChannel *io = get_io_channel();
496 if ( ! io->open( get_direction() ) ) {
497 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
507 // process work for this port
508 bool FGNMEA::process() {
509 SGIOChannel *io = get_io_channel();
511 if ( get_direction() == SG_IO_OUT ) {
513 if ( ! io->write( buf, length ) ) {
514 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
517 } else if ( get_direction() == SG_IO_IN ) {
518 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
521 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
524 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
527 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
537 bool FGNMEA::close() {
538 SGIOChannel *io = get_io_channel();
540 set_enabled( false );
542 if ( ! io->close() ) {