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