]> git.mxchange.org Git - flightgear.git/blob - src/Network/nmea.cxx
A lot of code reorganization relating to moving some core code from
[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/fg_geodesy.hxx>
26 #include <simgear/timing/fg_time.hxx>
27
28 #include <FDM/flight.hxx>
29
30 #include "iochannel.hxx"
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     FGTime *t = FGTime::cur_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             double sl_radius, lat_geoc;
277             fgGeodToGeoc( cur_fdm_state->get_Latitude(), 
278                           cur_fdm_state->get_Altitude(), 
279                           &sl_radius, &lat_geoc );
280             cur_fdm_state->set_Geocentric_Position( lat_geoc, 
281                            cur_fdm_state->get_Longitude(), 
282                            sl_radius + cur_fdm_state->get_Altitude() );
283
284             // speed
285             end = msg.find(",", begin);
286             if ( end == string::npos ) {
287                 return false;
288             }
289     
290             string speed_str = msg.substr(begin, end - begin);
291             begin = end + 1;
292             speed = atof( speed_str.c_str() );
293             cur_fdm_state->set_V_equiv_kts( speed );
294             cur_fdm_state->set_V_ground_speed( speed );
295             FG_LOG( FG_IO, FG_INFO, "  speed = " << speed );
296
297             // heading
298             end = msg.find(",", begin);
299             if ( end == string::npos ) {
300                 return false;
301             }
302     
303             string hdg_str = msg.substr(begin, end - begin);
304             begin = end + 1;
305             heading = atof( hdg_str.c_str() );
306             cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(), 
307                                              cur_fdm_state->get_Theta(), 
308                                              heading * DEG_TO_RAD );
309             FG_LOG( FG_IO, FG_INFO, "  heading = " << heading );
310         } else if ( sentence == "GPGGA" ) {
311             // time
312             end = msg.find(",", begin);
313             if ( end == string::npos ) {
314                 return false;
315             }
316     
317             string utc = msg.substr(begin, end - begin);
318             begin = end + 1;
319             FG_LOG( FG_IO, FG_INFO, "  utc = " << utc );
320
321             // lat val
322             end = msg.find(",", begin);
323             if ( end == string::npos ) {
324                 return false;
325             }
326     
327             string lat_str = msg.substr(begin, end - begin);
328             begin = end + 1;
329
330             lat_deg = atof( lat_str.substr(0, 2).c_str() );
331             lat_min = atof( lat_str.substr(2).c_str() );
332
333             // lat dir
334             end = msg.find(",", begin);
335             if ( end == string::npos ) {
336                 return false;
337             }
338     
339             string lat_dir = msg.substr(begin, end - begin);
340             begin = end + 1;
341
342             lat = lat_deg + ( lat_min / 60.0 );
343             if ( lat_dir == "S" ) {
344                 lat *= -1;
345             }
346
347             // cur_fdm_state->set_Latitude( lat * DEG_TO_RAD );
348             FG_LOG( FG_IO, FG_INFO, "  lat = " << lat );
349
350             // lon val
351             end = msg.find(",", begin);
352             if ( end == string::npos ) {
353                 return false;
354             }
355     
356             string lon_str = msg.substr(begin, end - begin);
357             begin = end + 1;
358
359             lon_deg = atof( lon_str.substr(0, 3).c_str() );
360             lon_min = atof( lon_str.substr(3).c_str() );
361
362             // lon dir
363             end = msg.find(",", begin);
364             if ( end == string::npos ) {
365                 return false;
366             }
367     
368             string lon_dir = msg.substr(begin, end - begin);
369             begin = end + 1;
370
371             lon = lon_deg + ( lon_min / 60.0 );
372             if ( lon_dir == "W" ) {
373                 lon *= -1;
374             }
375
376             // cur_fdm_state->set_Longitude( lon * DEG_TO_RAD );
377             FG_LOG( FG_IO, FG_INFO, "  lon = " << lon );
378
379             // junk
380             end = msg.find(",", begin);
381             if ( end == string::npos ) {
382                 return false;
383             }
384     
385             string junk = msg.substr(begin, end - begin);
386             begin = end + 1;
387             FG_LOG( FG_IO, FG_INFO, "  junk = " << junk );
388
389             // junk
390             end = msg.find(",", begin);
391             if ( end == string::npos ) {
392                 return false;
393             }
394     
395             junk = msg.substr(begin, end - begin);
396             begin = end + 1;
397             FG_LOG( FG_IO, FG_INFO, "  junk = " << junk );
398
399             // junk
400             end = msg.find(",", begin);
401             if ( end == string::npos ) {
402                 return false;
403             }
404     
405             junk = msg.substr(begin, end - begin);
406             begin = end + 1;
407             FG_LOG( FG_IO, FG_INFO, "  junk = " << junk );
408
409             // altitude
410             end = msg.find(",", begin);
411             if ( end == string::npos ) {
412                 return false;
413             }
414     
415             string alt_str = msg.substr(begin, end - begin);
416             altitude = atof( alt_str.c_str() );
417             begin = end + 1;
418
419             // altitude units
420             end = msg.find(",", begin);
421             if ( end == string::npos ) {
422                 return false;
423             }
424     
425             string alt_units = msg.substr(begin, end - begin);
426             begin = end + 1;
427
428             if ( alt_units != "F" ) {
429                 altitude *= METER_TO_FEET;
430             }
431
432             cur_fdm_state->set_Altitude( altitude );
433     
434             FG_LOG( FG_IO, FG_INFO, " altitude  = " << altitude );
435
436         }
437
438         // printf("%.8f %.8f\n", lon, lat);
439
440         begin = begin_line;
441         end_line = msg.find("\n", begin_line);
442     }
443
444     return true;
445 }
446
447
448 // open hailing frequencies
449 bool FGNMEA::open() {
450     if ( is_enabled() ) {
451         FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel " 
452                 << "is already in use, ignoring" );
453         return false;
454     }
455
456     FGIOChannel *io = get_io_channel();
457
458     if ( ! io->open( get_direction() ) ) {
459         FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
460         return false;
461     }
462
463     set_enabled( true );
464
465     return true;
466 }
467
468
469 // process work for this port
470 bool FGNMEA::process() {
471     FGIOChannel *io = get_io_channel();
472
473     if ( get_direction() == out ) {
474         gen_message();
475         if ( ! io->write( buf, length ) ) {
476             FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
477             return false;
478         }
479     } else if ( get_direction() == in ) {
480         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
481             parse_message();
482         } else {
483             FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
484             return false;
485         }
486         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
487             parse_message();
488         } else {
489             FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
490             return false;
491         }
492     }
493
494     return true;
495 }
496
497
498 // close the channel
499 bool FGNMEA::close() {
500     FGIOChannel *io = get_io_channel();
501
502     set_enabled( false );
503
504     if ( ! io->close() ) {
505         return false;
506     }
507
508     return true;
509 }