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.
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/math/sg_geodesy.hxx>
29 #include <simgear/io/iochannel.hxx>
30 #include <simgear/timing/sg_time.hxx>
32 #include <FDM/flight.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Main/globals.hxx>
38 SG_USING_NAMESPACE(std);
48 // calculate the nmea check sum
49 static char calc_nmea_cksum(char *sentence) {
50 unsigned char sum = 0;
53 // cout << sentence << endl;
55 len = strlen(sentence);
57 for ( i = 1; i < len; i++ ) {
58 // cout << sentence[i];
63 // printf("sum = %02x\n", sum);
68 // generate NMEA message
69 bool FGNMEA::gen_message() {
70 // cout << "generating nmea message" << endl;
72 char rmc[256], gga[256], gsa[256];
73 char rmc_sum[10], gga_sum[10];
78 SGTime *t = globals->get_time_params();
81 sprintf( utc, "%02d%02d%02d",
82 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
84 char gga_lat[20], rmc_lat[20];
85 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
93 min = (latd - (double)deg) * 60.0;
94 sprintf( gga_lat, "%02d%07.4f,%c", abs(deg), min, dir);
95 sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
97 char gga_lon[20], rmc_lon[20];
98 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
106 min = (lond - (double)deg) * 60.0;
107 sprintf( gga_lon, "%03d%07.4f,%c", abs(deg), min, dir);
108 sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
110 double vn = fgGetDouble( "/velocities/speed-north-fps" );
111 double ve = fgGetDouble( "/velocities/speed-east-fps" );
112 double fps = sqrt( vn*vn + ve*ve );
113 double mps = fps * SG_FEET_TO_METER;
114 double kts = mps * SG_METER_TO_NM * 3600;
116 sprintf( speed, "%.1f", kts );
118 double hdg_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
119 if ( hdg_true < 0 ) {
123 sprintf( heading, "%.1f", hdg_true );
126 sprintf( altitude_m, "%.1f",
127 cur_fdm_state->get_Altitude() * SG_FEET_TO_METER );
130 int year = t->getGmt()->tm_year;
131 while ( year >= 100 ) { year -= 100; }
132 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
133 t->getGmt()->tm_mon+1, year );
136 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
137 if ( magdeg < 0.0 ) {
143 sprintf( magvar, "%.1f,%c", magdeg, dir );
145 // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E,A*XX
146 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s,A",
147 utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
148 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
150 // $GPGGA,HHMMSS,DDMM.MMMM,N,DDDMM.MMMM,W,1,NN,H.H,AAAA.A,M,GG.G,M,,*XX
151 sprintf( gga, "GPGGA,%s,%s,%s,1,08,0.9,%s,M,0.0,M,,",
152 utc, gga_lat, gga_lon, altitude_m );
153 sprintf( gga_sum, "%02X", calc_nmea_cksum(gga) );
155 "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
157 SG_LOG( SG_IO, SG_DEBUG, rmc );
158 SG_LOG( SG_IO, SG_DEBUG, gga );
159 SG_LOG( SG_IO, SG_DEBUG, gsa );
161 string nmea_sentence;
165 nmea_sentence += rmc;
166 nmea_sentence += "*";
167 nmea_sentence += rmc_sum;
168 nmea_sentence += "\n";
171 nmea_sentence += "$";
172 nmea_sentence += gga;
173 nmea_sentence += "*";
174 nmea_sentence += gga_sum;
175 nmea_sentence += "\n";
177 // GSA sentence (totally faked)
178 nmea_sentence += gsa;
179 nmea_sentence += "\n";
181 // cout << nmea_sentence;
183 length = nmea_sentence.length();
184 strncpy( buf, nmea_sentence.c_str(), length );
190 // parse NMEA message. messages will look something like the
193 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
194 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
196 bool FGNMEA::parse_message() {
197 SG_LOG( SG_IO, SG_INFO, "parse nmea message" );
200 msg = msg.substr( 0, length );
201 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
203 string::size_type begin_line, end_line, begin, end;
204 begin_line = begin = 0;
206 // extract out each line
207 end_line = msg.find("\n", begin_line);
208 while ( end_line != string::npos ) {
209 string line = msg.substr(begin_line, end_line - begin_line);
210 begin_line = end_line + 1;
211 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
214 string start = msg.substr(begin, 1);
216 SG_LOG( SG_IO, SG_INFO, " start = " << start );
219 end = msg.find(",", begin);
220 if ( end == string::npos ) {
224 string sentence = msg.substr(begin, end - begin);
226 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
228 double lon_deg, lon_min, lat_deg, lat_min;
229 double lon, lat, speed, heading, altitude;
231 if ( sentence == "GPRMC" ) {
233 end = msg.find(",", begin);
234 if ( end == string::npos ) {
238 string utc = msg.substr(begin, end - begin);
240 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
243 end = msg.find(",", begin);
244 if ( end == string::npos ) {
248 string junk = msg.substr(begin, end - begin);
250 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
253 end = msg.find(",", begin);
254 if ( end == string::npos ) {
258 string lat_str = msg.substr(begin, end - begin);
261 lat_deg = atof( lat_str.substr(0, 2).c_str() );
262 lat_min = atof( lat_str.substr(2).c_str() );
265 end = msg.find(",", begin);
266 if ( end == string::npos ) {
270 string lat_dir = msg.substr(begin, end - begin);
273 lat = lat_deg + ( lat_min / 60.0 );
274 if ( lat_dir == "S" ) {
278 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
279 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
282 end = msg.find(",", begin);
283 if ( end == string::npos ) {
287 string lon_str = msg.substr(begin, end - begin);
290 lon_deg = atof( lon_str.substr(0, 3).c_str() );
291 lon_min = atof( lon_str.substr(3).c_str() );
294 end = msg.find(",", begin);
295 if ( end == string::npos ) {
299 string lon_dir = msg.substr(begin, end - begin);
302 lon = lon_deg + ( lon_min / 60.0 );
303 if ( lon_dir == "W" ) {
307 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
308 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
311 double sl_radius, lat_geoc;
312 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
313 cur_fdm_state->get_Altitude(),
314 &sl_radius, &lat_geoc );
315 cur_fdm_state->set_Geocentric_Position( lat_geoc,
316 cur_fdm_state->get_Longitude(),
317 sl_radius + cur_fdm_state->get_Altitude() );
321 end = msg.find(",", begin);
322 if ( end == string::npos ) {
326 string speed_str = msg.substr(begin, end - begin);
328 speed = atof( speed_str.c_str() );
329 cur_fdm_state->set_V_calibrated_kts( speed );
330 // cur_fdm_state->set_V_ground_speed( speed );
331 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
334 end = msg.find(",", begin);
335 if ( end == string::npos ) {
339 string hdg_str = msg.substr(begin, end - begin);
341 heading = atof( hdg_str.c_str() );
342 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
343 cur_fdm_state->get_Theta(),
344 heading * SGD_DEGREES_TO_RADIANS );
345 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
346 } else if ( sentence == "GPGGA" ) {
348 end = msg.find(",", begin);
349 if ( end == string::npos ) {
353 string utc = msg.substr(begin, end - begin);
355 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
358 end = msg.find(",", begin);
359 if ( end == string::npos ) {
363 string lat_str = msg.substr(begin, end - begin);
366 lat_deg = atof( lat_str.substr(0, 2).c_str() );
367 lat_min = atof( lat_str.substr(2).c_str() );
370 end = msg.find(",", begin);
371 if ( end == string::npos ) {
375 string lat_dir = msg.substr(begin, end - begin);
378 lat = lat_deg + ( lat_min / 60.0 );
379 if ( lat_dir == "S" ) {
383 // cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
384 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
387 end = msg.find(",", begin);
388 if ( end == string::npos ) {
392 string lon_str = msg.substr(begin, end - begin);
395 lon_deg = atof( lon_str.substr(0, 3).c_str() );
396 lon_min = atof( lon_str.substr(3).c_str() );
399 end = msg.find(",", begin);
400 if ( end == string::npos ) {
404 string lon_dir = msg.substr(begin, end - begin);
407 lon = lon_deg + ( lon_min / 60.0 );
408 if ( lon_dir == "W" ) {
412 // cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
413 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
416 end = msg.find(",", begin);
417 if ( end == string::npos ) {
421 string junk = msg.substr(begin, end - begin);
423 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
426 end = msg.find(",", begin);
427 if ( end == string::npos ) {
431 junk = msg.substr(begin, end - begin);
433 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
436 end = msg.find(",", begin);
437 if ( end == string::npos ) {
441 junk = msg.substr(begin, end - begin);
443 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
446 end = msg.find(",", begin);
447 if ( end == string::npos ) {
451 string alt_str = msg.substr(begin, end - begin);
452 altitude = atof( alt_str.c_str() );
456 end = msg.find(",", begin);
457 if ( end == string::npos ) {
461 string alt_units = msg.substr(begin, end - begin);
464 if ( alt_units != "F" ) {
465 altitude *= SG_METER_TO_FEET;
468 cur_fdm_state->set_Altitude( altitude );
470 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
474 // printf("%.8f %.8f\n", lon, lat);
477 end_line = msg.find("\n", begin_line);
484 // open hailing frequencies
485 bool FGNMEA::open() {
486 if ( is_enabled() ) {
487 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
488 << "is already in use, ignoring" );
492 SGIOChannel *io = get_io_channel();
494 if ( ! io->open( get_direction() ) ) {
495 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
505 // process work for this port
506 bool FGNMEA::process() {
507 SGIOChannel *io = get_io_channel();
509 if ( get_direction() == SG_IO_OUT ) {
511 if ( ! io->write( buf, length ) ) {
512 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
515 } else if ( get_direction() == SG_IO_IN ) {
516 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
519 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
522 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
525 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
535 bool FGNMEA::close() {
536 SGIOChannel *io = get_io_channel();
538 set_enabled( false );
540 if ( ! io->close() ) {