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/flight.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
42 FGGarmin::FGGarmin() {
45 FGGarmin::~FGGarmin() {
49 // calculate the garmin check sum
50 static char calc_nmea_cksum(char *sentence) {
51 unsigned char sum = 0;
54 // cout << sentence << endl;
56 len = strlen(sentence);
58 for ( i = 1; i < len; i++ ) {
59 // cout << sentence[i];
64 // printf("sum = %02x\n", sum);
69 // generate Garmin message
70 bool FGGarmin::gen_message() {
71 // cout << "generating garmin message" << endl;
73 char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256], gsa[256];
78 SGTime *t = globals->get_time_params();
81 sprintf( utc, "%02d%02d%02d",
82 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
85 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
93 min = (latd - (double)deg) * 60.0;
94 sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
97 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
105 min = (lond - (double)deg) * 60.0;
106 sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
109 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
112 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
115 sprintf( altitude_m, "%02d",
116 (int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
119 int year = t->getGmt()->tm_year;
120 while ( year >= 100 ) { year -= 100; }
121 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
122 t->getGmt()->tm_mon+1, year );
125 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
126 if ( magdeg < 0.0 ) {
132 sprintf( magvar, "%05.1f,%c", magdeg, dir );
134 // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
135 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s",
136 utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
137 sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
139 // sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
140 // utc, lat, lon, altitude_m );
142 sprintf( rmz, "PGRMZ,%s,M,3", altitude_m );
143 sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
146 "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
148 SG_LOG( SG_IO, SG_DEBUG, rmc );
149 SG_LOG( SG_IO, SG_DEBUG, rmz );
150 SG_LOG( SG_IO, SG_DEBUG, gsa );
152 string garmin_sentence;
155 garmin_sentence = "$";
156 garmin_sentence += rmc;
157 garmin_sentence += "*";
158 garmin_sentence += rmc_sum;
159 garmin_sentence += "\r\n";
161 // RMZ sentence (garmin proprietary)
162 garmin_sentence += "$";
163 garmin_sentence += rmz;
164 garmin_sentence += "*";
165 garmin_sentence += rmz_sum;
166 garmin_sentence += "\r\n";
168 // GSA sentence (totally faked)
169 garmin_sentence += gsa;
170 garmin_sentence += "\r\n";
172 std::cout << garmin_sentence;
174 length = garmin_sentence.length();
175 strncpy( buf, garmin_sentence.c_str(), length );
181 // parse Garmin message
182 bool FGGarmin::parse_message() {
183 SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
186 msg = msg.substr( 0, length );
187 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
189 string::size_type begin_line, end_line, begin, end;
190 begin_line = begin = 0;
192 // extract out each line
193 end_line = msg.find("\n", begin_line);
194 while ( end_line != string::npos ) {
195 string line = msg.substr(begin_line, end_line - begin_line);
196 begin_line = end_line + 1;
197 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
200 string start = msg.substr(begin, 1);
202 SG_LOG( SG_IO, SG_INFO, " start = " << start );
205 end = msg.find(",", begin);
206 if ( end == string::npos ) {
210 string sentence = msg.substr(begin, end - begin);
212 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
214 double lon_deg, lon_min, lat_deg, lat_min;
215 double lon, lat, speed, heading, altitude;
217 if ( sentence == "GPRMC" ) {
219 end = msg.find(",", begin);
220 if ( end == string::npos ) {
224 string utc = msg.substr(begin, end - begin);
226 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
229 end = msg.find(",", begin);
230 if ( end == string::npos ) {
234 string junk = msg.substr(begin, end - begin);
236 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
239 end = msg.find(",", begin);
240 if ( end == string::npos ) {
244 string lat_str = msg.substr(begin, end - begin);
247 lat_deg = atof( lat_str.substr(0, 2).c_str() );
248 lat_min = atof( lat_str.substr(2).c_str() );
251 end = msg.find(",", begin);
252 if ( end == string::npos ) {
256 string lat_dir = msg.substr(begin, end - begin);
259 lat = lat_deg + ( lat_min / 60.0 );
260 if ( lat_dir == "S" ) {
264 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
265 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
268 end = msg.find(",", begin);
269 if ( end == string::npos ) {
273 string lon_str = msg.substr(begin, end - begin);
276 lon_deg = atof( lon_str.substr(0, 3).c_str() );
277 lon_min = atof( lon_str.substr(3).c_str() );
280 end = msg.find(",", begin);
281 if ( end == string::npos ) {
285 string lon_dir = msg.substr(begin, end - begin);
288 lon = lon_deg + ( lon_min / 60.0 );
289 if ( lon_dir == "W" ) {
293 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
294 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
297 double sl_radius, lat_geoc;
298 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
299 cur_fdm_state->get_Altitude(),
300 &sl_radius, &lat_geoc );
301 cur_fdm_state->set_Geocentric_Position( lat_geoc,
302 cur_fdm_state->get_Longitude(),
303 sl_radius + cur_fdm_state->get_Altitude() );
307 end = msg.find(",", begin);
308 if ( end == string::npos ) {
312 string speed_str = msg.substr(begin, end - begin);
314 speed = atof( speed_str.c_str() );
315 cur_fdm_state->set_V_calibrated_kts( speed );
316 // cur_fdm_state->set_V_ground_speed( speed );
317 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
320 end = msg.find(",", begin);
321 if ( end == string::npos ) {
325 string hdg_str = msg.substr(begin, end - begin);
327 heading = atof( hdg_str.c_str() );
328 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
329 cur_fdm_state->get_Theta(),
330 heading * SGD_DEGREES_TO_RADIANS );
331 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
332 } else if ( sentence == "PGRMZ" ) {
334 end = msg.find(",", begin);
335 if ( end == string::npos ) {
339 string alt_str = msg.substr(begin, end - begin);
340 altitude = atof( alt_str.c_str() );
344 end = msg.find(",", begin);
345 if ( end == string::npos ) {
349 string alt_units = msg.substr(begin, end - begin);
352 if ( alt_units != "F" && alt_units != "f" ) {
353 altitude *= SG_METER_TO_FEET;
356 cur_fdm_state->set_Altitude( altitude );
358 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
362 // printf("%.8f %.8f\n", lon, lat);
365 end_line = msg.find("\n", begin_line);
372 // open hailing frequencies
373 bool FGGarmin::open() {
374 if ( is_enabled() ) {
375 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
376 << "is already in use, ignoring" );
380 SGIOChannel *io = get_io_channel();
382 if ( ! io->open( get_direction() ) ) {
383 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
393 // process work for this port
394 bool FGGarmin::process() {
395 SGIOChannel *io = get_io_channel();
397 if ( get_direction() == SG_IO_OUT ) {
399 if ( ! io->write( buf, length ) ) {
400 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
403 } else if ( get_direction() == SG_IO_IN ) {
404 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
405 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
406 if ( parse_message() ) {
407 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
409 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
412 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
415 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
416 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
417 if ( parse_message() ) {
418 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
420 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
423 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
433 bool FGGarmin::close() {
434 SGIOChannel *io = get_io_channel();
436 set_enabled( false );
438 if ( ! io->close() ) {