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.
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/io/iochannel.hxx>
35 #include <simgear/timing/sg_time.hxx>
37 #include <FDM/flightProperties.hxx>
38 #include <Main/fg_props.hxx>
39 #include <Main/globals.hxx>
45 FGGarmin::FGGarmin() {
46 fdm = new FlightProperties;
49 FGGarmin::~FGGarmin() {
54 // calculate the garmin check sum
55 static char calc_nmea_cksum(char *sentence) {
56 unsigned char sum = 0;
59 // cout << sentence << endl;
61 len = strlen(sentence);
63 for ( i = 1; i < len; i++ ) {
64 // cout << sentence[i];
69 // printf("sum = %02x\n", sum);
74 // generate Garmin message
75 bool FGGarmin::gen_message() {
76 // cout << "generating garmin message" << endl;
78 char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256], gsa[256];
83 SGTime *t = globals->get_time_params();
86 sprintf( utc, "%02d%02d%02d",
87 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
90 double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
98 min = (latd - (double)deg) * 60.0;
99 sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
102 double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
110 min = (lond - (double)deg) * 60.0;
111 sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
114 sprintf( speed, "%05.1f", fdm->get_V_equiv_kts() );
117 sprintf( heading, "%05.1f", fdm->get_Psi() * SGD_RADIANS_TO_DEGREES );
120 sprintf( altitude_m, "%02d",
121 (int)(fdm->get_Altitude() * SG_FEET_TO_METER) );
124 int year = t->getGmt()->tm_year;
125 while ( year >= 100 ) { year -= 100; }
126 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
127 t->getGmt()->tm_mon+1, year );
130 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
131 if ( magdeg < 0.0 ) {
137 sprintf( magvar, "%05.1f,%c", magdeg, dir );
139 // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
140 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s",
141 utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
142 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
144 // sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
145 // utc, lat, lon, altitude_m );
147 sprintf( rmz, "PGRMZ,%s,M,3", altitude_m );
148 sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
151 "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
153 SG_LOG( SG_IO, SG_DEBUG, rmc );
154 SG_LOG( SG_IO, SG_DEBUG, rmz );
155 SG_LOG( SG_IO, SG_DEBUG, gsa );
157 string garmin_sentence;
160 garmin_sentence = "$";
161 garmin_sentence += rmc;
162 garmin_sentence += "*";
163 garmin_sentence += rmc_sum;
164 garmin_sentence += "\r\n";
166 // RMZ sentence (garmin proprietary)
167 garmin_sentence += "$";
168 garmin_sentence += rmz;
169 garmin_sentence += "*";
170 garmin_sentence += rmz_sum;
171 garmin_sentence += "\r\n";
173 // GSA sentence (totally faked)
174 garmin_sentence += gsa;
175 garmin_sentence += "\r\n";
177 std::cout << garmin_sentence;
179 length = garmin_sentence.length();
180 strncpy( buf, garmin_sentence.c_str(), length );
186 // parse Garmin message
187 bool FGGarmin::parse_message() {
188 SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
191 msg = msg.substr( 0, length );
192 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
194 string::size_type begin_line, end_line, begin, end;
195 begin_line = begin = 0;
197 // extract out each line
198 end_line = msg.find("\n", begin_line);
199 while ( end_line != string::npos ) {
200 string line = msg.substr(begin_line, end_line - begin_line);
201 begin_line = end_line + 1;
202 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
205 string start = msg.substr(begin, 1);
207 SG_LOG( SG_IO, SG_INFO, " start = " << start );
210 end = msg.find(",", begin);
211 if ( end == string::npos ) {
215 string sentence = msg.substr(begin, end - begin);
217 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
219 double lon_deg, lon_min, lat_deg, lat_min;
220 double lon, lat, speed, heading, altitude;
222 if ( sentence == "GPRMC" ) {
224 end = msg.find(",", begin);
225 if ( end == string::npos ) {
229 string utc = msg.substr(begin, end - begin);
231 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
234 end = msg.find(",", begin);
235 if ( end == string::npos ) {
239 string junk = msg.substr(begin, end - begin);
241 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
244 end = msg.find(",", begin);
245 if ( end == string::npos ) {
249 string lat_str = msg.substr(begin, end - begin);
252 lat_deg = atof( lat_str.substr(0, 2).c_str() );
253 lat_min = atof( lat_str.substr(2).c_str() );
256 end = msg.find(",", begin);
257 if ( end == string::npos ) {
261 string lat_dir = msg.substr(begin, end - begin);
264 lat = lat_deg + ( lat_min / 60.0 );
265 if ( lat_dir == "S" ) {
269 fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
270 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
273 end = msg.find(",", begin);
274 if ( end == string::npos ) {
278 string lon_str = msg.substr(begin, end - begin);
281 lon_deg = atof( lon_str.substr(0, 3).c_str() );
282 lon_min = atof( lon_str.substr(3).c_str() );
285 end = msg.find(",", begin);
286 if ( end == string::npos ) {
290 string lon_dir = msg.substr(begin, end - begin);
293 lon = lon_deg + ( lon_min / 60.0 );
294 if ( lon_dir == "W" ) {
298 fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
299 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
302 double sl_radius, lat_geoc;
303 sgGeodToGeoc( fdm->get_Latitude(),
305 &sl_radius, &lat_geoc );
306 fdm->set_Geocentric_Position( lat_geoc,
307 fdm->get_Longitude(),
308 sl_radius + fdm->get_Altitude() );
312 end = msg.find(",", begin);
313 if ( end == string::npos ) {
317 string speed_str = msg.substr(begin, end - begin);
319 speed = atof( speed_str.c_str() );
320 fdm->set_V_calibrated_kts( speed );
321 // fdm->set_V_ground_speed( speed );
322 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
325 end = msg.find(",", begin);
326 if ( end == string::npos ) {
330 string hdg_str = msg.substr(begin, end - begin);
332 heading = atof( hdg_str.c_str() );
333 fdm->set_Euler_Angles( fdm->get_Phi(),
335 heading * SGD_DEGREES_TO_RADIANS );
336 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
337 } else if ( sentence == "PGRMZ" ) {
339 end = msg.find(",", begin);
340 if ( end == string::npos ) {
344 string alt_str = msg.substr(begin, end - begin);
345 altitude = atof( alt_str.c_str() );
349 end = msg.find(",", begin);
350 if ( end == string::npos ) {
354 string alt_units = msg.substr(begin, end - begin);
357 if ( alt_units != "F" && alt_units != "f" ) {
358 altitude *= SG_METER_TO_FEET;
361 fdm->set_Altitude( altitude );
363 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
367 // printf("%.8f %.8f\n", lon, lat);
370 end_line = msg.find("\n", begin_line);
377 // open hailing frequencies
378 bool FGGarmin::open() {
379 if ( is_enabled() ) {
380 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
381 << "is already in use, ignoring" );
385 SGIOChannel *io = get_io_channel();
387 if ( ! io->open( get_direction() ) ) {
388 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
398 // process work for this port
399 bool FGGarmin::process() {
400 SGIOChannel *io = get_io_channel();
402 if ( get_direction() == SG_IO_OUT ) {
404 if ( ! io->write( buf, length ) ) {
405 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
408 } else if ( get_direction() == SG_IO_IN ) {
409 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
410 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
411 if ( parse_message() ) {
412 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
414 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
417 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
420 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
421 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
422 if ( parse_message() ) {
423 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
425 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
428 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
438 bool FGGarmin::close() {
439 SGIOChannel *io = get_io_channel();
441 set_enabled( false );
443 if ( ! io->close() ) {