]> git.mxchange.org Git - flightgear.git/blob - src/Network/ray.cxx
749a9629b96e75659e201345137df454996579be
[flightgear.git] / src / Network / ray.cxx
1 // ray.cxx -- "RayWoodworth" motion chair support
2 //
3 // Written by Alexander Perry, started May 2000
4 //
5 // Copyright (C) 2000, Alexander Perry, alex.perry@ieee.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 <simgear/debug/logstream.hxx>
25 #include <simgear/math/fg_geodesy.hxx>
26
27 #include <FDM/flight.hxx>
28 #include <Time/fg_time.hxx>
29
30 #include "iochannel.hxx"
31 #include "ray.hxx"
32
33
34 FGRAY::FGRAY() {
35         chair_rising = 0.0;
36         chair_height = 0.0;
37         chair_heading = 0.0;
38         chair_vertical[0] = 0.0;
39         chair_vertical[1] = 0.0;
40 }
41
42
43 FGRAY::~FGRAY() {
44 }
45
46
47 // Ray Woodworth's motion chair has between 3 and 5 axes installed.
48 // It expects +/- 5V signals for full scale.  In channel order, axes are:
49 //      roll, pitch, yaw, sway, surge, heave
50 // The drivers are capable of generating (in the same order)
51 //      +/- 30deg, 30deg, 15deg, 12in, 12in, 12in
52 //
53 // In this code implementation, the voltage outputs are generated
54 // using a ComputerBoards DDA06/Jr card and the associated Linux driver.
55 // Data is written to the device /dev/dda06jr-A as byte triplets;
56 // The first byte is the channel number (0-5 respectively) and
57 // the remaining two bytes are an unsigned short for the signal.
58
59
60 bool FGRAY::gen_message() {
61     // cout << "generating RayWoodworth message" << endl;
62     FGInterface *f = cur_fdm_state;
63     int axis, subaxis;
64     const double fullscale[6] = { -0.8, -0.8, -0.25, /* radians */
65                                   -0.3, -0.3, -0.15  /* meters */ };
66
67     /* Figure out how big our timesteps are */
68     double dt = 0.05; /* seconds */
69
70     /* get basic information about gravity */
71     double grav_acc = -9.81;
72     double vert_acc = f->get_A_Z_pilot() * 0.3;
73     if ( -3.0 < vert_acc )
74         vert_acc = -3.0;
75
76     for ( axis = 0; axis < 3; axis++ )
77     {   /* Compute each angular axis together with the linear
78            axis which is coupled by smooth coordinated flight
79         */
80         double ang_pos;
81         double lin_pos, lin_acc;
82
83         /* Retrieve the desired components */
84         switch ( axis ) {
85         case 0: ang_pos = f->get_Phi();
86                 lin_acc = f->get_A_Y_pilot() * 0.3;
87                 break;
88         case 1: ang_pos = f->get_Theta();
89                 lin_acc =-f->get_A_X_pilot() * 0.3;
90                 break;
91         case 2: ang_pos = f->get_Psi();
92                 lin_acc = grav_acc - vert_acc;
93                 break;
94         default:
95                 ang_pos = 0.0;
96                 lin_acc = 0.0;
97                 break;
98         }
99
100         /* Make sure the angles are reasonable onscale */
101         while ( ang_pos < -M_PI ) {
102                 ang_pos += 2 * M_PI;
103         }
104         while ( ang_pos > M_PI ) {
105                 ang_pos -= 2 * M_PI;
106         }
107
108         /* Tell interested parties what the situation is */
109         printf ( "RAY %s, %8.3f rad %8.3f m/s/s  =>",
110                 ((axis==0)?"Roll ":((axis==1)?"Pitch":"Yaw  ")),
111                 ang_pos, lin_acc );
112
113         /* The upward direction and axis are special cases */
114         if ( axis == 2 )
115         {
116         /* heave */
117                 /* Integrate vertical acceleration into velocity,
118                    diluted by 50% and with a 0.2 second high pass */
119                 chair_rising += ( lin_acc - chair_rising ) * dt * 0.5;
120                 /* Integrate velocity into position, 0.2 sec high pass */
121                 chair_height += ( chair_rising - chair_height ) * dt * 0.5;
122                 lin_pos = chair_height;
123
124         /* yaw */
125                 /* Make sure that we walk through North cleanly */
126                 if ( fabs ( ang_pos - chair_heading ) > M_PI )
127                 {       /* Need to swing chair by 360 degrees */
128                         if ( ang_pos < chair_heading )
129                                 chair_heading -= 2 * M_PI;
130                         else    chair_heading += 2 * M_PI;
131                 }
132                 /* Remove the chair heading from the true heading */
133                 ang_pos -= chair_heading;
134                 /* Wash out the error at 5 sec timeconstant because
135                    a standard rate turn is 3 deg/sec and the chair
136                    can represent 15 degrees full scale.  */
137                 chair_heading += ang_pos * dt * 0.2;
138                 /* If they turn fast, at 90 deg error subtract 30 deg */
139                 if ( fabs(ang_pos) > M_PI / 2 )
140                         chair_heading += ang_pos / 3;
141
142         } else
143         {       /* 3 second low pass to find attitude and gravity vector */
144                 chair_vertical[axis] += ( dt / 3 ) *
145                         ( lin_acc / vert_acc + ang_pos 
146                                 - chair_vertical[axis] );
147                 /* find out how much linear acceleration is left */
148                 lin_acc -= chair_vertical[axis] * vert_acc;
149                 /* reposition the pilot tilt relative to the chair */
150                 ang_pos -= chair_vertical[axis];
151                 /* integrate linear acceleration into a position */
152                 lin_pos = lin_acc; /* HACK */
153         }
154
155         /* Tell interested parties what we'll do */
156         printf ( "  %8.3f deg %8.3f cm.\n",
157                 ang_pos * 60.0, lin_pos * 100.0 );
158
159         /* Write the resulting numbers to the command buffer */
160         /* The first pass number is linear, second pass is angle */
161         for ( subaxis = axis; subaxis < 6; subaxis += 3 )
162         {       unsigned short *dac;
163                 /* Select the DAC in the command buffer */
164                 buf [ 3*subaxis ] = subaxis;
165                 dac = (unsigned short *) ( buf + 1 + 3*subaxis );
166                 /* Select the relevant number to put there */
167                 double propose = ( subaxis < 3 ) ? ang_pos : lin_pos;
168                 /* Scale to the hardware's full scale range */
169                 propose /= fullscale [ subaxis ];
170                 /* Use a sine shaped washout on all axes */
171                 if ( propose < -M_PI / 2 ) *dac = 0x0000; else
172                 if ( propose >  M_PI / 2 ) *dac = 0xFFFF; else
173                    *dac = (unsigned short) ( 32767 * 
174                                 ( 1.0 + sin ( propose ) ) );
175         }
176
177         /* That concludes the per-axis calculations */
178     }
179
180     /* Tell the caller what we did */
181     length = 18;
182
183     /* Log bytes for debug */
184 //    for ( axis = 0; axis < length; axis++ )
185 //        printf ( "%02x ", (unsigned int) (unsigned char) buf[axis] );
186 //    printf ( "\n" );
187
188     return true;
189 }
190
191
192 // parse RUL message
193 bool FGRAY::parse_message() {
194     FG_LOG( FG_IO, FG_ALERT, "RAY input not supported" );
195
196     return false;
197 }
198
199
200 // process work for this port
201 bool FGRAY::process() {
202     FGIOChannel *io = get_io_channel();
203
204     if ( get_direction() == out ) {
205         gen_message();
206         if ( ! io->write( buf, length ) ) {
207             FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
208             return false;
209         }
210     } else if ( get_direction() == in ) {
211         FG_LOG( FG_IO, FG_ALERT, "in direction not supported for RAY." );
212         return false;
213     }
214
215     return true;
216 }