]> git.mxchange.org Git - flightgear.git/blob - src/Network/rul.cxx
A lot of code reorganization relating to moving some core code from
[flightgear.git] / src / Network / rul.cxx
1 // rul.cxx -- "RUL" protocal class (for some sort of motion platform
2 //            some guy was building)
3 //
4 // Written by Curtis Olson, started November 1999.
5 //
6 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/math/fg_geodesy.hxx>
27 #include <simgear/timing/fg_time.hxx>
28
29 #include <FDM/flight.hxx>
30
31 #include "iochannel.hxx"
32 #include "rul.hxx"
33
34
35 FGRUL::FGRUL() {
36 }
37
38
39 FGRUL::~FGRUL() {
40 }
41
42
43 // "RUL" output format (for some sort of motion platform)
44 //
45 // The Baud rate is 2400 , one start bit, eight data bits, 1 stop bit,
46 // no parity.
47 //
48 // For position it requires a 3-byte data packet defined as follows:
49 //
50 // First bite: ascII character "P" ( 0x50 or 80 decimal )
51 // Second byte X pos. (1-255) 1 being 0* and 255 being 359*
52 // Third byte Y pos.( 1-255) 1 being 0* and 255 359*
53 //
54 // So sending 80 127 127 to the two axis motors will position on 180*
55 // The RS- 232 port is a nine pin connector and the only pins used are
56 // 3&5.
57
58 bool FGRUL::gen_message() {
59     // cout << "generating rul message" << endl;
60     FGInterface *f = cur_fdm_state;
61
62     // get roll and pitch, convert to degrees
63     double roll_deg = f->get_Phi() * RAD_TO_DEG;
64     while ( roll_deg < -180.0 ) {
65         roll_deg += 360.0;
66     }
67     while ( roll_deg > 180.0 ) {
68         roll_deg -= 360.0;
69     }
70
71     double pitch_deg = f->get_Theta() * RAD_TO_DEG;
72     while ( pitch_deg < -180.0 ) {
73         pitch_deg += 360.0;
74     }
75     while ( pitch_deg > 180.0 ) {
76         pitch_deg -= 360.0;
77     }
78
79     // scale roll and pitch to output format (1 - 255)
80     // straight && level == (128, 128)
81
82     int roll = (int)( (roll_deg+180.0) * 255.0 / 360.0) + 1;
83     int pitch = (int)( (pitch_deg+180.0) * 255.0 / 360.0) + 1;
84
85     sprintf( buf, "p%c%c\n", roll, pitch);
86     length = 4;
87
88     FG_LOG( FG_IO, FG_INFO, "p " << roll << " " << pitch );
89
90     return true;
91 }
92
93
94 // parse RUL message
95 bool FGRUL::parse_message() {
96     FG_LOG( FG_IO, FG_ALERT, "RUL input not supported" );
97
98     return false;
99 }
100
101
102 // process work for this port
103 bool FGRUL::process() {
104     FGIOChannel *io = get_io_channel();
105
106     if ( get_direction() == out ) {
107         gen_message();
108         if ( ! io->write( buf, length ) ) {
109             FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
110             return false;
111         }
112     } else if ( get_direction() == in ) {
113         FG_LOG( FG_IO, FG_ALERT, "in direction not supported for RUL." );
114         return false;
115     }
116
117     return true;
118 }