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>
51 #include <Network/pve.hxx>
52 #include <Network/rul.hxx>
54 // #include <Time/fg_time.hxx>
55 #include <Time/timestamp.hxx>
60 // define the global I/O channel list
61 io_container global_io_list;
64 // configure a port based on the config string
65 static FGProtocol *parse_port_config( const string& config )
67 string::size_type begin, end;
71 FG_LOG( FG_IO, FG_INFO, "Parse I/O channel request: " << config );
74 end = config.find(",", begin);
75 if ( end == string::npos ) {
79 string protocol = config.substr(begin, end - begin);
81 FG_LOG( FG_IO, FG_INFO, " protocol = " << protocol );
84 if ( protocol == "garmin" ) {
85 FGGarmin *garmin = new FGGarmin;
87 } else if ( protocol == "nmea" ) {
88 FGNMEA *nmea = new FGNMEA;
90 } else if ( protocol == "pve" ) {
91 FGPVE *pve = new FGPVE;
93 } else if ( protocol == "rul" ) {
94 FGRUL *rul = new FGRUL;
101 end = config.find(",", begin);
102 if ( end == string::npos ) {
103 return NULL; // dummy
106 string medium = config.substr(begin, end - begin);
108 FG_LOG( FG_IO, FG_INFO, " medium = " << medium );
110 // determine direction
111 end = config.find(",", begin);
112 if ( end == string::npos ) {
113 return NULL; // dummy
116 string direction = config.substr(begin, end - begin);
118 io->set_direction( direction );
119 FG_LOG( FG_IO, FG_INFO, " direction = " << direction );
122 end = config.find(",", begin);
123 if ( end == string::npos ) {
124 return NULL; // dummy
127 string hertz_str = config.substr(begin, end - begin);
129 double hertz = atof( hertz_str.c_str() );
131 FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz );
133 if ( medium == "serial" ) {
134 FGSerial *ch = new FGSerial;
135 io->set_io_channel( ch );
138 end = config.find(",", begin);
139 if ( end == string::npos ) {
143 ch->set_device( config.substr(begin, end - begin) );
145 FG_LOG( FG_IO, FG_INFO, " device = " << ch->get_device() );
148 ch->set_baud( config.substr(begin) );
149 FG_LOG( FG_IO, FG_INFO, " baud = " << ch->get_baud() );
151 io->set_io_channel( ch );
152 } else if ( medium == "file" ) {
153 FGFile *ch = new FGFile;
156 ch->set_file_name( config.substr(begin) );
157 FG_LOG( FG_IO, FG_INFO, " file name = " << ch->get_file_name() );
159 io->set_io_channel( ch );
160 } else if ( medium == "socket" ) {
161 // ch = new FGSocket;
168 // step through the port config streams (from fgOPTIONS) and setup
169 // serial port channels for each
172 string_list channel_options_list =
173 current_options.get_channel_options_list();
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!!!
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] );
185 global_io_list.push_back( p );
186 if ( !p->is_enabled() ) {
187 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
190 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
196 static void send_fgfs_out( FGIOChannel *p ) {
199 static void read_fgfs_in( FGIOChannel *p ) {
203 // "RUL" output format (for some sort of motion platform)
205 // The Baud rate is 2400 , one start bit, eight data bits, 1 stop bit,
208 // For position it requires a 3-byte data packet defined as follows:
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*
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
218 static void send_rul_out( FGIOChannel *p ) {
225 f = current_aircraft.fdm_state;
226 t = FGTime::cur_time_params;
228 // run as often as possibleonce per second
230 // this runs once per second
231 // if ( p->last_time == t->get_cur_time() ) {
234 // p->last_time = t->get_cur_time();
235 // if ( t->get_cur_time() % 2 != 0 ) {
239 // get roll and pitch, convert to degrees
240 double roll_deg = f->get_Phi() * RAD_TO_DEG;
241 while ( roll_deg < -180.0 ) {
244 while ( roll_deg > 180.0 ) {
248 double pitch_deg = f->get_Theta() * RAD_TO_DEG;
249 while ( pitch_deg < -180.0 ) {
252 while ( pitch_deg > 180.0 ) {
256 // scale roll and pitch to output format (1 - 255)
257 // straight && level == (128, 128)
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;
262 sprintf( rul, "p%c%c\n", roll, pitch);
264 FG_LOG( FG_IO, FG_INFO, "p " << roll << " " << pitch );
266 string rul_sentence = rul;
267 p->port.write_port(rul_sentence);
272 // "PVE" (ProVision Entertainment) output format (for some sort of
275 // Outputs a 5-byte data packet defined as follows:
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?)
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
286 static void send_pve_out( FGIOChannel *p ) {
294 f = current_aircraft.fdm_state;
295 t = FGTime::cur_time_params;
297 // run as often as possibleonce per second
299 // this runs once per second
300 // if ( p->last_time == t->get_cur_time() ) {
303 // p->last_time = t->get_cur_time();
304 // if ( t->get_cur_time() % 2 != 0 ) {
308 // get roll and pitch, convert to degrees
309 double roll_deg = f->get_Phi() * RAD_TO_DEG;
310 while ( roll_deg <= -180.0 ) {
313 while ( roll_deg > 180.0 ) {
317 double pitch_deg = f->get_Theta() * RAD_TO_DEG;
318 while ( pitch_deg <= -180.0 ) {
321 while ( pitch_deg > 180.0 ) {
325 short int heave = (int)(f->get_W_body() * 128.0);
327 // scale roll and pitch to output format (1 - 255)
328 // straight && level == (128, 128)
330 short int roll = (int)(roll_deg * 32768 / 180.0);
331 short int pitch = (int)(pitch_deg * 32768 / 180.0);
333 unsigned char roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2;
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;
341 sprintf( pve, "p%c%c%c%c%c%c\n",
342 roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
344 // printf( "p [ %u %u ] [ %u %u ] [ %u %u ]\n",
345 // roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
347 FG_LOG( FG_IO, FG_INFO, "roll=" << roll << " pitch=" << pitch <<
348 " heave=" << heave );
350 string pve_sentence = pve;
351 p->port.write_port(pve_sentence);
356 // one more level of indirection ...
357 static void process_port( FGIOChannel *p ) {
359 if ( p->kind == fgIOCHANNEL::FG_SERIAL_NMEA_OUT ) {
361 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_NMEA_IN ) {
363 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_GARMIN_OUT ) {
365 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_GARMIN_IN ) {
367 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_FGFS_OUT ) {
369 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_FGFS_IN ) {
371 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_RUL_OUT ) {
373 } else if ( p->kind == fgIOCHANNEL::FG_SERIAL_PVE_OUT ) {
380 // process any serial port work
384 // cout << "processing I/O channels" << endl;
386 static int inited = 0;
388 static FGTimeStamp last;
397 interval = current - last;
401 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
402 // cout << " channel = " << i << endl;
403 p = global_io_list[i];
405 if ( p->is_enabled() ) {
406 p->dec_count_down( interval );
407 if ( p->get_count_down() < 0 ) {
409 p->set_count_down( 1000000.0 / p->get_hz() );