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.
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
33 #include <simgear/io/iochannel.hxx>
34 #include <simgear/timing/sg_time.hxx>
36 #include <FDM/flightProperties.hxx>
37 #include <Main/globals.hxx>
38 #include <Main/fg_props.hxx>
39 #include <Main/fg_init.hxx>
46 fdm(new FlightProperties)
48 _adf_freq = fgGetNode("/instrumentation/adf/frequencies/selected-khz", true);
49 _nav1_freq = fgGetNode("/instrumentation/nav/frequencies/selected-mhz", true);
50 _nav1_sel_radial = fgGetNode("/instrumentation/nav/radials/selected-deg", true);
51 _nav2_freq = fgGetNode("/instrumentation/nav[1]/frequencies/selected-mhz", true);
52 _nav2_sel_radial = fgGetNode("/instrumentation/nav[1]/radials/selected-deg", true);
60 // calculate the atlas check sum
61 static char calc_atlas_cksum(char *sentence) {
62 unsigned char sum = 0;
65 // cout << sentence << endl;
67 len = strlen(sentence);
69 for ( i = 1; i < len; i++ ) {
70 // cout << sentence[i];
75 // printf("sum = %02x\n", sum);
79 // generate Atlas message
80 bool FGAtlas::gen_message() {
81 // cout << "generating atlas message" << endl;
82 char rmc[256], gga[256], patla[256];
83 char rmc_sum[10], gga_sum[10], patla_sum[10];
88 SGTime *t = globals->get_time_params();
91 sprintf( utc, "%02d%02d%02d",
92 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
95 double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
103 min = (latd - (double)deg) * 60.0;
104 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
107 double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
115 min = (lond - (double)deg) * 60.0;
116 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
119 sprintf( speed, "%05.1f", fdm->get_V_equiv_kts() );
122 sprintf( heading, "%05.1f", fdm->get_Psi() * SGD_RADIANS_TO_DEGREES );
125 sprintf( altitude_m, "%02d",
126 (int)(fdm->get_Altitude() * SG_FEET_TO_METER) );
128 char altitude_ft[10];
129 sprintf( altitude_ft, "%02d", (int)fdm->get_Altitude() );
132 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
133 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
135 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
136 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,0.000,E",
137 utc, lat, lon, speed, heading, date );
138 sprintf( rmc_sum, "%02X", calc_atlas_cksum(rmc) );
140 sprintf( gga, "GPGGA,%s,%s,%s,1,,,%s,F,,,,",
141 utc, lat, lon, altitude_ft );
142 sprintf( gga_sum, "%02X", calc_atlas_cksum(gga) );
144 sprintf( patla, "PATLA,%.2f,%.1f,%.2f,%.1f,%.0f",
145 _nav1_freq->getDoubleValue(),
146 _nav1_sel_radial->getDoubleValue(),
147 _nav2_freq->getDoubleValue(),
148 _nav2_sel_radial->getDoubleValue(),
149 _adf_freq->getDoubleValue() );
150 sprintf( patla_sum, "%02X", calc_atlas_cksum(patla) );
152 SG_LOG( SG_IO, SG_DEBUG, rmc );
153 SG_LOG( SG_IO, SG_DEBUG, gga );
154 SG_LOG( SG_IO, SG_DEBUG, patla );
156 string atlas_sentence;
159 atlas_sentence = "$";
160 atlas_sentence += rmc;
161 atlas_sentence += "*";
162 atlas_sentence += rmc_sum;
163 atlas_sentence += "\n";
166 atlas_sentence += "$";
167 atlas_sentence += gga;
168 atlas_sentence += "*";
169 atlas_sentence += gga_sum;
170 atlas_sentence += "\n";
173 atlas_sentence += "$";
174 atlas_sentence += patla;
175 atlas_sentence += "*";
176 atlas_sentence += patla_sum;
177 atlas_sentence += "\n";
179 // cout << atlas_sentence;
181 length = atlas_sentence.length();
182 strncpy( buf, atlas_sentence.c_str(), length );
188 // parse Atlas message. messages will look something like the
191 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
192 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
194 bool FGAtlas::parse_message() {
195 SG_LOG( SG_IO, SG_INFO, "parse atlas message" );
198 msg = msg.substr( 0, length );
199 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
201 string::size_type begin_line, end_line, begin, end;
202 begin_line = begin = 0;
204 // extract out each line
205 end_line = msg.find("\n", begin_line);
206 while ( end_line != string::npos ) {
207 string line = msg.substr(begin_line, end_line - begin_line);
208 begin_line = end_line + 1;
209 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
212 string start = msg.substr(begin, 1);
214 SG_LOG( SG_IO, SG_INFO, " start = " << start );
217 end = msg.find(",", begin);
218 if ( end == string::npos ) {
222 string sentence = msg.substr(begin, end - begin);
224 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
226 double lon_deg, lon_min, lat_deg, lat_min;
227 double lon, lat, speed, heading, altitude;
229 if ( sentence == "GPRMC" ) {
231 end = msg.find(",", begin);
232 if ( end == string::npos ) {
236 string utc = msg.substr(begin, end - begin);
238 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
241 end = msg.find(",", begin);
242 if ( end == string::npos ) {
246 string junk = msg.substr(begin, end - begin);
248 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
251 end = msg.find(",", begin);
252 if ( end == string::npos ) {
256 string lat_str = msg.substr(begin, end - begin);
259 lat_deg = atof( lat_str.substr(0, 2).c_str() );
260 lat_min = atof( lat_str.substr(2).c_str() );
263 end = msg.find(",", begin);
264 if ( end == string::npos ) {
268 string lat_dir = msg.substr(begin, end - begin);
271 lat = lat_deg + ( lat_min / 60.0 );
272 if ( lat_dir == "S" ) {
276 fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
277 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
280 end = msg.find(",", begin);
281 if ( end == string::npos ) {
285 string lon_str = msg.substr(begin, end - begin);
288 lon_deg = atof( lon_str.substr(0, 3).c_str() );
289 lon_min = atof( lon_str.substr(3).c_str() );
292 end = msg.find(",", begin);
293 if ( end == string::npos ) {
297 string lon_dir = msg.substr(begin, end - begin);
300 lon = lon_deg + ( lon_min / 60.0 );
301 if ( lon_dir == "W" ) {
305 fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
306 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
309 double sl_radius, lat_geoc;
310 sgGeodToGeoc( fdm->get_Latitude(),
312 &sl_radius, &lat_geoc );
313 fdm->set_Geocentric_Position( lat_geoc,
314 fdm->get_Longitude(),
315 sl_radius + fdm->get_Altitude() );
319 end = msg.find(",", begin);
320 if ( end == string::npos ) {
324 string speed_str = msg.substr(begin, end - begin);
326 speed = atof( speed_str.c_str() );
327 fdm->set_V_calibrated_kts( speed );
328 // fdm->set_V_ground_speed( speed );
329 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
332 end = msg.find(",", begin);
333 if ( end == string::npos ) {
337 string hdg_str = msg.substr(begin, end - begin);
339 heading = atof( hdg_str.c_str() );
340 fdm->set_Euler_Angles( fdm->get_Phi(),
342 heading * SGD_DEGREES_TO_RADIANS );
343 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
344 } else if ( sentence == "GPGGA" ) {
346 end = msg.find(",", begin);
347 if ( end == string::npos ) {
351 string utc = msg.substr(begin, end - begin);
353 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
356 end = msg.find(",", begin);
357 if ( end == string::npos ) {
361 string lat_str = msg.substr(begin, end - begin);
364 lat_deg = atof( lat_str.substr(0, 2).c_str() );
365 lat_min = atof( lat_str.substr(2).c_str() );
368 end = msg.find(",", begin);
369 if ( end == string::npos ) {
373 string lat_dir = msg.substr(begin, end - begin);
376 lat = lat_deg + ( lat_min / 60.0 );
377 if ( lat_dir == "S" ) {
381 // fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
382 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
385 end = msg.find(",", begin);
386 if ( end == string::npos ) {
390 string lon_str = msg.substr(begin, end - begin);
393 lon_deg = atof( lon_str.substr(0, 3).c_str() );
394 lon_min = atof( lon_str.substr(3).c_str() );
397 end = msg.find(",", begin);
398 if ( end == string::npos ) {
402 string lon_dir = msg.substr(begin, end - begin);
405 lon = lon_deg + ( lon_min / 60.0 );
406 if ( lon_dir == "W" ) {
410 // fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
411 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
414 end = msg.find(",", begin);
415 if ( end == string::npos ) {
419 string junk = msg.substr(begin, end - begin);
421 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
424 end = msg.find(",", begin);
425 if ( end == string::npos ) {
429 junk = msg.substr(begin, end - begin);
431 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
434 end = msg.find(",", begin);
435 if ( end == string::npos ) {
439 junk = msg.substr(begin, end - begin);
441 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
444 end = msg.find(",", begin);
445 if ( end == string::npos ) {
449 string alt_str = msg.substr(begin, end - begin);
450 altitude = atof( alt_str.c_str() );
454 end = msg.find(",", begin);
455 if ( end == string::npos ) {
459 string alt_units = msg.substr(begin, end - begin);
462 if ( alt_units != "F" ) {
463 altitude *= SG_METER_TO_FEET;
466 fdm->set_Altitude( altitude );
468 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
470 } else if ( sentence == "PATLA" ) {
472 end = msg.find(",", begin);
473 if ( end == string::npos ) {
477 string nav1_freq = msg.substr(begin, end - begin);
479 SG_LOG( SG_IO, SG_INFO, " nav1_freq = " << nav1_freq );
481 // nav1 selected radial
482 end = msg.find(",", begin);
483 if ( end == string::npos ) {
487 string nav1_rad = msg.substr(begin, end - begin);
489 SG_LOG( SG_IO, SG_INFO, " nav1_rad = " << nav1_rad );
492 end = msg.find(",", begin);
493 if ( end == string::npos ) {
497 string nav2_freq = msg.substr(begin, end - begin);
499 SG_LOG( SG_IO, SG_INFO, " nav2_freq = " << nav2_freq );
501 // nav2 selected radial
502 end = msg.find(",", begin);
503 if ( end == string::npos ) {
507 string nav2_rad = msg.substr(begin, end - begin);
509 SG_LOG( SG_IO, SG_INFO, " nav2_rad = " << nav2_rad );
512 end = msg.find("*", begin);
513 if ( end == string::npos ) {
517 string adf_freq = msg.substr(begin, end - begin);
519 SG_LOG( SG_IO, SG_INFO, " adf_freq = " << adf_freq );
522 // printf("%.8f %.8f\n", lon, lat);
525 end_line = msg.find("\n", begin_line);
532 // open hailing frequencies
533 bool FGAtlas::open() {
534 if ( is_enabled() ) {
535 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
536 << "is already in use, ignoring" );
540 SGIOChannel *io = get_io_channel();
542 if ( ! io->open( get_direction() ) ) {
543 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
553 // process work for this port
554 bool FGAtlas::process() {
555 SGIOChannel *io = get_io_channel();
557 if ( get_direction() == SG_IO_OUT ) {
559 if ( ! io->write( buf, length ) ) {
560 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
563 } else if ( get_direction() == SG_IO_IN ) {
564 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
567 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
570 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
573 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
583 bool FGAtlas::close() {
584 SG_LOG( SG_IO, SG_INFO, "closing FGAtlas" );
585 SGIOChannel *io = get_io_channel();
587 set_enabled( false );
589 if ( ! io->close() ) {