1 // atlas.cxx -- Atlas protocal class
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
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>
28 #include <Cockpit/radiostack.hxx>
29 #include <FDM/flight.hxx>
30 #include <Main/globals.hxx>
42 // calculate the atlas check sum
43 static char calc_atlas_cksum(char *sentence) {
44 unsigned char sum = 0;
47 // cout << sentence << endl;
49 len = strlen(sentence);
51 for ( i = 1; i < len; i++ ) {
52 // cout << sentence[i];
57 // printf("sum = %02x\n", sum);
62 // generate Atlas message
63 bool FGAtlas::gen_message() {
64 // cout << "generating atlas message" << endl;
66 static SGPropertyNode *adf_freq
67 = fgGetNode("/radios/kr-87/outputs/selected-khz", true);
69 char rmc[256], gga[256], patla[256];
70 char rmc_sum[10], gga_sum[10], patla_sum[10];
75 SGTime *t = globals->get_time_params();
78 sprintf( utc, "%02d%02d%02d",
79 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
82 double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
90 min = (latd - (double)deg) * 60.0;
91 sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
94 double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
102 min = (lond - (double)deg) * 60.0;
103 sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
106 sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
109 sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
112 sprintf( altitude_m, "%02d",
113 (int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
115 char altitude_ft[10];
116 sprintf( altitude_ft, "%02d", (int)cur_fdm_state->get_Altitude() );
119 sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
120 t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
122 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
123 sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,0.000,E",
124 utc, lat, lon, speed, heading, date );
125 sprintf( rmc_sum, "%02X", calc_atlas_cksum(rmc) );
127 sprintf( gga, "GPGGA,%s,%s,%s,1,,,%s,F,,,,",
128 utc, lat, lon, altitude_ft );
129 sprintf( gga_sum, "%02X", calc_atlas_cksum(gga) );
131 sprintf( patla, "PATLA,%.2f,%.1f,%.2f,%.1f,%.0f",
132 current_radiostack->get_navcom1()->get_nav_freq(),
133 current_radiostack->get_navcom1()->get_nav_sel_radial(),
134 current_radiostack->get_navcom1()->get_nav_freq(),
135 current_radiostack->get_navcom1()->get_nav_sel_radial(),
136 adf_freq->getDoubleValue() );
137 sprintf( patla_sum, "%02X", calc_atlas_cksum(patla) );
139 SG_LOG( SG_IO, SG_DEBUG, rmc );
140 SG_LOG( SG_IO, SG_DEBUG, gga );
141 SG_LOG( SG_IO, SG_DEBUG, patla );
143 string atlas_sentence;
146 atlas_sentence = "$";
147 atlas_sentence += rmc;
148 atlas_sentence += "*";
149 atlas_sentence += rmc_sum;
150 atlas_sentence += "\n";
153 atlas_sentence += "$";
154 atlas_sentence += gga;
155 atlas_sentence += "*";
156 atlas_sentence += gga_sum;
157 atlas_sentence += "\n";
160 atlas_sentence += "$";
161 atlas_sentence += patla;
162 atlas_sentence += "*";
163 atlas_sentence += patla_sum;
164 atlas_sentence += "\n";
166 cout << atlas_sentence;
168 length = atlas_sentence.length();
169 strncpy( buf, atlas_sentence.c_str(), length );
175 // parse Atlas message. messages will look something like the
178 // $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
179 // $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
181 bool FGAtlas::parse_message() {
182 SG_LOG( SG_IO, SG_INFO, "parse atlas message" );
185 msg = msg.substr( 0, length );
186 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
188 string::size_type begin_line, end_line, begin, end;
189 begin_line = begin = 0;
191 // extract out each line
192 end_line = msg.find("\n", begin_line);
193 while ( end_line != string::npos ) {
194 string line = msg.substr(begin_line, end_line - begin_line);
195 begin_line = end_line + 1;
196 SG_LOG( SG_IO, SG_INFO, " input line = " << line );
199 string start = msg.substr(begin, 1);
201 SG_LOG( SG_IO, SG_INFO, " start = " << start );
204 end = msg.find(",", begin);
205 if ( end == string::npos ) {
209 string sentence = msg.substr(begin, end - begin);
211 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
213 double lon_deg, lon_min, lat_deg, lat_min;
214 double lon, lat, speed, heading, altitude;
216 if ( sentence == "GPRMC" ) {
218 end = msg.find(",", begin);
219 if ( end == string::npos ) {
223 string utc = msg.substr(begin, end - begin);
225 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
228 end = msg.find(",", begin);
229 if ( end == string::npos ) {
233 string junk = msg.substr(begin, end - begin);
235 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
238 end = msg.find(",", begin);
239 if ( end == string::npos ) {
243 string lat_str = msg.substr(begin, end - begin);
246 lat_deg = atof( lat_str.substr(0, 2).c_str() );
247 lat_min = atof( lat_str.substr(2).c_str() );
250 end = msg.find(",", begin);
251 if ( end == string::npos ) {
255 string lat_dir = msg.substr(begin, end - begin);
258 lat = lat_deg + ( lat_min / 60.0 );
259 if ( lat_dir == "S" ) {
263 cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
264 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
267 end = msg.find(",", begin);
268 if ( end == string::npos ) {
272 string lon_str = msg.substr(begin, end - begin);
275 lon_deg = atof( lon_str.substr(0, 3).c_str() );
276 lon_min = atof( lon_str.substr(3).c_str() );
279 end = msg.find(",", begin);
280 if ( end == string::npos ) {
284 string lon_dir = msg.substr(begin, end - begin);
287 lon = lon_deg + ( lon_min / 60.0 );
288 if ( lon_dir == "W" ) {
292 cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
293 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
296 double sl_radius, lat_geoc;
297 sgGeodToGeoc( cur_fdm_state->get_Latitude(),
298 cur_fdm_state->get_Altitude(),
299 &sl_radius, &lat_geoc );
300 cur_fdm_state->set_Geocentric_Position( lat_geoc,
301 cur_fdm_state->get_Longitude(),
302 sl_radius + cur_fdm_state->get_Altitude() );
306 end = msg.find(",", begin);
307 if ( end == string::npos ) {
311 string speed_str = msg.substr(begin, end - begin);
313 speed = atof( speed_str.c_str() );
314 cur_fdm_state->set_V_calibrated_kts( speed );
315 // cur_fdm_state->set_V_ground_speed( speed );
316 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
319 end = msg.find(",", begin);
320 if ( end == string::npos ) {
324 string hdg_str = msg.substr(begin, end - begin);
326 heading = atof( hdg_str.c_str() );
327 cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
328 cur_fdm_state->get_Theta(),
329 heading * SGD_DEGREES_TO_RADIANS );
330 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
331 } else if ( sentence == "GPGGA" ) {
333 end = msg.find(",", begin);
334 if ( end == string::npos ) {
338 string utc = msg.substr(begin, end - begin);
340 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
343 end = msg.find(",", begin);
344 if ( end == string::npos ) {
348 string lat_str = msg.substr(begin, end - begin);
351 lat_deg = atof( lat_str.substr(0, 2).c_str() );
352 lat_min = atof( lat_str.substr(2).c_str() );
355 end = msg.find(",", begin);
356 if ( end == string::npos ) {
360 string lat_dir = msg.substr(begin, end - begin);
363 lat = lat_deg + ( lat_min / 60.0 );
364 if ( lat_dir == "S" ) {
368 // cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
369 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
372 end = msg.find(",", begin);
373 if ( end == string::npos ) {
377 string lon_str = msg.substr(begin, end - begin);
380 lon_deg = atof( lon_str.substr(0, 3).c_str() );
381 lon_min = atof( lon_str.substr(3).c_str() );
384 end = msg.find(",", begin);
385 if ( end == string::npos ) {
389 string lon_dir = msg.substr(begin, end - begin);
392 lon = lon_deg + ( lon_min / 60.0 );
393 if ( lon_dir == "W" ) {
397 // cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
398 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
401 end = msg.find(",", begin);
402 if ( end == string::npos ) {
406 string junk = msg.substr(begin, end - begin);
408 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
411 end = msg.find(",", begin);
412 if ( end == string::npos ) {
416 junk = msg.substr(begin, end - begin);
418 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
421 end = msg.find(",", begin);
422 if ( end == string::npos ) {
426 junk = msg.substr(begin, end - begin);
428 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
431 end = msg.find(",", begin);
432 if ( end == string::npos ) {
436 string alt_str = msg.substr(begin, end - begin);
437 altitude = atof( alt_str.c_str() );
441 end = msg.find(",", begin);
442 if ( end == string::npos ) {
446 string alt_units = msg.substr(begin, end - begin);
449 if ( alt_units != (string)"F" ) {
450 altitude *= SG_METER_TO_FEET;
453 cur_fdm_state->set_Altitude( altitude );
455 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
457 } else if ( sentence == "PATLA" ) {
459 end = msg.find(",", begin);
460 if ( end == string::npos ) {
464 string nav1_freq = msg.substr(begin, end - begin);
466 SG_LOG( SG_IO, SG_INFO, " nav1_freq = " << nav1_freq );
468 // nav1 selected radial
469 end = msg.find(",", begin);
470 if ( end == string::npos ) {
474 string nav1_rad = msg.substr(begin, end - begin);
476 SG_LOG( SG_IO, SG_INFO, " nav1_rad = " << nav1_rad );
479 end = msg.find(",", begin);
480 if ( end == string::npos ) {
484 string nav2_freq = msg.substr(begin, end - begin);
486 SG_LOG( SG_IO, SG_INFO, " nav2_freq = " << nav2_freq );
488 // nav2 selected radial
489 end = msg.find(",", begin);
490 if ( end == string::npos ) {
494 string nav2_rad = msg.substr(begin, end - begin);
496 SG_LOG( SG_IO, SG_INFO, " nav2_rad = " << nav2_rad );
499 end = msg.find("*", begin);
500 if ( end == string::npos ) {
504 string adf_freq = msg.substr(begin, end - begin);
506 SG_LOG( SG_IO, SG_INFO, " adf_freq = " << adf_freq );
509 // printf("%.8f %.8f\n", lon, lat);
512 end_line = msg.find("\n", begin_line);
519 // open hailing frequencies
520 bool FGAtlas::open() {
521 if ( is_enabled() ) {
522 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
523 << "is already in use, ignoring" );
527 SGIOChannel *io = get_io_channel();
529 if ( ! io->open( get_direction() ) ) {
530 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
540 // process work for this port
541 bool FGAtlas::process() {
542 SGIOChannel *io = get_io_channel();
544 if ( get_direction() == SG_IO_OUT ) {
546 if ( ! io->write( buf, length ) ) {
547 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
550 } else if ( get_direction() == SG_IO_IN ) {
551 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
554 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
557 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
560 SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
570 bool FGAtlas::close() {
571 SGIOChannel *io = get_io_channel();
573 set_enabled( false );
575 if ( ! io->close() ) {