]> git.mxchange.org Git - flightgear.git/blob - src/Network/fg_serial.cxx
Revamping I/O system.
[flightgear.git] / src / Network / fg_serial.cxx
1 // fg_serial.cxx -- Serial I/O 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
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>
41
42 // #include "options.hxx"
43
44 #include "fg_serial.hxx"
45
46 FG_USING_STD(string);
47
48
49 FGSerial::FGSerial() {
50 }
51
52
53 FGSerial::~FGSerial() {
54 }
55
56
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 );
61         return false;
62     }
63
64     // cout << "fd = " << port.fd << endl;
65
66     if ( ! port.set_baud( atoi( baud.c_str() ) ) ) {
67         FG_LOG( FG_IO, FG_ALERT, "Error setting baud: " << baud );
68         return false;
69     }
70
71     return true;
72 }
73
74
75 #if 0
76 // "RUL" output format (for some sort of motion platform)
77 //
78 // The Baud rate is 2400 , one start bit, eight data bits, 1 stop bit,
79 // no parity.
80 //
81 // For position it requires a 3-byte data packet defined as follows:
82 //
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*
86 //
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
89 // 3&5.
90
91 static void send_rul_out( fgIOCHANNEL *p ) {
92     char rul[256];
93
94     FGInterface *f;
95     FGTime *t;
96
97     f = current_aircraft.fdm_state;
98     t = FGTime::cur_time_params;
99
100     // run as often as possibleonce per second
101
102     // this runs once per second
103     // if ( p->last_time == t->get_cur_time() ) {
104     //    return;
105     // }
106     // p->last_time = t->get_cur_time();
107     // if ( t->get_cur_time() % 2 != 0 ) {
108     //    return;
109     // }
110     
111     // get roll and pitch, convert to degrees
112     double roll_deg = f->get_Phi() * RAD_TO_DEG;
113     while ( roll_deg < -180.0 ) {
114         roll_deg += 360.0;
115     }
116     while ( roll_deg > 180.0 ) {
117         roll_deg -= 360.0;
118     }
119
120     double pitch_deg = f->get_Theta() * RAD_TO_DEG;
121     while ( pitch_deg < -180.0 ) {
122         pitch_deg += 360.0;
123     }
124     while ( pitch_deg > 180.0 ) {
125         pitch_deg -= 360.0;
126     }
127
128     // scale roll and pitch to output format (1 - 255)
129     // straight && level == (128, 128)
130
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;
133
134     sprintf( rul, "p%c%c\n", roll, pitch);
135
136     FG_LOG( FG_IO, FG_INFO, "p " << roll << " " << pitch );
137
138     string rul_sentence = rul;
139     p->port.write_port(rul_sentence);
140 }
141
142
143 // "PVE" (ProVision Entertainment) output format (for some sort of
144 // motion platform)
145 //
146 // Outputs a 5-byte data packet defined as follows:
147 //
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?)
152 //
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
155 // 3&5.
156
157 static void send_pve_out( fgIOCHANNEL *p ) {
158     char pve[256];
159
160     FGInterface *f;
161     FGTime *t;
162
163
164     f = current_aircraft.fdm_state;
165     t = FGTime::cur_time_params;
166
167     // run as often as possibleonce per second
168
169     // this runs once per second
170     // if ( p->last_time == t->get_cur_time() ) {
171     //    return;
172     // }
173     // p->last_time = t->get_cur_time();
174     // if ( t->get_cur_time() % 2 != 0 ) {
175     //    return;
176     // }
177     
178     // get roll and pitch, convert to degrees
179     double roll_deg = f->get_Phi() * RAD_TO_DEG;
180     while ( roll_deg <= -180.0 ) {
181         roll_deg += 360.0;
182     }
183     while ( roll_deg > 180.0 ) {
184         roll_deg -= 360.0;
185     }
186
187     double pitch_deg = f->get_Theta() * RAD_TO_DEG;
188     while ( pitch_deg <= -180.0 ) {
189         pitch_deg += 360.0;
190     }
191     while ( pitch_deg > 180.0 ) {
192         pitch_deg -= 360.0;
193     }
194
195     short int heave = (int)(f->get_W_body() * 128.0);
196
197     // scale roll and pitch to output format (1 - 255)
198     // straight && level == (128, 128)
199
200     short int roll = (int)(roll_deg * 32768 / 180.0);
201     short int pitch = (int)(pitch_deg * 32768 / 180.0);
202
203     unsigned char roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2;
204     roll_b1 = roll >> 8;
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;
210
211     sprintf( pve, "p%c%c%c%c%c%c\n", 
212              roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
213
214     // printf( "p [ %u %u ]  [ %u %u ]  [ %u %u ]\n", 
215     //         roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
216
217     FG_LOG( FG_IO, FG_INFO, "roll=" << roll << " pitch=" << pitch <<
218             " heave=" << heave );
219
220     string pve_sentence = pve;
221     p->port.write_port(pve_sentence);
222 }
223
224 #endif
225
226
227 // read data from port
228 bool FGSerial::read( char *buf, int *length ) {
229     // read a chunk
230     *length = port.read_port( buf );
231     
232     // just in case ...
233     buf[ *length ] = '\0';
234
235     return true;
236 }
237
238 // write data to port
239 bool FGSerial::write( char *buf, int length ) {
240     int result = port.write_port( buf, length );
241
242     if ( result != length ) {
243         FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << device );
244         return false;
245     }
246
247     return true;
248 }
249
250
251 // close the port
252 bool FGSerial::close() {
253     if ( ! port.close_port() ) {
254         return false;
255     }
256
257     return true;
258 }