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