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