1 // fg_io.cxx -- higher level I/O channel management routines
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
24 #include <Include/compiler.h>
26 // #ifdef FG_HAVE_STD_INCLUDES
28 // # include <cstdlib> // atoi()
31 // # include <stdlib.h> // atoi()
35 // #include STL_IOSTREAM
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>
44 #include <Network/iochannel.hxx>
45 #include <Network/fg_file.hxx>
46 #include <Network/fg_serial.hxx>
48 #include <Network/protocol.hxx>
49 #include <Network/garmin.hxx>
50 #include <Network/nmea.hxx>
52 // #include <Time/fg_time.hxx>
53 #include <Time/timestamp.hxx>
58 // define the global I/O channel list
59 io_container global_io_list;
62 // configure a port based on the config string
63 static FGProtocol *parse_port_config( const string& config )
65 string::size_type begin, end;
69 FG_LOG( FG_IO, FG_INFO, "Parse I/O channel request: " << config );
72 end = config.find(",", begin);
73 if ( end == string::npos ) {
77 string protocol = config.substr(begin, end - begin);
79 FG_LOG( FG_IO, FG_INFO, " protocol = " << protocol );
82 if ( protocol == "nmea" ) {
83 FGNMEA *nmea = new FGNMEA;
85 } else if ( protocol == "garmin" ) {
86 FGGarmin *garmin = new FGGarmin;
93 end = config.find(",", begin);
94 if ( end == string::npos ) {
98 string medium = config.substr(begin, end - begin);
100 FG_LOG( FG_IO, FG_INFO, " medium = " << medium );
102 // determine direction
103 end = config.find(",", begin);
104 if ( end == string::npos ) {
105 return NULL; // dummy
108 string direction = config.substr(begin, end - begin);
110 io->set_direction( direction );
111 FG_LOG( FG_IO, FG_INFO, " direction = " << direction );
114 end = config.find(",", begin);
115 if ( end == string::npos ) {
116 return NULL; // dummy
119 string hertz_str = config.substr(begin, end - begin);
121 double hertz = atof( hertz_str.c_str() );
123 FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz );
125 if ( medium == "serial" ) {
126 FGSerial *ch = new FGSerial;
127 io->set_io_channel( ch );
130 end = config.find(",", begin);
131 if ( end == string::npos ) {
135 ch->set_device( config.substr(begin, end - begin) );
137 FG_LOG( FG_IO, FG_INFO, " device = " << ch->get_device() );
140 ch->set_baud( config.substr(begin) );
141 FG_LOG( FG_IO, FG_INFO, " baud = " << ch->get_baud() );
143 io->set_io_channel( ch );
144 } else if ( medium == "file" ) {
145 FGFile *ch = new FGFile;
148 ch->set_file_name( config.substr(begin) );
149 FG_LOG( FG_IO, FG_INFO, " file name = " << ch->get_file_name() );
151 io->set_io_channel( ch );
152 } else if ( medium == "socket" ) {
153 // ch = new FGSocket;
160 // step through the port config streams (from fgOPTIONS) and setup
161 // serial port channels for each
164 string_list channel_options_list =
165 current_options.get_channel_options_list();
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!!!
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] );
177 global_io_list.push_back( p );
178 if ( !p->is_enabled() ) {
179 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
182 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
188 static void send_fgfs_out( FGIOChannel *p ) {
191 static void read_fgfs_in( FGIOChannel *p ) {
195 // "RUL" output format (for some sort of motion platform)
197 // The Baud rate is 2400 , one start bit, eight data bits, 1 stop bit,
200 // For position it requires a 3-byte data packet defined as follows:
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*
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
210 static void send_rul_out( FGIOChannel *p ) {
217 f = current_aircraft.fdm_state;
218 t = FGTime::cur_time_params;
220 // run as often as possibleonce per second
222 // this runs once per second
223 // if ( p->last_time == t->get_cur_time() ) {
226 // p->last_time = t->get_cur_time();
227 // if ( t->get_cur_time() % 2 != 0 ) {
231 // get roll and pitch, convert to degrees
232 double roll_deg = f->get_Phi() * RAD_TO_DEG;
233 while ( roll_deg < -180.0 ) {
236 while ( roll_deg > 180.0 ) {
240 double pitch_deg = f->get_Theta() * RAD_TO_DEG;
241 while ( pitch_deg < -180.0 ) {
244 while ( pitch_deg > 180.0 ) {
248 // scale roll and pitch to output format (1 - 255)
249 // straight && level == (128, 128)
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;
254 sprintf( rul, "p%c%c\n", roll, pitch);
256 FG_LOG( FG_IO, FG_INFO, "p " << roll << " " << pitch );
258 string rul_sentence = rul;
259 p->port.write_port(rul_sentence);
264 // "PVE" (ProVision Entertainment) output format (for some sort of
267 // Outputs a 5-byte data packet defined as follows:
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?)
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
278 static void send_pve_out( FGIOChannel *p ) {
286 f = current_aircraft.fdm_state;
287 t = FGTime::cur_time_params;
289 // run as often as possibleonce per second
291 // this runs once per second
292 // if ( p->last_time == t->get_cur_time() ) {
295 // p->last_time = t->get_cur_time();
296 // if ( t->get_cur_time() % 2 != 0 ) {
300 // get roll and pitch, convert to degrees
301 double roll_deg = f->get_Phi() * RAD_TO_DEG;
302 while ( roll_deg <= -180.0 ) {
305 while ( roll_deg > 180.0 ) {
309 double pitch_deg = f->get_Theta() * RAD_TO_DEG;
310 while ( pitch_deg <= -180.0 ) {
313 while ( pitch_deg > 180.0 ) {
317 short int heave = (int)(f->get_W_body() * 128.0);
319 // scale roll and pitch to output format (1 - 255)
320 // straight && level == (128, 128)
322 short int roll = (int)(roll_deg * 32768 / 180.0);
323 short int pitch = (int)(pitch_deg * 32768 / 180.0);
325 unsigned char roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2;
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;
333 sprintf( pve, "p%c%c%c%c%c%c\n",
334 roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
336 // printf( "p [ %u %u ] [ %u %u ] [ %u %u ]\n",
337 // roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
339 FG_LOG( FG_IO, FG_INFO, "roll=" << roll << " pitch=" << pitch <<
340 " heave=" << heave );
342 string pve_sentence = pve;
343 p->port.write_port(pve_sentence);
348 // one more level of indirection ...
349 static void process_port( FGIOChannel *p ) {
351 if ( p->kind == fgIOCHANNEL::FG_SERIAL_NMEA_OUT ) {
353 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_NMEA_IN ) {
355 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_GARMIN_OUT ) {
357 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_GARMIN_IN ) {
359 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_FGFS_OUT ) {
361 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_FGFS_IN ) {
363 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_RUL_OUT ) {
365 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_PVE_OUT ) {
372 // process any serial port work
376 // cout << "processing I/O channels" << endl;
378 static int inited = 0;
380 static FGTimeStamp last;
389 interval = current - last;
393 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
394 // cout << " channel = " << i << endl;
395 p = global_io_list[i];
397 if ( p->is_enabled() ) {
398 p->dec_count_down( interval );
399 if ( p->get_count_down() < 0 ) {
401 p->set_count_down( 1000000.0 / p->get_hz() );