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