1 // fg_serial.cxx -- Serial I/O 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()
36 #include <Debug/logstream.hxx>
37 #include <Aircraft/aircraft.hxx>
38 #include <Include/fg_constants.h>
39 #include <Serial/serial.hxx>
40 #include <Time/fg_time.hxx>
42 // #include "options.hxx"
44 #include "fg_serial.hxx"
49 FGSerial::FGSerial() {
53 FGSerial::~FGSerial() {
57 // open the serial port based on specified direction
58 bool FGSerial::open( FGProtocol::fgProtocolDir dir ) {
59 if ( ! port.open_port( device ) ) {
60 FG_LOG( FG_IO, FG_ALERT, "Error opening device: " << device );
64 // cout << "fd = " << port.fd << endl;
66 if ( ! port.set_baud( atoi( baud.c_str() ) ) ) {
67 FG_LOG( FG_IO, FG_ALERT, "Error setting baud: " << baud );
76 // "RUL" output format (for some sort of motion platform)
78 // The Baud rate is 2400 , one start bit, eight data bits, 1 stop bit,
81 // For position it requires a 3-byte data packet defined as follows:
83 // First bite: ascII character "P" ( 0x50 or 80 decimal )
84 // Second byte X pos. (1-255) 1 being 0* and 255 being 359*
85 // Third byte Y pos.( 1-255) 1 being 0* and 255 359*
87 // So sending 80 127 127 to the two axis motors will position on 180*
88 // The RS- 232 port is a nine pin connector and the only pins used are
91 static void send_rul_out( fgIOCHANNEL *p ) {
97 f = current_aircraft.fdm_state;
98 t = FGTime::cur_time_params;
100 // run as often as possibleonce per second
102 // this runs once per second
103 // if ( p->last_time == t->get_cur_time() ) {
106 // p->last_time = t->get_cur_time();
107 // if ( t->get_cur_time() % 2 != 0 ) {
111 // get roll and pitch, convert to degrees
112 double roll_deg = f->get_Phi() * RAD_TO_DEG;
113 while ( roll_deg < -180.0 ) {
116 while ( roll_deg > 180.0 ) {
120 double pitch_deg = f->get_Theta() * RAD_TO_DEG;
121 while ( pitch_deg < -180.0 ) {
124 while ( pitch_deg > 180.0 ) {
128 // scale roll and pitch to output format (1 - 255)
129 // straight && level == (128, 128)
131 int roll = (int)( (roll_deg+180.0) * 255.0 / 360.0) + 1;
132 int pitch = (int)( (pitch_deg+180.0) * 255.0 / 360.0) + 1;
134 sprintf( rul, "p%c%c\n", roll, pitch);
136 FG_LOG( FG_IO, FG_INFO, "p " << roll << " " << pitch );
138 string rul_sentence = rul;
139 p->port.write_port(rul_sentence);
143 // "PVE" (ProVision Entertainment) output format (for some sort of
146 // Outputs a 5-byte data packet defined as follows:
148 // First bite: ASCII character "P" ( 0x50 or 80 decimal )
149 // Second byte: "roll" value (1-255) 1 being 0* and 255 being 359*
150 // Third byte: "pitch" value (1-255) 1 being 0* and 255 being 359*
151 // Fourth byte: "heave" value (or vertical acceleration?)
153 // So sending 80 127 127 to the two axis motors will position on 180*
154 // The RS- 232 port is a nine pin connector and the only pins used are
157 static void send_pve_out( fgIOCHANNEL *p ) {
164 f = current_aircraft.fdm_state;
165 t = FGTime::cur_time_params;
167 // run as often as possibleonce per second
169 // this runs once per second
170 // if ( p->last_time == t->get_cur_time() ) {
173 // p->last_time = t->get_cur_time();
174 // if ( t->get_cur_time() % 2 != 0 ) {
178 // get roll and pitch, convert to degrees
179 double roll_deg = f->get_Phi() * RAD_TO_DEG;
180 while ( roll_deg <= -180.0 ) {
183 while ( roll_deg > 180.0 ) {
187 double pitch_deg = f->get_Theta() * RAD_TO_DEG;
188 while ( pitch_deg <= -180.0 ) {
191 while ( pitch_deg > 180.0 ) {
195 short int heave = (int)(f->get_W_body() * 128.0);
197 // scale roll and pitch to output format (1 - 255)
198 // straight && level == (128, 128)
200 short int roll = (int)(roll_deg * 32768 / 180.0);
201 short int pitch = (int)(pitch_deg * 32768 / 180.0);
203 unsigned char roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2;
205 roll_b2 = roll & 0x00ff;
206 pitch_b1 = pitch >> 8;
207 pitch_b2 = pitch & 0x00ff;
208 heave_b1 = heave >> 8;
209 heave_b2 = heave & 0x00ff;
211 sprintf( pve, "p%c%c%c%c%c%c\n",
212 roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
214 // printf( "p [ %u %u ] [ %u %u ] [ %u %u ]\n",
215 // roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
217 FG_LOG( FG_IO, FG_INFO, "roll=" << roll << " pitch=" << pitch <<
218 " heave=" << heave );
220 string pve_sentence = pve;
221 p->port.write_port(pve_sentence);
227 // read data from port
228 bool FGSerial::read( char *buf, int *length ) {
230 *length = port.read_port( buf );
233 buf[ *length ] = '\0';
238 // write data to port
239 bool FGSerial::write( char *buf, int length ) {
240 int result = port.write_port( buf, length );
242 if ( result != length ) {
243 FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << device );
252 bool FGSerial::close() {
253 if ( ! port.close_port() ) {