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