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., 675 Mass Ave, Cambridge, MA 02139, USA.
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>
29 #include <FDM/flight.hxx>
30 #include <Main/fg_props.hxx>
31 #include <Main/globals.hxx>
35 SG_USING_NAMESPACE(std);
37 FGGarmin::FGGarmin() {
40 FGGarmin::~FGGarmin() {
44 // calculate the garmin check sum
45 static char calc_nmea_cksum(char *sentence) {
46 unsigned char sum = 0;
49 // cout << sentence << endl;
51 len = strlen(sentence);
53 for ( i = 1; i < len; i++ ) {
54 // cout << sentence[i];
59 // printf("sum = %02x\n", sum);
64 // generate Garmin message
65 bool FGGarmin::gen_message() {
66 // cout << "generating garmin message" << endl;
68 char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256], gsa[256];
73 SGTime *t = globals->get_time_params();
76 sprintf( utc, "%02d%02d%02d",
77 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
80 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
88 min = (latd - (double)deg) * 60.0;
89 sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
92 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
100 min = (lond - (double)deg) * 60.0;
101 sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
104 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
107 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
110 sprintf( altitude_m, "%02d",
111 (int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
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 );
120 float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
121 if ( magdeg < 0.0 ) {
127 sprintf( magvar, "%05.1f,%c", magdeg, dir );
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) );
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 );
137 sprintf( rmz, "PGRMZ,%s,M,3", altitude_m );
138 sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
141 "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
143 SG_LOG( SG_IO, SG_DEBUG, rmc );
144 SG_LOG( SG_IO, SG_DEBUG, rmz );
145 SG_LOG( SG_IO, SG_DEBUG, gsa );
147 string garmin_sentence;
150 garmin_sentence = "$";
151 garmin_sentence += rmc;
152 garmin_sentence += "*";
153 garmin_sentence += rmc_sum;
154 garmin_sentence += "\r\n";
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";
163 // GSA sentence (totally faked)
164 garmin_sentence += gsa;
165 garmin_sentence += "\r\n";
167 cout << garmin_sentence;
169 length = garmin_sentence.length();
170 strncpy( buf, garmin_sentence.c_str(), length );
176 // parse Garmin message
177 bool FGGarmin::parse_message() {
178 SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
181 msg = msg.substr( 0, length );
182 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
184 string::size_type begin_line, end_line, begin, end;
185 begin_line = begin = 0;
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 );
195 string start = msg.substr(begin, 1);
197 SG_LOG( SG_IO, SG_INFO, " start = " << start );
200 end = msg.find(",", begin);
201 if ( end == string::npos ) {
205 string sentence = msg.substr(begin, end - begin);
207 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
209 double lon_deg, lon_min, lat_deg, lat_min;
210 double lon, lat, speed, heading, altitude;
212 if ( sentence == "GPRMC" ) {
214 end = msg.find(",", begin);
215 if ( end == string::npos ) {
219 string utc = msg.substr(begin, end - begin);
221 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
224 end = msg.find(",", begin);
225 if ( end == string::npos ) {
229 string junk = msg.substr(begin, end - begin);
231 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
234 end = msg.find(",", begin);
235 if ( end == string::npos ) {
239 string lat_str = msg.substr(begin, end - begin);
242 lat_deg = atof( lat_str.substr(0, 2).c_str() );
243 lat_min = atof( lat_str.substr(2).c_str() );
246 end = msg.find(",", begin);
247 if ( end == string::npos ) {
251 string lat_dir = msg.substr(begin, end - begin);
254 lat = lat_deg + ( lat_min / 60.0 );
255 if ( lat_dir == "S" ) {
259 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
260 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
263 end = msg.find(",", begin);
264 if ( end == string::npos ) {
268 string lon_str = msg.substr(begin, end - begin);
271 lon_deg = atof( lon_str.substr(0, 3).c_str() );
272 lon_min = atof( lon_str.substr(3).c_str() );
275 end = msg.find(",", begin);
276 if ( end == string::npos ) {
280 string lon_dir = msg.substr(begin, end - begin);
283 lon = lon_deg + ( lon_min / 60.0 );
284 if ( lon_dir == "W" ) {
288 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
289 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
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() );
302 end = msg.find(",", begin);
303 if ( end == string::npos ) {
307 string speed_str = msg.substr(begin, end - begin);
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 );
315 end = msg.find(",", begin);
316 if ( end == string::npos ) {
320 string hdg_str = msg.substr(begin, end - begin);
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" ) {
329 end = msg.find(",", begin);
330 if ( end == string::npos ) {
334 string alt_str = msg.substr(begin, end - begin);
335 altitude = atof( alt_str.c_str() );
339 end = msg.find(",", begin);
340 if ( end == string::npos ) {
344 string alt_units = msg.substr(begin, end - begin);
347 if ( alt_units != "F" && alt_units != "f" ) {
348 altitude *= SG_METER_TO_FEET;
351 cur_fdm_state->set_Altitude( altitude );
353 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
357 // printf("%.8f %.8f\n", lon, lat);
360 end_line = msg.find("\n", begin_line);
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" );
375 SGIOChannel *io = get_io_channel();
377 if ( ! io->open( get_direction() ) ) {
378 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
388 // process work for this port
389 bool FGGarmin::process() {
390 SGIOChannel *io = get_io_channel();
392 if ( get_direction() == SG_IO_OUT ) {
394 if ( ! io->write( buf, length ) ) {
395 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
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." );
404 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
407 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
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." );
415 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
418 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
428 bool FGGarmin::close() {
429 SGIOChannel *io = get_io_channel();
431 set_enabled( false );
433 if ( ! io->close() ) {