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.
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>
32 #include <FDM/flight.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Main/globals.hxx>
38 SG_USING_NAMESPACE(std);
40 FGGarmin::FGGarmin() {
43 FGGarmin::~FGGarmin() {
47 // calculate the garmin check sum
48 static char calc_nmea_cksum(char *sentence) {
49 unsigned char sum = 0;
52 // cout << sentence << endl;
54 len = strlen(sentence);
56 for ( i = 1; i < len; i++ ) {
57 // cout << sentence[i];
62 // printf("sum = %02x\n", sum);
67 // generate Garmin message
68 bool FGGarmin::gen_message() {
69 // cout << "generating garmin message" << endl;
71 char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256], gsa[256];
76 SGTime *t = globals->get_time_params();
79 sprintf( utc, "%02d%02d%02d",
80 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
83 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
91 min = (latd - (double)deg) * 60.0;
92 sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
95 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
103 min = (lond - (double)deg) * 60.0;
104 sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
107 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
110 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
113 sprintf( altitude_m, "%02d",
114 (int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
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 );
123 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
124 if ( magdeg < 0.0 ) {
130 sprintf( magvar, "%05.1f,%c", magdeg, dir );
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) );
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 );
140 sprintf( rmz, "PGRMZ,%s,M,3", altitude_m );
141 sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
144 "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
146 SG_LOG( SG_IO, SG_DEBUG, rmc );
147 SG_LOG( SG_IO, SG_DEBUG, rmz );
148 SG_LOG( SG_IO, SG_DEBUG, gsa );
150 string garmin_sentence;
153 garmin_sentence = "$";
154 garmin_sentence += rmc;
155 garmin_sentence += "*";
156 garmin_sentence += rmc_sum;
157 garmin_sentence += "\r\n";
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";
166 // GSA sentence (totally faked)
167 garmin_sentence += gsa;
168 garmin_sentence += "\r\n";
170 cout << garmin_sentence;
172 length = garmin_sentence.length();
173 strncpy( buf, garmin_sentence.c_str(), length );
179 // parse Garmin message
180 bool FGGarmin::parse_message() {
181 SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
184 msg = msg.substr( 0, length );
185 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
187 string::size_type begin_line, end_line, begin, end;
188 begin_line = begin = 0;
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 );
198 string start = msg.substr(begin, 1);
200 SG_LOG( SG_IO, SG_INFO, " start = " << start );
203 end = msg.find(",", begin);
204 if ( end == string::npos ) {
208 string sentence = msg.substr(begin, end - begin);
210 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
212 double lon_deg, lon_min, lat_deg, lat_min;
213 double lon, lat, speed, heading, altitude;
215 if ( sentence == "GPRMC" ) {
217 end = msg.find(",", begin);
218 if ( end == string::npos ) {
222 string utc = msg.substr(begin, end - begin);
224 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
227 end = msg.find(",", begin);
228 if ( end == string::npos ) {
232 string junk = msg.substr(begin, end - begin);
234 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
237 end = msg.find(",", begin);
238 if ( end == string::npos ) {
242 string lat_str = msg.substr(begin, end - begin);
245 lat_deg = atof( lat_str.substr(0, 2).c_str() );
246 lat_min = atof( lat_str.substr(2).c_str() );
249 end = msg.find(",", begin);
250 if ( end == string::npos ) {
254 string lat_dir = msg.substr(begin, end - begin);
257 lat = lat_deg + ( lat_min / 60.0 );
258 if ( lat_dir == "S" ) {
262 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
263 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
266 end = msg.find(",", begin);
267 if ( end == string::npos ) {
271 string lon_str = msg.substr(begin, end - begin);
274 lon_deg = atof( lon_str.substr(0, 3).c_str() );
275 lon_min = atof( lon_str.substr(3).c_str() );
278 end = msg.find(",", begin);
279 if ( end == string::npos ) {
283 string lon_dir = msg.substr(begin, end - begin);
286 lon = lon_deg + ( lon_min / 60.0 );
287 if ( lon_dir == "W" ) {
291 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
292 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
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() );
305 end = msg.find(",", begin);
306 if ( end == string::npos ) {
310 string speed_str = msg.substr(begin, end - begin);
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 );
318 end = msg.find(",", begin);
319 if ( end == string::npos ) {
323 string hdg_str = msg.substr(begin, end - begin);
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" ) {
332 end = msg.find(",", begin);
333 if ( end == string::npos ) {
337 string alt_str = msg.substr(begin, end - begin);
338 altitude = atof( alt_str.c_str() );
342 end = msg.find(",", begin);
343 if ( end == string::npos ) {
347 string alt_units = msg.substr(begin, end - begin);
350 if ( alt_units != "F" && alt_units != "f" ) {
351 altitude *= SG_METER_TO_FEET;
354 cur_fdm_state->set_Altitude( altitude );
356 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
360 // printf("%.8f %.8f\n", lon, lat);
363 end_line = msg.find("\n", begin_line);
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" );
378 SGIOChannel *io = get_io_channel();
380 if ( ! io->open( get_direction() ) ) {
381 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
391 // process work for this port
392 bool FGGarmin::process() {
393 SGIOChannel *io = get_io_channel();
395 if ( get_direction() == SG_IO_OUT ) {
397 if ( ! io->write( buf, length ) ) {
398 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
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." );
407 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
410 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
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." );
418 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
421 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
431 bool FGGarmin::close() {
432 SGIOChannel *io = get_io_channel();
434 set_enabled( false );
436 if ( ! io->close() ) {