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