]> git.mxchange.org Git - flightgear.git/blob - src/Network/AV400.cxx
New PathsDialog, for managing locations.
[flightgear.git] / src / Network / AV400.cxx
1 // AV400.cxx -- Garmin 400 series protocal class
2 //
3 // Written by Curtis Olson, started August 2006.
4 //
5 // Copyright (C) 2006  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <cstdio>
29 #include <cstdlib>
30 #include <cstring>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/io/iochannel.hxx>
35 #include <simgear/timing/sg_time.hxx>
36
37 #include <Main/fg_props.hxx>
38 #include <Main/globals.hxx>
39
40 #include "AV400.hxx"
41
42 FGAV400::FGAV400() {
43 }
44
45 FGAV400::~FGAV400() {
46 }
47
48
49 #if 0
50 // calculate the garmin check sum
51 static char calc_nmea_cksum(char *sentence) {
52     unsigned char sum = 0;
53     int i, len;
54
55     // cout << sentence << endl;
56
57     len = strlen(sentence);
58     sum = sentence[0];
59     for ( i = 1; i < len; i++ ) {
60         // cout << sentence[i];
61         sum ^= sentence[i];
62     }
63     // cout << endl;
64
65     // printf("sum = %02x\n", sum);
66     return sum;
67 }
68 #endif
69
70
71 // generate AV400 message
72 bool FGAV400::gen_message() {
73     // cout << "generating garmin message" << endl;
74
75     char msg_z[32], msg_A[32], msg_B[32], msg_C[32], msg_D[32];
76     char msg_Q[32], msg_T[32], msg_type2[256];
77     // the following could be implemented, but currently are unused
78     // char msg_E[32], msg_G[32], msg_I[32], msg_K[32], msg_L[32], msg_S[32];
79     // char msg_l[32];
80
81     char dir;
82     int deg;
83     double min;
84
85     // create msg_z
86     sprintf( msg_z, "z%05.0f\r\n", fdm.get_Altitude() );
87
88     // create msg_A
89     sprintf( msg_A, "A");
90
91     double latd = fdm.get_Latitude() * SGD_RADIANS_TO_DEGREES;
92     if ( latd < 0.0 ) {
93         latd = -latd;
94         dir = 'S';
95     } else {
96         dir = 'N';
97     }
98     deg = (int)latd;
99     min = (latd - (double)deg) * 60.0 * 100.0;
100     sprintf( msg_A, "A%c %02d %04.0f\r\n", dir, deg, min);
101
102     // create msg_B
103     double lond = fdm.get_Longitude() * SGD_RADIANS_TO_DEGREES;
104     if ( lond < 0.0 ) {
105         lond = -lond;
106         dir = 'W';
107     } else {
108         dir = 'E';
109     }
110     deg = (int)lond;
111     min = (lond - (double)deg) * 60.0 * 100.0;
112     sprintf( msg_B, "B%c %03d %04.0f\r\n", dir, deg, min);
113
114     // create msg_C
115     float magdeg = fgGetDouble( "/environment/magnetic-variation-deg" );
116     double vn = fgGetDouble( "/velocities/speed-north-fps" );
117     double ve = fgGetDouble( "/velocities/speed-east-fps" );
118     double gnd_trk_true = atan2( ve, vn ) * SGD_RADIANS_TO_DEGREES;
119     double gnd_trk_mag = gnd_trk_true - magdeg;
120     if ( gnd_trk_mag < 0.0 ) { gnd_trk_mag += 360.0; }
121     if ( gnd_trk_mag >= 360.0 ) { gnd_trk_mag -= 360.0; }
122     sprintf( msg_C, "C%03.0f\r\n", gnd_trk_mag);
123
124     // create msg_D
125     double speed_kt = sqrt( vn*vn + ve*ve ) * SG_FPS_TO_KT;
126     if ( speed_kt > 999.0 ) {
127         speed_kt = 999.0;
128     }
129     sprintf( msg_D, "D%03.0f\r\n", speed_kt);
130
131     // create msg_E (not implemented)
132     // create msg_G (not implemented)
133     // create msg_I (not implemented)
134     // create msg_K (not implemented)
135     // create msg_L (not implemented)
136
137     // create msg_Q
138     if ( magdeg < 0.0 ) {
139         magdeg = -magdeg;
140         dir = 'W';
141     } else {
142         dir = 'E';
143     }
144     sprintf( msg_Q, "Q%c%03.0f\r\n", dir, magdeg * 10.0 );
145
146     // create msg_S (not implemented)
147
148     // create msg_T
149     sprintf( msg_T, "T---------\r\n" );
150
151     // create msg_l (not implemented)
152
153     // sentence type 2
154     sprintf( msg_type2, "w01%c\r\n", (char)65 );
155
156     // assemble message
157     string sentence;
158     sentence += '\002';         // STX
159     sentence += msg_z;          // altitude
160     sentence += msg_A;          // latitude
161     sentence += msg_B;          // longitude
162     sentence += msg_C;          // ground track
163     sentence += msg_D;          // ground speed (kt)
164     // sentence += "E-----\r\n";
165     // sentence += "G-----\r\n";
166     // sentence += "I----\r\n";
167     // sentence += "K-----\r\n";
168     // sentence += "L----\r\n";
169     sentence += msg_Q;          // magvar
170     // sentence += "S-----\r\n";
171     sentence += msg_T;          // end of type 1 messages (must be sent)
172     sentence += msg_type2;      // type2 message
173     // sentence += "l------\r\n";
174     sentence += '\003';         // ETX
175
176     // cout << sentence;
177     length = sentence.length();
178     // cout << endl << "length = " << length << endl;
179     strncpy( buf, sentence.c_str(), length );
180
181     return true;
182 }
183
184
185 // parse AV400 message
186 bool FGAV400::parse_message() {
187     SG_LOG( SG_IO, SG_INFO, "parse garmin message" );
188
189     string msg = buf;
190     msg = msg.substr( 0, length );
191     SG_LOG( SG_IO, SG_INFO, "entire message = " << msg );
192
193     string::size_type begin_line, end_line, begin, end;
194     begin_line = begin = 0;
195
196     // extract out each line
197     end_line = msg.find("\n", begin_line);
198     while ( end_line != string::npos ) {
199         string line = msg.substr(begin_line, end_line - begin_line);
200         begin_line = end_line + 1;
201         SG_LOG( SG_IO, SG_INFO, "  input line = " << line );
202
203         // leading character
204         string start = msg.substr(begin, 1);
205         ++begin;
206         SG_LOG( SG_IO, SG_INFO, "  start = " << start );
207
208         // sentence
209         end = msg.find(",", begin);
210         if ( end == string::npos ) {
211             return false;
212         }
213     
214         string sentence = msg.substr(begin, end - begin);
215         begin = end + 1;
216         SG_LOG( SG_IO, SG_INFO, "  sentence = " << sentence );
217
218         double lon_deg, lon_min, lat_deg, lat_min;
219         double lon, lat, speed, heading, altitude;
220
221         if ( sentence == "GPRMC" ) {
222             // time
223             end = msg.find(",", begin);
224             if ( end == string::npos ) {
225                 return false;
226             }
227     
228             string utc = msg.substr(begin, end - begin);
229             begin = end + 1;
230             SG_LOG( SG_IO, SG_INFO, "  utc = " << utc );
231
232             // junk
233             end = msg.find(",", begin);
234             if ( end == string::npos ) {
235                 return false;
236             }
237     
238             string junk = msg.substr(begin, end - begin);
239             begin = end + 1;
240             SG_LOG( SG_IO, SG_INFO, "  junk = " << junk );
241
242             // lat val
243             end = msg.find(",", begin);
244             if ( end == string::npos ) {
245                 return false;
246             }
247     
248             string lat_str = msg.substr(begin, end - begin);
249             begin = end + 1;
250
251             lat_deg = atof( lat_str.substr(0, 2).c_str() );
252             lat_min = atof( lat_str.substr(2).c_str() );
253
254             // lat dir
255             end = msg.find(",", begin);
256             if ( end == string::npos ) {
257                 return false;
258             }
259     
260             string lat_dir = msg.substr(begin, end - begin);
261             begin = end + 1;
262
263             lat = lat_deg + ( lat_min / 60.0 );
264             if ( lat_dir == "S" ) {
265                 lat *= -1;
266             }
267
268             fdm.set_Latitude( lat * SGD_DEGREES_TO_RADIANS );
269             SG_LOG( SG_IO, SG_INFO, "  lat = " << lat );
270
271             // lon val
272             end = msg.find(",", begin);
273             if ( end == string::npos ) {
274                 return false;
275             }
276     
277             string lon_str = msg.substr(begin, end - begin);
278             begin = end + 1;
279
280             lon_deg = atof( lon_str.substr(0, 3).c_str() );
281             lon_min = atof( lon_str.substr(3).c_str() );
282
283             // lon dir
284             end = msg.find(",", begin);
285             if ( end == string::npos ) {
286                 return false;
287             }
288     
289             string lon_dir = msg.substr(begin, end - begin);
290             begin = end + 1;
291
292             lon = lon_deg + ( lon_min / 60.0 );
293             if ( lon_dir == "W" ) {
294                 lon *= -1;
295             }
296
297             fdm.set_Longitude( lon * SGD_DEGREES_TO_RADIANS );
298             SG_LOG( SG_IO, SG_INFO, "  lon = " << lon );
299
300 #if 0
301             double sl_radius, lat_geoc;
302             sgGeodToGeoc( fdm.get_Latitude(), 
303                           fdm.get_Altitude(), 
304                           &sl_radius, &lat_geoc );
305             fdm.set_Geocentric_Position( lat_geoc, 
306                            fdm.get_Longitude(), 
307                            sl_radius + fdm.get_Altitude() );
308 #endif
309
310             // speed
311             end = msg.find(",", begin);
312             if ( end == string::npos ) {
313                 return false;
314             }
315     
316             string speed_str = msg.substr(begin, end - begin);
317             begin = end + 1;
318             speed = atof( speed_str.c_str() );
319             fdm.set_V_calibrated_kts( speed );
320             // fdm.set_V_ground_speed( speed );
321             SG_LOG( SG_IO, SG_INFO, "  speed = " << speed );
322
323             // heading
324             end = msg.find(",", begin);
325             if ( end == string::npos ) {
326                 return false;
327             }
328     
329             string hdg_str = msg.substr(begin, end - begin);
330             begin = end + 1;
331             heading = atof( hdg_str.c_str() );
332             fdm.set_Euler_Angles( fdm.get_Phi(), 
333                                              fdm.get_Theta(), 
334                                              heading * SGD_DEGREES_TO_RADIANS );
335             SG_LOG( SG_IO, SG_INFO, "  heading = " << heading );
336         } else if ( sentence == "PGRMZ" ) {
337             // altitude
338             end = msg.find(",", begin);
339             if ( end == string::npos ) {
340                 return false;
341             }
342     
343             string alt_str = msg.substr(begin, end - begin);
344             altitude = atof( alt_str.c_str() );
345             begin = end + 1;
346
347             // altitude units
348             end = msg.find(",", begin);
349             if ( end == string::npos ) {
350                 return false;
351             }
352     
353             string alt_units = msg.substr(begin, end - begin);
354             begin = end + 1;
355
356             if ( alt_units != "F" && alt_units != "f" ) {
357                 altitude *= SG_METER_TO_FEET;
358             }
359
360             fdm.set_Altitude( altitude );
361     
362             SG_LOG( SG_IO, SG_INFO, " altitude  = " << altitude );
363
364         }
365
366         // printf("%.8f %.8f\n", lon, lat);
367
368         begin = begin_line;
369         end_line = msg.find("\n", begin_line);
370     }
371
372     return true;
373 }
374
375
376 // open hailing frequencies
377 bool FGAV400::open() {
378     if ( is_enabled() ) {
379         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
380                 << "is already in use, ignoring" );
381         return false;
382     }
383
384     SGIOChannel *io = get_io_channel();
385
386     if ( ! io->open( get_direction() ) ) {
387         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
388         return false;
389     }
390
391     set_enabled( true );
392
393     return true;
394 }
395
396
397 // process work for this port
398 bool FGAV400::process() {
399     SGIOChannel *io = get_io_channel();
400
401     if ( get_direction() == SG_IO_OUT ) {
402         gen_message();
403         if ( ! io->write( buf, length ) ) {
404             SG_LOG( SG_IO, SG_WARN, "Error writing data." );
405             return false;
406         }
407     } else if ( get_direction() == SG_IO_IN ) {
408         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
409             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
410             if ( parse_message() ) {
411                 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
412             } else {
413                 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
414             }
415         } else {
416             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
417             return false;
418         }
419         if ( (length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) {
420             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
421             if ( parse_message() ) {
422                 SG_LOG( SG_IO, SG_ALERT, "Success parsing data." );
423             } else {
424                 SG_LOG( SG_IO, SG_ALERT, "Error parsing data." );
425             }
426         } else {
427             SG_LOG( SG_IO, SG_ALERT, "Error reading data." );
428             return false;
429         }
430     }
431
432     return true;
433 }
434
435
436 // close the channel
437 bool FGAV400::close() {
438     SGIOChannel *io = get_io_channel();
439
440     set_enabled( false );
441
442     if ( ! io->close() ) {
443         return false;
444     }
445
446     return true;
447 }