FlightGear next
atlas.cxx
Go to the documentation of this file.
1/*
2 * SPDX-FileName: atlas.cxx
3 * SPDX-FileComment: Atlas protocol class
4 * SPDX-FileCopyrightText: Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#ifdef HAVE_CONFIG_H
9# include "config.h"
10#endif
11
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15
16#include <simgear/debug/logstream.hxx>
17#include <simgear/math/sg_geodesy.hxx>
18#include <simgear/io/iochannel.hxx>
19#include <simgear/timing/sg_time.hxx>
20
22#include <Main/globals.hxx>
23#include <Main/fg_props.hxx>
24#include <Main/fg_init.hxx>
25
26#include "atlas.hxx"
27
28
30 length(0),
31 fdm(new FlightProperties)
32{
33 _adf_freq = fgGetNode("/instrumentation/adf/frequencies/selected-khz", true);
34 _nav1_freq = fgGetNode("/instrumentation/nav/frequencies/selected-mhz", true);
35 _nav1_sel_radial = fgGetNode("/instrumentation/nav/radials/selected-deg", true);
36 _nav2_freq = fgGetNode("/instrumentation/nav[1]/frequencies/selected-mhz", true);
37 _nav2_sel_radial = fgGetNode("/instrumentation/nav[1]/radials/selected-deg", true);
38}
39
41 delete fdm;
42}
43
44
45// calculate the atlas check sum
46static char calc_atlas_cksum(char *sentence) {
47 unsigned char sum = 0;
48 int i, len;
49
50 // cout << sentence << endl;
51
52 len = strlen(sentence);
53 sum = sentence[0];
54 for ( i = 1; i < len; i++ ) {
55 // cout << sentence[i];
56 sum ^= sentence[i];
57 }
58 // cout << endl;
59
60 // printf("sum = %02x\n", sum);
61 return sum;
62}
63
64// generate Atlas message
66 // cout << "generating atlas message" << endl;
67 char rmc[256], gga[256], patla[256];
68 char rmc_sum[10], gga_sum[10], patla_sum[10];
69 char dir;
70 int deg;
71 double min;
72
73 SGTime *t = globals->get_time_params();
74
75 char utc[10];
76 snprintf( utc, sizeof(utc), "%02d%02d%02d",
77 t->getGmt()->tm_hour, t->getGmt()->tm_min, t->getGmt()->tm_sec );
78
79 char lat[20];
80 double latd = fdm->get_Latitude() * SGD_RADIANS_TO_DEGREES;
81 if ( latd < 0.0 ) {
82 latd *= -1.0;
83 dir = 'S';
84 } else {
85 dir = 'N';
86 }
87 deg = (int)(latd);
88 min = (latd - (double)deg) * 60.0;
89 snprintf( lat, sizeof(lat), "%02d%06.3f,%c", abs(deg), min, dir);
90
91 char lon[20];
92 double lond = fdm->get_Longitude() * SGD_RADIANS_TO_DEGREES;
93 if ( lond < 0.0 ) {
94 lond *= -1.0;
95 dir = 'W';
96 } else {
97 dir = 'E';
98 }
99 deg = (int)(lond);
100 min = (lond - (double)deg) * 60.0;
101 snprintf( lon, sizeof(lon), "%03d%06.3f,%c", abs(deg), min, dir);
102
103 char speed[10];
104 snprintf( speed, sizeof(speed), "%05.1f", fdm->get_V_equiv_kts() );
105
106 char heading[10];
107 snprintf( heading, sizeof(heading), "%05.1f", fdm->get_Psi() * SGD_RADIANS_TO_DEGREES );
108
109 char altitude_m[10];
110 snprintf( altitude_m, sizeof(altitude_m), "%02d",
111 (int)(fdm->get_Altitude() * SG_FEET_TO_METER) );
112
113 char altitude_ft[10];
114 snprintf( altitude_ft, sizeof(altitude_ft), "%02d", (int)fdm->get_Altitude() );
115
116 char date[16];
117 unsigned short tm_mday = t->getGmt()->tm_mday;
118 unsigned short tm_mon = t->getGmt()->tm_mon + 1;
119 unsigned short tm_year = t->getGmt()->tm_year;
120 snprintf( date, sizeof(date), "%02u%02u%02u", tm_mday, tm_mon, tm_year);
121
122 // $GPRMC,HHMMSS,A,DDMM.MMM,N,DDDMM.MMM,W,XXX.X,XXX.X,DDMMYY,XXX.X,E*XX
123 snprintf( rmc, sizeof(rmc), "GPRMC,%s,A,%s,%s,%s,%s,%s,0.000,E",
124 utc, lat, lon, speed, heading, date );
125 snprintf( rmc_sum, sizeof(rmc_sum), "%02X", calc_atlas_cksum(rmc) );
126
127 snprintf( gga, sizeof(gga), "GPGGA,%s,%s,%s,1,,,%s,F,,,,",
128 utc, lat, lon, altitude_ft );
129 snprintf( gga_sum, sizeof(gga_sum), "%02X", calc_atlas_cksum(gga) );
130
131 snprintf( patla, sizeof(patla), "PATLA,%.2f,%.1f,%.2f,%.1f,%.0f",
132 _nav1_freq->getDoubleValue(),
133 _nav1_sel_radial->getDoubleValue(),
134 _nav2_freq->getDoubleValue(),
135 _nav2_sel_radial->getDoubleValue(),
136 _adf_freq->getDoubleValue() );
137 snprintf( patla_sum, sizeof(patla_sum), "%02X", calc_atlas_cksum(patla) );
138
139 SG_LOG( SG_IO, SG_DEBUG, rmc );
140 SG_LOG( SG_IO, SG_DEBUG, gga );
141 SG_LOG( SG_IO, SG_DEBUG, patla );
142
143 std::string atlas_sentence;
144
145 // RMC sentence
146 atlas_sentence = "$";
147 atlas_sentence += rmc;
148 atlas_sentence += "*";
149 atlas_sentence += rmc_sum;
150 atlas_sentence += "\n";
151
152 // GGA sentence
153 atlas_sentence += "$";
154 atlas_sentence += gga;
155 atlas_sentence += "*";
156 atlas_sentence += gga_sum;
157 atlas_sentence += "\n";
158
159 // PATLA sentence
160 atlas_sentence += "$";
161 atlas_sentence += patla;
162 atlas_sentence += "*";
163 atlas_sentence += patla_sum;
164 atlas_sentence += "\n";
165
166 // cout << atlas_sentence;
167
168 length = atlas_sentence.length();
169 strncpy( buf, atlas_sentence.c_str(), length );
170
171 return true;
172}
173
174
175// parse Atlas message. messages will look something like the
176// following:
177//
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
180
182 SG_LOG( SG_IO, SG_INFO, "parse atlas message" );
183
184 std::string msg = buf;
185 msg = msg.substr( 0, length );
186 SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
187
188 std::string::size_type begin_line, end_line, begin, end;
189 begin_line = begin = 0;
190
191 // extract out each line
192 end_line = msg.find("\n", begin_line);
193 while ( end_line != std::string::npos ) {
194 std::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 );
197
198 // leading character
199 std::string start = msg.substr(begin, 1);
200 ++begin;
201 SG_LOG( SG_IO, SG_INFO, " start = " << start );
202
203 // sentence
204 end = msg.find(",", begin);
205 if ( end == std::string::npos ) {
206 return false;
207 }
208
209 std::string sentence = msg.substr(begin, end - begin);
210 begin = end + 1;
211 SG_LOG( SG_IO, SG_INFO, " sentence = " << sentence );
212
213 double lon_deg, lon_min, lat_deg, lat_min;
214 double lon, lat, speed, heading, altitude;
215
216 if ( sentence == "GPRMC" ) {
217 // time
218 end = msg.find(",", begin);
219 if ( end == std::string::npos ) {
220 return false;
221 }
222
223 std::string utc = msg.substr(begin, end - begin);
224 begin = end + 1;
225 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
226
227 // junk
228 end = msg.find(",", begin);
229 if ( end == std::string::npos ) {
230 return false;
231 }
232
233 std::string junk = msg.substr(begin, end - begin);
234 begin = end + 1;
235 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
236
237 // lat val
238 end = msg.find(",", begin);
239 if ( end == std::string::npos ) {
240 return false;
241 }
242
243 std::string lat_str = msg.substr(begin, end - begin);
244 begin = end + 1;
245
246 lat_deg = atof( lat_str.substr(0, 2).c_str() );
247 lat_min = atof( lat_str.substr(2).c_str() );
248
249 // lat dir
250 end = msg.find(",", begin);
251 if ( end == std::string::npos ) {
252 return false;
253 }
254
255 std::string lat_dir = msg.substr(begin, end - begin);
256 begin = end + 1;
257
258 lat = lat_deg + ( lat_min / 60.0 );
259 if ( lat_dir == "S" ) {
260 lat *= -1;
261 }
262
263 fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
264 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
265
266 // lon val
267 end = msg.find(",", begin);
268 if ( end == std::string::npos ) {
269 return false;
270 }
271
272 std::string lon_str = msg.substr(begin, end - begin);
273 begin = end + 1;
274
275 lon_deg = atof( lon_str.substr(0, 3).c_str() );
276 lon_min = atof( lon_str.substr(3).c_str() );
277
278 // lon dir
279 end = msg.find(",", begin);
280 if ( end == std::string::npos ) {
281 return false;
282 }
283
284 std::string lon_dir = msg.substr(begin, end - begin);
285 begin = end + 1;
286
287 lon = lon_deg + ( lon_min / 60.0 );
288 if ( lon_dir == "W" ) {
289 lon *= -1;
290 }
291
292 fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
293 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
294
295#if 0
296 double sl_radius, lat_geoc;
297 sgGeodToGeoc( fdm->get_Latitude(),
298 fdm->get_Altitude(),
299 &sl_radius, &lat_geoc );
300 fdm->set_Geocentric_Position( lat_geoc,
301 fdm->get_Longitude(),
302 sl_radius + fdm->get_Altitude() );
303#endif
304
305 // speed
306 end = msg.find(",", begin);
307 if ( end == std::string::npos ) {
308 return false;
309 }
310
311 std::string speed_str = msg.substr(begin, end - begin);
312 begin = end + 1;
313 speed = atof( speed_str.c_str() );
314 fdm->set_V_calibrated_kts( speed );
315 // fdm->set_V_ground_speed( speed );
316 SG_LOG( SG_IO, SG_INFO, " speed = " << speed );
317
318 // heading
319 end = msg.find(",", begin);
320 if ( end == std::string::npos ) {
321 return false;
322 }
323
324 std::string hdg_str = msg.substr(begin, end - begin);
325 begin = end + 1;
326 heading = atof( hdg_str.c_str() );
327 fdm->set_Euler_Angles( fdm->get_Phi(),
328 fdm->get_Theta(),
329 heading * SGD_DEGREES_TO_RADIANS );
330 SG_LOG( SG_IO, SG_INFO, " heading = " << heading );
331 } else if ( sentence == "GPGGA" ) {
332 // time
333 end = msg.find(",", begin);
334 if ( end == std::string::npos ) {
335 return false;
336 }
337
338 std::string utc = msg.substr(begin, end - begin);
339 begin = end + 1;
340 SG_LOG( SG_IO, SG_INFO, " utc = " << utc );
341
342 // lat val
343 end = msg.find(",", begin);
344 if ( end == std::string::npos ) {
345 return false;
346 }
347
348 std::string lat_str = msg.substr(begin, end - begin);
349 begin = end + 1;
350
351 lat_deg = atof( lat_str.substr(0, 2).c_str() );
352 lat_min = atof( lat_str.substr(2).c_str() );
353
354 // lat dir
355 end = msg.find(",", begin);
356 if ( end == std::string::npos ) {
357 return false;
358 }
359
360 std::string lat_dir = msg.substr(begin, end - begin);
361 begin = end + 1;
362
363 lat = lat_deg + ( lat_min / 60.0 );
364 if ( lat_dir == "S" ) {
365 lat *= -1;
366 }
367
368 // fdm->set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
369 SG_LOG( SG_IO, SG_INFO, " lat = " << lat );
370
371 // lon val
372 end = msg.find(",", begin);
373 if ( end == std::string::npos ) {
374 return false;
375 }
376
377 std::string lon_str = msg.substr(begin, end - begin);
378 begin = end + 1;
379
380 lon_deg = atof( lon_str.substr(0, 3).c_str() );
381 lon_min = atof( lon_str.substr(3).c_str() );
382
383 // lon dir
384 end = msg.find(",", begin);
385 if ( end == std::string::npos ) {
386 return false;
387 }
388
389 std::string lon_dir = msg.substr(begin, end - begin);
390 begin = end + 1;
391
392 lon = lon_deg + ( lon_min / 60.0 );
393 if ( lon_dir == "W" ) {
394 lon *= -1;
395 }
396
397 // fdm->set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
398 SG_LOG( SG_IO, SG_INFO, " lon = " << lon );
399
400 // junk
401 end = msg.find(",", begin);
402 if ( end == std::string::npos ) {
403 return false;
404 }
405
406 std::string junk = msg.substr(begin, end - begin);
407 begin = end + 1;
408 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
409
410 // junk
411 end = msg.find(",", begin);
412 if ( end == std::string::npos ) {
413 return false;
414 }
415
416 junk = msg.substr(begin, end - begin);
417 begin = end + 1;
418 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
419
420 // junk
421 end = msg.find(",", begin);
422 if ( end == std::string::npos ) {
423 return false;
424 }
425
426 junk = msg.substr(begin, end - begin);
427 begin = end + 1;
428 SG_LOG( SG_IO, SG_INFO, " junk = " << junk );
429
430 // altitude
431 end = msg.find(",", begin);
432 if ( end == std::string::npos ) {
433 return false;
434 }
435
436 std::string alt_str = msg.substr(begin, end - begin);
437 altitude = atof( alt_str.c_str() );
438 begin = end + 1;
439
440 // altitude units
441 end = msg.find(",", begin);
442 if ( end == std::string::npos ) {
443 return false;
444 }
445
446 std::string alt_units = msg.substr(begin, end - begin);
447 begin = end + 1;
448
449 if ( alt_units != "F" ) {
450 altitude *= SG_METER_TO_FEET;
451 }
452
453 fdm->set_Altitude( altitude );
454
455 SG_LOG( SG_IO, SG_INFO, " altitude = " << altitude );
456
457 } else if ( sentence == "PATLA" ) {
458 // nav1 freq
459 end = msg.find(",", begin);
460 if ( end == std::string::npos ) {
461 return false;
462 }
463
464 std::string nav1_freq = msg.substr(begin, end - begin);
465 begin = end + 1;
466 SG_LOG( SG_IO, SG_INFO, " nav1_freq = " << nav1_freq );
467
468 // nav1 selected radial
469 end = msg.find(",", begin);
470 if ( end == std::string::npos ) {
471 return false;
472 }
473
474 std::string nav1_rad = msg.substr(begin, end - begin);
475 begin = end + 1;
476 SG_LOG( SG_IO, SG_INFO, " nav1_rad = " << nav1_rad );
477
478 // nav2 freq
479 end = msg.find(",", begin);
480 if ( end == std::string::npos ) {
481 return false;
482 }
483
484 std::string nav2_freq = msg.substr(begin, end - begin);
485 begin = end + 1;
486 SG_LOG( SG_IO, SG_INFO, " nav2_freq = " << nav2_freq );
487
488 // nav2 selected radial
489 end = msg.find(",", begin);
490 if ( end == std::string::npos ) {
491 return false;
492 }
493
494 std::string nav2_rad = msg.substr(begin, end - begin);
495 begin = end + 1;
496 SG_LOG( SG_IO, SG_INFO, " nav2_rad = " << nav2_rad );
497
498 // adf freq
499 end = msg.find("*", begin);
500 if ( end == std::string::npos ) {
501 return false;
502 }
503
504 std::string adf_freq = msg.substr(begin, end - begin);
505 begin = end + 1;
506 SG_LOG( SG_IO, SG_INFO, " adf_freq = " << adf_freq );
507 }
508
509 // printf("%.8f %.8f\n", lon, lat);
510
511 begin = begin_line;
512 end_line = msg.find("\n", begin_line);
513 }
514
515 return true;
516}
517
518
519// open hailing frequencies
521 if ( is_enabled() ) {
522 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
523 << "is already in use, ignoring" );
524 return false;
525 }
526
527 SGIOChannel *io = get_io_channel();
528
529 if ( ! io->open( get_direction() ) ) {
530 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
531 return false;
532 }
533
534 set_enabled( true );
535
536 return true;
537}
538
539
540// process work for this port
542 SGIOChannel *io = get_io_channel();
543
544 if ( get_direction() == SG_IO_OUT ) {
545 gen_message();
546 if ( ! io->write( buf, length ) ) {
547 SG_LOG( SG_IO, SG_WARN, "Error writing data." );
548 return false;
549 }
550 } else if ( get_direction() == SG_IO_IN ) {
551 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
553 } else {
554 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
555 return false;
556 }
557 if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
559 } else {
560 SG_LOG( SG_IO, SG_WARN, "Error reading data." );
561 return false;
562 }
563 }
564
565 return true;
566}
567
568
569// close the channel
571 SG_LOG( SG_IO, SG_INFO, "closing FGAtlas" );
572 SGIOChannel *io = get_io_channel();
573
574 set_enabled( false );
575
576 if ( ! io->close() ) {
577 return false;
578 }
579
580 return true;
581}
double altitude
Definition ADA.cxx:46
double lat_geoc
Definition ADA.cxx:44
#define min(X, Y)
#define i(x)
static char calc_atlas_cksum(char *sentence)
Definition atlas.cxx:46
bool close()
Definition atlas.cxx:570
bool open()
Definition atlas.cxx:520
bool gen_message()
Definition atlas.cxx:65
FGAtlas()
Definition atlas.cxx:29
~FGAtlas()
Definition atlas.cxx:40
bool process()
Definition atlas.cxx:541
bool parse_message()
Definition atlas.cxx:181
SGProtocolDir get_direction() const
Definition protocol.hxx:65
SGIOChannel * get_io_channel() const
Definition protocol.hxx:90
void set_enabled(const bool b)
Definition protocol.hxx:88
bool is_enabled() const
Definition protocol.hxx:87
Encapsulate the FDM properties in some getter/setter helpers.
FGGlobals * globals
Definition globals.cxx:142
static double atof(const string &str)
Definition options.cxx:107
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27
#define FG_MAX_MSG_SIZE
Definition protocol.hxx:33