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 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 # include <simgear/compiler.h>
43 # ifdef SG_HAVE_STD_INCLUDES
53 #include <models/flight_control/FGFCSComponent.h>
54 #include <models/FGModel.h>
55 #include <models/FGLGear.h>
56 #include <input_output/FGXMLFileRead.h>
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70 typedef enum { ofRad=0, ofDeg, ofNorm, ofMag , NForms} OutputForm;
72 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76 /** Encapsulates the Flight Control System (FCS) functionality.
77 This class also encapsulates the identical "system" and "autopilot" capability.
78 FGFCS owns and contains the list of FGFCSComponents
79 that define a system or systems for the modeled aircraft. The config file
80 for the aircraft contains a description of the control path that starts at
81 an input or command and ends at an effector, e.g. an aerosurface. The FCS
82 components which comprise the control laws for an axis are defined
83 sequentially in the configuration file. For instance, for the X-15:
86 <flight_control name="X-15 SAS">
88 <summer name="Pitch Trim Sum">
89 <input> fcs/elevator-cmd-norm </input>
90 <input> fcs/pitch-trim-cmd-norm </input>
97 <aerosurface_scale name="Pitch Command Scale">
98 <input> fcs/pitch-trim-sum </input>
108 In the above case we can see the first few components of the pitch channel
109 defined. The input to the first component (a summer), 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.
112 The next component created is an aerosurface scale component - a type of
113 gain (see the LoadFCS() method for insight on how the various types of
114 components map into the actual component classes). This continues until the
115 final component for an axis when the
116 \<output> element is usually used to specify where the output is supposed to go. See the
117 individual components for more information on how they are mechanized.
119 Another option for the flight controls portion of the config file is that in
120 addition to using the "NAME" attribute in,
123 <flight_control name="X-15 SAS">
126 one can also supply a filename:
129 <flight_control name="X-15 SAS" file="X15.xml">
133 In this case, the FCS would be read in from another file.
136 @property fcs/aileron-cmd-norm normalized aileron command
137 @property fcs/elevator-cmd-norm normalized elevator command
138 @property fcs/rudder-cmd-norm
139 @property fcs/steer-cmd-norm
140 @property fcs/flap-cmd-norm
141 @property fcs/speedbrake-cmd-norm
142 @property fcs/spoiler-cmd-norm
143 @property fcs/pitch-trim-cmd-norm
144 @property fcs/roll-trim-cmd-norm
145 @property fcs/yaw-trim-cmd-norm
146 @property gear/gear-cmd-norm
147 @property fcs/left-aileron-pos-rad
148 @property fcs/left-aileron-pos-deg
149 @property fcs/left-aileron-pos-norm
150 @property fcs/mag-left-aileron-pos-rad
151 @property fcs/right-aileron-pos-rad
152 @property fcs/right-aileron-pos-deg
153 @property fcs/right-aileron-pos-norm
154 @property fcs/mag-right-aileron-pos-rad
155 @property fcs/elevator-pos-rad
156 @property fcs/elevator-pos-deg
157 @property fcs/elevator-pos-norm
158 @property fcs/mag-elevator-pos-rad
159 @property fcs/rudder-pos-rad
160 @property fcs/rudder-pos-deg
161 @property fcs/rudder-pos-norm
162 @property fcs/mag-rudder-pos-rad
163 @property fcs/flap-pos-rad
164 @property fcs/flap-pos-deg
165 @property fcs/flap-pos-norm
166 @property fcs/speedbrake-pos-rad
167 @property fcs/speedbrake-pos-deg
168 @property fcs/speedbrake-pos-norm
169 @property fcs/mag-speedbrake-pos-rad
170 @property fcs/spoiler-pos-rad
171 @property fcs/spoiler-pos-deg
172 @property fcs/spoiler-pos-norm
173 @property fcs/mag-spoiler-pos-rad
174 @property gear/gear-pos-norm
176 @author Jon S. Berndt
192 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
196 class FGFCS : public FGModel, public FGXMLFileRead
201 @param Executive a pointer to the parent executive object */
206 bool InitModel(void);
208 /** Runs the Flight Controls model; called by the Executive
209 @return false if no error */
212 /// @name Pilot input command retrieval
214 /** Gets the aileron command.
215 @return aileron command in range from -1.0 - 1.0 */
216 inline double GetDaCmd(void) const { return DaCmd; }
218 /** Gets the elevator command.
219 @return elevator command in range from -1.0 - 1.0 */
220 inline double GetDeCmd(void) const { return DeCmd; }
222 /** Gets the rudder command.
223 @return rudder command in range from -1.0 - 1.0 */
224 inline double GetDrCmd(void) const { return DrCmd; }
226 /** Gets the steering command.
227 @return steering command in range from -1.0 - 1.0 */
228 inline double GetDsCmd(void) const { return DsCmd; }
230 /** Gets the flaps command.
231 @return flaps command in range from 0 to 1.0 */
232 inline double GetDfCmd(void) const { return DfCmd; }
234 /** Gets the speedbrake command.
235 @return speedbrake command in range from 0 to 1.0 */
236 inline double GetDsbCmd(void) const { return DsbCmd; }
238 /** Gets the spoiler command.
239 @return spoiler command in range from 0 to 1.0 */
240 inline double GetDspCmd(void) const { return DspCmd; }
242 /** Gets the throttle command.
243 @param engine engine ID number
244 @return throttle command in range from 0 - 1.0 for the given engine */
245 double GetThrottleCmd(int engine) const;
247 /** Gets the mixture command.
248 @param engine engine ID number
249 @return mixture command in range from 0 - 1.0 for the given engine */
250 inline double GetMixtureCmd(int engine) const { return MixtureCmd[engine]; }
252 /** Gets the prop pitch command.
253 @param engine engine ID number
254 @return pitch command in range from 0.0 - 1.0 for the given engine */
255 inline double GetPropAdvanceCmd(int engine) const { return PropAdvanceCmd[engine]; }
257 /** Gets the prop feather command.
258 @param engine engine ID number
259 @return feather command for the given engine (on / off)*/
260 inline bool GetFeatherCmd(int engine) const { return PropFeatherCmd[engine]; }
262 /** Gets the pitch trim command.
263 @return pitch trim command in range from -1.0 to 1.0 */
264 inline double GetPitchTrimCmd(void) const { return PTrimCmd; }
266 /** Gets the rudder trim command.
267 @return rudder trim command in range from -1.0 - 1.0 */
268 inline double GetYawTrimCmd(void) const { return YTrimCmd; }
270 /** Gets the aileron trim command.
271 @return aileron trim command in range from -1.0 - 1.0 */
272 inline double GetRollTrimCmd(void) const { return RTrimCmd; }
274 /** Get the gear extend/retract command. 0 commands gear up, 1 down.
276 @return the current value of the gear extend/retract command*/
277 inline double GetGearCmd(void) const { return GearCmd; }
280 /// @name Aerosurface position retrieval
282 /** Gets the left aileron position.
283 @return aileron position in radians */
284 inline double GetDaLPos( int form = ofRad )
285 const { return DaLPos[form]; }
287 /// @name Aerosurface position retrieval
289 /** Gets the right aileron position.
290 @return aileron position in radians */
291 inline double GetDaRPos( int form = ofRad )
292 const { return DaRPos[form]; }
294 /** Gets the elevator position.
295 @return elevator position in radians */
296 inline double GetDePos( int form = ofRad )
297 const { return DePos[form]; }
299 /** Gets the rudder position.
300 @return rudder position in radians */
301 inline double GetDrPos( int form = ofRad )
302 const { return DrPos[form]; }
304 /** Gets the speedbrake position.
305 @return speedbrake position in radians */
306 inline double GetDsbPos( int form = ofRad )
307 const { return DsbPos[form]; }
309 /** Gets the spoiler position.
310 @return spoiler position in radians */
311 inline double GetDspPos( int form = ofRad )
312 const { return DspPos[form]; }
314 /** Gets the flaps position.
315 @return flaps position in radians */
316 inline double GetDfPos( int form = ofRad )
317 const { return DfPos[form]; }
319 /** Gets the throttle position.
320 @param engine engine ID number
321 @return throttle position for the given engine in range from 0 - 1.0 */
322 double GetThrottlePos(int engine) const;
324 /** Gets the mixture position.
325 @param engine engine ID number
326 @return mixture position for the given engine in range from 0 - 1.0 */
327 inline double GetMixturePos(int engine) const { return MixturePos[engine]; }
329 /** Gets the steering position.
330 @return steering position in degrees */
331 double GetSteerPosDeg(int gear) const { return SteerPosDeg[gear]; }
333 /** Gets the gear position (0 up, 1 down), defaults to down
334 @return gear position (0 up, 1 down) */
335 inline double GetGearPos(void) const { return GearPos; }
337 /** Gets the prop pitch position.
338 @param engine engine ID number
339 @return prop pitch position for the given engine in range from 0 - 1.0 */
340 inline double GetPropAdvance(int engine) const { return PropAdvance[engine]; }
342 /** Gets the prop feather position.
343 @param engine engine ID number
344 @return prop fether for the given engine (on / off)*/
345 inline bool GetPropFeather(int engine) const { return PropFeather[engine]; }
348 /** Retrieves the State object pointer.
349 This is used by the FGFCS-owned components.
350 @return pointer to the State object */
351 inline FGState* GetState(void) { return State; }
353 /** Retrieves all component names for inclusion in output stream
354 @param delimeter either a tab or comma string depending on output type
355 @return a string containing the descriptive names for all components */
356 string GetComponentStrings(string delimeter);
358 /** Retrieves all component outputs for inclusion in output stream
359 @param delimeter either a tab or comma string depending on output type
360 @return a string containing the numeric values for the current set of
362 string GetComponentValues(string delimeter);
364 /// @name Pilot input command setting
366 /** Sets the aileron command
367 @param cmd aileron command */
368 inline void SetDaCmd( double cmd ) { DaCmd = cmd; }
370 /** Sets the elevator command
371 @param cmd elevator command in percent*/
372 inline void SetDeCmd(double cmd ) { DeCmd = cmd; }
374 /** Sets the rudder command
375 @param cmd rudder command in percent*/
376 inline void SetDrCmd(double cmd) { DrCmd = cmd; }
378 /** Sets the steering command
379 @param cmd steering command in percent*/
380 inline void SetDsCmd(double cmd) { DsCmd = cmd; }
382 /** Sets the flaps command
383 @param cmd flaps command in percent*/
384 inline void SetDfCmd(double cmd) { DfCmd = cmd; }
386 /** Sets the speedbrake command
387 @param cmd speedbrake command in percent*/
388 inline void SetDsbCmd(double cmd) { DsbCmd = cmd; }
390 /** Sets the spoilers command
391 @param cmd spoilers command in percent*/
392 inline void SetDspCmd(double cmd) { DspCmd = cmd; }
394 /** Sets the pitch trim command
395 @param cmd pitch trim command in percent*/
396 inline void SetPitchTrimCmd(double cmd) { PTrimCmd = cmd; }
398 /** Sets the rudder trim command
399 @param cmd rudder trim command in percent*/
400 inline void SetYawTrimCmd(double cmd) { YTrimCmd = cmd; }
402 /** Sets the aileron trim command
403 @param cmd aileron trim command in percent*/
404 inline void SetRollTrimCmd(double cmd) { RTrimCmd = cmd; }
406 /** Sets the throttle command for the specified engine
407 @param engine engine ID number
408 @param cmd throttle command in percent (0 - 100)*/
409 void SetThrottleCmd(int engine, double cmd);
411 /** Sets the mixture command for the specified engine
412 @param engine engine ID number
413 @param cmd mixture command in percent (0 - 100)*/
414 void SetMixtureCmd(int engine, double cmd);
416 /** Set the gear extend/retract command, defaults to down
417 @param gear command 0 for up, 1 for down */
418 void SetGearCmd(double gearcmd) { GearCmd = gearcmd; }
420 /** Sets the propeller pitch command for the specified engine
421 @param engine engine ID number
422 @param cmd mixture command in percent (0.0 - 1.0)*/
423 void SetPropAdvanceCmd(int engine, double cmd);
425 /** Sets the propeller feather command for the specified engine
426 @param engine engine ID number
427 @param cmd feather (bool)*/
428 void SetFeatherCmd(int engine, bool cmd);
431 /// @name Aerosurface position setting
433 /** Sets the left aileron position
434 @param cmd left aileron position in radians*/
435 inline void SetDaLPos( int form , double pos );
437 /** Sets the right aileron position
438 @param cmd right aileron position in radians*/
439 inline void SetDaRPos( int form , double pos );
441 /** Sets the elevator position
442 @param cmd elevator position in radians*/
443 inline void SetDePos( int form , double pos );
445 /** Sets the rudder position
446 @param cmd rudder position in radians*/
447 inline void SetDrPos( int form , double pos );
449 /** Sets the flaps position
450 @param cmd flaps position in radians*/
451 inline void SetDfPos( int form , double pos );
453 /** Sets the speedbrake position
454 @param cmd speedbrake position in radians*/
455 inline void SetDsbPos( int form , double pos );
457 /** Sets the spoiler position
458 @param cmd spoiler position in radians*/
459 inline void SetDspPos( int form , double pos );
461 /** Sets the actual throttle setting for the specified engine
462 @param engine engine ID number
463 @param cmd throttle setting in percent (0 - 100)*/
464 void SetThrottlePos(int engine, double cmd);
466 /** Sets the actual mixture setting for the specified engine
467 @param engine engine ID number
468 @param cmd mixture setting in percent (0 - 100)*/
469 void SetMixturePos(int engine, double cmd);
471 /** Sets the steering position
472 @param cmd steering position in degrees*/
473 void SetSteerPosDeg(int gear, double pos) { SteerPosDeg[gear] = pos; }
475 /** Set the gear extend/retract position, defaults to down
476 @param gear position 0 up, 1 down */
477 void SetGearPos(double gearpos) { GearPos = gearpos; }
480 /** Sets the actual prop pitch setting for the specified engine
481 @param engine engine ID number
482 @param cmd prop pitch setting in percent (0.0 - 1.0)*/
483 void SetPropAdvance(int engine, double cmd);
485 /** Sets the actual prop feather setting for the specified engine
486 @param engine engine ID number
487 @param cmd prop fether setting (bool)*/
488 void SetPropFeather(int engine, bool cmd);
491 /// @name Landing Gear brakes
493 /** Sets the left brake group
494 @param cmd brake setting in percent (0.0 - 1.0) */
495 void SetLBrake(double cmd) {LeftBrake = cmd;}
497 /** Sets the right brake group
498 @param cmd brake setting in percent (0.0 - 1.0) */
499 void SetRBrake(double cmd) {RightBrake = cmd;}
501 /** Sets the center brake group
502 @param cmd brake setting in percent (0.0 - 1.0) */
503 void SetCBrake(double cmd) {CenterBrake = cmd;}
505 /** Gets the brake for a specified group.
506 @param bg which brakegroup to retrieve the command for
507 @return the brake setting for the supplied brake group argument */
508 double GetBrake(FGLGear::BrakeGroup bg);
510 /** Gets the left brake.
511 @return the left brake setting. */
512 double GetLBrake(void) const {return LeftBrake;}
514 /** Gets the right brake.
515 @return the right brake setting. */
516 double GetRBrake(void) const {return RightBrake;}
518 /** Gets the center brake.
519 @return the center brake setting. */
520 double GetCBrake(void) const {return CenterBrake;}
523 enum SystemType { stFCS, stSystem, stAutoPilot };
525 /** Loads the Flight Control System.
526 Load() is called from FGFDMExec.
527 @param el pointer to the Element instance
528 @param systype type of system (FCS, Autopilot, System)
529 @return true if succesful */
530 bool Load(Element* el, SystemType systype);
532 ifstream* FindSystemFile(string system_filename);
533 string FindSystemFullPathname(string system_filename);
535 void AddThrottle(void);
539 FGPropertyManager* GetPropertyManager(void) { return PropertyManager; }
542 double DaCmd, DeCmd, DrCmd, DsCmd, DfCmd, DsbCmd, DspCmd;
543 double DePos[NForms], DaLPos[NForms], DaRPos[NForms], DrPos[NForms];
544 double DfPos[NForms], DsbPos[NForms], DspPos[NForms];
545 double PTrimCmd, YTrimCmd, RTrimCmd;
546 vector <double> ThrottleCmd;
547 vector <double> ThrottlePos;
548 vector <double> MixtureCmd;
549 vector <double> MixturePos;
550 vector <double> PropAdvanceCmd;
551 vector <double> PropAdvance;
552 vector <bool> PropFeatherCmd;
553 vector <bool> PropFeather;
554 vector <double> SteerPosDeg;
555 double LeftBrake, RightBrake, CenterBrake; // Brake settings
556 double GearCmd,GearPos;
558 typedef vector <FGFCSComponent*> FCSCompVec;
560 FCSCompVec FCSComponents;
561 FCSCompVec APComponents;
563 vector <double*> interface_properties;
565 void bindModel(void);
566 void bindThrottle(unsigned int);
567 void Debug(int from);