1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 You should have received a copy of the GNU General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA.
23 Further information about the GNU General Public License can also be found on
24 the world wide web at http://www.gnu.org.
27 --------------------------------------------------------------------------------
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 # include <simgear/compiler.h>
43 # ifdef SG_HAVE_STD_INCLUDES
53 #include "filtersjb/FGFCSComponent.h"
56 #include "FGConfigFile.h"
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70 typedef enum { iDe=0, iDaL, iDaR, iDr, iDsb, iDsp, iDf, NNorm } FcIdx;
71 typedef enum { ofRad=0, ofNorm, ofMag , NForms} OutputForm;
73 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
77 /** Encapsulates the Flight Control System (FCS) functionality.
78 This class owns and contains the list of FGFCSComponents
79 that define the control system for this aircraft. The config file for the
80 aircraft contains a description of the control path that starts at an input
81 or command and ends at an effector, e.g. an aerosurface. The FCS components
82 which comprise the control laws for an axis are defined sequentially in
83 the configuration file. For instance, for the X-15:
86 < FLIGHT_CONTROL NAME="X-15 SAS">
88 < COMPONENT NAME="Pitch Trim Sum" TYPE="SUMMER">
91 INPUT FG_PITCH_TRIM_CMD
95 < COMPONENT NAME="Pitch Command Scale" TYPE="AEROSURFACE_SCALE">
102 < COMPONENT NAME="Pitch Gain 1" TYPE="PURE_GAIN">
108 < COMPONENT NAME="Pitch Scheduled Gain 1" TYPE="SCHEDULED_GAIN">
112 SCHEDULED_BY FG_ELEVATOR_POS
124 In the above case we can see the first few components of the pitch channel
125 defined. The input to the first component, as can be seen in the "Pitch trim
126 sum" component, is really the sum of two parameters: elevator command (from
127 the stick - a pilot input), and pitch trim. The type of this component is
128 "Summer". Its ID is 0 - the ID is used by other components to reference it.
129 The next component created is an aerosurface scale component - a type of
130 gain (see the LoadFCS() method for insight on how the various types of
131 components map into the actual component classes). You can see the input of
132 the "Pitch Command Scale" component takes "0" as input. When a number is
133 specified as an input, it refers to the ID of another FCS component. In this
134 case, ID 0 refers to the previously defined and discussed "Pitch Trim Sum"
135 component. This continues until the final component for an axis when the
136 OUTPUT keyword specifies where the output is supposed to go. See the
137 individual components for more information on how they are mechanized.
139 @author Jon S. Berndt
151 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
155 class FGFCS : public FGModel {
159 @param Executive a pointer to the parent executive object */
164 /** Runs the Flight Controls model; called by the Executive
165 @return false if no error */
168 /// @name Pilot input command retrieval
170 /** Gets the aileron command.
171 @return aileron command in percent */
172 inline double GetDaCmd(void) const { return DaCmd; }
174 /** Gets the elevator command.
175 @return elevator command in percent */
176 inline double GetDeCmd(void) const { return DeCmd; }
178 /** Gets the rudder command.
179 @return rudder command in percent */
180 inline double GetDrCmd(void) const { return DrCmd; }
182 /** Gets the flaps command.
183 @return flaps command in percent */
184 inline double GetDfCmd(void) const { return DfCmd; }
186 /** Gets the speedbrake command.
187 @return speedbrake command in percent */
188 inline double GetDsbCmd(void) const { return DsbCmd; }
190 /** Gets the spoiler command.
191 @return spoiler command in percent */
192 inline double GetDspCmd(void) const { return DspCmd; }
194 /** Gets the throttle command.
195 @param engine engine ID number
196 @return throttle command in percent ( 0 - 100) for the given engine */
197 double GetThrottleCmd(int engine) const;
199 /** Gets the mixture command.
200 @param engine engine ID number
201 @return mixture command in percent ( 0 - 100) for the given engine */
202 inline double GetMixtureCmd(int engine) const { return MixtureCmd[engine]; }
204 /** Gets the prop pitch command.
205 @param engine engine ID number
206 @return pitch command in percent ( 0.0 - 1.0) for the given engine */
207 inline double GetPropAdvanceCmd(int engine) const { return PropAdvanceCmd[engine]; }
209 /** Gets the pitch trim command.
210 @return pitch trim command in percent */
211 inline double GetPitchTrimCmd(void) const { return PTrimCmd; }
213 /** Gets the rudder trim command.
214 @return rudder trim command in percent */
215 inline double GetYawTrimCmd(void) const { return YTrimCmd; }
217 /** Gets the aileron trim command.
218 @return aileron trim command in percent */
219 inline double GetRollTrimCmd(void) const { return RTrimCmd; }
221 /** Get the gear extend/retract command. 0 commands gear up, 1 down.
223 @return the current value of the gear extend/retract command*/
224 inline double GetGearCmd(void) const { return GearCmd; }
227 /// @name AUTOPilot -> FCS effectors command retrieval
229 /** Gets the AUTOPilot aileron command.
230 @return aileron command in radians */
231 inline double GetAPDaCmd(void) const { return AP_DaCmd; }
233 /** Gets the AUTOPilot elevator command.
234 @return elevator command in radians */
235 inline double GetAPDeCmd(void) const { return AP_DeCmd; }
237 /** Gets the AUTOPilot rudder command.
238 @return rudder command in radians */
239 inline double GetAPDrCmd(void) const { return AP_DrCmd; }
241 /** Gets the AUTOPilot throttle (all engines) command.
242 @return throttle command in percent */
243 inline double GetAPThrottleCmd(void) const { return AP_ThrottleCmd; }
246 /// @name AUTOPilot setpoint retrieval
248 /** Gets the autopilot pitch attitude setpoint
249 @return Pitch attitude setpoint in radians */
250 inline double GetAPAttitudeSetPt(void) const {return APAttitudeSetPt;}
252 /** Gets the autopilot altitude setpoint
253 @return Altitude setpoint in feet */
254 inline double GetAPAltitudeSetPt(void) const {return APAltitudeSetPt;}
256 /** Gets the autopilot heading setpoint
257 @return Heading setpoint in radians */
258 inline double GetAPHeadingSetPt(void) const {return APHeadingSetPt;}
260 /** Gets the autopilot airspeed setpoint
261 @return Airspeed setpoint in fps */
262 inline double GetAPAirspeedSetPt(void) const {return APAirspeedSetPt;}
265 /// @name AUTOPilot setpoint setting
267 /// Sets the autopilot pitch attitude setpoint
268 inline void SetAPAttitudeSetPt(double set) {APAttitudeSetPt = set;}
270 /// Sets the autopilot altitude setpoint
271 inline void SetAPAltitudeSetPt(double set) {APAltitudeSetPt = set;}
273 /// Sets the autopilot heading setpoint
274 inline void SetAPHeadingSetPt(double set) {APHeadingSetPt = set;}
276 /// Sets the autopilot airspeed setpoint
277 inline void SetAPAirspeedSetPt(double set) {APAirspeedSetPt = set;}
281 /// @name AUTOPilot mode setting
283 /** Turns on/off the attitude-seeking autopilot.
284 @param set true turns the mode on, false turns it off **/
285 inline void SetAPAcquireAttitude(bool set) {APAcquireAttitude = set;}
287 /** Turns on/off the altitude-seeking autopilot.
288 @param set true turns the mode on, false turns it off **/
289 inline void SetAPAcquireAltitude(bool set) {APAcquireAltitude = set;}
291 /** Turns on/off the heading-seeking autopilot.
292 @param set true turns the mode on, false turns it off **/
293 inline void SetAPAcquireHeading(bool set) {APAcquireHeading = set;}
295 /** Turns on/off the airspeed-seeking autopilot.
296 @param set true turns the mode on, false turns it off **/
297 inline void SetAPAcquireAirspeed(bool set) {APAcquireAirspeed = set;}
299 /** Turns on/off the attitude-holding autopilot.
300 @param set true turns the mode on, false turns it off **/
301 inline void SetAPAttitudeHold(bool set) {APAttitudeHold = set;}
303 /** Turns on/off the altitude-holding autopilot.
304 @param set true turns the mode on, false turns it off **/
305 inline void SetAPAltitudeHold(bool set) {APAltitudeHold = set;}
307 /** Turns on/off the heading-holding autopilot.
308 @param set true turns the mode on, false turns it off **/
309 inline void SetAPHeadingHold(bool set) {APHeadingHold = set;}
311 /** Turns on/off the airspeed-holding autopilot.
312 @param set true turns the mode on, false turns it off **/
313 inline void SetAPAirspeedHold(bool set) {APAirspeedHold = set;}
315 /** Turns on/off the wing-leveler autopilot.
316 @param set true turns the mode on, false turns it off **/
317 inline void SetAPWingsLevelHold(bool set) {APWingsLevelHold = set;}
320 /// @name AUTOPilot mode retrieval
322 /** Retrieves the on/off mode of the autopilot AcquireAttitude mode
323 @return true if on, false if off */
324 inline bool GetAPAcquireAttitude(void) const {return APAcquireAttitude;}
326 /** Retrieves the on/off mode of the autopilot AcquireAltitude mode
327 @return true if on, false if off */
328 inline bool GetAPAcquireAltitude(void) const {return APAcquireAltitude;}
330 /** Retrieves the on/off mode of the autopilot AcquireHeading mode
331 @return true if on, false if off */
332 inline bool GetAPAcquireHeading(void) const {return APAcquireHeading;}
334 /** Retrieves the on/off mode of the autopilot AcquireAirspeed mode
335 @return true if on, false if off */
336 inline bool GetAPAcquireAirspeed(void) const {return APAcquireAirspeed;}
338 /** Retrieves the on/off mode of the autopilot AttitudeHold mode
339 @return true if on, false if off */
340 inline bool GetAPAttitudeHold(void) const {return APAttitudeHold;}
342 /** Retrieves the on/off mode of the autopilot AltitudeHold mode
343 @return true if on, false if off */
344 inline bool GetAPAltitudeHold(void) const {return APAltitudeHold;}
346 /** Retrieves the on/off mode of the autopilot HeadingHold mode
347 @return true if on, false if off */
348 inline bool GetAPHeadingHold(void) const {return APHeadingHold;}
350 /** Retrieves the on/off mode of the autopilot AirspeedHold mode
351 @return true if on, false if off */
352 inline bool GetAPAirspeedHold(void) const {return APAirspeedHold;}
354 /** Retrieves the on/off mode of the autopilot WingsLevelHold mode
355 @return true if on, false if off */
356 inline bool GetAPWingsLevelHold(void) const {return APWingsLevelHold;}
359 /// @name Aerosurface position retrieval
361 /** Gets the left aileron position.
362 @return aileron position in radians */
363 inline double GetDaLPos( int form = ofRad )
364 const { return DaLPos[form]; }
366 /// @name Aerosurface position retrieval
368 /** Gets the right aileron position.
369 @return aileron position in radians */
370 inline double GetDaRPos( int form = ofRad )
371 const { return DaRPos[form]; }
373 /** Gets the elevator position.
374 @return elevator position in radians */
375 inline double GetDePos( int form = ofRad )
376 const { return DePos[form]; }
378 /** Gets the rudder position.
379 @return rudder position in radians */
380 inline double GetDrPos( int form = ofRad )
381 const { return DrPos[form]; }
383 /** Gets the speedbrake position.
384 @return speedbrake position in radians */
385 inline double GetDsbPos( int form = ofRad )
386 const { return DsbPos[form]; }
388 /** Gets the spoiler position.
389 @return spoiler position in radians */
390 inline double GetDspPos( int form = ofRad )
391 const { return DspPos[form]; }
393 /** Gets the flaps position.
394 @return flaps position in radians */
395 inline double GetDfPos( int form = ofRad )
396 const { return DfPos[form]; }
398 /** Gets the throttle position.
399 @param engine engine ID number
400 @return throttle position for the given engine in percent ( 0 - 100)*/
401 double GetThrottlePos(int engine) const;
403 /** Gets the mixture position.
404 @param engine engine ID number
405 @return mixture position for the given engine in percent ( 0 - 100)*/
406 inline double GetMixturePos(int engine) const { return MixturePos[engine]; }
408 /** Gets the gear position (0 up, 1 down), defaults to down
409 @return gear position (0 up, 1 down) */
410 inline double GetGearPos(void) const { return GearPos; }
412 /** Gets the prop pitch position.
413 @param engine engine ID number
414 @return prop pitch position for the given engine in percent ( 0.0-1.0)*/
415 inline double GetPropAdvance(int engine) const { return PropAdvance[engine]; }
418 /** Retrieves the State object pointer.
419 This is used by the FGFCS-owned components.
420 @return pointer to the State object */
421 inline FGState* GetState(void) { return State; }
423 /** Retrieves a components output value
424 @param idx the index of the component (the component ID)
425 @return output value from the component */
426 double GetComponentOutput(int idx);
428 /** Retrieves the component name
429 @param idx the index of the component (the component ID)
430 @return name of the component */
431 string GetComponentName(int idx);
433 /** Retrieves all component names for inclusion in output stream */
434 string GetComponentStrings(void);
436 /** Retrieves all component outputs for inclusion in output stream */
437 string GetComponentValues(void);
439 /// @name Pilot input command setting
441 /** Sets the aileron command
442 @param cmd aileron command in percent*/
443 inline void SetDaCmd( double cmd ) { DaCmd = cmd; }
445 /** Sets the elevator command
446 @param cmd elevator command in percent*/
447 inline void SetDeCmd(double cmd ) { DeCmd = cmd; }
449 /** Sets the rudder command
450 @param cmd rudder command in percent*/
451 inline void SetDrCmd(double cmd) { DrCmd = cmd; }
453 /** Sets the flaps command
454 @param cmd flaps command in percent*/
455 inline void SetDfCmd(double cmd) { DfCmd = cmd; }
457 /** Sets the speedbrake command
458 @param cmd speedbrake command in percent*/
459 inline void SetDsbCmd(double cmd) { DsbCmd = cmd; }
461 /** Sets the spoilers command
462 @param cmd spoilers command in percent*/
463 inline void SetDspCmd(double cmd) { DspCmd = cmd; }
465 /** Sets the pitch trim command
466 @param cmd pitch trim command in percent*/
467 inline void SetPitchTrimCmd(double cmd) { PTrimCmd = cmd; }
469 /** Sets the rudder trim command
470 @param cmd rudder trim command in percent*/
471 inline void SetYawTrimCmd(double cmd) { YTrimCmd = cmd; }
473 /** Sets the aileron trim command
474 @param cmd aileron trim command in percent*/
475 inline void SetRollTrimCmd(double cmd) { RTrimCmd = cmd; }
477 /** Sets the throttle command for the specified engine
478 @param engine engine ID number
479 @param cmd throttle command in percent (0 - 100)*/
480 void SetThrottleCmd(int engine, double cmd);
482 /** Sets the mixture command for the specified engine
483 @param engine engine ID number
484 @param cmd mixture command in percent (0 - 100)*/
485 void SetMixtureCmd(int engine, double cmd);
487 /** Set the gear extend/retract command, defaults to down
488 @param gear command 0 for up, 1 for down */
489 void SetGearCmd(double gearcmd) { GearCmd = gearcmd; }
491 /** Sets the propeller pitch command for the specified engine
492 @param engine engine ID number
493 @param cmd mixture command in percent (0.0 - 1.0)*/
494 void SetPropAdvanceCmd(int engine, double cmd);
497 /// @name AUTOPilot -> FCS effector command setting
499 /** Sets the AUTOPilot aileron command
500 @param cmd AUTOPilot aileron command in radians*/
501 inline void SetAPDaCmd( double cmd ) { AP_DaCmd = cmd; }
503 /** Sets the AUTOPilot elevator command
504 @param cmd AUTOPilot elevator command in radians*/
505 inline void SetAPDeCmd(double cmd ) { AP_DeCmd = cmd; }
507 /** Sets the AUTOPilot rudder command
508 @param cmd AUTOPilot rudder command in radians*/
509 inline void SetAPDrCmd(double cmd) { AP_DrCmd = cmd; }
511 /** Sets the AUTOPilot throttle command
512 @param cmd AUTOPilot throttle command in percent*/
513 inline void SetAPThrottleCmd(double cmd) { AP_ThrottleCmd = cmd; }
516 /// @name Aerosurface position setting
518 /** Sets the left aileron position
519 @param cmd left aileron position in radians*/
520 inline void SetDaLPos( int form , double pos )
521 { DaLPos[form] = pos; }
523 /** Sets the right aileron position
524 @param cmd right aileron position in radians*/
525 inline void SetDaRPos( int form , double pos )
526 { DaRPos[form] = pos; }
528 /** Sets the elevator position
529 @param cmd elevator position in radians*/
530 inline void SetDePos( int form , double pos )
531 { DePos[form] = pos; }
533 /** Sets the rudder position
534 @param cmd rudder position in radians*/
535 inline void SetDrPos( int form , double pos )
536 { DrPos[form] = pos; }
538 /** Sets the flaps position
539 @param cmd flaps position in radians*/
540 inline void SetDfPos( int form , double pos )
541 { DfPos[form] = pos; }
543 /** Sets the speedbrake position
544 @param cmd speedbrake position in radians*/
545 inline void SetDsbPos( int form , double pos )
546 { DsbPos[form] = pos; }
548 /** Sets the spoiler position
549 @param cmd spoiler position in radians*/
550 inline void SetDspPos( int form , double pos )
551 { DspPos[form] = pos; }
553 /** Sets the actual throttle setting for the specified engine
554 @param engine engine ID number
555 @param cmd throttle setting in percent (0 - 100)*/
556 void SetThrottlePos(int engine, double cmd);
558 /** Sets the actual mixture setting for the specified engine
559 @param engine engine ID number
560 @param cmd mixture setting in percent (0 - 100)*/
561 void SetMixturePos(int engine, double cmd);
563 /** Set the gear extend/retract position, defaults to down
564 @param gear position 0 up, 1 down */
565 void SetGearPos(double gearpos) { GearPos = gearpos; }
568 /** Sets the actual prop pitch setting for the specified engine
569 @param engine engine ID number
570 @param cmd prop pitch setting in percent (0.0 - 1.0)*/
571 void SetPropAdvance(int engine, double cmd);
574 /// @name Landing Gear brakes
576 /** Sets the left brake group
577 @param cmd brake setting in percent (0.0 - 1.0) */
578 void SetLBrake(double cmd) {LeftBrake = cmd;}
580 /** Sets the right brake group
581 @param cmd brake setting in percent (0.0 - 1.0) */
582 void SetRBrake(double cmd) {RightBrake = cmd;}
584 /** Sets the center brake group
585 @param cmd brake setting in percent (0.0 - 1.0) */
586 void SetCBrake(double cmd) {CenterBrake = cmd;}
588 /** Gets the brake for a specified group.
589 @param bg which brakegroup to retrieve the command for
590 @return the brake setting for the supplied brake group argument */
591 double GetBrake(FGLGear::BrakeGroup bg);
594 /** Loads the Flight Control System.
595 The FGAircraft instance is actually responsible for reading the config file
596 and calling the various Loadxx() methods of the other systems, passing in
597 the config file instance pointer. LoadFCS() is called from FGAircraft.
598 @param AC_cfg pointer to the config file instance
599 @return true if succesful */
600 bool Load(FGConfigFile* AC_cfg);
602 void AddThrottle(void);
604 FGPropertyManager* GetPropertyManager(void) { return PropertyManager; }
607 void bindModel(void);
608 void unbind(FGPropertyManager *node);
611 double DaCmd, DeCmd, DrCmd, DfCmd, DsbCmd, DspCmd;
612 double AP_DaCmd, AP_DeCmd, AP_DrCmd, AP_ThrottleCmd;
613 double DePos[NForms], DaLPos[NForms], DaRPos[NForms], DrPos[NForms];
614 double DfPos[NForms], DsbPos[NForms], DspPos[NForms];
615 double PTrimCmd, YTrimCmd, RTrimCmd;
616 vector <double> ThrottleCmd;
617 vector <double> ThrottlePos;
618 vector <double> MixtureCmd;
619 vector <double> MixturePos;
620 vector <double> PropAdvanceCmd;
621 vector <double> PropAdvance;
622 double LeftBrake, RightBrake, CenterBrake; // Brake settings
623 double GearCmd,GearPos;
625 enum Mode {mAP, mFCS, mNone} eMode;
627 double APAttitudeSetPt, APAltitudeSetPt, APHeadingSetPt, APAirspeedSetPt;
628 bool APAcquireAttitude, APAcquireAltitude, APAcquireHeading, APAcquireAirspeed;
629 bool APAttitudeHold, APAltitudeHold, APHeadingHold, APAirspeedHold, APWingsLevelHold;
632 void Normalize(void);
634 vector <FGFCSComponent*> FCSComponents;
635 vector <FGFCSComponent*> APComponents;
636 int ToNormalize[NNorm];
637 void Debug(int from);