]> git.mxchange.org Git - flightgear.git/blob - src/Network/rul.cxx
Merge branch 'work4' into next
[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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <stdio.h>              // sprintf()
29
30 #include <simgear/constants.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/io/iochannel.hxx>
33
34 #include <FDM/flightProperties.hxx>
35
36 #include "rul.hxx"
37
38
39 FGRUL::FGRUL() {
40 }
41
42
43 FGRUL::~FGRUL() {
44 }
45
46
47 // "RUL" output format (for some sort of motion platform)
48 //
49 // The Baud rate is 2400 , one start bit, eight data bits, 1 stop bit,
50 // no parity.
51 //
52 // For position it requires a 3-byte data packet defined as follows:
53 //
54 // First bite: ascII character "P" ( 0x50 or 80 decimal )
55 // Second byte X pos. (1-255) 1 being 0* and 255 being 359*
56 // Third byte Y pos.( 1-255) 1 being 0* and 255 359*
57 //
58 // So sending 80 127 127 to the two axis motors will position on 180*
59 // The RS- 232 port is a nine pin connector and the only pins used are
60 // 3&5.
61
62 bool FGRUL::gen_message() {
63     // cout << "generating rul message" << endl;
64     FlightProperties f;
65
66     // get roll and pitch, convert to degrees
67     double roll_deg = f.get_Phi() * SGD_RADIANS_TO_DEGREES;
68     while ( roll_deg < -180.0 ) {
69         roll_deg += 360.0;
70     }
71     while ( roll_deg > 180.0 ) {
72         roll_deg -= 360.0;
73     }
74
75     double pitch_deg = f.get_Theta() * SGD_RADIANS_TO_DEGREES;
76     while ( pitch_deg < -180.0 ) {
77         pitch_deg += 360.0;
78     }
79     while ( pitch_deg > 180.0 ) {
80         pitch_deg -= 360.0;
81     }
82
83     // scale roll and pitch to output format (1 - 255)
84     // straight && level == (128, 128)
85
86     int roll = (int)( (roll_deg+180.0) * 255.0 / 360.0) + 1;
87     int pitch = (int)( (pitch_deg+180.0) * 255.0 / 360.0) + 1;
88
89     sprintf( buf, "p%c%c\n", roll, pitch);
90     length = 4;
91
92     SG_LOG( SG_IO, SG_INFO, "p " << roll << " " << pitch );
93
94     return true;
95 }
96
97
98 // parse RUL message
99 bool FGRUL::parse_message() {
100     SG_LOG( SG_IO, SG_ALERT, "RUL input not supported" );
101
102     return false;
103 }
104
105
106 // process work for this port
107 bool FGRUL::process() {
108     SGIOChannel *io = get_io_channel();
109
110     if ( get_direction() == SG_IO_OUT ) {
111         gen_message();
112         if ( ! io->write( buf, length ) ) {
113             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
114             return false;
115         }
116     } else if ( get_direction() == SG_IO_IN ) {
117         SG_LOG( SG_IO, SG_ALERT, "in direction not supported for RUL." );
118         return false;
119     }
120
121     return true;
122 }