1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser 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 Lesser General Public License for more
19 You should have received a copy of the GNU Lesser 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 Lesser General Public License can also be found on
24 the world wide web at http://www.gnu.org.
27 --------------------------------------------------------------------------------
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 #include "models/flight_control/FGFCSComponent.h"
46 #include "models/FGModel.h"
47 #include "models/FGLGear.h"
48 #include "input_output/FGXMLFileRead.h"
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62 typedef enum { ofRad=0, ofDeg, ofNorm, ofMag , NForms} OutputForm;
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 /** Encapsulates the Flight Control System (FCS) functionality.
69 This class also encapsulates the identical "system" and "autopilot" capability.
70 FGFCS owns and contains the list of FGFCSComponents
71 that define a system or systems for the modeled aircraft. The config file
72 for the aircraft contains a description of the control path that starts at
73 an input or command and ends at an effector, e.g. an aerosurface. The FCS
74 components which comprise the control laws for an axis are defined
75 sequentially in the configuration file. For instance, for the X-15:
78 <flight_control name="X-15 SAS">
80 <summer name="Pitch Trim Sum">
81 <input> fcs/elevator-cmd-norm </input>
82 <input> fcs/pitch-trim-cmd-norm </input>
89 <aerosurface_scale name="Pitch Command Scale">
90 <input> fcs/pitch-trim-sum </input>
100 In the above case we can see the first few components of the pitch channel
101 defined. The input to the first component (a summer), as can be seen in the "Pitch trim
102 sum" component, is really the sum of two parameters: elevator command (from
103 the stick - a pilot input), and pitch trim.
104 The next component created is an aerosurface scale component - a type of
105 gain (see the LoadFCS() method for insight on how the various types of
106 components map into the actual component classes). This continues until the
107 final component for an axis when the
108 \<output> element is usually used to specify where the output is supposed to go. See the
109 individual components for more information on how they are mechanized.
111 Another option for the flight controls portion of the config file is that in
112 addition to using the "NAME" attribute in,
115 <flight_control name="X-15 SAS">
118 one can also supply a filename:
121 <flight_control name="X-15 SAS" file="X15.xml">
125 In this case, the FCS would be read in from another file.
128 @property fcs/aileron-cmd-norm normalized aileron command
129 @property fcs/elevator-cmd-norm normalized elevator command
130 @property fcs/rudder-cmd-norm
131 @property fcs/steer-cmd-norm
132 @property fcs/flap-cmd-norm
133 @property fcs/speedbrake-cmd-norm
134 @property fcs/spoiler-cmd-norm
135 @property fcs/pitch-trim-cmd-norm
136 @property fcs/roll-trim-cmd-norm
137 @property fcs/yaw-trim-cmd-norm
138 @property gear/gear-cmd-norm
139 @property fcs/left-aileron-pos-rad
140 @property fcs/left-aileron-pos-deg
141 @property fcs/left-aileron-pos-norm
142 @property fcs/mag-left-aileron-pos-rad
143 @property fcs/right-aileron-pos-rad
144 @property fcs/right-aileron-pos-deg
145 @property fcs/right-aileron-pos-norm
146 @property fcs/mag-right-aileron-pos-rad
147 @property fcs/elevator-pos-rad
148 @property fcs/elevator-pos-deg
149 @property fcs/elevator-pos-norm
150 @property fcs/mag-elevator-pos-rad
151 @property fcs/rudder-pos-rad
152 @property fcs/rudder-pos-deg
153 @property fcs/rudder-pos-norm
154 @property fcs/mag-rudder-pos-rad
155 @property fcs/flap-pos-rad
156 @property fcs/flap-pos-deg
157 @property fcs/flap-pos-norm
158 @property fcs/speedbrake-pos-rad
159 @property fcs/speedbrake-pos-deg
160 @property fcs/speedbrake-pos-norm
161 @property fcs/mag-speedbrake-pos-rad
162 @property fcs/spoiler-pos-rad
163 @property fcs/spoiler-pos-deg
164 @property fcs/spoiler-pos-norm
165 @property fcs/mag-spoiler-pos-rad
166 @property fcs/wing-fold-pos-norm
167 @property gear/gear-pos-norm
168 @property gear/tailhook-pos-norm
170 @author Jon S. Berndt
186 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
190 class FGFCS : public FGModel, public FGXMLFileRead
195 @param Executive a pointer to the parent executive object */
200 bool InitModel(void);
202 /** Runs the Flight Controls model; called by the Executive
203 @return false if no error */
206 /// @name Pilot input command retrieval
208 /** Gets the aileron command.
209 @return aileron command in range from -1.0 - 1.0 */
210 double GetDaCmd(void) const { return DaCmd; }
212 /** Gets the elevator command.
213 @return elevator command in range from -1.0 - 1.0 */
214 double GetDeCmd(void) const { return DeCmd; }
216 /** Gets the rudder command.
217 @return rudder command in range from -1.0 - 1.0 */
218 double GetDrCmd(void) const { return DrCmd; }
220 /** Gets the steering command.
221 @return steering command in range from -1.0 - 1.0 */
222 double GetDsCmd(void) const { return DsCmd; }
224 /** Gets the flaps command.
225 @return flaps command in range from 0 to 1.0 */
226 double GetDfCmd(void) const { return DfCmd; }
228 /** Gets the speedbrake command.
229 @return speedbrake command in range from 0 to 1.0 */
230 double GetDsbCmd(void) const { return DsbCmd; }
232 /** Gets the spoiler command.
233 @return spoiler command in range from 0 to 1.0 */
234 double GetDspCmd(void) const { return DspCmd; }
236 /** Gets the throttle command.
237 @param engine engine ID number
238 @return throttle command in range from 0 - 1.0 for the given engine */
239 double GetThrottleCmd(int engine) const;
241 /** Gets the mixture command.
242 @param engine engine ID number
243 @return mixture command in range from 0 - 1.0 for the given engine */
244 double GetMixtureCmd(int engine) const { return MixtureCmd[engine]; }
246 /** Gets the prop pitch command.
247 @param engine engine ID number
248 @return pitch command in range from 0.0 - 1.0 for the given engine */
249 double GetPropAdvanceCmd(int engine) const { return PropAdvanceCmd[engine]; }
251 /** Gets the prop feather command.
252 @param engine engine ID number
253 @return feather command for the given engine (on / off)*/
254 bool GetFeatherCmd(int engine) const { return PropFeatherCmd[engine]; }
256 /** Gets the pitch trim command.
257 @return pitch trim command in range from -1.0 to 1.0 */
258 double GetPitchTrimCmd(void) const { return PTrimCmd; }
260 /** Gets the rudder trim command.
261 @return rudder trim command in range from -1.0 - 1.0 */
262 double GetYawTrimCmd(void) const { return YTrimCmd; }
264 /** Gets the aileron trim command.
265 @return aileron trim command in range from -1.0 - 1.0 */
266 double GetRollTrimCmd(void) const { return RTrimCmd; }
268 /** Get the gear extend/retract command. 0 commands gear up, 1 down.
270 @return the current value of the gear extend/retract command*/
271 double GetGearCmd(void) const { return GearCmd; }
274 /// @name Aerosurface position retrieval
276 /** Gets the left aileron position.
277 @return aileron position in radians */
278 double GetDaLPos( int form = ofRad )
279 const { return DaLPos[form]; }
281 /// @name Aerosurface position retrieval
283 /** Gets the right aileron position.
284 @return aileron position in radians */
285 double GetDaRPos( int form = ofRad )
286 const { return DaRPos[form]; }
288 /** Gets the elevator position.
289 @return elevator position in radians */
290 double GetDePos( int form = ofRad )
291 const { return DePos[form]; }
293 /** Gets the rudder position.
294 @return rudder position in radians */
295 double GetDrPos( int form = ofRad )
296 const { return DrPos[form]; }
298 /** Gets the speedbrake position.
299 @return speedbrake position in radians */
300 double GetDsbPos( int form = ofRad )
301 const { return DsbPos[form]; }
303 /** Gets the spoiler position.
304 @return spoiler position in radians */
305 double GetDspPos( int form = ofRad )
306 const { return DspPos[form]; }
308 /** Gets the flaps position.
309 @return flaps position in radians */
310 double GetDfPos( int form = ofRad )
311 const { return DfPos[form]; }
313 /** Gets the throttle position.
314 @param engine engine ID number
315 @return throttle position for the given engine in range from 0 - 1.0 */
316 double GetThrottlePos(int engine) const;
318 /** Gets the mixture position.
319 @param engine engine ID number
320 @return mixture position for the given engine in range from 0 - 1.0 */
321 double GetMixturePos(int engine) const { return MixturePos[engine]; }
323 /** Gets the steering position.
324 @return steering position in degrees */
325 double GetSteerPosDeg(int gear) const { return SteerPosDeg[gear]; }
327 /** Gets the gear position (0 up, 1 down), defaults to down
328 @return gear position (0 up, 1 down) */
329 double GetGearPos(void) const { return GearPos; }
331 /** Gets the tailhook position (0 up, 1 down)
332 @return tailhook position (0 up, 1 down) */
333 double GetTailhookPos(void) const { return TailhookPos; }
335 /** Gets the wing fold position (0 unfolded, 1 folded)
336 @return wing fold position (0 unfolded, 1 folded) */
337 double GetWingFoldPos(void) const { return WingFoldPos; }
339 /** Gets the prop pitch position.
340 @param engine engine ID number
341 @return prop pitch position for the given engine in range from 0 - 1.0 */
342 double GetPropAdvance(int engine) const { return PropAdvance[engine]; }
344 /** Gets the prop feather position.
345 @param engine engine ID number
346 @return prop fether for the given engine (on / off)*/
347 bool GetPropFeather(int engine) const { return PropFeather[engine]; }
350 /** Retrieves the State object pointer.
351 This is used by the FGFCS-owned components.
352 @return pointer to the State object */
353 FGState* GetState(void) { return State; }
355 /** Retrieves all component names for inclusion in output stream
356 @param delimiter either a tab or comma string depending on output type
357 @return a string containing the descriptive names for all components */
358 std::string GetComponentStrings(const std::string& delimiter);
360 /** Retrieves all component outputs for inclusion in output stream
361 @param delimiter either a tab or comma string depending on output type
362 @return a string containing the numeric values for the current set of
364 std::string GetComponentValues(const std::string& delimiter);
366 /// @name Pilot input command setting
368 /** Sets the aileron command
369 @param cmd aileron command */
370 void SetDaCmd( double cmd ) { DaCmd = cmd; }
372 /** Sets the elevator command
373 @param cmd elevator command in percent*/
374 void SetDeCmd(double cmd ) { DeCmd = cmd; }
376 /** Sets the rudder command
377 @param cmd rudder command in percent*/
378 void SetDrCmd(double cmd) { DrCmd = cmd; }
380 /** Sets the steering command
381 @param cmd steering command in percent*/
382 void SetDsCmd(double cmd) { DsCmd = cmd; }
384 /** Sets the flaps command
385 @param cmd flaps command in percent*/
386 void SetDfCmd(double cmd) { DfCmd = cmd; }
388 /** Sets the speedbrake command
389 @param cmd speedbrake command in percent*/
390 void SetDsbCmd(double cmd) { DsbCmd = cmd; }
392 /** Sets the spoilers command
393 @param cmd spoilers command in percent*/
394 void SetDspCmd(double cmd) { DspCmd = cmd; }
396 /** Sets the pitch trim command
397 @param cmd pitch trim command in percent*/
398 void SetPitchTrimCmd(double cmd) { PTrimCmd = cmd; }
400 /** Sets the rudder trim command
401 @param cmd rudder trim command in percent*/
402 void SetYawTrimCmd(double cmd) { YTrimCmd = cmd; }
404 /** Sets the aileron trim command
405 @param cmd aileron trim command in percent*/
406 void SetRollTrimCmd(double cmd) { RTrimCmd = cmd; }
408 /** Sets the throttle command for the specified engine
409 @param engine engine ID number
410 @param cmd throttle command in percent (0 - 100)*/
411 void SetThrottleCmd(int engine, double cmd);
413 /** Sets the mixture command for the specified engine
414 @param engine engine ID number
415 @param cmd mixture command in percent (0 - 100)*/
416 void SetMixtureCmd(int engine, double cmd);
418 /** Set the gear extend/retract command, defaults to down
419 @param gear command 0 for up, 1 for down */
420 void SetGearCmd(double gearcmd) { GearCmd = gearcmd; }
422 /** Sets the propeller pitch command for the specified engine
423 @param engine engine ID number
424 @param cmd mixture command in percent (0.0 - 1.0)*/
425 void SetPropAdvanceCmd(int engine, double cmd);
427 /** Sets the propeller feather command for the specified engine
428 @param engine engine ID number
429 @param cmd feather (bool)*/
430 void SetFeatherCmd(int engine, bool cmd);
433 /// @name Aerosurface position setting
435 /** Sets the left aileron position
436 @param cmd left aileron position in radians*/
437 void SetDaLPos( int form , double pos );
439 /** Sets the right aileron position
440 @param cmd right aileron position in radians*/
441 void SetDaRPos( int form , double pos );
443 /** Sets the elevator position
444 @param cmd elevator position in radians*/
445 void SetDePos( int form , double pos );
447 /** Sets the rudder position
448 @param cmd rudder position in radians*/
449 void SetDrPos( int form , double pos );
451 /** Sets the flaps position
452 @param cmd flaps position in radians*/
453 void SetDfPos( int form , double pos );
455 /** Sets the speedbrake position
456 @param cmd speedbrake position in radians*/
457 void SetDsbPos( int form , double pos );
459 /** Sets the spoiler position
460 @param cmd spoiler position in radians*/
461 void SetDspPos( int form , double pos );
463 /** Sets the actual throttle setting for the specified engine
464 @param engine engine ID number
465 @param cmd throttle setting in percent (0 - 100)*/
466 void SetThrottlePos(int engine, double cmd);
468 /** Sets the actual mixture setting for the specified engine
469 @param engine engine ID number
470 @param cmd mixture setting in percent (0 - 100)*/
471 void SetMixturePos(int engine, double cmd);
473 /** Sets the steering position
474 @param cmd steering position in degrees*/
475 void SetSteerPosDeg(int gear, double pos) { SteerPosDeg[gear] = pos; }
477 /** Set the gear extend/retract position, defaults to down
478 @param gear position 0 up, 1 down */
479 void SetGearPos(double gearpos) { GearPos = gearpos; }
481 /** Set the tailhook position
482 @param tailhook position 0 up, 1 down */
483 void SetTailhookPos(double hookpos) { TailhookPos = hookpos; }
485 /** Set the wing fold position
486 @param wing fold position 0 unfolded, 1 folded */
487 void SetWingFoldPos(double foldpos) { WingFoldPos = foldpos; }
489 /** Sets the actual prop pitch setting for the specified engine
490 @param engine engine ID number
491 @param cmd prop pitch setting in percent (0.0 - 1.0)*/
492 void SetPropAdvance(int engine, double cmd);
494 /** Sets the actual prop feather setting for the specified engine
495 @param engine engine ID number
496 @param cmd prop fether setting (bool)*/
497 void SetPropFeather(int engine, bool cmd);
500 /// @name Landing Gear brakes
502 /** Sets the left brake group
503 @param cmd brake setting in percent (0.0 - 1.0) */
504 void SetLBrake(double cmd) {LeftBrake = cmd;}
506 /** Sets the right brake group
507 @param cmd brake setting in percent (0.0 - 1.0) */
508 void SetRBrake(double cmd) {RightBrake = cmd;}
510 /** Sets the center brake group
511 @param cmd brake setting in percent (0.0 - 1.0) */
512 void SetCBrake(double cmd) {CenterBrake = cmd;}
514 /** Gets the brake for a specified group.
515 @param bg which brakegroup to retrieve the command for
516 @return the brake setting for the supplied brake group argument */
517 double GetBrake(FGLGear::BrakeGroup bg);
519 /** Gets the left brake.
520 @return the left brake setting. */
521 double GetLBrake(void) const {return LeftBrake;}
523 /** Gets the right brake.
524 @return the right brake setting. */
525 double GetRBrake(void) const {return RightBrake;}
527 /** Gets the center brake.
528 @return the center brake setting. */
529 double GetCBrake(void) const {return CenterBrake;}
532 enum SystemType { stFCS, stSystem, stAutoPilot };
534 /** Loads the Flight Control System.
535 Load() is called from FGFDMExec.
536 @param el pointer to the Element instance
537 @param systype type of system (FCS, Autopilot, System)
538 @return true if succesful */
539 bool Load(Element* el, SystemType systype);
541 std::ifstream* FindSystemFile(const std::string& system_filename);
542 std::string FindSystemFullPathname(const std::string& system_filename);
544 void AddThrottle(void);
548 FGPropertyManager* GetPropertyManager(void) { return PropertyManager; }
551 double DaCmd, DeCmd, DrCmd, DsCmd, DfCmd, DsbCmd, DspCmd;
552 double DePos[NForms], DaLPos[NForms], DaRPos[NForms], DrPos[NForms];
553 double DfPos[NForms], DsbPos[NForms], DspPos[NForms];
554 double PTrimCmd, YTrimCmd, RTrimCmd;
555 std::vector <double> ThrottleCmd;
556 std::vector <double> ThrottlePos;
557 std::vector <double> MixtureCmd;
558 std::vector <double> MixturePos;
559 std::vector <double> PropAdvanceCmd;
560 std::vector <double> PropAdvance;
561 std::vector <bool> PropFeatherCmd;
562 std::vector <bool> PropFeather;
563 std::vector <double> SteerPosDeg;
564 double LeftBrake, RightBrake, CenterBrake; // Brake settings
565 double GearCmd,GearPos;
566 double TailhookPos, WingFoldPos;
568 typedef std::vector <FGFCSComponent*> FCSCompVec;
570 FCSCompVec FCSComponents;
571 FCSCompVec APComponents;
573 void bindModel(void);
574 void bindThrottle(unsigned int);
575 void Debug(int from);