]> git.mxchange.org Git - flightgear.git/blob - src/Controls/controls.cxx
Oops, fixed a typo.
[flightgear.git] / src / Controls / controls.cxx
1 // controls.cxx -- defines a standard interface to all flight sim controls
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
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 "controls.hxx"
25
26 #include <simgear/debug/logstream.hxx>
27 #include <Main/fg_props.hxx>
28
29
30 \f
31 ////////////////////////////////////////////////////////////////////////
32 // Inline utility methods.
33 ////////////////////////////////////////////////////////////////////////
34
35 static inline void
36 CLAMP(double *x, double min, double max )
37 {
38   if ( *x < min ) { *x = min; }
39   if ( *x > max ) { *x = max; }
40 }
41
42 static inline void
43 CLAMP(int *i, int min, int max )
44 {
45   if ( *i < min ) { *i = min; }
46   if ( *i > max ) { *i = max; }
47 }
48
49 \f
50 ////////////////////////////////////////////////////////////////////////
51 // Implementation of FGControls.
52 ////////////////////////////////////////////////////////////////////////
53
54 // Constructor
55 FGControls::FGControls() :
56     aileron( 0.0 ),
57     aileron_trim( 0.0 ),
58     elevator( 0.0 ),
59     elevator_trim( 0.0 ),
60     rudder( 0.0 ),
61     rudder_trim( 0.0 ),
62     flaps( 0.0 ),
63     parking_brake( 0.0 ),
64     throttle_idle( true ),
65     fuel_selector( FUEL_BOTH ),
66     gear_down( false )
67 {
68 }
69
70
71 void FGControls::reset_all()
72 {
73     set_aileron( 0.0 );
74     set_aileron_trim( 0.0 );
75     set_elevator( 0.0 );
76     set_elevator_trim( 0.0 );
77     set_rudder( 0.0 );
78     set_rudder_trim( 0.0 );
79     set_throttle( ALL_ENGINES, 0.0 );
80     set_starter( ALL_ENGINES, false );
81     set_magnetos( ALL_ENGINES, 0 );
82     throttle_idle = true;
83     fuel_selector = FUEL_BOTH;
84     gear_down = true;
85 }
86
87
88 // Destructor
89 FGControls::~FGControls() {
90 }
91
92
93 void
94 FGControls::init ()
95 {
96     for ( int engine = 0; engine < MAX_ENGINES; engine++ ) {
97         throttle[engine] = 0.0;
98         mixture[engine] = 1.0;
99         prop_advance[engine] = 1.0;
100         magnetos[engine] = 0;
101         starter[engine] = false;
102     }
103
104     for ( int wheel = 0; wheel < MAX_WHEELS; wheel++ ) {
105         brake[wheel] = 0.0;
106     }
107
108     auto_coordination = fgGetNode("/sim/auto-coordination", true);
109 }
110
111
112 void
113 FGControls::bind ()
114 {
115   fgTie("/controls/aileron", this,
116         &FGControls::get_aileron, &FGControls::set_aileron);
117   fgSetArchivable("/controls/aileron");
118   fgTie("/controls/aileron-trim", this,
119        &FGControls::get_aileron_trim, &FGControls::set_aileron_trim);
120   fgSetArchivable("/controls/aileron-trim");
121   fgTie("/controls/elevator", this,
122        &FGControls::get_elevator, &FGControls::set_elevator);
123   fgSetArchivable("/controls/elevator");
124   fgTie("/controls/elevator-trim", this,
125        &FGControls::get_elevator_trim, &FGControls::set_elevator_trim);
126   fgSetArchivable("/controls/elevator-trim");
127   fgTie("/controls/rudder", this,
128        &FGControls::get_rudder, &FGControls::set_rudder);
129   fgSetArchivable("/controls/rudder");
130   fgTie("/controls/rudder-trim", this,
131        &FGControls::get_rudder_trim, &FGControls::set_rudder_trim);
132   fgSetArchivable("/controls/rudder-trim");
133   fgTie("/controls/flaps", this,
134        &FGControls::get_flaps, &FGControls::set_flaps);
135   fgSetArchivable("/controls/flaps");
136   int index;
137   for (index = 0; index < MAX_ENGINES; index++) {
138     char name[32];
139     sprintf(name, "/controls/throttle[%d]", index);
140     fgTie(name, this, index,
141           &FGControls::get_throttle, &FGControls::set_throttle);
142     fgSetArchivable(name);
143     sprintf(name, "/controls/mixture[%d]", index);
144     fgTie(name, this, index,
145          &FGControls::get_mixture, &FGControls::set_mixture);
146     fgSetArchivable(name);
147     sprintf(name, "/controls/propeller-pitch[%d]", index);
148     fgTie(name, this, index,
149          &FGControls::get_prop_advance, &FGControls::set_prop_advance);
150     fgSetArchivable(name);
151     sprintf(name, "/controls/magnetos[%d]", index);
152     fgTie(name, this, index,
153          &FGControls::get_magnetos, &FGControls::set_magnetos);
154     fgSetArchivable(name);
155     sprintf(name, "/controls/starter[%d]", index);
156     fgTie(name, this, index,
157          &FGControls::get_starter, &FGControls::set_starter);
158     fgSetArchivable(name);
159   }
160   fgTie("/controls/parking-brake", this,
161         &FGControls::get_parking_brake, &FGControls::set_parking_brake);
162   fgSetArchivable("/controls/parking-brake");
163   for (index = 0; index < MAX_WHEELS; index++) {
164     char name[32];
165     sprintf(name, "/controls/brakes[%d]", index);
166     fgTie(name, this, index,
167          &FGControls::get_brake, &FGControls::set_brake);
168     fgSetArchivable(name);
169   }
170   fgTie("/controls/fuel-selector", this,
171         &FGControls::get_fuel_selector, &FGControls::set_fuel_selector);
172   fgSetArchivable("/controls/fuel-selector");
173   fgTie("/controls/gear-down", this,
174         &FGControls::get_gear_down, &FGControls::set_gear_down);
175   fgSetArchivable("/controls/gear-down");
176 }
177
178
179 void
180 FGControls::unbind ()
181 {
182                                 // Tie control properties.
183   fgUntie("/controls/aileron");
184   fgUntie("/controls/aileron-trim");
185   fgUntie("/controls/elevator");
186   fgUntie("/controls/elevator-trim");
187   fgUntie("/controls/rudder");
188   fgUntie("/controls/rudder-trim");
189   fgUntie("/controls/flaps");
190   int index;
191   for (index = 0; index < MAX_ENGINES; index++) {
192     char name[32];
193     sprintf(name, "/controls/throttle[%d]", index);
194     fgUntie(name);
195     sprintf(name, "/controls/mixture[%d]", index);
196     fgUntie(name);
197     sprintf(name, "/controls/propeller-pitch[%d]", index);
198     fgUntie(name);
199     sprintf(name, "/controls/magnetos[%d]", index);
200     fgUntie(name);
201     sprintf(name, "/controls/starter[%d]", index);
202     fgUntie(name);
203   }
204   for (index = 0; index < MAX_WHEELS; index++) {
205     char name[32];
206     sprintf(name, "/controls/brakes[%d]", index);
207     fgUntie(name);
208   }
209   fgUntie("/controls/fuel-selector");
210   fgUntie("/controls/gear-down");
211 }
212
213
214 void
215 FGControls::update (double dt)
216 {
217 }
218
219
220 \f
221 ////////////////////////////////////////////////////////////////////////
222 // Setters and adjusters.
223 ////////////////////////////////////////////////////////////////////////
224
225 void
226 FGControls::set_aileron (double pos)
227 {
228   aileron = pos;
229   CLAMP( &aileron, -1.0, 1.0 );
230                         
231   // check for autocoordination
232   if ( auto_coordination->getBoolValue() ) {
233     set_rudder( aileron / 2.0 );
234   }
235 }
236
237 void
238 FGControls::move_aileron (double amt)
239 {
240   aileron += amt;
241   CLAMP( &aileron, -1.0, 1.0 );
242                         
243   // check for autocoordination
244   if ( auto_coordination->getBoolValue() ) {
245     set_rudder( aileron / 2.0 );
246   }
247 }
248
249 void
250 FGControls::set_aileron_trim( double pos )
251 {
252     aileron_trim = pos;
253     CLAMP( &aileron_trim, -1.0, 1.0 );
254 }
255
256 void
257 FGControls::move_aileron_trim( double amt )
258 {
259     aileron_trim += amt;
260     CLAMP( &aileron_trim, -1.0, 1.0 );
261 }
262
263 void
264 FGControls::set_elevator( double pos )
265 {
266     elevator = pos;
267     CLAMP( &elevator, -1.0, 1.0 );
268 }
269
270 void
271 FGControls::move_elevator( double amt )
272 {
273     elevator += amt;
274     CLAMP( &elevator, -1.0, 1.0 );
275 }
276
277 void
278 FGControls::set_elevator_trim( double pos )
279 {
280     elevator_trim = pos;
281     CLAMP( &elevator_trim, -1.0, 1.0 );
282 }
283
284 void
285 FGControls::move_elevator_trim( double amt )
286 {
287     elevator_trim += amt;
288     CLAMP( &elevator_trim, -1.0, 1.0 );
289 }
290
291 void
292 FGControls::set_rudder( double pos )
293 {
294     rudder = pos;
295     CLAMP( &rudder, -1.0, 1.0 );
296 }
297
298 void
299 FGControls::move_rudder( double amt )
300 {
301     rudder += amt;
302     CLAMP( &rudder, -1.0, 1.0 );
303 }
304
305 void
306 FGControls::set_rudder_trim( double pos )
307 {
308     rudder_trim = pos;
309     CLAMP( &rudder_trim, -1.0, 1.0 );
310 }
311
312 void
313 FGControls::move_rudder_trim( double amt )
314 {
315     rudder_trim += amt;
316     CLAMP( &rudder_trim, -1.0, 1.0 );
317 }
318
319 void
320 FGControls::set_flaps( double pos )
321 {
322     flaps = pos;
323     CLAMP( &flaps, 0.0, 1.0 );
324 }
325
326 void
327 FGControls::move_flaps( double amt )
328 {
329     flaps += amt;
330     CLAMP( &flaps, 0.0, 1.0 );
331 }
332
333 void
334 FGControls::set_throttle( int engine, double pos )
335 {
336   if ( engine == ALL_ENGINES ) {
337     for ( int i = 0; i < MAX_ENGINES; i++ ) {
338       throttle[i] = pos;
339       CLAMP( &throttle[i], 0.0, 1.0 );
340     }
341   } else {
342     if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
343       throttle[engine] = pos;
344       CLAMP( &throttle[engine], 0.0, 1.0 );
345     }
346   }
347 }
348
349 void
350 FGControls::move_throttle( int engine, double amt )
351 {
352     if ( engine == ALL_ENGINES ) {
353         for ( int i = 0; i < MAX_ENGINES; i++ ) {
354             throttle[i] += amt;
355             CLAMP( &throttle[i], 0.0, 1.0 );
356         }
357     } else {
358         if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
359             throttle[engine] += amt;
360             CLAMP( &throttle[engine], 0.0, 1.0 );
361         }
362     }
363 }
364
365 void
366 FGControls::set_mixture( int engine, double pos )
367 {
368     if ( engine == ALL_ENGINES ) {
369         for ( int i = 0; i < MAX_ENGINES; i++ ) {
370             mixture[i] = pos;
371             CLAMP( &mixture[i], 0.0, 1.0 );
372         }
373     } else {
374         if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
375             mixture[engine] = pos;
376             CLAMP( &mixture[engine], 0.0, 1.0 );
377         }
378     }
379 }
380
381 void
382 FGControls::move_mixture( int engine, double amt )
383 {
384     if ( engine == ALL_ENGINES ) {
385         for ( int i = 0; i < MAX_ENGINES; i++ ) {
386             mixture[i] += amt;
387             CLAMP( &mixture[i], 0.0, 1.0 );
388         }
389     } else {
390         if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
391             mixture[engine] += amt;
392             CLAMP( &mixture[engine], 0.0, 1.0 );
393         }
394     }
395 }
396
397 void
398 FGControls::set_prop_advance( int engine, double pos )
399 {
400     if ( engine == ALL_ENGINES ) {
401         for ( int i = 0; i < MAX_ENGINES; i++ ) {
402             prop_advance[i] = pos;
403             CLAMP( &prop_advance[i], 0.0, 1.0 );
404         }
405     } else {
406         if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
407             prop_advance[engine] = pos;
408             CLAMP( &prop_advance[engine], 0.0, 1.0 );
409         }
410     }
411 }
412
413 void
414 FGControls::move_prop_advance( int engine, double amt )
415 {
416     if ( engine == ALL_ENGINES ) {
417         for ( int i = 0; i < MAX_ENGINES; i++ ) {
418             prop_advance[i] += amt;
419             CLAMP( &prop_advance[i], 0.0, 1.0 );
420         }
421     } else {
422         if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
423             prop_advance[engine] += amt;
424             CLAMP( &prop_advance[engine], 0.0, 1.0 );
425         }
426     }
427 }
428
429 void
430 FGControls::set_magnetos( int engine, int pos )
431 {
432     if ( engine == ALL_ENGINES ) {
433         for ( int i = 0; i < MAX_ENGINES; i++ ) {
434             magnetos[i] = pos;
435             CLAMP( &magnetos[i], 0, 3 );
436         }
437     } else {
438         if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
439             magnetos[engine] = pos;
440             CLAMP( &magnetos[engine], 0, 3 );
441         }
442     }
443 }
444
445 void
446 FGControls::move_magnetos( int engine, int amt )
447 {
448     if ( engine == ALL_ENGINES ) {
449         for ( int i = 0; i < MAX_ENGINES; i++ ) {
450             magnetos[i] += amt;
451             CLAMP( &magnetos[i], 0, 3 );
452         }
453     } else {
454         if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
455             magnetos[engine] += amt;
456             CLAMP( &magnetos[engine], 0, 3 );
457         }
458     }
459 }
460
461 void
462 FGControls::set_starter( int engine, bool flag )
463 {
464     if ( engine == ALL_ENGINES ) {
465         for ( int i = 0; i < MAX_ENGINES; i++ ) {
466             starter[i] = flag;
467         }
468     } else {
469         if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
470             starter[engine] = flag;
471         }
472     }
473 }
474
475 void
476 FGControls::set_parking_brake( double pos )
477 {
478     parking_brake = pos;
479     CLAMP(&parking_brake, 0.0, 1.0);
480 }
481
482 void
483 FGControls::set_brake( int wheel, double pos )
484 {
485     if ( wheel == ALL_WHEELS ) {
486         for ( int i = 0; i < MAX_WHEELS; i++ ) {
487             brake[i] = pos;
488             CLAMP( &brake[i], 0.0, 1.0 );
489         }
490     } else {
491         if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
492             brake[wheel] = pos;
493             CLAMP( &brake[wheel], 0.0, 1.0 );
494         }
495     }
496 }
497
498 void
499 FGControls::move_brake( int wheel, double amt )
500 {
501     if ( wheel == ALL_WHEELS ) {
502         for ( int i = 0; i < MAX_WHEELS; i++ ) {
503             brake[i] += amt;
504             CLAMP( &brake[i], 0.0, 1.0 );
505         }
506     } else {
507         if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
508             brake[wheel] += amt;
509             CLAMP( &brake[wheel], 0.0, 1.0 );
510         }
511     }
512 }
513
514 void
515 FGControls::set_gear_down( bool gear )
516 {
517   gear_down = gear;
518 }
519
520