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