//
// Written by Curtis Olson, started November 1999.
//
-// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
+// Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id$
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <iostream>
#include <simgear/debug/logstream.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/io/iochannel.hxx>
+#include <simgear/timing/sg_time.hxx>
#include <FDM/flight.hxx>
+#include <Main/fg_props.hxx>
#include <Main/globals.hxx>
#include "garmin.hxx"
+using std::string;
FGGarmin::FGGarmin() {
}
bool FGGarmin::gen_message() {
// cout << "generating garmin message" << endl;
- char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256];
+ char rmc[256], rmc_sum[256], rmz[256], rmz_sum[256], gsa[256];
char dir;
int deg;
double min;
sprintf( utc, "%02d%02d%02d",
t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
- char lat[20];
+ char rmc_lat[20];
double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
if ( latd < 0.0 ) {
- latd *= -1.0;
+ latd = -latd;
dir = 'S';
} else {
dir = 'N';
}
deg = (int)(latd);
min = (latd - (double)deg) * 60.0;
- sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
+ sprintf( rmc_lat, "%02d%07.4f,%c", abs(deg), min, dir);
- char lon[20];
+ char rmc_lon[20];
double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
if ( lond < 0.0 ) {
- lond *= -1.0;
+ lond = -lond;
dir = 'W';
} else {
dir = 'E';
}
deg = (int)(lond);
min = (lond - (double)deg) * 60.0;
- sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
+ sprintf( rmc_lon, "%03d%07.4f,%c", abs(deg), min, dir);
char speed[10];
sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
sprintf( altitude_m, "%02d",
(int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
- char altitude_ft[10];
- sprintf( altitude_ft, "%02d", (int)cur_fdm_state->get_Altitude() );
-
char date[10];
+ int year = t->getGmt()->tm_year;
+ while ( year >= 100 ) { year -= 100; }
sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
- t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
+ t->getGmt()->tm_mon+1, year );
+
+ char magvar[10];
+ float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
+ if ( magdeg < 0.0 ) {
+ magdeg = -magdeg;
+ dir = 'W';
+ } else {
+ dir = 'E';
+ }
+ sprintf( magvar, "%05.1f,%c", magdeg, dir );
- // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
- sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,000.0,E",
- utc, lat, lon, speed, heading, date );
+ // $GPRMC,HHMMSS,A,DDMM.MMMM,N,DDDMM.MMMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
+ sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,%s",
+ utc, rmc_lat, rmc_lon, speed, heading, date, magvar );
sprintf( rmc_sum, "%02X", calc_nmea_cksum(rmc) );
// sprintf( gga, "$GPGGA,%s,%s,%s,1,04,0.0,%s,M,00.0,M,,*00\r\n",
// utc, lat, lon, altitude_m );
- sprintf( rmz, "PGRMZ,%s,f,3", altitude_ft );
+ sprintf( rmz, "PGRMZ,%s,M,3", altitude_m );
sprintf( rmz_sum, "%02X", calc_nmea_cksum(rmz) );
- FG_LOG( FG_IO, FG_DEBUG, rmc );
- FG_LOG( FG_IO, FG_DEBUG, rmz );
+ sprintf( gsa, "%s",
+ "$GPGSA,A,3,01,02,03,,05,,07,,09,,11,12,0.9,0.9,2.0*38" );
+
+ SG_LOG( SG_IO, SG_DEBUG, rmc );
+ SG_LOG( SG_IO, SG_DEBUG, rmz );
+ SG_LOG( SG_IO, SG_DEBUG, gsa );
string garmin_sentence;
garmin_sentence += rmc;
garmin_sentence += "*";
garmin_sentence += rmc_sum;
- garmin_sentence += "\n";
+ garmin_sentence += "\r\n";
// RMZ sentence (garmin proprietary)
garmin_sentence += "$";
garmin_sentence += rmz;
garmin_sentence += "*";
garmin_sentence += rmz_sum;
- garmin_sentence += "\n";
+ garmin_sentence += "\r\n";
+
+ // GSA sentence (totally faked)
+ garmin_sentence += gsa;
+ garmin_sentence += "\r\n";
- cout << garmin_sentence;
+ std::cout << garmin_sentence;
length = garmin_sentence.length();
strncpy( buf, garmin_sentence.c_str(), length );
// parse Garmin message
bool FGGarmin::parse_message() {
- FG_LOG( FG_IO, FG_INFO, "parse garmin message" );
+ SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
string msg = buf;
msg = msg.substr( 0, length );
- FG_LOG( FG_IO, FG_INFO, "entire message = " << msg );
+ SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
string::size_type begin_line, end_line, begin, end;
begin_line = begin = 0;
while ( end_line != string::npos ) {
string line = msg.substr(begin_line, end_line - begin_line);
begin_line = end_line + 1;
- FG_LOG( FG_IO, FG_INFO, " input line = " << line );
+ SG_LOG( SG_IO, SG_INFO, " input line = " << line );
// leading character
string start = msg.substr(begin, 1);
++begin;
- FG_LOG( FG_IO, FG_INFO, " start = " << start );
+ SG_LOG( SG_IO, SG_INFO, " start = " << start );
// sentence
end = msg.find(",", begin);
string sentence = msg.substr(begin, end - begin);
begin = end + 1;
- FG_LOG( FG_IO, FG_INFO, " sentence = " << sentence );
+ SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
double lon_deg, lon_min, lat_deg, lat_min;
double lon, lat, speed, heading, altitude;
string utc = msg.substr(begin, end - begin);
begin = end + 1;
- FG_LOG( FG_IO, FG_INFO, " utc = " << utc );
+ SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
// junk
end = msg.find(",", begin);
string junk = msg.substr(begin, end - begin);
begin = end + 1;
- FG_LOG( FG_IO, FG_INFO, " junk = " << junk );
+ SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
// lat val
end = msg.find(",", begin);
}
cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
- FG_LOG( FG_IO, FG_INFO, " lat = " << lat );
+ SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
// lon val
end = msg.find(",", begin);
}
cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
- FG_LOG( FG_IO, FG_INFO, " lon = " << lon );
+ SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
#if 0
double sl_radius, lat_geoc;
speed = atof( speed_str.c_str() );
cur_fdm_state->set_V_calibrated_kts( speed );
// cur_fdm_state->set_V_ground_speed( speed );
- FG_LOG( FG_IO, FG_INFO, " speed = " << speed );
+ SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
// heading
end = msg.find(",", begin);
cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
cur_fdm_state->get_Theta(),
heading * SGD_DEGREES_TO_RADIANS );
- FG_LOG( FG_IO, FG_INFO, " heading = " << heading );
+ SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
} else if ( sentence == "PGRMZ" ) {
// altitude
end = msg.find(",", begin);
cur_fdm_state->set_Altitude( altitude );
- FG_LOG( FG_IO, FG_INFO, " altitude = " << altitude );
+ SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
}
// open hailing frequencies
bool FGGarmin::open() {
if ( is_enabled() ) {
- FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel "
+ SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
<< "is already in use, ignoring" );
return false;
}
SGIOChannel *io = get_io_channel();
if ( ! io->open( get_direction() ) ) {
- FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
+ SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
return false;
}
if ( get_direction() == SG_IO_OUT ) {
gen_message();
if ( ! io->write( buf, length ) ) {
- FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
+ SG_LOG( SG_IO, SG_WARN, "Error writing data." );
return false;
}
} else if ( get_direction() == SG_IO_IN ) {
if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
- FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
+ SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
if ( parse_message() ) {
- FG_LOG( FG_IO, FG_ALERT, "Success parsing data." );
+ SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
} else {
- FG_LOG( FG_IO, FG_ALERT, "Error parsing data." );
+ SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
}
} else {
- FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
+ SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
return false;
}
if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
- FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
+ SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
if ( parse_message() ) {
- FG_LOG( FG_IO, FG_ALERT, "Success parsing data." );
+ SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
} else {
- FG_LOG( FG_IO, FG_ALERT, "Error parsing data." );
+ SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
}
} else {
- FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
+ SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
return false;
}
}