1 // atlas.cxx -- Atlas 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.
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/math/sg_geodesy.hxx>
32 #include <simgear/io/iochannel.hxx>
33 #include <simgear/timing/sg_time.hxx>
35 #include <FDM/flightProperties.hxx>
36 #include <Main/globals.hxx>
37 #include <Main/fg_props.hxx>
38 #include <Main/fg_init.hxx>
45 fdm(new FlightProperties)
47 _adf_freq = fgGetNode("/instrumentation/adf/frequencies/selected-khz", true);
48 _nav1_freq = fgGetNode("/instrumentation/nav/frequencies/selected-mhz", true);
49 _nav1_sel_radial = fgGetNode("/instrumentation/nav/radials/selected-deg", true);
50 _nav2_freq = fgGetNode("/instrumentation/nav[1]/frequencies/selected-mhz", true);
51 _nav2_sel_radial = fgGetNode("/instrumentation/nav[1]/radials/selected-deg", true);
59 // calculate the atlas check sum
60 static char calc_atlas_cksum(char *sentence) {
61 unsigned char sum = 0;
64 // cout << sentence << endl;
66 len = strlen(sentence);
68 for ( i = 1; i < len; i++ ) {
69 // cout << sentence[i];
74 // printf("sum = %02x\n", sum);
78 // generate Atlas message
79 bool FGAtlas::gen_message() {
80 // cout << "generating atlas message" << endl;
81 char rmc[256], gga[256], patla[256];
82 char rmc_sum[10], gga_sum[10], patla_sum[10];
87 SGTime *t = globals->get_time_params();
90 sprintf( utc, "%02d%02d%02d",
91 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
94 double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
102 min = (latd - (double)deg) * 60.0;
103 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
106 double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
114 min = (lond - (double)deg) * 60.0;
115 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
118 sprintf( speed, "%05.1f", fdm->get_V_equiv_kts() );
121 sprintf( heading, "%05.1f", fdm->get_Psi() * SGD_RADIANS_TO_DEGREES );
124 sprintf( altitude_m, "%02d",
125 (int)(fdm->get_Altitude() * SG_FEET_TO_METER) );
127 char altitude_ft[10];
128 sprintf( altitude_ft, "%02d", (int)fdm->get_Altitude() );
131 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
132 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
134 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
135 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,0.000,E",
136 utc, lat, lon, speed, heading, date );
137 sprintf( rmc_sum, "%02X", calc_atlas_cksum(rmc) );
139 sprintf( gga, "GPGGA,%s,%s,%s,1,,,%s,F,,,,",
140 utc, lat, lon, altitude_ft );
141 sprintf( gga_sum, "%02X", calc_atlas_cksum(gga) );
143 sprintf( patla, "PATLA,%.2f,%.1f,%.2f,%.1f,%.0f",
144 _nav1_freq->getDoubleValue(),
145 _nav1_sel_radial->getDoubleValue(),
146 _nav2_freq->getDoubleValue(),
147 _nav2_sel_radial->getDoubleValue(),
148 _adf_freq->getDoubleValue() );
149 sprintf( patla_sum, "%02X", calc_atlas_cksum(patla) );
151 SG_LOG( SG_IO, SG_DEBUG, rmc );
152 SG_LOG( SG_IO, SG_DEBUG, gga );
153 SG_LOG( SG_IO, SG_DEBUG, patla );
155 string atlas_sentence;
158 atlas_sentence = "$";
159 atlas_sentence += rmc;
160 atlas_sentence += "*";
161 atlas_sentence += rmc_sum;
162 atlas_sentence += "\n";
165 atlas_sentence += "$";
166 atlas_sentence += gga;
167 atlas_sentence += "*";
168 atlas_sentence += gga_sum;
169 atlas_sentence += "\n";
172 atlas_sentence += "$";
173 atlas_sentence += patla;
174 atlas_sentence += "*";
175 atlas_sentence += patla_sum;
176 atlas_sentence += "\n";
178 // cout << atlas_sentence;
180 length = atlas_sentence.length();
181 strncpy( buf, atlas_sentence.c_str(), length );
187 // parse Atlas message. messages will look something like the
190 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
191 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
193 bool FGAtlas::parse_message() {
194 SG_LOG( SG_IO, SG_INFO, "parse atlas message" );
197 msg = msg.substr( 0, length );
198 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
200 string::size_type begin_line, end_line, begin, end;
201 begin_line = begin = 0;
203 // extract out each line
204 end_line = msg.find("\n", begin_line);
205 while ( end_line != string::npos ) {
206 string line = msg.substr(begin_line, end_line - begin_line);
207 begin_line = end_line + 1;
208 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
211 string start = msg.substr(begin, 1);
213 SG_LOG( SG_IO, SG_INFO, " start = " << start );
216 end = msg.find(",", begin);
217 if ( end == string::npos ) {
221 string sentence = msg.substr(begin, end - begin);
223 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
225 double lon_deg, lon_min, lat_deg, lat_min;
226 double lon, lat, speed, heading, altitude;
228 if ( sentence == "GPRMC" ) {
230 end = msg.find(",", begin);
231 if ( end == string::npos ) {
235 string utc = msg.substr(begin, end - begin);
237 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
240 end = msg.find(",", begin);
241 if ( end == string::npos ) {
245 string junk = msg.substr(begin, end - begin);
247 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
250 end = msg.find(",", begin);
251 if ( end == string::npos ) {
255 string lat_str = msg.substr(begin, end - begin);
258 lat_deg = atof( lat_str.substr(0, 2).c_str() );
259 lat_min = atof( lat_str.substr(2).c_str() );
262 end = msg.find(",", begin);
263 if ( end == string::npos ) {
267 string lat_dir = msg.substr(begin, end - begin);
270 lat = lat_deg + ( lat_min / 60.0 );
271 if ( lat_dir == "S" ) {
275 fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
276 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
279 end = msg.find(",", begin);
280 if ( end == string::npos ) {
284 string lon_str = msg.substr(begin, end - begin);
287 lon_deg = atof( lon_str.substr(0, 3).c_str() );
288 lon_min = atof( lon_str.substr(3).c_str() );
291 end = msg.find(",", begin);
292 if ( end == string::npos ) {
296 string lon_dir = msg.substr(begin, end - begin);
299 lon = lon_deg + ( lon_min / 60.0 );
300 if ( lon_dir == "W" ) {
304 fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
305 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
308 double sl_radius, lat_geoc;
309 sgGeodToGeoc( fdm->get_Latitude(),
311 &sl_radius, &lat_geoc );
312 fdm->set_Geocentric_Position( lat_geoc,
313 fdm->get_Longitude(),
314 sl_radius + fdm->get_Altitude() );
318 end = msg.find(",", begin);
319 if ( end == string::npos ) {
323 string speed_str = msg.substr(begin, end - begin);
325 speed = atof( speed_str.c_str() );
326 fdm->set_V_calibrated_kts( speed );
327 // fdm->set_V_ground_speed( speed );
328 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
331 end = msg.find(",", begin);
332 if ( end == string::npos ) {
336 string hdg_str = msg.substr(begin, end - begin);
338 heading = atof( hdg_str.c_str() );
339 fdm->set_Euler_Angles( fdm->get_Phi(),
341 heading * SGD_DEGREES_TO_RADIANS );
342 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
343 } else if ( sentence == "GPGGA" ) {
345 end = msg.find(",", begin);
346 if ( end == string::npos ) {
350 string utc = msg.substr(begin, end - begin);
352 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
355 end = msg.find(",", begin);
356 if ( end == string::npos ) {
360 string lat_str = msg.substr(begin, end - begin);
363 lat_deg = atof( lat_str.substr(0, 2).c_str() );
364 lat_min = atof( lat_str.substr(2).c_str() );
367 end = msg.find(",", begin);
368 if ( end == string::npos ) {
372 string lat_dir = msg.substr(begin, end - begin);
375 lat = lat_deg + ( lat_min / 60.0 );
376 if ( lat_dir == "S" ) {
380 // fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
381 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
384 end = msg.find(",", begin);
385 if ( end == string::npos ) {
389 string lon_str = msg.substr(begin, end - begin);
392 lon_deg = atof( lon_str.substr(0, 3).c_str() );
393 lon_min = atof( lon_str.substr(3).c_str() );
396 end = msg.find(",", begin);
397 if ( end == string::npos ) {
401 string lon_dir = msg.substr(begin, end - begin);
404 lon = lon_deg + ( lon_min / 60.0 );
405 if ( lon_dir == "W" ) {
409 // fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
410 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
413 end = msg.find(",", begin);
414 if ( end == string::npos ) {
418 string junk = msg.substr(begin, end - begin);
420 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
423 end = msg.find(",", begin);
424 if ( end == string::npos ) {
428 junk = msg.substr(begin, end - begin);
430 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
433 end = msg.find(",", begin);
434 if ( end == string::npos ) {
438 junk = msg.substr(begin, end - begin);
440 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
443 end = msg.find(",", begin);
444 if ( end == string::npos ) {
448 string alt_str = msg.substr(begin, end - begin);
449 altitude = atof( alt_str.c_str() );
453 end = msg.find(",", begin);
454 if ( end == string::npos ) {
458 string alt_units = msg.substr(begin, end - begin);
461 if ( alt_units != "F" ) {
462 altitude *= SG_METER_TO_FEET;
465 fdm->set_Altitude( altitude );
467 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
469 } else if ( sentence == "PATLA" ) {
471 end = msg.find(",", begin);
472 if ( end == string::npos ) {
476 string nav1_freq = msg.substr(begin, end - begin);
478 SG_LOG( SG_IO, SG_INFO, " nav1_freq = " << nav1_freq );
480 // nav1 selected radial
481 end = msg.find(",", begin);
482 if ( end == string::npos ) {
486 string nav1_rad = msg.substr(begin, end - begin);
488 SG_LOG( SG_IO, SG_INFO, " nav1_rad = " << nav1_rad );
491 end = msg.find(",", begin);
492 if ( end == string::npos ) {
496 string nav2_freq = msg.substr(begin, end - begin);
498 SG_LOG( SG_IO, SG_INFO, " nav2_freq = " << nav2_freq );
500 // nav2 selected radial
501 end = msg.find(",", begin);
502 if ( end == string::npos ) {
506 string nav2_rad = msg.substr(begin, end - begin);
508 SG_LOG( SG_IO, SG_INFO, " nav2_rad = " << nav2_rad );
511 end = msg.find("*", begin);
512 if ( end == string::npos ) {
516 string adf_freq = msg.substr(begin, end - begin);
518 SG_LOG( SG_IO, SG_INFO, " adf_freq = " << adf_freq );
521 // printf("%.8f %.8f\n", lon, lat);
524 end_line = msg.find("\n", begin_line);
531 // open hailing frequencies
532 bool FGAtlas::open() {
533 if ( is_enabled() ) {
534 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
535 << "is already in use, ignoring" );
539 SGIOChannel *io = get_io_channel();
541 if ( ! io->open( get_direction() ) ) {
542 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
552 // process work for this port
553 bool FGAtlas::process() {
554 SGIOChannel *io = get_io_channel();
556 if ( get_direction() == SG_IO_OUT ) {
558 if ( ! io->write( buf, length ) ) {
559 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
562 } else if ( get_direction() == SG_IO_IN ) {
563 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
566 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
569 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
572 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
582 bool FGAtlas::close() {
583 SG_LOG( SG_IO, SG_INFO, "closing FGAtlas" );
584 SGIOChannel *io = get_io_channel();
586 set_enabled( false );
588 if ( ! io->close() ) {