]> git.mxchange.org Git - flightgear.git/blob - src/Network/nmea.cxx
Remove hard-coded values wherever possible;
[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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <cstring>
28
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>
33
34 #include <FDM/flightProperties.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
37
38 #include "nmea.hxx"
39
40 FGNMEA::FGNMEA() {
41   fdm = new FlightProperties();
42 }
43
44 FGNMEA::~FGNMEA() {
45   delete fdm;
46 }
47
48
49 // calculate the nmea check sum
50 static char calc_nmea_cksum(char *sentence) {
51     unsigned char sum = 0;
52     int i, len;
53
54     // cout << sentence << endl;
55
56     len = strlen(sentence);
57     sum = sentence[0];
58     for ( i = 1; i < len; i++ ) {
59         // cout << sentence[i];
60         sum ^= sentence[i];
61     }
62     // cout << endl;
63
64     // printf("sum = %02x\n", sum);
65     return sum;
66 }
67
68
69 // generate NMEA message
70 bool FGNMEA::gen_message() {
71     // cout << "generating nmea message" << endl;
72
73     char rmc[256], gga[256], gsa[256];
74     char rmc_sum[10], gga_sum[10];
75     char dir;
76     int deg;
77     double min;
78
79     SGTime *t = globals->get_time_params();
80
81     char utc[10];
82     sprintf( utc, "%02d%02d%02d", 
83              t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
84
85     char gga_lat[20], rmc_lat[20];
86     double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
87     if ( latd < 0.0 ) {
88         latd = -latd;
89         dir = 'S';
90     } else {
91         dir = 'N';
92     }
93     deg = (int)(latd);
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);
97
98     char gga_lon[20], rmc_lon[20];
99     double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
100     if ( lond < 0.0 ) {
101         lond = -lond;
102         dir = 'W';
103     } else {
104         dir = 'E';
105     }
106     deg = (int)(lond);
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);
110
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;
116     char speed[10];
117     sprintf( speed, "%.1f", kts );
118
119     double hdg_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
120     if ( hdg_true < 0 ) {
121       hdg_true += 360.0;
122     }
123     char heading[10];
124     sprintf( heading, "%.1f", hdg_true );
125
126     char altitude_m[10];
127     sprintf( altitude_m, "%.1f", 
128              fdm->get_Altitude() * SG_FEET_TO_METER );
129
130     char date[10];
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 );
135
136     char magvar[10];
137     float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
138     if ( magdeg < 0.0 ) {
139         magdeg = -magdeg;
140         dir = 'W';
141     } else {
142         dir = 'E';
143     }
144     sprintf( magvar, "%.1f,%c", magdeg, dir );
145  
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) );
150
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) );
155     sprintf( gsa, "%s",
156              "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
157
158     SG_LOG( SG_IO, SG_DEBUG, rmc );
159     SG_LOG( SG_IO, SG_DEBUG, gga );
160     SG_LOG( SG_IO, SG_DEBUG, gsa );
161
162     string nmea_sentence;
163
164     // RMC sentence
165     nmea_sentence = "$";
166     nmea_sentence += rmc;
167     nmea_sentence += "*";
168     nmea_sentence += rmc_sum;
169     nmea_sentence += "\n";
170
171     // GGA sentence
172     nmea_sentence += "$";
173     nmea_sentence += gga;
174     nmea_sentence += "*";
175     nmea_sentence += gga_sum;
176     nmea_sentence += "\n";
177
178     // GSA sentence (totally faked)
179     nmea_sentence += gsa;
180     nmea_sentence += "\n";
181
182     // cout << nmea_sentence;
183
184     length = nmea_sentence.length();
185     strncpy( buf, nmea_sentence.c_str(), length );
186
187     return true;
188 }
189
190
191 // parse NMEA message.  messages will look something like the
192 // following:
193 //
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
196
197 bool FGNMEA::parse_message() {
198     SG_LOG( SG_IO, SG_INFO, "parse nmea message" );
199
200     string msg = buf;
201     msg = msg.substr( 0, length );
202     SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
203
204     string::size_type begin_line, end_line, begin, end;
205     begin_line = begin = 0;
206
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 );
213
214         // leading character
215         string start = msg.substr(begin, 1);
216         ++begin;
217         SG_LOG( SG_IO, SG_INFO, "  start = " << start );
218
219         // sentence
220         end = msg.find(",", begin);
221         if ( end == string::npos ) {
222             return false;
223         }
224     
225         string sentence = msg.substr(begin, end - begin);
226         begin = end + 1;
227         SG_LOG( SG_IO, SG_INFO, "  sentence = " << sentence );
228
229         double lon_deg, lon_min, lat_deg, lat_min;
230         double lon, lat, speed, heading, altitude;
231
232         if ( sentence == "GPRMC" ) {
233             // time
234             end = msg.find(",", begin);
235             if ( end == string::npos ) {
236                 return false;
237             }
238     
239             string utc = msg.substr(begin, end - begin);
240             begin = end + 1;
241             SG_LOG( SG_IO, SG_INFO, "  utc = " << utc );
242
243             // junk
244             end = msg.find(",", begin);
245             if ( end == string::npos ) {
246                 return false;
247             }
248     
249             string junk = msg.substr(begin, end - begin);
250             begin = end + 1;
251             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
252
253             // lat val
254             end = msg.find(",", begin);
255             if ( end == string::npos ) {
256                 return false;
257             }
258     
259             string lat_str = msg.substr(begin, end - begin);
260             begin = end + 1;
261
262             lat_deg = atof( lat_str.substr(0, 2).c_str() );
263             lat_min = atof( lat_str.substr(2).c_str() );
264
265             // lat dir
266             end = msg.find(",", begin);
267             if ( end == string::npos ) {
268                 return false;
269             }
270     
271             string lat_dir = msg.substr(begin, end - begin);
272             begin = end + 1;
273
274             lat = lat_deg + ( lat_min / 60.0 );
275             if ( lat_dir == "S" ) {
276                 lat *= -1;
277             }
278
279             fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
280             SG_LOG( SG_IO, SG_INFO, "  lat = " << lat );
281
282             // lon val
283             end = msg.find(",", begin);
284             if ( end == string::npos ) {
285                 return false;
286             }
287     
288             string lon_str = msg.substr(begin, end - begin);
289             begin = end + 1;
290
291             lon_deg = atof( lon_str.substr(0, 3).c_str() );
292             lon_min = atof( lon_str.substr(3).c_str() );
293
294             // lon dir
295             end = msg.find(",", begin);
296             if ( end == string::npos ) {
297                 return false;
298             }
299     
300             string lon_dir = msg.substr(begin, end - begin);
301             begin = end + 1;
302
303             lon = lon_deg + ( lon_min / 60.0 );
304             if ( lon_dir == "W" ) {
305                 lon *= -1;
306             }
307
308             fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
309             SG_LOG( SG_IO, SG_INFO, "  lon = " << lon );
310
311 #if 0
312             double sl_radius, lat_geoc;
313             sgGeodToGeoc( fdm->get_Latitude(), 
314                           fdm->get_Altitude(), 
315                           &sl_radius, &lat_geoc );
316             fdm->set_Geocentric_Position( lat_geoc, 
317                            fdm->get_Longitude(), 
318                            sl_radius + fdm->get_Altitude() );
319 #endif
320
321             // speed
322             end = msg.find(",", begin);
323             if ( end == string::npos ) {
324                 return false;
325             }
326     
327             string speed_str = msg.substr(begin, end - begin);
328             begin = end + 1;
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 );
333
334             // heading
335             end = msg.find(",", begin);
336             if ( end == string::npos ) {
337                 return false;
338             }
339     
340             string hdg_str = msg.substr(begin, end - begin);
341             begin = end + 1;
342             heading = atof( hdg_str.c_str() );
343             fdm->set_Euler_Angles( fdm->get_Phi(), 
344                                              fdm->get_Theta(), 
345                                              heading * SGD_DEGREES_TO_RADIANS );
346             SG_LOG( SG_IO, SG_INFO, "  heading = " << heading );
347         } else if ( sentence == "GPGGA" ) {
348             // time
349             end = msg.find(",", begin);
350             if ( end == string::npos ) {
351                 return false;
352             }
353     
354             string utc = msg.substr(begin, end - begin);
355             begin = end + 1;
356             SG_LOG( SG_IO, SG_INFO, "  utc = " << utc );
357
358             // lat val
359             end = msg.find(",", begin);
360             if ( end == string::npos ) {
361                 return false;
362             }
363     
364             string lat_str = msg.substr(begin, end - begin);
365             begin = end + 1;
366
367             lat_deg = atof( lat_str.substr(0, 2).c_str() );
368             lat_min = atof( lat_str.substr(2).c_str() );
369
370             // lat dir
371             end = msg.find(",", begin);
372             if ( end == string::npos ) {
373                 return false;
374             }
375     
376             string lat_dir = msg.substr(begin, end - begin);
377             begin = end + 1;
378
379             lat = lat_deg + ( lat_min / 60.0 );
380             if ( lat_dir == "S" ) {
381                 lat *= -1;
382             }
383
384             // fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
385             SG_LOG( SG_IO, SG_INFO, "  lat = " << lat );
386
387             // lon val
388             end = msg.find(",", begin);
389             if ( end == string::npos ) {
390                 return false;
391             }
392     
393             string lon_str = msg.substr(begin, end - begin);
394             begin = end + 1;
395
396             lon_deg = atof( lon_str.substr(0, 3).c_str() );
397             lon_min = atof( lon_str.substr(3).c_str() );
398
399             // lon dir
400             end = msg.find(",", begin);
401             if ( end == string::npos ) {
402                 return false;
403             }
404     
405             string lon_dir = msg.substr(begin, end - begin);
406             begin = end + 1;
407
408             lon = lon_deg + ( lon_min / 60.0 );
409             if ( lon_dir == "W" ) {
410                 lon *= -1;
411             }
412
413             // fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
414             SG_LOG( SG_IO, SG_INFO, "  lon = " << lon );
415
416             // junk
417             end = msg.find(",", begin);
418             if ( end == string::npos ) {
419                 return false;
420             }
421     
422             string junk = msg.substr(begin, end - begin);
423             begin = end + 1;
424             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
425
426             // junk
427             end = msg.find(",", begin);
428             if ( end == string::npos ) {
429                 return false;
430             }
431     
432             junk = msg.substr(begin, end - begin);
433             begin = end + 1;
434             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
435
436             // junk
437             end = msg.find(",", begin);
438             if ( end == string::npos ) {
439                 return false;
440             }
441     
442             junk = msg.substr(begin, end - begin);
443             begin = end + 1;
444             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
445
446             // altitude
447             end = msg.find(",", begin);
448             if ( end == string::npos ) {
449                 return false;
450             }
451     
452             string alt_str = msg.substr(begin, end - begin);
453             altitude = atof( alt_str.c_str() );
454             begin = end + 1;
455
456             // altitude units
457             end = msg.find(",", begin);
458             if ( end == string::npos ) {
459                 return false;
460             }
461     
462             string alt_units = msg.substr(begin, end - begin);
463             begin = end + 1;
464
465             if ( alt_units != "F" ) {
466                 altitude *= SG_METER_TO_FEET;
467             }
468
469             fdm->set_Altitude( altitude );
470     
471             SG_LOG( SG_IO, SG_INFO, " altitude  = " << altitude );
472
473         }
474
475         // printf("%.8f %.8f\n", lon, lat);
476
477         begin = begin_line;
478         end_line = msg.find("\n", begin_line);
479     }
480
481     return true;
482 }
483
484
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" );
490         return false;
491     }
492
493     SGIOChannel *io = get_io_channel();
494
495     if ( ! io->open( get_direction() ) ) {
496         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
497         return false;
498     }
499
500     set_enabled( true );
501
502     return true;
503 }
504
505
506 // process work for this port
507 bool FGNMEA::process() {
508     SGIOChannel *io = get_io_channel();
509
510     if ( get_direction() == SG_IO_OUT ) {
511         gen_message();
512         if ( ! io->write( buf, length ) ) {
513             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
514             return false;
515         }
516     } else if ( get_direction() == SG_IO_IN ) {
517         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
518             parse_message();
519         } else {
520             SG_LOG( SG_IO, SG_WARN, "Error reading data." );
521             return false;
522         }
523         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
524             parse_message();
525         } else {
526             SG_LOG( SG_IO, SG_WARN, "Error reading data." );
527             return false;
528         }
529     }
530
531     return true;
532 }
533
534
535 // close the channel
536 bool FGNMEA::close() {
537     SGIOChannel *io = get_io_channel();
538
539     set_enabled( false );
540
541     if ( ! io->close() ) {
542         return false;
543     }
544
545     return true;
546 }