1 // ray.cxx -- "RayWoodworth" motion chair support
3 // Written by Alexander Perry, started May 2000
5 // Copyright (C) 2000, Alexander Perry, alex.perry@ieee.org
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.
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.
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.
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/math/fg_geodesy.hxx>
27 #include <FDM/flight.hxx>
29 #include "iochannel.hxx"
37 chair_vertical[0] = 0.0;
38 chair_vertical[1] = 0.0;
39 // chair_FILE = stderr;
48 // Ray Woodworth's motion chair has between 3 and 5 axes installed.
49 // It expects +/- 5V signals for full scale. In channel order, axes are:
50 // roll, pitch, yaw, sway, surge, heave
51 // The drivers are capable of generating (in the same order)
52 // +/- 30deg, 30deg, 15deg, 12in, 12in, 12in
54 // In this code implementation, the voltage outputs are generated
55 // using a ComputerBoards DDA06/Jr card and the associated Linux driver.
56 // Data is written to the device /dev/dda06jr-A as byte triplets;
57 // The first byte is the channel number (0-5 respectively) and
58 // the remaining two bytes are an unsigned short for the signal.
61 bool FGRAY::gen_message() {
62 // cout << "generating RayWoodworth message" << endl;
63 FGInterface *f = cur_fdm_state;
65 const double fullscale[6] = { -0.8, -0.8, -0.25, /* radians */
66 -0.3, -0.3, -0.15 /* meters */ };
68 /* Figure out how big our timesteps are */
69 double dt = 0.05; /* seconds */
71 /* get basic information about gravity */
72 double grav_acc = -9.81;
73 double vert_acc = f->get_A_Z_pilot() * 0.3;
74 if ( -3.0 < vert_acc )
77 for ( axis = 0; axis < 3; axis++ )
78 { /* Compute each angular axis together with the linear
79 axis which is coupled by smooth coordinated flight
82 double lin_pos, lin_acc;
84 /* Retrieve the desired components */
86 case 0: ang_pos = f->get_Phi();
87 lin_acc = f->get_A_Y_pilot() * 0.3;
89 case 1: ang_pos = f->get_Theta();
90 lin_acc = f->get_A_X_pilot() * 0.3;
92 case 2: ang_pos = f->get_Psi();
93 lin_acc = grav_acc - vert_acc;
101 /* Make sure the angles are reasonable onscale */
102 while ( ang_pos < -M_PI ) {
105 while ( ang_pos > M_PI ) {
109 /* Tell interested parties what the situation is */
111 fprintf ( chair_FILE, "RAY %s, %8.3f rad %8.3f m/s/s =>",
112 ((axis==0)?"Roll ":((axis==1)?"Pitch":"Yaw ")),
116 /* The upward direction and axis are special cases */
120 /* Integrate vertical acceleration into velocity,
121 diluted by 50% and with a 0.2 second high pass */
122 chair_rising += ( lin_acc - chair_rising ) * dt * 0.5;
123 /* Integrate velocity into position, 0.2 sec high pass */
124 chair_height += ( chair_rising - chair_height ) * dt * 0.5;
125 lin_pos = chair_height;
128 /* Make sure that we walk through North cleanly */
129 if ( fabs ( ang_pos - chair_heading ) > M_PI )
130 { /* Need to swing chair by 360 degrees */
131 if ( ang_pos < chair_heading )
132 chair_heading -= 2 * M_PI;
133 else chair_heading += 2 * M_PI;
135 /* Remove the chair heading from the true heading */
136 ang_pos -= chair_heading;
137 /* Wash out the error at 5 sec timeconstant because
138 a standard rate turn is 3 deg/sec and the chair
139 can represent 15 degrees full scale. */
140 chair_heading += ang_pos * dt * 0.2;
141 /* If they turn fast, at 90 deg error subtract 30 deg */
142 if ( fabs(ang_pos) > M_PI / 2 )
143 chair_heading += ang_pos / 3;
146 { /* 3 second low pass to find attitude and gravity vector */
147 chair_vertical[axis] += ( dt / 3 ) *
148 ( lin_acc / vert_acc + ang_pos
149 - chair_vertical[axis] );
150 /* find out how much linear acceleration is left */
151 lin_acc -= chair_vertical[axis] * vert_acc;
152 /* reposition the pilot tilt relative to the chair */
153 ang_pos -= chair_vertical[axis];
154 /* integrate linear acceleration into a position */
155 lin_pos = lin_acc; /* HACK */
158 /* Tell interested parties what we'll do */
160 fprintf ( chair_FILE, " %8.3f deg %8.3f cm.\n",
161 ang_pos * 60.0, lin_pos * 100.0 );
164 /* Write the resulting numbers to the command buffer */
165 /* The first pass number is linear, second pass is angle */
166 for ( subaxis = axis; subaxis < 6; subaxis += 3 )
167 { unsigned short *dac;
168 /* Select the DAC in the command buffer */
169 buf [ 3*subaxis ] = subaxis;
170 dac = (unsigned short *) ( buf + 1 + 3*subaxis );
171 /* Select the relevant number to put there */
172 double propose = ( subaxis < 3 ) ? ang_pos : lin_pos;
173 /* Scale to the hardware's full scale range */
174 propose /= fullscale [ subaxis ];
175 /* Use a sine shaped washout on all axes */
176 if ( propose < -M_PI / 2 ) *dac = 0x0000; else
177 if ( propose > M_PI / 2 ) *dac = 0xFFFF; else
178 *dac = (unsigned short) ( 32767 *
179 ( 1.0 + sin ( propose ) ) );
182 /* That concludes the per-axis calculations */
185 /* Tell the caller what we did */
193 bool FGRAY::parse_message() {
194 FG_LOG( FG_IO, FG_ALERT, "RAY input not supported" );
200 // process work for this port
201 bool FGRAY::process() {
202 FGIOChannel *io = get_io_channel();
204 if ( get_direction() == out ) {
206 if ( ! io->write( buf, length ) ) {
207 FG_LOG( FG_IO, FG_ALERT, "Error writing data." );
210 } else if ( get_direction() == in ) {
211 FG_LOG( FG_IO, FG_ALERT, "in direction not supported for RAY." );