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