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