]> git.mxchange.org Git - flightgear.git/blob - src/Network/garmin.cxx
NavDisplay - custom symbol support enabled.
[flightgear.git] / src / Network / garmin.cxx
1 // garmin.cxx -- Garmin 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 <iostream>
28 #include <cstdlib>
29 #include <cstring>
30
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
33 #include <simgear/io/iochannel.hxx>
34 #include <simgear/timing/sg_time.hxx>
35
36 #include <FDM/flightProperties.hxx>
37 #include <Main/fg_props.hxx>
38 #include <Main/globals.hxx>
39
40 #include "garmin.hxx"
41
42 using std::string;
43
44 FGGarmin::FGGarmin() {
45   fdm = new FlightProperties;
46 }
47
48 FGGarmin::~FGGarmin() {
49   delete fdm;
50 }
51
52
53 // calculate the garmin check sum
54 static char calc_nmea_cksum(char *sentence) {
55     unsigned char sum = 0;
56     int i, len;
57
58     // cout << sentence << endl;
59
60     len = strlen(sentence);
61     sum = sentence[0];
62     for ( i = 1; i < len; i++ ) {
63         // cout << sentence[i];
64         sum ^= sentence[i];
65     }
66     // cout << endl;
67
68     // printf("sum = %02x\n", sum);
69     return sum;
70 }
71
72
73 // generate Garmin message
74 bool FGGarmin::gen_message() {
75     // cout << "generating garmin message" << endl;
76
77     char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256], gsa[256];
78     char dir;
79     int deg;
80     double min;
81
82     SGTime *t = globals->get_time_params();
83
84     char utc[10];
85     sprintf( utc, "%02d%02d%02d", 
86              t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
87
88     char rmc_lat[20];
89     double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
90     if ( latd < 0.0 ) {
91         latd = -latd;
92         dir = 'S';
93     } else {
94         dir = 'N';
95     }
96     deg = (int)(latd);
97     min = (latd - (double)deg) * 60.0;
98     sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
99
100     char rmc_lon[20];
101     double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
102     if ( lond < 0.0 ) {
103         lond = -lond;
104         dir = 'W';
105     } else {
106         dir = 'E';
107     }
108     deg = (int)(lond);
109     min = (lond - (double)deg) * 60.0;
110     sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
111
112     char speed[10];
113     sprintf( speed, "%05.1f", fdm->get_V_equiv_kts() );
114
115     char heading[10];
116     sprintf( heading, "%05.1f", fdm->get_Psi() * SGD_RADIANS_TO_DEGREES );
117
118     char altitude_m[10];
119     sprintf( altitude_m, "%02d", 
120              (int)(fdm->get_Altitude() * SG_FEET_TO_METER) );
121
122     char date[10];
123     int year = t->getGmt()->tm_year;
124     while ( year >= 100 ) { year -= 100; }
125     sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday, 
126              t->getGmt()->tm_mon+1, year );
127
128     char magvar[10];
129     float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
130     if ( magdeg < 0.0 ) {
131         magdeg = -magdeg;
132         dir = 'W';
133     } else {
134         dir = 'E';
135     }
136     sprintf( magvar, "%05.1f,%c", magdeg, dir );
137
138     // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
139     sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s",
140              utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
141     sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
142
143     // sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
144     //          utc, lat, lon, altitude_m );
145
146     sprintf( rmz, "PGRMZ,%s,M,3", altitude_m );
147     sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
148
149     sprintf( gsa, "%s",
150              "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
151
152     SG_LOG( SG_IO, SG_DEBUG, rmc );
153     SG_LOG( SG_IO, SG_DEBUG, rmz );
154     SG_LOG( SG_IO, SG_DEBUG, gsa );
155
156     string garmin_sentence;
157
158     // RMC sentence
159     garmin_sentence = "$";
160     garmin_sentence += rmc;
161     garmin_sentence += "*";
162     garmin_sentence += rmc_sum;
163     garmin_sentence += "\r\n";
164
165     // RMZ sentence (garmin proprietary)
166     garmin_sentence += "$";
167     garmin_sentence += rmz;
168     garmin_sentence += "*";
169     garmin_sentence += rmz_sum;
170     garmin_sentence += "\r\n";
171
172     // GSA sentence (totally faked)
173     garmin_sentence += gsa;
174     garmin_sentence += "\r\n";
175
176     std::cout << garmin_sentence;
177
178     length = garmin_sentence.length();
179     strncpy( buf, garmin_sentence.c_str(), length );
180
181     return true;
182 }
183
184
185 // parse Garmin message
186 bool FGGarmin::parse_message() {
187     SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
188
189     string msg = buf;
190     msg = msg.substr( 0, length );
191     SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
192
193     string::size_type begin_line, end_line, begin, end;
194     begin_line = begin = 0;
195
196     // extract out each line
197     end_line = msg.find("\n", begin_line);
198     while ( end_line != string::npos ) {
199         string line = msg.substr(begin_line, end_line - begin_line);
200         begin_line = end_line + 1;
201         SG_LOG( SG_IO, SG_INFO, "  input line = " << line );
202
203         // leading character
204         string start = msg.substr(begin, 1);
205         ++begin;
206         SG_LOG( SG_IO, SG_INFO, "  start = " << start );
207
208         // sentence
209         end = msg.find(",", begin);
210         if ( end == string::npos ) {
211             return false;
212         }
213     
214         string sentence = msg.substr(begin, end - begin);
215         begin = end + 1;
216         SG_LOG( SG_IO, SG_INFO, "  sentence = " << sentence );
217
218         double lon_deg, lon_min, lat_deg, lat_min;
219         double lon, lat, speed, heading, altitude;
220
221         if ( sentence == "GPRMC" ) {
222             // time
223             end = msg.find(",", begin);
224             if ( end == string::npos ) {
225                 return false;
226             }
227     
228             string utc = msg.substr(begin, end - begin);
229             begin = end + 1;
230             SG_LOG( SG_IO, SG_INFO, "  utc = " << utc );
231
232             // junk
233             end = msg.find(",", begin);
234             if ( end == string::npos ) {
235                 return false;
236             }
237     
238             string junk = msg.substr(begin, end - begin);
239             begin = end + 1;
240             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
241
242             // lat val
243             end = msg.find(",", begin);
244             if ( end == string::npos ) {
245                 return false;
246             }
247     
248             string lat_str = msg.substr(begin, end - begin);
249             begin = end + 1;
250
251             lat_deg = atof( lat_str.substr(0, 2).c_str() );
252             lat_min = atof( lat_str.substr(2).c_str() );
253
254             // lat dir
255             end = msg.find(",", begin);
256             if ( end == string::npos ) {
257                 return false;
258             }
259     
260             string lat_dir = msg.substr(begin, end - begin);
261             begin = end + 1;
262
263             lat = lat_deg + ( lat_min / 60.0 );
264             if ( lat_dir == "S" ) {
265                 lat *= -1;
266             }
267
268             fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
269             SG_LOG( SG_IO, SG_INFO, "  lat = " << lat );
270
271             // lon val
272             end = msg.find(",", begin);
273             if ( end == string::npos ) {
274                 return false;
275             }
276     
277             string lon_str = msg.substr(begin, end - begin);
278             begin = end + 1;
279
280             lon_deg = atof( lon_str.substr(0, 3).c_str() );
281             lon_min = atof( lon_str.substr(3).c_str() );
282
283             // lon dir
284             end = msg.find(",", begin);
285             if ( end == string::npos ) {
286                 return false;
287             }
288     
289             string lon_dir = msg.substr(begin, end - begin);
290             begin = end + 1;
291
292             lon = lon_deg + ( lon_min / 60.0 );
293             if ( lon_dir == "W" ) {
294                 lon *= -1;
295             }
296
297             fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
298             SG_LOG( SG_IO, SG_INFO, "  lon = " << lon );
299
300 #if 0
301             double sl_radius, lat_geoc;
302             sgGeodToGeoc( fdm->get_Latitude(), 
303                           fdm->get_Altitude(), 
304                           &sl_radius, &lat_geoc );
305             fdm->set_Geocentric_Position( lat_geoc, 
306                            fdm->get_Longitude(), 
307                            sl_radius + fdm->get_Altitude() );
308 #endif
309
310             // speed
311             end = msg.find(",", begin);
312             if ( end == string::npos ) {
313                 return false;
314             }
315     
316             string speed_str = msg.substr(begin, end - begin);
317             begin = end + 1;
318             speed = atof( speed_str.c_str() );
319             fdm->set_V_calibrated_kts( speed );
320             // fdm->set_V_ground_speed( speed );
321             SG_LOG( SG_IO, SG_INFO, "  speed = " << speed );
322
323             // heading
324             end = msg.find(",", begin);
325             if ( end == string::npos ) {
326                 return false;
327             }
328     
329             string hdg_str = msg.substr(begin, end - begin);
330             begin = end + 1;
331             heading = atof( hdg_str.c_str() );
332             fdm->set_Euler_Angles( fdm->get_Phi(), 
333                                              fdm->get_Theta(), 
334                                              heading * SGD_DEGREES_TO_RADIANS );
335             SG_LOG( SG_IO, SG_INFO, "  heading = " << heading );
336         } else if ( sentence == "PGRMZ" ) {
337             // altitude
338             end = msg.find(",", begin);
339             if ( end == string::npos ) {
340                 return false;
341             }
342     
343             string alt_str = msg.substr(begin, end - begin);
344             altitude = atof( alt_str.c_str() );
345             begin = end + 1;
346
347             // altitude units
348             end = msg.find(",", begin);
349             if ( end == string::npos ) {
350                 return false;
351             }
352     
353             string alt_units = msg.substr(begin, end - begin);
354             begin = end + 1;
355
356             if ( alt_units != "F" && alt_units != "f" ) {
357                 altitude *= SG_METER_TO_FEET;
358             }
359
360             fdm->set_Altitude( altitude );
361     
362             SG_LOG( SG_IO, SG_INFO, " altitude  = " << altitude );
363
364         }
365
366         // printf("%.8f %.8f\n", lon, lat);
367
368         begin = begin_line;
369         end_line = msg.find("\n", begin_line);
370     }
371
372     return true;
373 }
374
375
376 // open hailing frequencies
377 bool FGGarmin::open() {
378     if ( is_enabled() ) {
379         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
380                 << "is already in use, ignoring" );
381         return false;
382     }
383
384     SGIOChannel *io = get_io_channel();
385
386     if ( ! io->open( get_direction() ) ) {
387         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
388         return false;
389     }
390
391     set_enabled( true );
392
393     return true;
394 }
395
396
397 // process work for this port
398 bool FGGarmin::process() {
399     SGIOChannel *io = get_io_channel();
400
401     if ( get_direction() == SG_IO_OUT ) {
402         gen_message();
403         if ( ! io->write( buf, length ) ) {
404             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
405             return false;
406         }
407     } else if ( get_direction() == SG_IO_IN ) {
408         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
409             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
410             if ( parse_message() ) {
411                 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
412             } else {
413                 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
414             }
415         } else {
416             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
417             return false;
418         }
419         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
420             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
421             if ( parse_message() ) {
422                 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
423             } else {
424                 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
425             }
426         } else {
427             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
428             return false;
429         }
430     }
431
432     return true;
433 }
434
435
436 // close the channel
437 bool FGGarmin::close() {
438     SGIOChannel *io = get_io_channel();
439
440     set_enabled( false );
441
442     if ( ! io->close() ) {
443         return false;
444     }
445
446     return true;
447 }