]> git.mxchange.org Git - flightgear.git/blob - src/Network/nmea.cxx
I tested:
[flightgear.git] / src / Network / nmea.cxx
1 // nmea.cxx -- NMEA protocal class
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/math/sg_geodesy.hxx>
26 #include <simgear/io/iochannel.hxx>
27
28 #include <FDM/flight.hxx>
29 #include <Main/globals.hxx>
30
31 #include "nmea.hxx"
32
33
34 FGNMEA::FGNMEA() {
35 }
36
37 FGNMEA::~FGNMEA() {
38 }
39
40
41 // calculate the nmea check sum
42 static char calc_nmea_cksum(char *sentence) {
43     unsigned char sum = 0;
44     int i, len;
45
46     // cout << sentence << endl;
47
48     len = strlen(sentence);
49     sum = sentence[0];
50     for ( i = 1; i < len; i++ ) {
51         // cout << sentence[i];
52         sum ^= sentence[i];
53     }
54     // cout << endl;
55
56     // printf("sum = %02x\n", sum);
57     return sum;
58 }
59
60
61 // generate NMEA message
62 bool FGNMEA::gen_message() {
63     // cout << "generating nmea message" << endl;
64
65     char rmc[256], gga[256];
66     char rmc_sum[10], gga_sum[10];
67     char dir;
68     int deg;
69     double min;
70
71     SGTime *t = globals->get_time_params();
72
73     char utc[10];
74     sprintf( utc, "%02d%02d%02d", 
75              t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
76
77     char lat[20];
78     double latd = cur_fdm_state->get_Latitude() * RAD_TO_DEG;
79     if ( latd < 0.0 ) {
80         latd *= -1.0;
81         dir = 'S';
82     } else {
83         dir = 'N';
84     }
85     deg = (int)(latd);
86     min = (latd - (double)deg) * 60.0;
87     sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
88
89     char lon[20];
90     double lond = cur_fdm_state->get_Longitude() * RAD_TO_DEG;
91     if ( lond < 0.0 ) {
92         lond *= -1.0;
93         dir = 'W';
94     } else {
95         dir = 'E';
96     }
97     deg = (int)(lond);
98     min = (lond - (double)deg) * 60.0;
99     sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
100
101     char speed[10];
102     sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
103
104     char heading[10];
105     sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * RAD_TO_DEG );
106
107     char altitude_m[10];
108     sprintf( altitude_m, "%02d", 
109              (int)(cur_fdm_state->get_Altitude() * FEET_TO_METER) );
110
111     char altitude_ft[10];
112     sprintf( altitude_ft, "%02d", (int)cur_fdm_state->get_Altitude() );
113
114     char date[10];
115     sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday, 
116              t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
117
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) );
122
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) );
126
127
128     FG_LOG( FG_IO, FG_DEBUG, rmc );
129     FG_LOG( FG_IO, FG_DEBUG, gga );
130
131     string nmea_sentence;
132
133     // RMC sentence
134     nmea_sentence = "$";
135     nmea_sentence += rmc;
136     nmea_sentence += "*";
137     nmea_sentence += rmc_sum;
138     nmea_sentence += "\n";
139
140     // GGA sentence
141     nmea_sentence += "$";
142     nmea_sentence += gga;
143     nmea_sentence += "*";
144     nmea_sentence += gga_sum;
145     nmea_sentence += "\n";
146
147     cout << nmea_sentence;
148
149     length = nmea_sentence.length();
150     strncpy( buf, nmea_sentence.c_str(), length );
151
152     return true;
153 }
154
155
156 // parse NMEA message.  messages will look something like the
157 // following:
158 //
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
161
162 bool FGNMEA::parse_message() {
163     FG_LOG( FG_IO, FG_INFO, "parse nmea message" );
164
165     string msg = buf;
166     msg = msg.substr( 0, length );
167     FG_LOG( FG_IO, FG_INFO, "entire message = " << msg );
168
169     string::size_type begin_line, end_line, begin, end;
170     begin_line = begin = 0;
171
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 );
178
179         // leading character
180         string start = msg.substr(begin, 1);
181         ++begin;
182         FG_LOG( FG_IO, FG_INFO, "  start = " << start );
183
184         // sentence
185         end = msg.find(",", begin);
186         if ( end == string::npos ) {
187             return false;
188         }
189     
190         string sentence = msg.substr(begin, end - begin);
191         begin = end + 1;
192         FG_LOG( FG_IO, FG_INFO, "  sentence = " << sentence );
193
194         double lon_deg, lon_min, lat_deg, lat_min;
195         double lon, lat, speed, heading, altitude;
196
197         if ( sentence == "GPRMC" ) {
198             // time
199             end = msg.find(",", begin);
200             if ( end == string::npos ) {
201                 return false;
202             }
203     
204             string utc = msg.substr(begin, end - begin);
205             begin = end + 1;
206             FG_LOG( FG_IO, FG_INFO, "  utc = " << utc );
207
208             // junk
209             end = msg.find(",", begin);
210             if ( end == string::npos ) {
211                 return false;
212             }
213     
214             string junk = msg.substr(begin, end - begin);
215             begin = end + 1;
216             FG_LOG( FG_IO, FG_INFO, "  junk = " << junk );
217
218             // lat val
219             end = msg.find(",", begin);
220             if ( end == string::npos ) {
221                 return false;
222             }
223     
224             string lat_str = msg.substr(begin, end - begin);
225             begin = end + 1;
226
227             lat_deg = atof( lat_str.substr(0, 2).c_str() );
228             lat_min = atof( lat_str.substr(2).c_str() );
229
230             // lat dir
231             end = msg.find(",", begin);
232             if ( end == string::npos ) {
233                 return false;
234             }
235     
236             string lat_dir = msg.substr(begin, end - begin);
237             begin = end + 1;
238
239             lat = lat_deg + ( lat_min / 60.0 );
240             if ( lat_dir == "S" ) {
241                 lat *= -1;
242             }
243
244             cur_fdm_state->set_Latitude( lat * DEG_TO_RAD );
245             FG_LOG( FG_IO, FG_INFO, "  lat = " << lat );
246
247             // lon val
248             end = msg.find(",", begin);
249             if ( end == string::npos ) {
250                 return false;
251             }
252     
253             string lon_str = msg.substr(begin, end - begin);
254             begin = end + 1;
255
256             lon_deg = atof( lon_str.substr(0, 3).c_str() );
257             lon_min = atof( lon_str.substr(3).c_str() );
258
259             // lon dir
260             end = msg.find(",", begin);
261             if ( end == string::npos ) {
262                 return false;
263             }
264     
265             string lon_dir = msg.substr(begin, end - begin);
266             begin = end + 1;
267
268             lon = lon_deg + ( lon_min / 60.0 );
269             if ( lon_dir == "W" ) {
270                 lon *= -1;
271             }
272
273             cur_fdm_state->set_Longitude( lon * DEG_TO_RAD );
274             FG_LOG( FG_IO, FG_INFO, "  lon = " << lon );
275
276 #if 0
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() );
284 #endif
285
286             // speed
287             end = msg.find(",", begin);
288             if ( end == string::npos ) {
289                 return false;
290             }
291     
292             string speed_str = msg.substr(begin, end - begin);
293             begin = end + 1;
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 );
298
299             // heading
300             end = msg.find(",", begin);
301             if ( end == string::npos ) {
302                 return false;
303             }
304     
305             string hdg_str = msg.substr(begin, end - begin);
306             begin = end + 1;
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" ) {
313             // time
314             end = msg.find(",", begin);
315             if ( end == string::npos ) {
316                 return false;
317             }
318     
319             string utc = msg.substr(begin, end - begin);
320             begin = end + 1;
321             FG_LOG( FG_IO, FG_INFO, "  utc = " << utc );
322
323             // lat val
324             end = msg.find(",", begin);
325             if ( end == string::npos ) {
326                 return false;
327             }
328     
329             string lat_str = msg.substr(begin, end - begin);
330             begin = end + 1;
331
332             lat_deg = atof( lat_str.substr(0, 2).c_str() );
333             lat_min = atof( lat_str.substr(2).c_str() );
334
335             // lat dir
336             end = msg.find(",", begin);
337             if ( end == string::npos ) {
338                 return false;
339             }
340     
341             string lat_dir = msg.substr(begin, end - begin);
342             begin = end + 1;
343
344             lat = lat_deg + ( lat_min / 60.0 );
345             if ( lat_dir == "S" ) {
346                 lat *= -1;
347             }
348
349             // cur_fdm_state->set_Latitude( lat * DEG_TO_RAD );
350             FG_LOG( FG_IO, FG_INFO, "  lat = " << lat );
351
352             // lon val
353             end = msg.find(",", begin);
354             if ( end == string::npos ) {
355                 return false;
356             }
357     
358             string lon_str = msg.substr(begin, end - begin);
359             begin = end + 1;
360
361             lon_deg = atof( lon_str.substr(0, 3).c_str() );
362             lon_min = atof( lon_str.substr(3).c_str() );
363
364             // lon dir
365             end = msg.find(",", begin);
366             if ( end == string::npos ) {
367                 return false;
368             }
369     
370             string lon_dir = msg.substr(begin, end - begin);
371             begin = end + 1;
372
373             lon = lon_deg + ( lon_min / 60.0 );
374             if ( lon_dir == "W" ) {
375                 lon *= -1;
376             }
377
378             // cur_fdm_state->set_Longitude( lon * DEG_TO_RAD );
379             FG_LOG( FG_IO, FG_INFO, "  lon = " << lon );
380
381             // junk
382             end = msg.find(",", begin);
383             if ( end == string::npos ) {
384                 return false;
385             }
386     
387             string junk = msg.substr(begin, end - begin);
388             begin = end + 1;
389             FG_LOG( FG_IO, FG_INFO, "  junk = " << junk );
390
391             // junk
392             end = msg.find(",", begin);
393             if ( end == string::npos ) {
394                 return false;
395             }
396     
397             junk = msg.substr(begin, end - begin);
398             begin = end + 1;
399             FG_LOG( FG_IO, FG_INFO, "  junk = " << junk );
400
401             // junk
402             end = msg.find(",", begin);
403             if ( end == string::npos ) {
404                 return false;
405             }
406     
407             junk = msg.substr(begin, end - begin);
408             begin = end + 1;
409             FG_LOG( FG_IO, FG_INFO, "  junk = " << junk );
410
411             // altitude
412             end = msg.find(",", begin);
413             if ( end == string::npos ) {
414                 return false;
415             }
416     
417             string alt_str = msg.substr(begin, end - begin);
418             altitude = atof( alt_str.c_str() );
419             begin = end + 1;
420
421             // altitude units
422             end = msg.find(",", begin);
423             if ( end == string::npos ) {
424                 return false;
425             }
426     
427             string alt_units = msg.substr(begin, end - begin);
428             begin = end + 1;
429
430             if ( alt_units != "F" ) {
431                 altitude *= METER_TO_FEET;
432             }
433
434             cur_fdm_state->set_Altitude( altitude );
435     
436             FG_LOG( FG_IO, FG_INFO, " altitude  = " << altitude );
437
438         }
439
440         // printf("%.8f %.8f\n", lon, lat);
441
442         begin = begin_line;
443         end_line = msg.find("\n", begin_line);
444     }
445
446     return true;
447 }
448
449
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" );
455         return false;
456     }
457
458     SGIOChannel *io = get_io_channel();
459
460     if ( ! io->open( get_direction() ) ) {
461         FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
462         return false;
463     }
464
465     set_enabled( true );
466
467     return true;
468 }
469
470
471 // process work for this port
472 bool FGNMEA::process() {
473     SGIOChannel *io = get_io_channel();
474
475     if ( get_direction() == SG_IO_OUT ) {
476         gen_message();
477         if ( ! io->write( buf, length ) ) {
478             FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
479             return false;
480         }
481     } else if ( get_direction() == SG_IO_IN ) {
482         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
483             parse_message();
484         } else {
485             FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
486             return false;
487         }
488         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
489             parse_message();
490         } else {
491             FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
492             return false;
493         }
494     }
495
496     return true;
497 }
498
499
500 // close the channel
501 bool FGNMEA::close() {
502     SGIOChannel *io = get_io_channel();
503
504     set_enabled( false );
505
506     if ( ! io->close() ) {
507         return false;
508     }
509
510     return true;
511 }