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