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.
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/math/sg_geodesy.hxx>
29 #include <simgear/io/iochannel.hxx>
34 #include <FDM/flight.hxx>
35 #include <Main/globals.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/fg_init.hxx>
49 // calculate the atlas check sum
50 static char calc_atlas_cksum(char *sentence) {
51 unsigned char sum = 0;
54 // cout << sentence << endl;
56 len = strlen(sentence);
58 for ( i = 1; i < len; i++ ) {
59 // cout << sentence[i];
64 // printf("sum = %02x\n", sum);
69 // generate Atlas message
70 bool FGAtlas::gen_message() {
71 // cout << "generating atlas message" << endl;
73 static SGPropertyNode *adf_freq
74 = fgGetNode("/instrumentation/adf/frequencies/selected-khz", true);
75 static SGPropertyNode *nav1_freq
76 = fgGetNode("/instrumentation/nav/frequencies/selected-mhz", true);
77 static SGPropertyNode *nav1_sel_radial
78 = fgGetNode("/instrumentation/nav/radials/selected-deg", true);
79 static SGPropertyNode *nav2_freq
80 = fgGetNode("/instrumentation/nav[1]/frequencies/selected-mhz", true);
81 static SGPropertyNode *nav2_sel_radial
82 = fgGetNode("/instrumentation/nav[1]/radials/selected-deg", true);
84 char rmc[256], gga[256], patla[256];
85 char rmc_sum[10], gga_sum[10], patla_sum[10];
90 SGTime *t = globals->get_time_params();
93 sprintf( utc, "%02d%02d%02d",
94 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
97 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
105 min = (latd - (double)deg) * 60.0;
106 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
109 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
117 min = (lond - (double)deg) * 60.0;
118 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
121 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
124 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
127 sprintf( altitude_m, "%02d",
128 (int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
130 char altitude_ft[10];
131 sprintf( altitude_ft, "%02d", (int)cur_fdm_state->get_Altitude() );
134 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
135 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
137 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
138 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,0.000,E",
139 utc, lat, lon, speed, heading, date );
140 sprintf( rmc_sum, "%02X", calc_atlas_cksum(rmc) );
142 sprintf( gga, "GPGGA,%s,%s,%s,1,,,%s,F,,,,",
143 utc, lat, lon, altitude_ft );
144 sprintf( gga_sum, "%02X", calc_atlas_cksum(gga) );
146 sprintf( patla, "PATLA,%.2f,%.1f,%.2f,%.1f,%.0f",
147 nav1_freq->getDoubleValue(),
148 nav1_sel_radial->getDoubleValue(),
149 nav2_freq->getDoubleValue(),
150 nav2_sel_radial->getDoubleValue(),
151 adf_freq->getDoubleValue() );
152 sprintf( patla_sum, "%02X", calc_atlas_cksum(patla) );
154 SG_LOG( SG_IO, SG_DEBUG, rmc );
155 SG_LOG( SG_IO, SG_DEBUG, gga );
156 SG_LOG( SG_IO, SG_DEBUG, patla );
158 string atlas_sentence;
161 atlas_sentence = "$";
162 atlas_sentence += rmc;
163 atlas_sentence += "*";
164 atlas_sentence += rmc_sum;
165 atlas_sentence += "\n";
168 atlas_sentence += "$";
169 atlas_sentence += gga;
170 atlas_sentence += "*";
171 atlas_sentence += gga_sum;
172 atlas_sentence += "\n";
175 atlas_sentence += "$";
176 atlas_sentence += patla;
177 atlas_sentence += "*";
178 atlas_sentence += patla_sum;
179 atlas_sentence += "\n";
181 // cout << atlas_sentence;
183 length = atlas_sentence.length();
184 strncpy( buf, atlas_sentence.c_str(), length );
190 // parse Atlas message. messages will look something like the
193 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
194 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
196 bool FGAtlas::parse_message() {
197 SG_LOG( SG_IO, SG_INFO, "parse atlas message" );
200 msg = msg.substr( 0, length );
201 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
203 string::size_type begin_line, end_line, begin, end;
204 begin_line = begin = 0;
206 // extract out each line
207 end_line = msg.find("\n", begin_line);
208 while ( end_line != string::npos ) {
209 string line = msg.substr(begin_line, end_line - begin_line);
210 begin_line = end_line + 1;
211 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
214 string start = msg.substr(begin, 1);
216 SG_LOG( SG_IO, SG_INFO, " start = " << start );
219 end = msg.find(",", begin);
220 if ( end == string::npos ) {
224 string sentence = msg.substr(begin, end - begin);
226 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
228 double lon_deg, lon_min, lat_deg, lat_min;
229 double lon, lat, speed, heading, altitude;
231 if ( sentence == "GPRMC" ) {
233 end = msg.find(",", begin);
234 if ( end == string::npos ) {
238 string utc = msg.substr(begin, end - begin);
240 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
243 end = msg.find(",", begin);
244 if ( end == string::npos ) {
248 string junk = msg.substr(begin, end - begin);
250 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
253 end = msg.find(",", begin);
254 if ( end == string::npos ) {
258 string lat_str = msg.substr(begin, end - begin);
261 lat_deg = atof( lat_str.substr(0, 2).c_str() );
262 lat_min = atof( lat_str.substr(2).c_str() );
265 end = msg.find(",", begin);
266 if ( end == string::npos ) {
270 string lat_dir = msg.substr(begin, end - begin);
273 lat = lat_deg + ( lat_min / 60.0 );
274 if ( lat_dir == "S" ) {
278 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
279 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
282 end = msg.find(",", begin);
283 if ( end == string::npos ) {
287 string lon_str = msg.substr(begin, end - begin);
290 lon_deg = atof( lon_str.substr(0, 3).c_str() );
291 lon_min = atof( lon_str.substr(3).c_str() );
294 end = msg.find(",", begin);
295 if ( end == string::npos ) {
299 string lon_dir = msg.substr(begin, end - begin);
302 lon = lon_deg + ( lon_min / 60.0 );
303 if ( lon_dir == "W" ) {
307 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
308 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
311 double sl_radius, lat_geoc;
312 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
313 cur_fdm_state->get_Altitude(),
314 &sl_radius, &lat_geoc );
315 cur_fdm_state->set_Geocentric_Position( lat_geoc,
316 cur_fdm_state->get_Longitude(),
317 sl_radius + cur_fdm_state->get_Altitude() );
321 end = msg.find(",", begin);
322 if ( end == string::npos ) {
326 string speed_str = msg.substr(begin, end - begin);
328 speed = atof( speed_str.c_str() );
329 cur_fdm_state->set_V_calibrated_kts( speed );
330 // cur_fdm_state->set_V_ground_speed( speed );
331 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
334 end = msg.find(",", begin);
335 if ( end == string::npos ) {
339 string hdg_str = msg.substr(begin, end - begin);
341 heading = atof( hdg_str.c_str() );
342 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
343 cur_fdm_state->get_Theta(),
344 heading * SGD_DEGREES_TO_RADIANS );
345 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
346 } else if ( sentence == "GPGGA" ) {
348 end = msg.find(",", begin);
349 if ( end == string::npos ) {
353 string utc = msg.substr(begin, end - begin);
355 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
358 end = msg.find(",", begin);
359 if ( end == string::npos ) {
363 string lat_str = msg.substr(begin, end - begin);
366 lat_deg = atof( lat_str.substr(0, 2).c_str() );
367 lat_min = atof( lat_str.substr(2).c_str() );
370 end = msg.find(",", begin);
371 if ( end == string::npos ) {
375 string lat_dir = msg.substr(begin, end - begin);
378 lat = lat_deg + ( lat_min / 60.0 );
379 if ( lat_dir == "S" ) {
383 // cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
384 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
387 end = msg.find(",", begin);
388 if ( end == string::npos ) {
392 string lon_str = msg.substr(begin, end - begin);
395 lon_deg = atof( lon_str.substr(0, 3).c_str() );
396 lon_min = atof( lon_str.substr(3).c_str() );
399 end = msg.find(",", begin);
400 if ( end == string::npos ) {
404 string lon_dir = msg.substr(begin, end - begin);
407 lon = lon_deg + ( lon_min / 60.0 );
408 if ( lon_dir == "W" ) {
412 // cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
413 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
416 end = msg.find(",", begin);
417 if ( end == string::npos ) {
421 string junk = msg.substr(begin, end - begin);
423 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
426 end = msg.find(",", begin);
427 if ( end == string::npos ) {
431 junk = msg.substr(begin, end - begin);
433 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
436 end = msg.find(",", begin);
437 if ( end == string::npos ) {
441 junk = msg.substr(begin, end - begin);
443 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
446 end = msg.find(",", begin);
447 if ( end == string::npos ) {
451 string alt_str = msg.substr(begin, end - begin);
452 altitude = atof( alt_str.c_str() );
456 end = msg.find(",", begin);
457 if ( end == string::npos ) {
461 string alt_units = msg.substr(begin, end - begin);
464 if ( alt_units != "F" ) {
465 altitude *= SG_METER_TO_FEET;
468 cur_fdm_state->set_Altitude( altitude );
470 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
472 } else if ( sentence == "PATLA" ) {
474 end = msg.find(",", begin);
475 if ( end == string::npos ) {
479 string nav1_freq = msg.substr(begin, end - begin);
481 SG_LOG( SG_IO, SG_INFO, " nav1_freq = " << nav1_freq );
483 // nav1 selected radial
484 end = msg.find(",", begin);
485 if ( end == string::npos ) {
489 string nav1_rad = msg.substr(begin, end - begin);
491 SG_LOG( SG_IO, SG_INFO, " nav1_rad = " << nav1_rad );
494 end = msg.find(",", begin);
495 if ( end == string::npos ) {
499 string nav2_freq = msg.substr(begin, end - begin);
501 SG_LOG( SG_IO, SG_INFO, " nav2_freq = " << nav2_freq );
503 // nav2 selected radial
504 end = msg.find(",", begin);
505 if ( end == string::npos ) {
509 string nav2_rad = msg.substr(begin, end - begin);
511 SG_LOG( SG_IO, SG_INFO, " nav2_rad = " << nav2_rad );
514 end = msg.find("*", begin);
515 if ( end == string::npos ) {
519 string adf_freq = msg.substr(begin, end - begin);
521 SG_LOG( SG_IO, SG_INFO, " adf_freq = " << adf_freq );
524 // printf("%.8f %.8f\n", lon, lat);
527 end_line = msg.find("\n", begin_line);
534 // open hailing frequencies
535 bool FGAtlas::open() {
536 if ( is_enabled() ) {
537 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
538 << "is already in use, ignoring" );
542 SGIOChannel *io = get_io_channel();
544 if ( ! io->open( get_direction() ) ) {
545 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
555 // process work for this port
556 bool FGAtlas::process() {
557 SGIOChannel *io = get_io_channel();
559 if ( get_direction() == SG_IO_OUT ) {
561 if ( ! io->write( buf, length ) ) {
562 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
565 } else if ( get_direction() == SG_IO_IN ) {
566 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
569 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
572 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
575 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
585 bool FGAtlas::close() {
586 SG_LOG( SG_IO, SG_INFO, "closing FGAtlas" );
587 SGIOChannel *io = get_io_channel();
589 set_enabled( false );
591 if ( ! io->close() ) {