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