]> git.mxchange.org Git - flightgear.git/blob - src/Network/pve.cxx
Make a subtle change to tile loading/unloading policy in order to make the tile
[flightgear.git] / src / Network / pve.cxx
1 // pve.cxx -- "PVE" protocal class (for Provision Entertainment)
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 <stdio.h>              // sprintf()
25
26 #include <simgear/constants.h>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/io/iochannel.hxx>
29
30 #include <FDM/flight.hxx>
31
32 #include "pve.hxx"
33
34
35 FGPVE::FGPVE() {
36 }
37
38
39 FGPVE::~FGPVE() {
40 }
41
42
43 // "PVE" (ProVision Entertainment) output format (for some sort of
44 // motion platform)
45 //
46 // Outputs a 5-byte data packet defined as follows:
47 //
48 // First bite:  ASCII character "P" ( 0x50 or 80 decimal )
49 // Second byte:  "roll" value (1-255) 1 being 0* and 255 being 359*
50 // Third byte:  "pitch" value (1-255) 1 being 0* and 255 being 359*
51 // Fourth byte:  "heave" value (or vertical acceleration?)
52
53 bool FGPVE::gen_message() {
54     // cout << "generating pve message" << endl;
55     FGInterface *f = cur_fdm_state;
56
57     // get roll and pitch, convert to degrees
58     double roll_deg = f->get_Phi() * SGD_RADIANS_TO_DEGREES;
59     while ( roll_deg <= -180.0 ) {
60         roll_deg += 360.0;
61     }
62     while ( roll_deg > 180.0 ) {
63         roll_deg -= 360.0;
64     }
65
66     double pitch_deg = f->get_Theta() * SGD_RADIANS_TO_DEGREES;
67     while ( pitch_deg <= -180.0 ) {
68         pitch_deg += 360.0;
69     }
70     while ( pitch_deg > 180.0 ) {
71         pitch_deg -= 360.0;
72     }
73
74     short int heave = (int)(f->get_W_body() * 128.0);
75
76     // scale roll and pitch to output format (1 - 255)
77     // straight && level == (128, 128)
78
79     short int roll = (int)(roll_deg * 32768 / 180.0);
80     short int pitch = (int)(pitch_deg * 32768 / 180.0);
81
82     unsigned char roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2;
83     roll_b1 = roll >> 8;
84     roll_b2 = roll & 0x00ff;
85     pitch_b1 = pitch >> 8;
86     pitch_b2 = pitch & 0x00ff;
87     heave_b1 = heave >> 8;
88     heave_b2 = heave & 0x00ff;
89
90     sprintf( buf, "p%c%c%c%c%c%c\n", 
91              roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
92     length = 8;
93
94     // printf( "p [ %u %u ]  [ %u %u ]  [ %u %u ]\n", 
95     //         roll_b1, roll_b2, pitch_b1, pitch_b2, heave_b1, heave_b2 );
96
97     SG_LOG( SG_IO, SG_INFO, "roll=" << roll << " pitch=" << pitch <<
98             " heave=" << heave );
99
100     return true;
101 }
102
103
104 // parse RUL message
105 bool FGPVE::parse_message() {
106     SG_LOG( SG_IO, SG_ALERT, "PVE input not supported" );
107
108     return false;
109 }
110
111
112 // process work for this port
113 bool FGPVE::process() {
114     SGIOChannel *io = get_io_channel();
115
116     if ( get_direction() == SG_IO_OUT ) {
117         gen_message();
118         if ( ! io->write( buf, length ) ) {
119             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
120             return false;
121         }
122     } else if ( get_direction() == SG_IO_IN ) {
123         SG_LOG( SG_IO, SG_ALERT, "in direction not supported for RUL." );
124         return false;
125     }
126
127     return true;
128 }