1 // garmin.cxx -- Garmin protocal class
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
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>
34 #include <FDM/flightProperties.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
42 FGGarmin::FGGarmin() {
43 fdm = new FlightProperties;
46 FGGarmin::~FGGarmin() {
51 // calculate the garmin check sum
52 static char calc_nmea_cksum(char *sentence) {
53 unsigned char sum = 0;
56 // cout << sentence << endl;
58 len = strlen(sentence);
60 for ( i = 1; i < len; i++ ) {
61 // cout << sentence[i];
66 // printf("sum = %02x\n", sum);
71 // generate Garmin message
72 bool FGGarmin::gen_message() {
73 // cout << "generating garmin message" << endl;
75 char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256], gsa[256];
80 SGTime *t = globals->get_time_params();
83 sprintf( utc, "%02d%02d%02d",
84 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
87 double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
95 min = (latd - (double)deg) * 60.0;
96 sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
99 double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
107 min = (lond - (double)deg) * 60.0;
108 sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
111 sprintf( speed, "%05.1f", fdm->get_V_equiv_kts() );
114 sprintf( heading, "%05.1f", fdm->get_Psi() * SGD_RADIANS_TO_DEGREES );
117 sprintf( altitude_m, "%02d",
118 (int)(fdm->get_Altitude() * SG_FEET_TO_METER) );
121 int year = t->getGmt()->tm_year;
122 while ( year >= 100 ) { year -= 100; }
123 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
124 t->getGmt()->tm_mon+1, year );
127 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
128 if ( magdeg < 0.0 ) {
134 sprintf( magvar, "%05.1f,%c", magdeg, dir );
136 // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
137 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s",
138 utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
139 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
141 // sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
142 // utc, lat, lon, altitude_m );
144 sprintf( rmz, "PGRMZ,%s,M,3", altitude_m );
145 sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
148 "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
150 SG_LOG( SG_IO, SG_DEBUG, rmc );
151 SG_LOG( SG_IO, SG_DEBUG, rmz );
152 SG_LOG( SG_IO, SG_DEBUG, gsa );
154 string garmin_sentence;
157 garmin_sentence = "$";
158 garmin_sentence += rmc;
159 garmin_sentence += "*";
160 garmin_sentence += rmc_sum;
161 garmin_sentence += "\r\n";
163 // RMZ sentence (garmin proprietary)
164 garmin_sentence += "$";
165 garmin_sentence += rmz;
166 garmin_sentence += "*";
167 garmin_sentence += rmz_sum;
168 garmin_sentence += "\r\n";
170 // GSA sentence (totally faked)
171 garmin_sentence += gsa;
172 garmin_sentence += "\r\n";
174 std::cout << garmin_sentence;
176 length = garmin_sentence.length();
177 strncpy( buf, garmin_sentence.c_str(), length );
183 // parse Garmin message
184 bool FGGarmin::parse_message() {
185 SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
188 msg = msg.substr( 0, length );
189 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
191 string::size_type begin_line, end_line, begin, end;
192 begin_line = begin = 0;
194 // extract out each line
195 end_line = msg.find("\n", begin_line);
196 while ( end_line != string::npos ) {
197 string line = msg.substr(begin_line, end_line - begin_line);
198 begin_line = end_line + 1;
199 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
202 string start = msg.substr(begin, 1);
204 SG_LOG( SG_IO, SG_INFO, " start = " << start );
207 end = msg.find(",", begin);
208 if ( end == string::npos ) {
212 string sentence = msg.substr(begin, end - begin);
214 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
216 double lon_deg, lon_min, lat_deg, lat_min;
217 double lon, lat, speed, heading, altitude;
219 if ( sentence == "GPRMC" ) {
221 end = msg.find(",", begin);
222 if ( end == string::npos ) {
226 string utc = msg.substr(begin, end - begin);
228 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
231 end = msg.find(",", begin);
232 if ( end == string::npos ) {
236 string junk = msg.substr(begin, end - begin);
238 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
241 end = msg.find(",", begin);
242 if ( end == string::npos ) {
246 string lat_str = msg.substr(begin, end - begin);
249 lat_deg = atof( lat_str.substr(0, 2).c_str() );
250 lat_min = atof( lat_str.substr(2).c_str() );
253 end = msg.find(",", begin);
254 if ( end == string::npos ) {
258 string lat_dir = msg.substr(begin, end - begin);
261 lat = lat_deg + ( lat_min / 60.0 );
262 if ( lat_dir == "S" ) {
266 fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
267 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
270 end = msg.find(",", begin);
271 if ( end == string::npos ) {
275 string lon_str = msg.substr(begin, end - begin);
278 lon_deg = atof( lon_str.substr(0, 3).c_str() );
279 lon_min = atof( lon_str.substr(3).c_str() );
282 end = msg.find(",", begin);
283 if ( end == string::npos ) {
287 string lon_dir = msg.substr(begin, end - begin);
290 lon = lon_deg + ( lon_min / 60.0 );
291 if ( lon_dir == "W" ) {
295 fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
296 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
299 double sl_radius, lat_geoc;
300 sgGeodToGeoc( fdm->get_Latitude(),
302 &sl_radius, &lat_geoc );
303 fdm->set_Geocentric_Position( lat_geoc,
304 fdm->get_Longitude(),
305 sl_radius + fdm->get_Altitude() );
309 end = msg.find(",", begin);
310 if ( end == string::npos ) {
314 string speed_str = msg.substr(begin, end - begin);
316 speed = atof( speed_str.c_str() );
317 fdm->set_V_calibrated_kts( speed );
318 // fdm->set_V_ground_speed( speed );
319 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
322 end = msg.find(",", begin);
323 if ( end == string::npos ) {
327 string hdg_str = msg.substr(begin, end - begin);
329 heading = atof( hdg_str.c_str() );
330 fdm->set_Euler_Angles( fdm->get_Phi(),
332 heading * SGD_DEGREES_TO_RADIANS );
333 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
334 } else if ( sentence == "PGRMZ" ) {
336 end = msg.find(",", begin);
337 if ( end == string::npos ) {
341 string alt_str = msg.substr(begin, end - begin);
342 altitude = atof( alt_str.c_str() );
346 end = msg.find(",", begin);
347 if ( end == string::npos ) {
351 string alt_units = msg.substr(begin, end - begin);
354 if ( alt_units != "F" && alt_units != "f" ) {
355 altitude *= SG_METER_TO_FEET;
358 fdm->set_Altitude( altitude );
360 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
364 // printf("%.8f %.8f\n", lon, lat);
367 end_line = msg.find("\n", begin_line);
374 // open hailing frequencies
375 bool FGGarmin::open() {
376 if ( is_enabled() ) {
377 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
378 << "is already in use, ignoring" );
382 SGIOChannel *io = get_io_channel();
384 if ( ! io->open( get_direction() ) ) {
385 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
395 // process work for this port
396 bool FGGarmin::process() {
397 SGIOChannel *io = get_io_channel();
399 if ( get_direction() == SG_IO_OUT ) {
401 if ( ! io->write( buf, length ) ) {
402 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
405 } else if ( get_direction() == SG_IO_IN ) {
406 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
407 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
408 if ( parse_message() ) {
409 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
411 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
414 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
417 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
418 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
419 if ( parse_message() ) {
420 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
422 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
425 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
435 bool FGGarmin::close() {
436 SGIOChannel *io = get_io_channel();
438 set_enabled( false );
440 if ( ! io->close() ) {