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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/math/sg_geodesy.hxx>
26 #include <simgear/io/iochannel.hxx>
29 #include <FDM/flight.hxx>
30 #include <Main/globals.hxx>
31 #include <Main/fg_props.hxx>
32 #include <Main/fg_init.hxx>
44 // calculate the atlas check sum
45 static char calc_atlas_cksum(char *sentence) {
46 unsigned char sum = 0;
49 // cout << sentence << endl;
51 len = strlen(sentence);
53 for ( i = 1; i < len; i++ ) {
54 // cout << sentence[i];
59 // printf("sum = %02x\n", sum);
64 // generate Atlas message
65 bool FGAtlas::gen_message() {
66 // cout << "generating atlas message" << endl;
68 static SGPropertyNode *adf_freq
69 = fgGetNode("/instrumentation/kr-87/outputs/selected-khz", true);
70 static SGPropertyNode *nav_freq
71 = fgGetNode("/instrumentation/nav/frequencies/selected-mhz", true);
72 static SGPropertyNode *nav_sel_radial
73 = fgGetNode("/instrumentation/nav/radials/selected-deg", true);
75 char rmc[256], gga[256], patla[256];
76 char rmc_sum[10], gga_sum[10], patla_sum[10];
81 SGTime *t = globals->get_time_params();
84 sprintf( utc, "%02d%02d%02d",
85 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
88 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
96 min = (latd - (double)deg) * 60.0;
97 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
100 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
108 min = (lond - (double)deg) * 60.0;
109 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
112 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
115 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
118 sprintf( altitude_m, "%02d",
119 (int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
121 char altitude_ft[10];
122 sprintf( altitude_ft, "%02d", (int)cur_fdm_state->get_Altitude() );
125 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
126 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
128 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
129 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,0.000,E",
130 utc, lat, lon, speed, heading, date );
131 sprintf( rmc_sum, "%02X", calc_atlas_cksum(rmc) );
133 sprintf( gga, "GPGGA,%s,%s,%s,1,,,%s,F,,,,",
134 utc, lat, lon, altitude_ft );
135 sprintf( gga_sum, "%02X", calc_atlas_cksum(gga) );
137 sprintf( patla, "PATLA,%.2f,%.1f,%.2f,%.1f,%.0f",
138 nav_freq->getDoubleValue(),
139 nav_sel_radial->getDoubleValue(),
140 nav_freq->getDoubleValue(),
141 nav_sel_radial->getDoubleValue(),
142 adf_freq->getDoubleValue() );
143 sprintf( patla_sum, "%02X", calc_atlas_cksum(patla) );
145 SG_LOG( SG_IO, SG_DEBUG, rmc );
146 SG_LOG( SG_IO, SG_DEBUG, gga );
147 SG_LOG( SG_IO, SG_DEBUG, patla );
149 string atlas_sentence;
152 atlas_sentence = "$";
153 atlas_sentence += rmc;
154 atlas_sentence += "*";
155 atlas_sentence += rmc_sum;
156 atlas_sentence += "\n";
159 atlas_sentence += "$";
160 atlas_sentence += gga;
161 atlas_sentence += "*";
162 atlas_sentence += gga_sum;
163 atlas_sentence += "\n";
166 atlas_sentence += "$";
167 atlas_sentence += patla;
168 atlas_sentence += "*";
169 atlas_sentence += patla_sum;
170 atlas_sentence += "\n";
172 // cout << atlas_sentence;
174 length = atlas_sentence.length();
175 strncpy( buf, atlas_sentence.c_str(), length );
181 // parse Atlas message. messages will look something like the
184 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
185 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
187 bool FGAtlas::parse_message() {
188 SG_LOG( SG_IO, SG_INFO, "parse atlas message" );
191 msg = msg.substr( 0, length );
192 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
194 string::size_type begin_line, end_line, begin, end;
195 begin_line = begin = 0;
197 // extract out each line
198 end_line = msg.find("\n", begin_line);
199 while ( end_line != string::npos ) {
200 string line = msg.substr(begin_line, end_line - begin_line);
201 begin_line = end_line + 1;
202 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
205 string start = msg.substr(begin, 1);
207 SG_LOG( SG_IO, SG_INFO, " start = " << start );
210 end = msg.find(",", begin);
211 if ( end == string::npos ) {
215 string sentence = msg.substr(begin, end - begin);
217 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
219 double lon_deg, lon_min, lat_deg, lat_min;
220 double lon, lat, speed, heading, altitude;
222 if ( sentence == "GPRMC" ) {
224 end = msg.find(",", begin);
225 if ( end == string::npos ) {
229 string utc = msg.substr(begin, end - begin);
231 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
234 end = msg.find(",", begin);
235 if ( end == string::npos ) {
239 string junk = msg.substr(begin, end - begin);
241 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
244 end = msg.find(",", begin);
245 if ( end == string::npos ) {
249 string lat_str = msg.substr(begin, end - begin);
252 lat_deg = atof( lat_str.substr(0, 2).c_str() );
253 lat_min = atof( lat_str.substr(2).c_str() );
256 end = msg.find(",", begin);
257 if ( end == string::npos ) {
261 string lat_dir = msg.substr(begin, end - begin);
264 lat = lat_deg + ( lat_min / 60.0 );
265 if ( lat_dir == "S" ) {
269 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
270 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
273 end = msg.find(",", begin);
274 if ( end == string::npos ) {
278 string lon_str = msg.substr(begin, end - begin);
281 lon_deg = atof( lon_str.substr(0, 3).c_str() );
282 lon_min = atof( lon_str.substr(3).c_str() );
285 end = msg.find(",", begin);
286 if ( end == string::npos ) {
290 string lon_dir = msg.substr(begin, end - begin);
293 lon = lon_deg + ( lon_min / 60.0 );
294 if ( lon_dir == "W" ) {
298 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
299 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
302 double sl_radius, lat_geoc;
303 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
304 cur_fdm_state->get_Altitude(),
305 &sl_radius, &lat_geoc );
306 cur_fdm_state->set_Geocentric_Position( lat_geoc,
307 cur_fdm_state->get_Longitude(),
308 sl_radius + cur_fdm_state->get_Altitude() );
312 end = msg.find(",", begin);
313 if ( end == string::npos ) {
317 string speed_str = msg.substr(begin, end - begin);
319 speed = atof( speed_str.c_str() );
320 cur_fdm_state->set_V_calibrated_kts( speed );
321 // cur_fdm_state->set_V_ground_speed( speed );
322 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
325 end = msg.find(",", begin);
326 if ( end == string::npos ) {
330 string hdg_str = msg.substr(begin, end - begin);
332 heading = atof( hdg_str.c_str() );
333 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
334 cur_fdm_state->get_Theta(),
335 heading * SGD_DEGREES_TO_RADIANS );
336 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
337 } else if ( sentence == "GPGGA" ) {
339 end = msg.find(",", begin);
340 if ( end == string::npos ) {
344 string utc = msg.substr(begin, end - begin);
346 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
349 end = msg.find(",", begin);
350 if ( end == string::npos ) {
354 string lat_str = msg.substr(begin, end - begin);
357 lat_deg = atof( lat_str.substr(0, 2).c_str() );
358 lat_min = atof( lat_str.substr(2).c_str() );
361 end = msg.find(",", begin);
362 if ( end == string::npos ) {
366 string lat_dir = msg.substr(begin, end - begin);
369 lat = lat_deg + ( lat_min / 60.0 );
370 if ( lat_dir == "S" ) {
374 // cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
375 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
378 end = msg.find(",", begin);
379 if ( end == string::npos ) {
383 string lon_str = msg.substr(begin, end - begin);
386 lon_deg = atof( lon_str.substr(0, 3).c_str() );
387 lon_min = atof( lon_str.substr(3).c_str() );
390 end = msg.find(",", begin);
391 if ( end == string::npos ) {
395 string lon_dir = msg.substr(begin, end - begin);
398 lon = lon_deg + ( lon_min / 60.0 );
399 if ( lon_dir == "W" ) {
403 // cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
404 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
407 end = msg.find(",", begin);
408 if ( end == string::npos ) {
412 string junk = msg.substr(begin, end - begin);
414 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
417 end = msg.find(",", begin);
418 if ( end == string::npos ) {
422 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 string alt_str = msg.substr(begin, end - begin);
443 altitude = atof( alt_str.c_str() );
447 end = msg.find(",", begin);
448 if ( end == string::npos ) {
452 string alt_units = msg.substr(begin, end - begin);
455 if ( alt_units != "F" ) {
456 altitude *= SG_METER_TO_FEET;
459 cur_fdm_state->set_Altitude( altitude );
461 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
463 } else if ( sentence == "PATLA" ) {
465 end = msg.find(",", begin);
466 if ( end == string::npos ) {
470 string nav1_freq = msg.substr(begin, end - begin);
472 SG_LOG( SG_IO, SG_INFO, " nav1_freq = " << nav1_freq );
474 // nav1 selected radial
475 end = msg.find(",", begin);
476 if ( end == string::npos ) {
480 string nav1_rad = msg.substr(begin, end - begin);
482 SG_LOG( SG_IO, SG_INFO, " nav1_rad = " << nav1_rad );
485 end = msg.find(",", begin);
486 if ( end == string::npos ) {
490 string nav2_freq = msg.substr(begin, end - begin);
492 SG_LOG( SG_IO, SG_INFO, " nav2_freq = " << nav2_freq );
494 // nav2 selected radial
495 end = msg.find(",", begin);
496 if ( end == string::npos ) {
500 string nav2_rad = msg.substr(begin, end - begin);
502 SG_LOG( SG_IO, SG_INFO, " nav2_rad = " << nav2_rad );
505 end = msg.find("*", begin);
506 if ( end == string::npos ) {
510 string adf_freq = msg.substr(begin, end - begin);
512 SG_LOG( SG_IO, SG_INFO, " adf_freq = " << adf_freq );
515 // printf("%.8f %.8f\n", lon, lat);
518 end_line = msg.find("\n", begin_line);
525 // open hailing frequencies
526 bool FGAtlas::open() {
527 if ( is_enabled() ) {
528 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
529 << "is already in use, ignoring" );
533 SGIOChannel *io = get_io_channel();
535 if ( ! io->open( get_direction() ) ) {
536 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
546 // process work for this port
547 bool FGAtlas::process() {
548 SGIOChannel *io = get_io_channel();
550 if ( get_direction() == SG_IO_OUT ) {
552 if ( ! io->write( buf, length ) ) {
553 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
556 } else if ( get_direction() == SG_IO_IN ) {
557 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
560 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
563 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
566 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
576 bool FGAtlas::close() {
577 SG_LOG( SG_IO, SG_INFO, "closing FGAtlas" );
578 SGIOChannel *io = get_io_channel();
580 set_enabled( false );
582 if ( ! io->close() ) {