1 // controls.cxx -- defines a standard interface to all flight sim controls
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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 "controls.hxx"
26 #include <simgear/debug/logstream.hxx>
27 #include <Main/fg_props.hxx>
31 ////////////////////////////////////////////////////////////////////////
32 // Inline utility methods.
33 ////////////////////////////////////////////////////////////////////////
36 CLAMP(double *x, double min, double max )
38 if ( *x < min ) { *x = min; }
39 if ( *x > max ) { *x = max; }
43 CLAMP(int *i, int min, int max )
45 if ( *i < min ) { *i = min; }
46 if ( *i > max ) { *i = max; }
50 ////////////////////////////////////////////////////////////////////////
51 // Implementation of FGControls.
52 ////////////////////////////////////////////////////////////////////////
55 FGControls::FGControls() :
64 throttle_idle( true ),
70 void FGControls::reset_all()
73 set_aileron_trim( 0.0 );
75 set_elevator_trim( 0.0 );
77 set_rudder_trim( 0.0 );
78 set_throttle( ALL_ENGINES, 0.0 );
79 set_starter( ALL_ENGINES, false );
80 set_magnetos( ALL_ENGINES, 0 );
82 set_fuel_selector( ALL_TANKS, true );
88 FGControls::~FGControls() {
95 for ( int engine = 0; engine < MAX_ENGINES; engine++ ) {
96 throttle[engine] = 0.0;
97 mixture[engine] = 1.0;
98 prop_advance[engine] = 1.0;
100 starter[engine] = false;
103 for ( int wheel = 0; wheel < MAX_WHEELS; wheel++ ) {
107 auto_coordination = fgGetNode("/sim/auto-coordination", true);
114 fgTie("/controls/aileron", this,
115 &FGControls::get_aileron, &FGControls::set_aileron);
116 fgSetArchivable("/controls/aileron");
117 fgTie("/controls/aileron-trim", this,
118 &FGControls::get_aileron_trim, &FGControls::set_aileron_trim);
119 fgSetArchivable("/controls/aileron-trim");
120 fgTie("/controls/elevator", this,
121 &FGControls::get_elevator, &FGControls::set_elevator);
122 fgSetArchivable("/controls/elevator");
123 fgTie("/controls/elevator-trim", this,
124 &FGControls::get_elevator_trim, &FGControls::set_elevator_trim);
125 fgSetArchivable("/controls/elevator-trim");
126 fgTie("/controls/rudder", this,
127 &FGControls::get_rudder, &FGControls::set_rudder);
128 fgSetArchivable("/controls/rudder");
129 fgTie("/controls/rudder-trim", this,
130 &FGControls::get_rudder_trim, &FGControls::set_rudder_trim);
131 fgSetArchivable("/controls/rudder-trim");
132 fgTie("/controls/flaps", this,
133 &FGControls::get_flaps, &FGControls::set_flaps);
134 fgSetArchivable("/controls/flaps");
136 for (index = 0; index < MAX_ENGINES; index++) {
138 sprintf(name, "/controls/throttle[%d]", index);
139 fgTie(name, this, index,
140 &FGControls::get_throttle, &FGControls::set_throttle);
141 fgSetArchivable(name);
142 sprintf(name, "/controls/mixture[%d]", index);
143 fgTie(name, this, index,
144 &FGControls::get_mixture, &FGControls::set_mixture);
145 fgSetArchivable(name);
146 sprintf(name, "/controls/propeller-pitch[%d]", index);
147 fgTie(name, this, index,
148 &FGControls::get_prop_advance, &FGControls::set_prop_advance);
149 fgSetArchivable(name);
150 sprintf(name, "/controls/magnetos[%d]", index);
151 fgTie(name, this, index,
152 &FGControls::get_magnetos, &FGControls::set_magnetos);
153 fgSetArchivable(name);
154 sprintf(name, "/controls/starter[%d]", index);
155 fgTie(name, this, index,
156 &FGControls::get_starter, &FGControls::set_starter);
157 fgSetArchivable(name);
159 fgTie("/controls/parking-brake", this,
160 &FGControls::get_parking_brake, &FGControls::set_parking_brake);
161 fgSetArchivable("/controls/parking-brake");
162 for (index = 0; index < MAX_WHEELS; index++) {
164 sprintf(name, "/controls/brakes[%d]", index);
165 fgTie(name, this, index,
166 &FGControls::get_brake, &FGControls::set_brake);
167 fgSetArchivable(name);
169 for (index = 0; index < MAX_TANKS; index++) {
171 sprintf(name, "/controls/fuel-selector[%d]", index);
172 fgTie(name, this, index,
173 &FGControls::get_fuel_selector, &FGControls::set_fuel_selector);
174 fgSetArchivable(name);
176 fgTie("/controls/gear-down", this,
177 &FGControls::get_gear_down, &FGControls::set_gear_down);
178 fgSetArchivable("/controls/gear-down");
183 FGControls::unbind ()
185 // Tie control properties.
186 fgUntie("/controls/aileron");
187 fgUntie("/controls/aileron-trim");
188 fgUntie("/controls/elevator");
189 fgUntie("/controls/elevator-trim");
190 fgUntie("/controls/rudder");
191 fgUntie("/controls/rudder-trim");
192 fgUntie("/controls/flaps");
194 for (index = 0; index < MAX_ENGINES; index++) {
196 sprintf(name, "/controls/throttle[%d]", index);
198 sprintf(name, "/controls/mixture[%d]", index);
200 sprintf(name, "/controls/propeller-pitch[%d]", index);
202 sprintf(name, "/controls/magnetos[%d]", index);
204 sprintf(name, "/controls/starter[%d]", index);
207 for (index = 0; index < MAX_WHEELS; index++) {
209 sprintf(name, "/controls/brakes[%d]", index);
212 fgUntie("/controls/fuel-selector");
213 fgUntie("/controls/gear-down");
218 FGControls::update (double dt)
224 ////////////////////////////////////////////////////////////////////////
225 // Setters and adjusters.
226 ////////////////////////////////////////////////////////////////////////
229 FGControls::set_aileron (double pos)
232 CLAMP( &aileron, -1.0, 1.0 );
234 // check for autocoordination
235 if ( auto_coordination->getBoolValue() ) {
236 set_rudder( aileron / 2.0 );
241 FGControls::move_aileron (double amt)
244 CLAMP( &aileron, -1.0, 1.0 );
246 // check for autocoordination
247 if ( auto_coordination->getBoolValue() ) {
248 set_rudder( aileron / 2.0 );
253 FGControls::set_aileron_trim( double pos )
256 CLAMP( &aileron_trim, -1.0, 1.0 );
260 FGControls::move_aileron_trim( double amt )
263 CLAMP( &aileron_trim, -1.0, 1.0 );
267 FGControls::set_elevator( double pos )
270 CLAMP( &elevator, -1.0, 1.0 );
274 FGControls::move_elevator( double amt )
277 CLAMP( &elevator, -1.0, 1.0 );
281 FGControls::set_elevator_trim( double pos )
284 CLAMP( &elevator_trim, -1.0, 1.0 );
288 FGControls::move_elevator_trim( double amt )
290 elevator_trim += amt;
291 CLAMP( &elevator_trim, -1.0, 1.0 );
295 FGControls::set_rudder( double pos )
298 CLAMP( &rudder, -1.0, 1.0 );
302 FGControls::move_rudder( double amt )
305 CLAMP( &rudder, -1.0, 1.0 );
309 FGControls::set_rudder_trim( double pos )
312 CLAMP( &rudder_trim, -1.0, 1.0 );
316 FGControls::move_rudder_trim( double amt )
319 CLAMP( &rudder_trim, -1.0, 1.0 );
323 FGControls::set_flaps( double pos )
326 CLAMP( &flaps, 0.0, 1.0 );
330 FGControls::move_flaps( double amt )
333 CLAMP( &flaps, 0.0, 1.0 );
337 FGControls::set_throttle( int engine, double pos )
339 if ( engine == ALL_ENGINES ) {
340 for ( int i = 0; i < MAX_ENGINES; i++ ) {
342 CLAMP( &throttle[i], 0.0, 1.0 );
345 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
346 throttle[engine] = pos;
347 CLAMP( &throttle[engine], 0.0, 1.0 );
353 FGControls::move_throttle( int engine, double amt )
355 if ( engine == ALL_ENGINES ) {
356 for ( int i = 0; i < MAX_ENGINES; i++ ) {
358 CLAMP( &throttle[i], 0.0, 1.0 );
361 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
362 throttle[engine] += amt;
363 CLAMP( &throttle[engine], 0.0, 1.0 );
369 FGControls::set_mixture( int engine, double pos )
371 if ( engine == ALL_ENGINES ) {
372 for ( int i = 0; i < MAX_ENGINES; i++ ) {
374 CLAMP( &mixture[i], 0.0, 1.0 );
377 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
378 mixture[engine] = pos;
379 CLAMP( &mixture[engine], 0.0, 1.0 );
385 FGControls::move_mixture( int engine, double amt )
387 if ( engine == ALL_ENGINES ) {
388 for ( int i = 0; i < MAX_ENGINES; i++ ) {
390 CLAMP( &mixture[i], 0.0, 1.0 );
393 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
394 mixture[engine] += amt;
395 CLAMP( &mixture[engine], 0.0, 1.0 );
401 FGControls::set_prop_advance( int engine, double pos )
403 if ( engine == ALL_ENGINES ) {
404 for ( int i = 0; i < MAX_ENGINES; i++ ) {
405 prop_advance[i] = pos;
406 CLAMP( &prop_advance[i], 0.0, 1.0 );
409 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
410 prop_advance[engine] = pos;
411 CLAMP( &prop_advance[engine], 0.0, 1.0 );
417 FGControls::move_prop_advance( int engine, double amt )
419 if ( engine == ALL_ENGINES ) {
420 for ( int i = 0; i < MAX_ENGINES; i++ ) {
421 prop_advance[i] += amt;
422 CLAMP( &prop_advance[i], 0.0, 1.0 );
425 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
426 prop_advance[engine] += amt;
427 CLAMP( &prop_advance[engine], 0.0, 1.0 );
433 FGControls::set_magnetos( int engine, int pos )
435 if ( engine == ALL_ENGINES ) {
436 for ( int i = 0; i < MAX_ENGINES; i++ ) {
438 CLAMP( &magnetos[i], 0, 3 );
441 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
442 magnetos[engine] = pos;
443 CLAMP( &magnetos[engine], 0, 3 );
449 FGControls::move_magnetos( int engine, int amt )
451 if ( engine == ALL_ENGINES ) {
452 for ( int i = 0; i < MAX_ENGINES; i++ ) {
454 CLAMP( &magnetos[i], 0, 3 );
457 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
458 magnetos[engine] += amt;
459 CLAMP( &magnetos[engine], 0, 3 );
465 FGControls::set_starter( int engine, bool flag )
467 if ( engine == ALL_ENGINES ) {
468 for ( int i = 0; i < MAX_ENGINES; i++ ) {
472 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
473 starter[engine] = flag;
479 FGControls::set_fuel_selector( int tank, bool pos )
481 if ( tank == ALL_TANKS ) {
482 for ( int i = 0; i < MAX_TANKS; i++ ) {
483 fuel_selector[i] = pos;
486 if ( (tank >= 0) && (tank < MAX_TANKS) ) {
487 fuel_selector[tank] = pos;
494 FGControls::set_parking_brake( double pos )
497 CLAMP(&parking_brake, 0.0, 1.0);
501 FGControls::set_brake( int wheel, double pos )
503 if ( wheel == ALL_WHEELS ) {
504 for ( int i = 0; i < MAX_WHEELS; i++ ) {
506 CLAMP( &brake[i], 0.0, 1.0 );
509 if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
511 CLAMP( &brake[wheel], 0.0, 1.0 );
517 FGControls::move_brake( int wheel, double amt )
519 if ( wheel == ALL_WHEELS ) {
520 for ( int i = 0; i < MAX_WHEELS; i++ ) {
522 CLAMP( &brake[i], 0.0, 1.0 );
525 if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
527 CLAMP( &brake[wheel], 0.0, 1.0 );
533 FGControls::set_gear_down( bool gear )