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