1 // controls.hxx -- 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.
27 #include <simgear/misc/props.hxx>
29 #include <Main/fgfs.hxx>
30 #include <Main/globals.hxx>
33 # error This library requires C++
37 // Define a structure containing the control parameters
39 class FGControls : public FGSubsystem
63 double throttle[MAX_ENGINES];
64 double mixture[MAX_ENGINES];
65 double prop_advance[MAX_ENGINES];
66 double brake[MAX_WHEELS];
67 int magnetos[MAX_ENGINES];
69 bool starter[MAX_ENGINES];
72 SGPropertyNode * auto_coordination;
74 inline void CLAMP(double *x, double min, double max ) {
75 if ( *x < min ) { *x = min; }
76 if ( *x > max ) { *x = max; }
79 inline void CLAMP(int *i, int min, int max ) {
80 if ( *i < min ) { *i = min; }
81 if ( *i > max ) { *i = max; }
89 // Implementation of FGSubsystem.
99 inline double get_aileron() const { return aileron; }
100 inline double get_elevator() const { return elevator; }
101 inline double get_elevator_trim() const { return elevator_trim; }
102 inline double get_rudder() const { return rudder; }
103 inline double get_flaps() const { return flaps; }
104 inline double get_throttle(int engine) const { return throttle[engine]; }
105 inline double get_mixture(int engine) const { return mixture[engine]; }
106 inline double get_prop_advance(int engine) const {
107 return prop_advance[engine];
109 inline double get_brake(int wheel) const { return brake[wheel]; }
110 inline int get_magnetos(int engine) const { return magnetos[engine]; }
111 inline bool get_starter(int engine) const { return starter[engine]; }
112 inline bool get_gear_down() const { return gear_down; }
115 inline void set_aileron( double pos ) {
117 CLAMP( &aileron, -1.0, 1.0 );
119 // check for autocoordination
120 if ( auto_coordination->getBoolValue() )
122 set_rudder( aileron / 2.0 );
125 inline void move_aileron( double amt ) {
127 CLAMP( &aileron, -1.0, 1.0 );
129 // check for autocoordination
130 if ( auto_coordination->getBoolValue() )
132 set_rudder( aileron / 2.0 );
135 inline void set_elevator( double pos ) {
137 CLAMP( &elevator, -1.0, 1.0 );
139 inline void move_elevator( double amt ) {
141 CLAMP( &elevator, -1.0, 1.0 );
143 inline void set_elevator_trim( double pos ) {
145 CLAMP( &elevator_trim, -1.0, 1.0 );
147 inline void move_elevator_trim( double amt ) {
148 elevator_trim += amt;
149 CLAMP( &elevator_trim, -1.0, 1.0 );
151 inline void set_rudder( double pos ) {
153 CLAMP( &rudder, -1.0, 1.0 );
155 inline void move_rudder( double amt ) {
157 CLAMP( &rudder, -1.0, 1.0 );
159 inline void set_flaps( double pos ) {
161 CLAMP( &flaps, 0.0, 1.0 );
163 inline void move_flaps( double amt ) {
165 CLAMP( &flaps, 0.0, 1.0 );
167 inline void set_throttle( int engine, double pos ) {
168 if ( engine == ALL_ENGINES ) {
169 for ( int i = 0; i < MAX_ENGINES; i++ ) {
171 CLAMP( &throttle[i], 0.0, 1.0 );
174 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
175 throttle[engine] = pos;
176 CLAMP( &throttle[engine], 0.0, 1.0 );
180 inline void move_throttle( int engine, double amt ) {
181 if ( engine == ALL_ENGINES ) {
182 for ( int i = 0; i < MAX_ENGINES; i++ ) {
184 CLAMP( &throttle[i], 0.0, 1.0 );
187 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
188 throttle[engine] += amt;
189 CLAMP( &throttle[engine], 0.0, 1.0 );
193 inline void set_mixture( int engine, double pos ) {
194 if ( engine == ALL_ENGINES ) {
195 for ( int i = 0; i < MAX_ENGINES; i++ ) {
197 CLAMP( &mixture[i], 0.0, 1.0 );
200 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
201 mixture[engine] = pos;
202 CLAMP( &mixture[engine], 0.0, 1.0 );
206 inline void move_mixture( int engine, double amt ) {
207 if ( engine == ALL_ENGINES ) {
208 for ( int i = 0; i < MAX_ENGINES; i++ ) {
210 CLAMP( &mixture[i], 0.0, 1.0 );
213 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
214 mixture[engine] += amt;
215 CLAMP( &mixture[engine], 0.0, 1.0 );
219 inline void set_prop_advance( int engine, double pos ) {
220 if ( engine == ALL_ENGINES ) {
221 for ( int i = 0; i < MAX_ENGINES; i++ ) {
222 prop_advance[i] = pos;
223 CLAMP( &prop_advance[i], 0.0, 1.0 );
226 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
227 prop_advance[engine] = pos;
228 CLAMP( &prop_advance[engine], 0.0, 1.0 );
232 inline void move_prop_advance( int engine, double amt ) {
233 if ( engine == ALL_ENGINES ) {
234 for ( int i = 0; i < MAX_ENGINES; i++ ) {
235 prop_advance[i] += amt;
236 CLAMP( &prop_advance[i], 0.0, 1.0 );
239 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
240 prop_advance[engine] += amt;
241 CLAMP( &prop_advance[engine], 0.0, 1.0 );
245 inline void set_magnetos( int engine, int pos ) {
246 if ( engine == ALL_ENGINES ) {
247 for ( int i = 0; i < MAX_ENGINES; i++ ) {
249 CLAMP( &magnetos[i], 0, 3 );
252 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
253 magnetos[engine] = pos;
254 CLAMP( &magnetos[engine], 0, 3 );
258 inline void move_magnetos( int engine, int amt ) {
259 if ( engine == ALL_ENGINES ) {
260 for ( int i = 0; i < MAX_ENGINES; i++ ) {
262 CLAMP( &magnetos[i], 0, 3 );
265 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
266 magnetos[engine] += amt;
267 CLAMP( &magnetos[engine], 0, 3 );
271 inline void set_starter( int engine, bool flag ) {
272 if ( engine == ALL_ENGINES ) {
273 for ( int i = 0; i < MAX_ENGINES; i++ ) {
277 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
278 starter[engine] = flag;
282 inline void set_brake( int wheel, double pos ) {
283 if ( wheel == ALL_WHEELS ) {
284 for ( int i = 0; i < MAX_WHEELS; i++ ) {
286 CLAMP( &brake[i], 0.0, 1.0 );
289 if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
291 CLAMP( &brake[wheel], 0.0, 1.0 );
295 inline void move_brake( int wheel, double amt ) {
296 if ( wheel == ALL_WHEELS ) {
297 for ( int i = 0; i < MAX_WHEELS; i++ ) {
299 CLAMP( &brake[i], 0.0, 1.0 );
302 if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
304 CLAMP( &brake[wheel], 0.0, 1.0 );
308 inline void set_gear_down( bool gear ) { gear_down = gear; }
312 #endif // _CONTROLS_HXX