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.
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/flightProperties.hxx>
35 #include <Main/globals.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/fg_init.hxx>
43 fdm = new FlightProperties;
51 // calculate the atlas check sum
52 static char calc_atlas_cksum(char *sentence) {
53 unsigned char sum = 0;
56 // cout << sentence << endl;
58 len = strlen(sentence);
60 for ( i = 1; i < len; i++ ) {
61 // cout << sentence[i];
66 // printf("sum = %02x\n", sum);
70 // generate Atlas message
71 bool FGAtlas::gen_message() {
72 // cout << "generating atlas message" << endl;
74 static SGPropertyNode *adf_freq
75 = fgGetNode("/instrumentation/adf/frequencies/selected-khz", true);
76 static SGPropertyNode *nav1_freq
77 = fgGetNode("/instrumentation/nav/frequencies/selected-mhz", true);
78 static SGPropertyNode *nav1_sel_radial
79 = fgGetNode("/instrumentation/nav/radials/selected-deg", true);
80 static SGPropertyNode *nav2_freq
81 = fgGetNode("/instrumentation/nav[1]/frequencies/selected-mhz", true);
82 static SGPropertyNode *nav2_sel_radial
83 = fgGetNode("/instrumentation/nav[1]/radials/selected-deg", true);
85 char rmc[256], gga[256], patla[256];
86 char rmc_sum[10], gga_sum[10], patla_sum[10];
91 SGTime *t = globals->get_time_params();
94 sprintf( utc, "%02d%02d%02d",
95 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
98 double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
106 min = (latd - (double)deg) * 60.0;
107 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
110 double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
118 min = (lond - (double)deg) * 60.0;
119 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
122 sprintf( speed, "%05.1f", fdm->get_V_equiv_kts() );
125 sprintf( heading, "%05.1f", fdm->get_Psi() * SGD_RADIANS_TO_DEGREES );
128 sprintf( altitude_m, "%02d",
129 (int)(fdm->get_Altitude() * SG_FEET_TO_METER) );
131 char altitude_ft[10];
132 sprintf( altitude_ft, "%02d", (int)fdm->get_Altitude() );
135 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
136 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
138 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
139 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,0.000,E",
140 utc, lat, lon, speed, heading, date );
141 sprintf( rmc_sum, "%02X", calc_atlas_cksum(rmc) );
143 sprintf( gga, "GPGGA,%s,%s,%s,1,,,%s,F,,,,",
144 utc, lat, lon, altitude_ft );
145 sprintf( gga_sum, "%02X", calc_atlas_cksum(gga) );
147 sprintf( patla, "PATLA,%.2f,%.1f,%.2f,%.1f,%.0f",
148 nav1_freq->getDoubleValue(),
149 nav1_sel_radial->getDoubleValue(),
150 nav2_freq->getDoubleValue(),
151 nav2_sel_radial->getDoubleValue(),
152 adf_freq->getDoubleValue() );
153 sprintf( patla_sum, "%02X", calc_atlas_cksum(patla) );
155 SG_LOG( SG_IO, SG_DEBUG, rmc );
156 SG_LOG( SG_IO, SG_DEBUG, gga );
157 SG_LOG( SG_IO, SG_DEBUG, patla );
159 string atlas_sentence;
162 atlas_sentence = "$";
163 atlas_sentence += rmc;
164 atlas_sentence += "*";
165 atlas_sentence += rmc_sum;
166 atlas_sentence += "\n";
169 atlas_sentence += "$";
170 atlas_sentence += gga;
171 atlas_sentence += "*";
172 atlas_sentence += gga_sum;
173 atlas_sentence += "\n";
176 atlas_sentence += "$";
177 atlas_sentence += patla;
178 atlas_sentence += "*";
179 atlas_sentence += patla_sum;
180 atlas_sentence += "\n";
182 // cout << atlas_sentence;
184 length = atlas_sentence.length();
185 strncpy( buf, atlas_sentence.c_str(), length );
191 // parse Atlas message. messages will look something like the
194 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
195 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
197 bool FGAtlas::parse_message() {
198 SG_LOG( SG_IO, SG_INFO, "parse atlas message" );
201 msg = msg.substr( 0, length );
202 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
204 string::size_type begin_line, end_line, begin, end;
205 begin_line = begin = 0;
207 // extract out each line
208 end_line = msg.find("\n", begin_line);
209 while ( end_line != string::npos ) {
210 string line = msg.substr(begin_line, end_line - begin_line);
211 begin_line = end_line + 1;
212 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
215 string start = msg.substr(begin, 1);
217 SG_LOG( SG_IO, SG_INFO, " start = " << start );
220 end = msg.find(",", begin);
221 if ( end == string::npos ) {
225 string sentence = msg.substr(begin, end - begin);
227 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
229 double lon_deg, lon_min, lat_deg, lat_min;
230 double lon, lat, speed, heading, altitude;
232 if ( sentence == "GPRMC" ) {
234 end = msg.find(",", begin);
235 if ( end == string::npos ) {
239 string utc = msg.substr(begin, end - begin);
241 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
244 end = msg.find(",", begin);
245 if ( end == string::npos ) {
249 string junk = msg.substr(begin, end - begin);
251 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
254 end = msg.find(",", begin);
255 if ( end == string::npos ) {
259 string lat_str = msg.substr(begin, end - begin);
262 lat_deg = atof( lat_str.substr(0, 2).c_str() );
263 lat_min = atof( lat_str.substr(2).c_str() );
266 end = msg.find(",", begin);
267 if ( end == string::npos ) {
271 string lat_dir = msg.substr(begin, end - begin);
274 lat = lat_deg + ( lat_min / 60.0 );
275 if ( lat_dir == "S" ) {
279 fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
280 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
283 end = msg.find(",", begin);
284 if ( end == string::npos ) {
288 string lon_str = msg.substr(begin, end - begin);
291 lon_deg = atof( lon_str.substr(0, 3).c_str() );
292 lon_min = atof( lon_str.substr(3).c_str() );
295 end = msg.find(",", begin);
296 if ( end == string::npos ) {
300 string lon_dir = msg.substr(begin, end - begin);
303 lon = lon_deg + ( lon_min / 60.0 );
304 if ( lon_dir == "W" ) {
308 fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
309 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
312 double sl_radius, lat_geoc;
313 sgGeodToGeoc( fdm->get_Latitude(),
315 &sl_radius, &lat_geoc );
316 fdm->set_Geocentric_Position( lat_geoc,
317 fdm->get_Longitude(),
318 sl_radius + fdm->get_Altitude() );
322 end = msg.find(",", begin);
323 if ( end == string::npos ) {
327 string speed_str = msg.substr(begin, end - begin);
329 speed = atof( speed_str.c_str() );
330 fdm->set_V_calibrated_kts( speed );
331 // fdm->set_V_ground_speed( speed );
332 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
335 end = msg.find(",", begin);
336 if ( end == string::npos ) {
340 string hdg_str = msg.substr(begin, end - begin);
342 heading = atof( hdg_str.c_str() );
343 fdm->set_Euler_Angles( fdm->get_Phi(),
345 heading * SGD_DEGREES_TO_RADIANS );
346 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
347 } else if ( sentence == "GPGGA" ) {
349 end = msg.find(",", begin);
350 if ( end == string::npos ) {
354 string utc = msg.substr(begin, end - begin);
356 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
359 end = msg.find(",", begin);
360 if ( end == string::npos ) {
364 string lat_str = msg.substr(begin, end - begin);
367 lat_deg = atof( lat_str.substr(0, 2).c_str() );
368 lat_min = atof( lat_str.substr(2).c_str() );
371 end = msg.find(",", begin);
372 if ( end == string::npos ) {
376 string lat_dir = msg.substr(begin, end - begin);
379 lat = lat_deg + ( lat_min / 60.0 );
380 if ( lat_dir == "S" ) {
384 // fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
385 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
388 end = msg.find(",", begin);
389 if ( end == string::npos ) {
393 string lon_str = msg.substr(begin, end - begin);
396 lon_deg = atof( lon_str.substr(0, 3).c_str() );
397 lon_min = atof( lon_str.substr(3).c_str() );
400 end = msg.find(",", begin);
401 if ( end == string::npos ) {
405 string lon_dir = msg.substr(begin, end - begin);
408 lon = lon_deg + ( lon_min / 60.0 );
409 if ( lon_dir == "W" ) {
413 // fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
414 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
417 end = msg.find(",", begin);
418 if ( end == string::npos ) {
422 string junk = msg.substr(begin, end - begin);
424 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
427 end = msg.find(",", begin);
428 if ( end == string::npos ) {
432 junk = msg.substr(begin, end - begin);
434 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
437 end = msg.find(",", begin);
438 if ( end == string::npos ) {
442 junk = msg.substr(begin, end - begin);
444 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
447 end = msg.find(",", begin);
448 if ( end == string::npos ) {
452 string alt_str = msg.substr(begin, end - begin);
453 altitude = atof( alt_str.c_str() );
457 end = msg.find(",", begin);
458 if ( end == string::npos ) {
462 string alt_units = msg.substr(begin, end - begin);
465 if ( alt_units != "F" ) {
466 altitude *= SG_METER_TO_FEET;
469 fdm->set_Altitude( altitude );
471 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
473 } else if ( sentence == "PATLA" ) {
475 end = msg.find(",", begin);
476 if ( end == string::npos ) {
480 string nav1_freq = msg.substr(begin, end - begin);
482 SG_LOG( SG_IO, SG_INFO, " nav1_freq = " << nav1_freq );
484 // nav1 selected radial
485 end = msg.find(",", begin);
486 if ( end == string::npos ) {
490 string nav1_rad = msg.substr(begin, end - begin);
492 SG_LOG( SG_IO, SG_INFO, " nav1_rad = " << nav1_rad );
495 end = msg.find(",", begin);
496 if ( end == string::npos ) {
500 string nav2_freq = msg.substr(begin, end - begin);
502 SG_LOG( SG_IO, SG_INFO, " nav2_freq = " << nav2_freq );
504 // nav2 selected radial
505 end = msg.find(",", begin);
506 if ( end == string::npos ) {
510 string nav2_rad = msg.substr(begin, end - begin);
512 SG_LOG( SG_IO, SG_INFO, " nav2_rad = " << nav2_rad );
515 end = msg.find("*", begin);
516 if ( end == string::npos ) {
520 string adf_freq = msg.substr(begin, end - begin);
522 SG_LOG( SG_IO, SG_INFO, " adf_freq = " << adf_freq );
525 // printf("%.8f %.8f\n", lon, lat);
528 end_line = msg.find("\n", begin_line);
535 // open hailing frequencies
536 bool FGAtlas::open() {
537 if ( is_enabled() ) {
538 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
539 << "is already in use, ignoring" );
543 SGIOChannel *io = get_io_channel();
545 if ( ! io->open( get_direction() ) ) {
546 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
556 // process work for this port
557 bool FGAtlas::process() {
558 SGIOChannel *io = get_io_channel();
560 if ( get_direction() == SG_IO_OUT ) {
562 if ( ! io->write( buf, length ) ) {
563 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
566 } else if ( get_direction() == SG_IO_IN ) {
567 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
570 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
573 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
576 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
586 bool FGAtlas::close() {
587 SG_LOG( SG_IO, SG_INFO, "closing FGAtlas" );
588 SGIOChannel *io = get_io_channel();
590 set_enabled( false );
592 if ( ! io->close() ) {