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.
21
// $Id: atlas.cxx,v 1.6 2001/03/26 16:22:31 curt Exp $
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
char rmc[256], gga[256], patla[256];
67
char rmc_sum[10], gga_sum[10], patla_sum[10];
72
SGTime *t = globals->get_time_params();
75
sprintf( utc, "%02d%02d%02d",
76
t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
79
double latd = cur_fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
87
min = (latd - (double)deg) * 60.0;
88
sprintf( lat, "%02d%06.3f,%c", abs(deg), min, dir);
91
double lond = cur_fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
99
min = (lond - (double)deg) * 60.0;
100
sprintf( lon, "%03d%06.3f,%c", abs(deg), min, dir);
103
sprintf( speed, "%05.1f", cur_fdm_state->get_V_equiv_kts() );
106
sprintf( heading, "%05.1f", cur_fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES );
109
sprintf( altitude_m, "%02d",
110
(int)(cur_fdm_state->get_Altitude() * SG_FEET_TO_METER) );
112
char altitude_ft[10];
113
sprintf( altitude_ft, "%02d", (int)cur_fdm_state->get_Altitude() );
116
sprintf( date, "%02d%02d%02d", t->getGmt()->tm_mday,
117
t->getGmt()->tm_mon+1, t->getGmt()->tm_year );
119
// $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
120
sprintf( rmc, "GPRMC,%s,A,%s,%s,%s,%s,%s,0.000,E",
121
utc, lat, lon, speed, heading, date );
122
sprintf( rmc_sum, "%02X", calc_atlas_cksum(rmc) );
124
sprintf( gga, "GPGGA,%s,%s,%s,1,,,%s,F,,,,",
125
utc, lat, lon, altitude_ft );
126
sprintf( gga_sum, "%02X", calc_atlas_cksum(gga) );
128
sprintf( patla, "PATLA,%.2f,%.1f,%.2f,%.1f,%.0f",
129
current_radiostack->get_nav1_freq(),
130
current_radiostack->get_nav1_sel_radial(),
131
current_radiostack->get_nav2_freq(),
132
current_radiostack->get_nav2_sel_radial(),
133
current_radiostack->get_adf_freq() );
134
sprintf( patla_sum, "%02X", calc_atlas_cksum(patla) );
136
SG_LOG( SG_IO, SG_DEBUG, rmc );
137
SG_LOG( SG_IO, SG_DEBUG, gga );
138
SG_LOG( SG_IO, SG_DEBUG, patla );
140
string atlas_sentence;
143
atlas_sentence = "$";
144
atlas_sentence += rmc;
145
atlas_sentence += "*";
146
atlas_sentence += rmc_sum;
147
atlas_sentence += "\n";
150
atlas_sentence += "$";
151
atlas_sentence += gga;
152
atlas_sentence += "*";
153
atlas_sentence += gga_sum;
154
atlas_sentence += "\n";
157
atlas_sentence += "$";
158
atlas_sentence += patla;
159
atlas_sentence += "*";
160
atlas_sentence += patla_sum;
161
atlas_sentence += "\n";
163
cout << atlas_sentence;
165
length = atlas_sentence.length();
166
strncpy( buf, atlas_sentence.c_str(), length );
172
// parse Atlas message. messages will look something like the
175
// $GPRMC,163227,A,3321.173,N,11039.855,W,000.1,270.0,171199,0.000,E*61
176
// $GPGGA,163227,3321.173,N,11039.855,W,1,,,3333,F,,,,*0F
178
bool FGAtlas::parse_message() {
179
SG_LOG( SG_IO, SG_INFO, "parse atlas message" );
182
msg = msg.substr( 0, length );
183
SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
185
string::size_type begin_line, end_line, begin, end;
186
begin_line = begin = 0;
188
// extract out each line
189
end_line = msg.find("\n", begin_line);
190
while ( end_line != string::npos ) {
191
string line = msg.substr(begin_line, end_line - begin_line);
192
begin_line = end_line + 1;
193
SG_LOG( SG_IO, SG_INFO, " input line = " << line );
196
string start = msg.substr(begin, 1);
198
SG_LOG( SG_IO, SG_INFO, " start = " << start );
201
end = msg.find(",", begin);
202
if ( end == string::npos ) {
206
string sentence = msg.substr(begin, end - begin);
208
SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
210
double lon_deg, lon_min, lat_deg, lat_min;
211
double lon, lat, speed, heading, altitude;
213
if ( sentence == "GPRMC" ) {
215
end = msg.find(",", begin);
216
if ( end == string::npos ) {
220
string utc = msg.substr(begin, end - begin);
222
SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
225
end = msg.find(",", begin);
226
if ( end == string::npos ) {
230
string junk = msg.substr(begin, end - begin);
232
SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
235
end = msg.find(",", begin);
236
if ( end == string::npos ) {
240
string lat_str = msg.substr(begin, end - begin);
243
lat_deg = atof( lat_str.substr(0, 2).c_str() );
244
lat_min = atof( lat_str.substr(2).c_str() );
247
end = msg.find(",", begin);
248
if ( end == string::npos ) {
252
string lat_dir = msg.substr(begin, end - begin);
255
lat = lat_deg + ( lat_min / 60.0 );
256
if ( lat_dir == "S" ) {
260
cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
261
SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
264
end = msg.find(",", begin);
265
if ( end == string::npos ) {
269
string lon_str = msg.substr(begin, end - begin);
272
lon_deg = atof( lon_str.substr(0, 3).c_str() );
273
lon_min = atof( lon_str.substr(3).c_str() );
276
end = msg.find(",", begin);
277
if ( end == string::npos ) {
281
string lon_dir = msg.substr(begin, end - begin);
284
lon = lon_deg + ( lon_min / 60.0 );
285
if ( lon_dir == "W" ) {
289
cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
290
SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
293
double sl_radius, lat_geoc;
294
sgGeodToGeoc( cur_fdm_state->get_Latitude(),
295
cur_fdm_state->get_Altitude(),
296
&sl_radius, &lat_geoc );
297
cur_fdm_state->set_Geocentric_Position( lat_geoc,
298
cur_fdm_state->get_Longitude(),
299
sl_radius + cur_fdm_state->get_Altitude() );
303
end = msg.find(",", begin);
304
if ( end == string::npos ) {
308
string speed_str = msg.substr(begin, end - begin);
310
speed = atof( speed_str.c_str() );
311
cur_fdm_state->set_V_calibrated_kts( speed );
312
// cur_fdm_state->set_V_ground_speed( speed );
313
SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
316
end = msg.find(",", begin);
317
if ( end == string::npos ) {
321
string hdg_str = msg.substr(begin, end - begin);
323
heading = atof( hdg_str.c_str() );
324
cur_fdm_state->set_Euler_Angles( cur_fdm_state->get_Phi(),
325
cur_fdm_state->get_Theta(),
326
heading * SGD_DEGREES_TO_RADIANS );
327
SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
328
} else if ( sentence == "GPGGA" ) {
330
end = msg.find(",", begin);
331
if ( end == string::npos ) {
335
string utc = msg.substr(begin, end - begin);
337
SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
340
end = msg.find(",", begin);
341
if ( end == string::npos ) {
345
string lat_str = msg.substr(begin, end - begin);
348
lat_deg = atof( lat_str.substr(0, 2).c_str() );
349
lat_min = atof( lat_str.substr(2).c_str() );
352
end = msg.find(",", begin);
353
if ( end == string::npos ) {
357
string lat_dir = msg.substr(begin, end - begin);
360
lat = lat_deg + ( lat_min / 60.0 );
361
if ( lat_dir == "S" ) {
365
// cur_fdm_state->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
366
SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
369
end = msg.find(",", begin);
370
if ( end == string::npos ) {
374
string lon_str = msg.substr(begin, end - begin);
377
lon_deg = atof( lon_str.substr(0, 3).c_str() );
378
lon_min = atof( lon_str.substr(3).c_str() );
381
end = msg.find(",", begin);
382
if ( end == string::npos ) {
386
string lon_dir = msg.substr(begin, end - begin);
389
lon = lon_deg + ( lon_min / 60.0 );
390
if ( lon_dir == "W" ) {
394
// cur_fdm_state->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
395
SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
398
end = msg.find(",", begin);
399
if ( end == string::npos ) {
403
string junk = msg.substr(begin, end - begin);
405
SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
408
end = msg.find(",", begin);
409
if ( end == string::npos ) {
413
junk = msg.substr(begin, end - begin);
415
SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
418
end = msg.find(",", begin);
419
if ( end == string::npos ) {
423
junk = msg.substr(begin, end - begin);
425
SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
428
end = msg.find(",", begin);
429
if ( end == string::npos ) {
433
string alt_str = msg.substr(begin, end - begin);
434
altitude = atof( alt_str.c_str() );
438
end = msg.find(",", begin);
439
if ( end == string::npos ) {
443
string alt_units = msg.substr(begin, end - begin);
446
if ( alt_units != (string)"F" ) {
447
altitude *= SG_METER_TO_FEET;
450
cur_fdm_state->set_Altitude( altitude );
452
SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
454
} else if ( sentence == "PATLA" ) {
456
end = msg.find(",", begin);
457
if ( end == string::npos ) {
461
string nav1_freq = msg.substr(begin, end - begin);
463
SG_LOG( SG_IO, SG_INFO, " nav1_freq = " << nav1_freq );
465
// nav1 selected radial
466
end = msg.find(",", begin);
467
if ( end == string::npos ) {
471
string nav1_rad = msg.substr(begin, end - begin);
473
SG_LOG( SG_IO, SG_INFO, " nav1_rad = " << nav1_rad );
476
end = msg.find(",", begin);
477
if ( end == string::npos ) {
481
string nav2_freq = msg.substr(begin, end - begin);
483
SG_LOG( SG_IO, SG_INFO, " nav2_freq = " << nav2_freq );
485
// nav2 selected radial
486
end = msg.find(",", begin);
487
if ( end == string::npos ) {
491
string nav2_rad = msg.substr(begin, end - begin);
493
SG_LOG( SG_IO, SG_INFO, " nav2_rad = " << nav2_rad );
496
end = msg.find("*", begin);
497
if ( end == string::npos ) {
501
string adf_freq = msg.substr(begin, end - begin);
503
SG_LOG( SG_IO, SG_INFO, " adf_freq = " << adf_freq );
506
// printf("%.8f %.8f\n", lon, lat);
509
end_line = msg.find("\n", begin_line);
516
// open hailing frequencies
517
bool FGAtlas::open() {
518
if ( is_enabled() ) {
519
SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
520
<< "is already in use, ignoring" );
524
SGIOChannel *io = get_io_channel();
526
if ( ! io->open( get_direction() ) ) {
527
SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
537
// process work for this port
538
bool FGAtlas::process() {
539
SGIOChannel *io = get_io_channel();
541
if ( get_direction() == SG_IO_OUT ) {
543
if ( ! io->write( buf, length ) ) {
544
SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
547
} else if ( get_direction() == SG_IO_IN ) {
548
if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
551
SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
554
if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
557
SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
567
bool FGAtlas::close() {
568
SGIOChannel *io = get_io_channel();
570
set_enabled( false );
572
if ( ! io->close() ) {