]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
e480614010168e60facae47cf64f337be95d751c
[flightgear.git] / src / Main / fg_io.cxx
1 // fg_io.cxx -- higher level I/O channel management routines
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <Include/compiler.h>
25
26 // #ifdef FG_HAVE_STD_INCLUDES
27 // #  include <cmath>
28 // #  include <cstdlib>    // atoi()
29 // #else
30 // #  include <math.h>
31 // #  include <stdlib.h>   // atoi()
32 // #endif
33
34 #include STL_STRING
35 // #include STL_IOSTREAM                                           
36 // #include <vector>                                                               
37
38 #include <Debug/logstream.hxx>
39 // #include <Aircraft/aircraft.hxx>
40 // #include <Include/fg_constants.h>
41 #include <Include/fg_types.hxx>
42 #include <Main/options.hxx>
43
44 #include <Network/iochannel.hxx>
45 #include <Network/fg_file.hxx>
46 #include <Network/fg_serial.hxx>
47
48 #include <Network/protocol.hxx>
49 #include <Network/garmin.hxx>
50 #include <Network/nmea.hxx>
51 #include <Network/pve.hxx>
52 #include <Network/rul.hxx>
53
54 // #include <Time/fg_time.hxx>
55 #include <Time/timestamp.hxx>
56
57 FG_USING_STD(string);
58
59
60 // define the global I/O channel list
61 io_container global_io_list;
62
63
64 // configure a port based on the config string
65 static FGProtocol *parse_port_config( const string& config )
66 {
67     string::size_type begin, end;
68
69     begin = 0;
70
71     FG_LOG( FG_IO, FG_INFO, "Parse I/O channel request: " << config );
72
73     // determine protocol
74     end = config.find(",", begin);
75     if ( end == string::npos ) {
76         return NULL;            // dummy
77     }
78     
79     string protocol = config.substr(begin, end - begin);
80     begin = end + 1;
81     FG_LOG( FG_IO, FG_INFO, "  protocol = " << protocol );
82
83     FGProtocol *io;
84     if ( protocol == "garmin" ) {
85         FGGarmin *garmin = new FGGarmin;
86         io = garmin;
87     } else if ( protocol == "nmea" ) {
88         FGNMEA *nmea = new FGNMEA;
89         io = nmea;
90     } else if ( protocol == "pve" ) {
91         FGPVE *pve = new FGPVE;
92         io = pve;
93     } else if ( protocol == "rul" ) {
94         FGRUL *rul = new FGRUL;
95         io = rul;
96     } else {
97         return NULL;
98     }
99
100     // determine medium
101     end = config.find(",", begin);
102     if ( end == string::npos ) {
103         return NULL;            // dummy
104     }
105     
106     string medium = config.substr(begin, end - begin);
107     begin = end + 1;
108     FG_LOG( FG_IO, FG_INFO, "  medium = " << medium );
109
110     // determine direction
111     end = config.find(",", begin);
112     if ( end == string::npos ) {
113         return NULL;            // dummy
114     }
115     
116     string direction = config.substr(begin, end - begin);
117     begin = end + 1;
118     io->set_direction( direction );
119     FG_LOG( FG_IO, FG_INFO, "  direction = " << direction );
120
121     // determine hertz
122     end = config.find(",", begin);
123     if ( end == string::npos ) {
124         return NULL;            // dummy
125     }
126     
127     string hertz_str = config.substr(begin, end - begin);
128     begin = end + 1;
129     double hertz = atof( hertz_str.c_str() );
130     io->set_hz( hertz );
131     FG_LOG( FG_IO, FG_INFO, "  hertz = " << hertz );
132
133     if ( medium == "serial" ) {
134         FGSerial *ch = new FGSerial;
135         io->set_io_channel( ch );
136
137         // device name
138         end = config.find(",", begin);
139         if ( end == string::npos ) {
140             return NULL;
141         }
142     
143         ch->set_device( config.substr(begin, end - begin) );
144         begin = end + 1;
145         FG_LOG( FG_IO, FG_INFO, "  device = " << ch->get_device() );
146
147         // baud
148         ch->set_baud( config.substr(begin) );
149         FG_LOG( FG_IO, FG_INFO, "  baud = " << ch->get_baud() );
150
151         io->set_io_channel( ch );
152     } else if ( medium == "file" ) {
153         FGFile *ch = new FGFile;
154
155         // file name
156         ch->set_file_name( config.substr(begin) );
157         FG_LOG( FG_IO, FG_INFO, "  file name = " << ch->get_file_name() );
158
159         io->set_io_channel( ch );
160     } else if ( medium == "socket" ) {
161         // ch = new FGSocket;
162     }
163
164     return io;
165 }
166
167
168 // step through the port config streams (from fgOPTIONS) and setup
169 // serial port channels for each
170 void fgIOInit() {
171     FGProtocol *p;
172     string_list channel_options_list = 
173         current_options.get_channel_options_list();
174
175     // we could almost do this in a single step except pushing a valid
176     // port onto the port list copies the structure and destroys the
177     // original, which closes the port and frees up the fd ... doh!!!
178
179     // parse the configuration strings and store the results in the
180     // appropriate FGIOChannel structures
181     for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
182         p = parse_port_config( channel_options_list[i] );
183         if ( p != NULL ) {
184             p->open();
185             global_io_list.push_back( p );
186             if ( !p->is_enabled() ) {
187                 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
188             }
189         } else {
190             FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
191         }
192     }
193 }
194
195
196 static void send_fgfs_out( FGIOChannel *p ) {
197 }
198
199 static void read_fgfs_in( FGIOChannel *p ) {
200 }
201
202
203 // "RUL" output format (for some sort of motion platform)
204 //
205 // The Baud rate is 2400 , one start bit, eight data bits, 1 stop bit,
206 // no parity.
207 //
208 // For position it requires a 3-byte data packet defined as follows:
209 //
210 // First bite: ascII character "P" ( 0x50 or 80 decimal )
211 // Second byte X pos. (1-255) 1 being 0* and 255 being 359*
212 // Third byte Y pos.( 1-255) 1 being 0* and 255 359*
213 //
214 // So sending 80 127 127 to the two axis motors will position on 180*
215 // The RS- 232 port is a nine pin connector and the only pins used are
216 // 3&5.
217
218 static void send_rul_out( FGIOChannel *p ) {
219 #if 0
220     char rul[256];
221
222     FGInterface *f;
223     FGTime *t;
224
225     f = current_aircraft.fdm_state;
226     t = FGTime::cur_time_params;
227
228     // run as often as possibleonce per second
229
230     // this runs once per second
231     // if ( p->last_time == t->get_cur_time() ) {
232     //    return;
233     // }
234     // p->last_time = t->get_cur_time();
235     // if ( t->get_cur_time() % 2 != 0 ) {
236     //    return;
237     // }
238     
239     // get roll and pitch, convert to degrees
240     double roll_deg = f->get_Phi() * RAD_TO_DEG;
241     while ( roll_deg < -180.0 ) {
242         roll_deg += 360.0;
243     }
244     while ( roll_deg > 180.0 ) {
245         roll_deg -= 360.0;
246     }
247
248     double pitch_deg = f->get_Theta() * RAD_TO_DEG;
249     while ( pitch_deg < -180.0 ) {
250         pitch_deg += 360.0;
251     }
252     while ( pitch_deg > 180.0 ) {
253         pitch_deg -= 360.0;
254     }
255
256     // scale roll and pitch to output format (1 - 255)
257     // straight && level == (128, 128)
258
259     int roll = (int)( (roll_deg+180.0) * 255.0 / 360.0) + 1;
260     int pitch = (int)( (pitch_deg+180.0) * 255.0 / 360.0) + 1;
261
262     sprintf( rul, "p%c%c\n", roll, pitch);
263
264     FG_LOG( FG_IO, FG_INFO, "p " << roll << " " << pitch );
265
266     string rul_sentence = rul;
267     p->port.write_port(rul_sentence);
268 #endif
269 }
270
271
272 // "PVE" (ProVision Entertainment) output format (for some sort of
273 // motion platform)
274 //
275 // Outputs a 5-byte data packet defined as follows:
276 //
277 // First bite:  ASCII character "P" ( 0x50 or 80 decimal )
278 // Second byte:  "roll" value (1-255) 1 being 0* and 255 being 359*
279 // Third byte:  "pitch" value (1-255) 1 being 0* and 255 being 359*
280 // Fourth byte:  "heave" value (or vertical acceleration?)
281 //
282 // So sending 80 127 127 to the two axis motors will position on 180*
283 // The RS- 232 port is a nine pin connector and the only pins used are
284 // 3&5.
285
286 static void send_pve_out( FGIOChannel *p ) {
287 #if 0
288     char pve[256];
289
290     FGInterface *f;
291     FGTime *t;
292
293
294     f = current_aircraft.fdm_state;
295     t = FGTime::cur_time_params;
296
297     // run as often as possibleonce per second
298
299     // this runs once per second
300     // if ( p->last_time == t->get_cur_time() ) {
301     //    return;
302     // }
303     // p->last_time = t->get_cur_time();
304     // if ( t->get_cur_time() % 2 != 0 ) {
305     //    return;
306     // }
307     
308     // get roll and pitch, convert to degrees
309     double roll_deg = f->get_Phi() * RAD_TO_DEG;
310     while ( roll_deg <= -180.0 ) {
311         roll_deg += 360.0;
312     }
313     while ( roll_deg > 180.0 ) {
314         roll_deg -= 360.0;
315     }
316
317     double pitch_deg = f->get_Theta() * RAD_TO_DEG;
318     while ( pitch_deg <= -180.0 ) {
319         pitch_deg += 360.0;
320     }
321     while ( pitch_deg > 180.0 ) {
322         pitch_deg -= 360.0;
323     }
324
325     short int heave = (int)(f->get_W_body() * 128.0);
326
327     // scale roll and pitch to output format (1 - 255)
328     // straight && level == (128, 128)
329
330     short int roll = (int)(roll_deg * 32768 / 180.0);
331     short int pitch = (int)(pitch_deg * 32768 / 180.0);
332
333     unsigned char roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2;
334     roll_b1 = roll >> 8;
335     roll_b2 = roll & 0x00ff;
336     pitch_b1 = pitch >> 8;
337     pitch_b2 = pitch & 0x00ff;
338     heave_b1 = heave >> 8;
339     heave_b2 = heave & 0x00ff;
340
341     sprintf( pve, "p%c%c%c%c%c%c\n", 
342              roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
343
344     // printf( "p [ %u %u ]  [ %u %u ]  [ %u %u ]\n", 
345     //         roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
346
347     FG_LOG( FG_IO, FG_INFO, "roll=" << roll << " pitch=" << pitch <<
348             " heave=" << heave );
349
350     string pve_sentence = pve;
351     p->port.write_port(pve_sentence);
352 #endif
353 }
354
355
356 // one more level of indirection ...
357 static void process_port( FGIOChannel *p ) {
358 #if 0
359     if ( p->kind == fgIOCHANNEL::FG_SERIAL_NMEA_OUT ) {
360         send_nmea_out(p);
361     } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_NMEA_IN ) {
362         read_nmea_in(p);
363     } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_GARMIN_OUT ) {
364         send_garmin_out(p);
365     } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_GARMIN_IN ) {
366         read_garmin_in(p);
367     } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_FGFS_OUT ) {
368         send_fgfs_out(p);
369     } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_FGFS_IN ) {
370         read_fgfs_in(p);
371     } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_RUL_OUT ) {
372         send_rul_out(p);
373     } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_PVE_OUT ) {
374         send_pve_out(p);
375     }
376 #endif
377 }
378
379
380 // process any serial port work
381 void fgIOProcess() {
382     FGProtocol *p;
383
384     // cout << "processing I/O channels" << endl;
385
386     static int inited = 0;
387     int interval;
388     static FGTimeStamp last;
389     FGTimeStamp current;
390
391     if ( ! inited ) {
392         inited = 1;
393         last.stamp();
394         interval = 0;
395     } else {
396         current.stamp();
397         interval = current - last;
398         last = current;
399     }
400
401     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
402         // cout << "  channel = " << i << endl;
403         p = global_io_list[i];
404
405         if ( p->is_enabled() ) {
406             p->dec_count_down( interval );
407             if ( p->get_count_down() < 0 ) {
408                 p->process();
409                 p->set_count_down( 1000000.0 / p->get_hz() );
410             }
411         }
412     }
413 }