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">
89 INPUT fcs/elevator-cmd-norm
90 INPUT fcs/pitch-trim-cmd-norm
94 \<COMPONENT NAME="Pitch Command Scale" TYPE="AEROSURFACE_SCALE">
95 INPUT fcs/pitch-trim-sum
100 \<COMPONENT NAME="Pitch Gain 1" TYPE="PURE_GAIN">
101 INPUT fcs/pitch-command-scale
108 In the above case we can see the first few components of the pitch channel
109 defined. The input to the first component, as can be seen in the "Pitch trim
110 sum" component, is really the sum of two parameters: elevator command (from
111 the stick - a pilot input), and pitch trim. The type of this component is
113 The next component created is an aerosurface scale component - a type of
114 gain (see the LoadFCS() method for insight on how the various types of
115 components map into the actual component classes). This continues until the
116 final component for an axis when the
117 OUTPUT keyword specifies where the output is supposed to go. See the
118 individual components for more information on how they are mechanized.
120 Another option for the flight controls portion of the config file is that in
121 addition to using the "NAME" attribute in,
124 \<FLIGHT_CONTROL NAME="X-15 SAS">
127 one can also supply a filename:
130 \<FLIGHT_CONTROL NAME="X-15 SAS" FILE="X15.xml">
134 In this case, the FCS would be read in from another file.
136 @author Jon S. Berndt
148 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
152 class FGFCS : public FGModel {
156 @param Executive a pointer to the parent executive object */
161 /** Runs the Flight Controls model; called by the Executive
162 @return false if no error */
165 /// @name Pilot input command retrieval
167 /** Gets the aileron command.
168 @return aileron command in percent */
169 inline double GetDaCmd(void) const { return DaCmd; }
171 /** Gets the elevator command.
172 @return elevator command in percent */
173 inline double GetDeCmd(void) const { return DeCmd; }
175 /** Gets the rudder command.
176 @return rudder command in percent */
177 inline double GetDrCmd(void) const { return DrCmd; }
179 /** Gets the flaps command.
180 @return flaps command in percent */
181 inline double GetDfCmd(void) const { return DfCmd; }
183 /** Gets the speedbrake command.
184 @return speedbrake command in percent */
185 inline double GetDsbCmd(void) const { return DsbCmd; }
187 /** Gets the spoiler command.
188 @return spoiler command in percent */
189 inline double GetDspCmd(void) const { return DspCmd; }
191 /** Gets the throttle command.
192 @param engine engine ID number
193 @return throttle command in percent ( 0 - 100) for the given engine */
194 double GetThrottleCmd(int engine) const;
196 /** Gets the mixture command.
197 @param engine engine ID number
198 @return mixture command in percent ( 0 - 100) for the given engine */
199 inline double GetMixtureCmd(int engine) const { return MixtureCmd[engine]; }
201 /** Gets the prop pitch command.
202 @param engine engine ID number
203 @return pitch command in percent ( 0.0 - 1.0) for the given engine */
204 inline double GetPropAdvanceCmd(int engine) const { return PropAdvanceCmd[engine]; }
206 /** Gets the pitch trim command.
207 @return pitch trim command in percent */
208 inline double GetPitchTrimCmd(void) const { return PTrimCmd; }
210 /** Gets the rudder trim command.
211 @return rudder trim command in percent */
212 inline double GetYawTrimCmd(void) const { return YTrimCmd; }
214 /** Gets the aileron trim command.
215 @return aileron trim command in percent */
216 inline double GetRollTrimCmd(void) const { return RTrimCmd; }
218 /** Get the gear extend/retract command. 0 commands gear up, 1 down.
220 @return the current value of the gear extend/retract command*/
221 inline double GetGearCmd(void) const { return GearCmd; }
224 /// @name AUTOPilot -> FCS effectors command retrieval
226 /** Gets the AUTOPilot aileron command.
227 @return aileron command in radians */
228 inline double GetAPDaCmd(void) const { return AP_DaCmd; }
230 /** Gets the AUTOPilot elevator command.
231 @return elevator command in radians */
232 inline double GetAPDeCmd(void) const { return AP_DeCmd; }
234 /** Gets the AUTOPilot rudder command.
235 @return rudder command in radians */
236 inline double GetAPDrCmd(void) const { return AP_DrCmd; }
238 /** Gets the AUTOPilot throttle (all engines) command.
239 @return throttle command in percent */
240 inline double GetAPThrottleCmd(void) const { return AP_ThrottleCmd; }
243 /// @name AUTOPilot setpoint retrieval
245 /** Gets the autopilot pitch attitude setpoint
246 @return Pitch attitude setpoint in radians */
247 inline double GetAPAttitudeSetPt(void) const {return APAttitudeSetPt;}
249 /** Gets the autopilot altitude setpoint
250 @return Altitude setpoint in feet */
251 inline double GetAPAltitudeSetPt(void) const {return APAltitudeSetPt;}
253 /** Gets the autopilot heading setpoint
254 @return Heading setpoint in radians */
255 inline double GetAPHeadingSetPt(void) const {return APHeadingSetPt;}
257 /** Gets the autopilot airspeed setpoint
258 @return Airspeed setpoint in fps */
259 inline double GetAPAirspeedSetPt(void) const {return APAirspeedSetPt;}
262 /// @name AUTOPilot setpoint setting
264 /// Sets the autopilot pitch attitude setpoint
265 inline void SetAPAttitudeSetPt(double set) {APAttitudeSetPt = set;}
267 /// Sets the autopilot altitude setpoint
268 inline void SetAPAltitudeSetPt(double set) {APAltitudeSetPt = set;}
270 /// Sets the autopilot heading setpoint
271 inline void SetAPHeadingSetPt(double set) {APHeadingSetPt = set;}
273 /// Sets the autopilot airspeed setpoint
274 inline void SetAPAirspeedSetPt(double set) {APAirspeedSetPt = set;}
278 /// @name AUTOPilot mode setting
280 /** Turns on/off the attitude-seeking autopilot.
281 @param set true turns the mode on, false turns it off **/
282 inline void SetAPAcquireAttitude(bool set) {APAcquireAttitude = set;}
284 /** Turns on/off the altitude-seeking autopilot.
285 @param set true turns the mode on, false turns it off **/
286 inline void SetAPAcquireAltitude(bool set) {APAcquireAltitude = set;}
288 /** Turns on/off the heading-seeking autopilot.
289 @param set true turns the mode on, false turns it off **/
290 inline void SetAPAcquireHeading(bool set) {APAcquireHeading = set;}
292 /** Turns on/off the airspeed-seeking autopilot.
293 @param set true turns the mode on, false turns it off **/
294 inline void SetAPAcquireAirspeed(bool set) {APAcquireAirspeed = set;}
296 /** Turns on/off the attitude-holding autopilot.
297 @param set true turns the mode on, false turns it off **/
298 inline void SetAPAttitudeHold(bool set) {APAttitudeHold = set;}
300 /** Turns on/off the altitude-holding autopilot.
301 @param set true turns the mode on, false turns it off **/
302 inline void SetAPAltitudeHold(bool set) {APAltitudeHold = set;}
304 /** Turns on/off the heading-holding autopilot.
305 @param set true turns the mode on, false turns it off **/
306 inline void SetAPHeadingHold(bool set) {APHeadingHold = set;}
308 /** Turns on/off the airspeed-holding autopilot.
309 @param set true turns the mode on, false turns it off **/
310 inline void SetAPAirspeedHold(bool set) {APAirspeedHold = set;}
312 /** Turns on/off the wing-leveler autopilot.
313 @param set true turns the mode on, false turns it off **/
314 inline void SetAPWingsLevelHold(bool set) {APWingsLevelHold = set;}
317 /// @name AUTOPilot mode retrieval
319 /** Retrieves the on/off mode of the autopilot AcquireAttitude mode
320 @return true if on, false if off */
321 inline bool GetAPAcquireAttitude(void) const {return APAcquireAttitude;}
323 /** Retrieves the on/off mode of the autopilot AcquireAltitude mode
324 @return true if on, false if off */
325 inline bool GetAPAcquireAltitude(void) const {return APAcquireAltitude;}
327 /** Retrieves the on/off mode of the autopilot AcquireHeading mode
328 @return true if on, false if off */
329 inline bool GetAPAcquireHeading(void) const {return APAcquireHeading;}
331 /** Retrieves the on/off mode of the autopilot AcquireAirspeed mode
332 @return true if on, false if off */
333 inline bool GetAPAcquireAirspeed(void) const {return APAcquireAirspeed;}
335 /** Retrieves the on/off mode of the autopilot AttitudeHold mode
336 @return true if on, false if off */
337 inline bool GetAPAttitudeHold(void) const {return APAttitudeHold;}
339 /** Retrieves the on/off mode of the autopilot AltitudeHold mode
340 @return true if on, false if off */
341 inline bool GetAPAltitudeHold(void) const {return APAltitudeHold;}
343 /** Retrieves the on/off mode of the autopilot HeadingHold mode
344 @return true if on, false if off */
345 inline bool GetAPHeadingHold(void) const {return APHeadingHold;}
347 /** Retrieves the on/off mode of the autopilot AirspeedHold mode
348 @return true if on, false if off */
349 inline bool GetAPAirspeedHold(void) const {return APAirspeedHold;}
351 /** Retrieves the on/off mode of the autopilot WingsLevelHold mode
352 @return true if on, false if off */
353 inline bool GetAPWingsLevelHold(void) const {return APWingsLevelHold;}
356 /// @name Aerosurface position retrieval
358 /** Gets the left aileron position.
359 @return aileron position in radians */
360 inline double GetDaLPos( int form = ofRad )
361 const { return DaLPos[form]; }
363 /// @name Aerosurface position retrieval
365 /** Gets the right aileron position.
366 @return aileron position in radians */
367 inline double GetDaRPos( int form = ofRad )
368 const { return DaRPos[form]; }
370 /** Gets the elevator position.
371 @return elevator position in radians */
372 inline double GetDePos( int form = ofRad )
373 const { return DePos[form]; }
375 /** Gets the rudder position.
376 @return rudder position in radians */
377 inline double GetDrPos( int form = ofRad )
378 const { return DrPos[form]; }
380 /** Gets the speedbrake position.
381 @return speedbrake position in radians */
382 inline double GetDsbPos( int form = ofRad )
383 const { return DsbPos[form]; }
385 /** Gets the spoiler position.
386 @return spoiler position in radians */
387 inline double GetDspPos( int form = ofRad )
388 const { return DspPos[form]; }
390 /** Gets the flaps position.
391 @return flaps position in radians */
392 inline double GetDfPos( int form = ofRad )
393 const { return DfPos[form]; }
395 /** Gets the throttle position.
396 @param engine engine ID number
397 @return throttle position for the given engine in percent ( 0 - 100)*/
398 double GetThrottlePos(int engine) const;
400 /** Gets the mixture position.
401 @param engine engine ID number
402 @return mixture position for the given engine in percent ( 0 - 100)*/
403 inline double GetMixturePos(int engine) const { return MixturePos[engine]; }
405 /** Gets the gear position (0 up, 1 down), defaults to down
406 @return gear position (0 up, 1 down) */
407 inline double GetGearPos(void) const { return GearPos; }
409 /** Gets the prop pitch position.
410 @param engine engine ID number
411 @return prop pitch position for the given engine in percent ( 0.0-1.0)*/
412 inline double GetPropAdvance(int engine) const { return PropAdvance[engine]; }
415 /** Retrieves the State object pointer.
416 This is used by the FGFCS-owned components.
417 @return pointer to the State object */
418 inline FGState* GetState(void) { return State; }
420 /** Retrieves all component names for inclusion in output stream */
421 string GetComponentStrings(void);
423 /** Retrieves all component outputs for inclusion in output stream */
424 string GetComponentValues(void);
426 /// @name Pilot input command setting
428 /** Sets the aileron command
429 @param cmd aileron command in percent*/
430 inline void SetDaCmd( double cmd ) { DaCmd = cmd; }
432 /** Sets the elevator command
433 @param cmd elevator command in percent*/
434 inline void SetDeCmd(double cmd ) { DeCmd = cmd; }
436 /** Sets the rudder command
437 @param cmd rudder command in percent*/
438 inline void SetDrCmd(double cmd) { DrCmd = cmd; }
440 /** Sets the flaps command
441 @param cmd flaps command in percent*/
442 inline void SetDfCmd(double cmd) { DfCmd = cmd; }
444 /** Sets the speedbrake command
445 @param cmd speedbrake command in percent*/
446 inline void SetDsbCmd(double cmd) { DsbCmd = cmd; }
448 /** Sets the spoilers command
449 @param cmd spoilers command in percent*/
450 inline void SetDspCmd(double cmd) { DspCmd = cmd; }
452 /** Sets the pitch trim command
453 @param cmd pitch trim command in percent*/
454 inline void SetPitchTrimCmd(double cmd) { PTrimCmd = cmd; }
456 /** Sets the rudder trim command
457 @param cmd rudder trim command in percent*/
458 inline void SetYawTrimCmd(double cmd) { YTrimCmd = cmd; }
460 /** Sets the aileron trim command
461 @param cmd aileron trim command in percent*/
462 inline void SetRollTrimCmd(double cmd) { RTrimCmd = cmd; }
464 /** Sets the throttle command for the specified engine
465 @param engine engine ID number
466 @param cmd throttle command in percent (0 - 100)*/
467 void SetThrottleCmd(int engine, double cmd);
469 /** Sets the mixture command for the specified engine
470 @param engine engine ID number
471 @param cmd mixture command in percent (0 - 100)*/
472 void SetMixtureCmd(int engine, double cmd);
474 /** Set the gear extend/retract command, defaults to down
475 @param gear command 0 for up, 1 for down */
476 void SetGearCmd(double gearcmd) { GearCmd = gearcmd; }
478 /** Sets the propeller pitch command for the specified engine
479 @param engine engine ID number
480 @param cmd mixture command in percent (0.0 - 1.0)*/
481 void SetPropAdvanceCmd(int engine, double cmd);
484 /// @name AUTOPilot -> FCS effector command setting
486 /** Sets the AUTOPilot aileron command
487 @param cmd AUTOPilot aileron command in radians*/
488 inline void SetAPDaCmd( double cmd ) { AP_DaCmd = cmd; }
490 /** Sets the AUTOPilot elevator command
491 @param cmd AUTOPilot elevator command in radians*/
492 inline void SetAPDeCmd(double cmd ) { AP_DeCmd = cmd; }
494 /** Sets the AUTOPilot rudder command
495 @param cmd AUTOPilot rudder command in radians*/
496 inline void SetAPDrCmd(double cmd) { AP_DrCmd = cmd; }
498 /** Sets the AUTOPilot throttle command
499 @param cmd AUTOPilot throttle command in percent*/
500 inline void SetAPThrottleCmd(double cmd) { AP_ThrottleCmd = cmd; }
503 /// @name Aerosurface position setting
505 /** Sets the left aileron position
506 @param cmd left aileron position in radians*/
507 inline void SetDaLPos( int form , double pos )
508 { DaLPos[form] = pos; }
510 /** Sets the right aileron position
511 @param cmd right aileron position in radians*/
512 inline void SetDaRPos( int form , double pos )
513 { DaRPos[form] = pos; }
515 /** Sets the elevator position
516 @param cmd elevator position in radians*/
517 inline void SetDePos( int form , double pos )
518 { DePos[form] = pos; }
520 /** Sets the rudder position
521 @param cmd rudder position in radians*/
522 inline void SetDrPos( int form , double pos )
523 { DrPos[form] = pos; }
525 /** Sets the flaps position
526 @param cmd flaps position in radians*/
527 inline void SetDfPos( int form , double pos )
528 { DfPos[form] = pos; }
530 /** Sets the speedbrake position
531 @param cmd speedbrake position in radians*/
532 inline void SetDsbPos( int form , double pos )
533 { DsbPos[form] = pos; }
535 /** Sets the spoiler position
536 @param cmd spoiler position in radians*/
537 inline void SetDspPos( int form , double pos )
538 { DspPos[form] = pos; }
540 /** Sets the actual throttle setting for the specified engine
541 @param engine engine ID number
542 @param cmd throttle setting in percent (0 - 100)*/
543 void SetThrottlePos(int engine, double cmd);
545 /** Sets the actual mixture setting for the specified engine
546 @param engine engine ID number
547 @param cmd mixture setting in percent (0 - 100)*/
548 void SetMixturePos(int engine, double cmd);
550 /** Set the gear extend/retract position, defaults to down
551 @param gear position 0 up, 1 down */
552 void SetGearPos(double gearpos) { GearPos = gearpos; }
555 /** Sets the actual prop pitch setting for the specified engine
556 @param engine engine ID number
557 @param cmd prop pitch setting in percent (0.0 - 1.0)*/
558 void SetPropAdvance(int engine, double cmd);
561 /// @name Landing Gear brakes
563 /** Sets the left brake group
564 @param cmd brake setting in percent (0.0 - 1.0) */
565 void SetLBrake(double cmd) {LeftBrake = cmd;}
567 /** Sets the right brake group
568 @param cmd brake setting in percent (0.0 - 1.0) */
569 void SetRBrake(double cmd) {RightBrake = cmd;}
571 /** Sets the center brake group
572 @param cmd brake setting in percent (0.0 - 1.0) */
573 void SetCBrake(double cmd) {CenterBrake = cmd;}
575 /** Gets the brake for a specified group.
576 @param bg which brakegroup to retrieve the command for
577 @return the brake setting for the supplied brake group argument */
578 double GetBrake(FGLGear::BrakeGroup bg);
581 /** Loads the Flight Control System.
582 The FGAircraft instance is actually responsible for reading the config file
583 and calling the various Loadxx() methods of the other systems, passing in
584 the config file instance pointer. LoadFCS() is called from FGAircraft.
585 @param AC_cfg pointer to the config file instance
586 @return true if succesful */
587 bool Load(FGConfigFile* AC_cfg);
589 void AddThrottle(void);
591 FGPropertyManager* GetPropertyManager(void) { return PropertyManager; }
594 void bindModel(void);
595 void unbind(FGPropertyManager *node);
598 double DaCmd, DeCmd, DrCmd, DfCmd, DsbCmd, DspCmd;
599 double AP_DaCmd, AP_DeCmd, AP_DrCmd, AP_ThrottleCmd;
600 double DePos[NForms], DaLPos[NForms], DaRPos[NForms], DrPos[NForms];
601 double DfPos[NForms], DsbPos[NForms], DspPos[NForms];
602 double PTrimCmd, YTrimCmd, RTrimCmd;
603 vector <double> ThrottleCmd;
604 vector <double> ThrottlePos;
605 vector <double> MixtureCmd;
606 vector <double> MixturePos;
607 vector <double> PropAdvanceCmd;
608 vector <double> PropAdvance;
609 double LeftBrake, RightBrake, CenterBrake; // Brake settings
610 double GearCmd,GearPos;
612 double APAttitudeSetPt, APAltitudeSetPt, APHeadingSetPt, APAirspeedSetPt;
613 bool APAcquireAttitude, APAcquireAltitude, APAcquireHeading, APAcquireAirspeed;
614 bool APAttitudeHold, APAltitudeHold, APHeadingHold, APAirspeedHold, APWingsLevelHold;
617 void Normalize(void);
619 vector <FGFCSComponent*> FCSComponents;
620 vector <FGFCSComponent*> APComponents;
621 int ToNormalize[NNorm];
622 void Debug(int from);