]> git.mxchange.org Git - flightgear.git/blob - src/Network/garmin.cxx
877baca1b5c01bfb75230133c5a53627e241c64b
[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 - 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 #include <simgear/timing/sg_time.hxx>
28
29 #include <FDM/flight.hxx>
30 #include <Main/globals.hxx>
31
32 #include "garmin.hxx"
33
34
35 FGGarmin::FGGarmin() {
36 }
37
38 FGGarmin::~FGGarmin() {
39 }
40
41
42 // calculate the garmin check sum
43 static char calc_nmea_cksum(char *sentence) {
44     unsigned char sum = 0;
45     int i, len;
46
47     // cout << sentence << endl;
48
49     len = strlen(sentence);
50     sum = sentence[0];
51     for ( i = 1; i < len; i++ ) {
52         // cout << sentence[i];
53         sum ^= sentence[i];
54     }
55     // cout << endl;
56
57     // printf("sum = %02x\n", sum);
58     return sum;
59 }
60
61
62 // generate Garmin message
63 bool FGGarmin::gen_message() {
64     // cout << "generating garmin message" << endl;
65
66     char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256];
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() * SGD_RADIANS_TO_DEGREES;
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() * SGD_RADIANS_TO_DEGREES;
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() * SGD_RADIANS_TO_DEGREES );
106
107     char altitude_m[10];
108     sprintf( altitude_m, "%02d", 
109              (int)(cur_fdm_state->get_Altitude() * SG_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,000.0,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,04,0.0,%s,M,00.0,M,,*00\r\n",
124     //          utc, lat, lon, altitude_m );
125
126     sprintf( rmz, "PGRMZ,%s,f,3", altitude_ft );
127     sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
128
129     SG_LOG( SG_IO, SG_DEBUG, rmc );
130     SG_LOG( SG_IO, SG_DEBUG, rmz );
131
132     string garmin_sentence;
133
134     // RMC sentence
135     garmin_sentence = "$";
136     garmin_sentence += rmc;
137     garmin_sentence += "*";
138     garmin_sentence += rmc_sum;
139     garmin_sentence += "\n";
140
141     // RMZ sentence (garmin proprietary)
142     garmin_sentence += "$";
143     garmin_sentence += rmz;
144     garmin_sentence += "*";
145     garmin_sentence += rmz_sum;
146     garmin_sentence += "\n";
147
148     std::cout << garmin_sentence;
149
150     length = garmin_sentence.length();
151     strncpy( buf, garmin_sentence.c_str(), length );
152
153     return true;
154 }
155
156
157 // parse Garmin message
158 bool FGGarmin::parse_message() {
159     SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
160
161     string msg = buf;
162     msg = msg.substr( 0, length );
163     SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
164
165     string::size_type begin_line, end_line, begin, end;
166     begin_line = begin = 0;
167
168     // extract out each line
169     end_line = msg.find("\n", begin_line);
170     while ( end_line != string::npos ) {
171         string line = msg.substr(begin_line, end_line - begin_line);
172         begin_line = end_line + 1;
173         SG_LOG( SG_IO, SG_INFO, "  input line = " << line );
174
175         // leading character
176         string start = msg.substr(begin, 1);
177         ++begin;
178         SG_LOG( SG_IO, SG_INFO, "  start = " << start );
179
180         // sentence
181         end = msg.find(",", begin);
182         if ( end == string::npos ) {
183             return false;
184         }
185     
186         string sentence = msg.substr(begin, end - begin);
187         begin = end + 1;
188         SG_LOG( SG_IO, SG_INFO, "  sentence = " << sentence );
189
190         double lon_deg, lon_min, lat_deg, lat_min;
191         double lon, lat, speed, heading, altitude;
192
193         if ( sentence == "GPRMC" ) {
194             // time
195             end = msg.find(",", begin);
196             if ( end == string::npos ) {
197                 return false;
198             }
199     
200             string utc = msg.substr(begin, end - begin);
201             begin = end + 1;
202             SG_LOG( SG_IO, SG_INFO, "  utc = " << utc );
203
204             // junk
205             end = msg.find(",", begin);
206             if ( end == string::npos ) {
207                 return false;
208             }
209     
210             string junk = msg.substr(begin, end - begin);
211             begin = end + 1;
212             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
213
214             // lat val
215             end = msg.find(",", begin);
216             if ( end == string::npos ) {
217                 return false;
218             }
219     
220             string lat_str = msg.substr(begin, end - begin);
221             begin = end + 1;
222
223             lat_deg = atof( lat_str.substr(0, 2).c_str() );
224             lat_min = atof( lat_str.substr(2).c_str() );
225
226             // lat dir
227             end = msg.find(",", begin);
228             if ( end == string::npos ) {
229                 return false;
230             }
231     
232             string lat_dir = msg.substr(begin, end - begin);
233             begin = end + 1;
234
235             lat = lat_deg + ( lat_min / 60.0 );
236             if ( lat_dir == "S" ) {
237                 lat *= -1;
238             }
239
240             cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
241             SG_LOG( SG_IO, SG_INFO, "  lat = " << lat );
242
243             // lon val
244             end = msg.find(",", begin);
245             if ( end == string::npos ) {
246                 return false;
247             }
248     
249             string lon_str = msg.substr(begin, end - begin);
250             begin = end + 1;
251
252             lon_deg = atof( lon_str.substr(0, 3).c_str() );
253             lon_min = atof( lon_str.substr(3).c_str() );
254
255             // lon dir
256             end = msg.find(",", begin);
257             if ( end == string::npos ) {
258                 return false;
259             }
260     
261             string lon_dir = msg.substr(begin, end - begin);
262             begin = end + 1;
263
264             lon = lon_deg + ( lon_min / 60.0 );
265             if ( lon_dir == "W" ) {
266                 lon *= -1;
267             }
268
269             cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
270             SG_LOG( SG_IO, SG_INFO, "  lon = " << lon );
271
272 #if 0
273             double sl_radius, lat_geoc;
274             sgGeodToGeoc( cur_fdm_state->get_Latitude(), 
275                           cur_fdm_state->get_Altitude(), 
276                           &sl_radius, &lat_geoc );
277             cur_fdm_state->set_Geocentric_Position( lat_geoc, 
278                            cur_fdm_state->get_Longitude(), 
279                            sl_radius + cur_fdm_state->get_Altitude() );
280 #endif
281
282             // speed
283             end = msg.find(",", begin);
284             if ( end == string::npos ) {
285                 return false;
286             }
287     
288             string speed_str = msg.substr(begin, end - begin);
289             begin = end + 1;
290             speed = atof( speed_str.c_str() );
291             cur_fdm_state->set_V_calibrated_kts( speed );
292             // cur_fdm_state->set_V_ground_speed( speed );
293             SG_LOG( SG_IO, SG_INFO, "  speed = " << speed );
294
295             // heading
296             end = msg.find(",", begin);
297             if ( end == string::npos ) {
298                 return false;
299             }
300     
301             string hdg_str = msg.substr(begin, end - begin);
302             begin = end + 1;
303             heading = atof( hdg_str.c_str() );
304             cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(), 
305                                              cur_fdm_state->get_Theta(), 
306                                              heading * SGD_DEGREES_TO_RADIANS );
307             SG_LOG( SG_IO, SG_INFO, "  heading = " << heading );
308         } else if ( sentence == "PGRMZ" ) {
309             // altitude
310             end = msg.find(",", begin);
311             if ( end == string::npos ) {
312                 return false;
313             }
314     
315             string alt_str = msg.substr(begin, end - begin);
316             altitude = atof( alt_str.c_str() );
317             begin = end + 1;
318
319             // altitude units
320             end = msg.find(",", begin);
321             if ( end == string::npos ) {
322                 return false;
323             }
324     
325             string alt_units = msg.substr(begin, end - begin);
326             begin = end + 1;
327
328             if ( alt_units != (string)"F" && alt_units != (string)"f" ) {
329                 altitude *= SG_METER_TO_FEET;
330             }
331
332             cur_fdm_state->set_Altitude( altitude );
333     
334             SG_LOG( SG_IO, SG_INFO, " altitude  = " << altitude );
335
336         }
337
338         // printf("%.8f %.8f\n", lon, lat);
339
340         begin = begin_line;
341         end_line = msg.find("\n", begin_line);
342     }
343
344     return true;
345 }
346
347
348 // open hailing frequencies
349 bool FGGarmin::open() {
350     if ( is_enabled() ) {
351         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
352                 << "is already in use, ignoring" );
353         return false;
354     }
355
356     SGIOChannel *io = get_io_channel();
357
358     if ( ! io->open( get_direction() ) ) {
359         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
360         return false;
361     }
362
363     set_enabled( true );
364
365     return true;
366 }
367
368
369 // process work for this port
370 bool FGGarmin::process() {
371     SGIOChannel *io = get_io_channel();
372
373     if ( get_direction() == SG_IO_OUT ) {
374         gen_message();
375         if ( ! io->write( buf, length ) ) {
376             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
377             return false;
378         }
379     } else if ( get_direction() == SG_IO_IN ) {
380         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
381             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
382             if ( parse_message() ) {
383                 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
384             } else {
385                 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
386             }
387         } else {
388             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
389             return false;
390         }
391         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
392             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
393             if ( parse_message() ) {
394                 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
395             } else {
396                 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
397             }
398         } else {
399             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
400             return false;
401         }
402     }
403
404     return true;
405 }
406
407
408 // close the channel
409 bool FGGarmin::close() {
410     SGIOChannel *io = get_io_channel();
411
412     set_enabled( false );
413
414     if ( ! io->close() ) {
415         return false;
416     }
417
418     return true;
419 }