1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Model the flight controls
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place - Suite 330, Boston, MA 02111-1307, USA.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class models the flight controls for a specific airplane
33 --------------------------------------------------------------------------------
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 #include "FGFDMExec.h"
43 #include "FGAtmosphere.h"
44 #include "FGAircraft.h"
45 #include "FGTranslation.h"
46 #include "FGRotation.h"
47 #include "FGPosition.h"
48 #include "FGAuxiliary.h"
51 #include "filtersjb/FGFilter.h"
52 #include "filtersjb/FGDeadBand.h"
53 #include "filtersjb/FGGain.h"
54 #include "filtersjb/FGGradient.h"
55 #include "filtersjb/FGSwitch.h"
56 #include "filtersjb/FGSummer.h"
57 #include "filtersjb/FGKinemat.h"
59 static const char *IdSrc = "$Id$";
60 static const char *IdHdr = ID_FCS;
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
66 FGFCS::FGFCS(FGFDMExec* fdmex) : FGModel(fdmex)
71 DaCmd = DeCmd = DrCmd = DfCmd = DsbCmd = DspCmd = 0.0;
72 PTrimCmd = YTrimCmd = RTrimCmd = 0.0;
73 DaLPos = DaRPos = DePos = DrPos = DfPos = DsbPos = DspPos = 0.0;
74 GearCmd = GearPos = 1; // default to gear down
75 LeftBrake = RightBrake = CenterBrake = 0.0;
78 for(i=0;i<6;i++) { ToNormalize[i]=-1;}
82 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 PropAdvanceCmd.clear();
95 for (i=0;i<Components.size();i++) delete Components[i];
99 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 bool FGFCS::Run(void)
105 if (!FGModel::Run()) {
106 for (i=0; i<ThrottlePos.size(); i++) ThrottlePos[i] = ThrottleCmd[i];
107 for (i=0; i<MixturePos.size(); i++) MixturePos[i] = MixtureCmd[i];
108 for (i=0; i<PropAdvance.size(); i++) PropAdvance[i] = PropAdvanceCmd[i];
109 for (i=0; i<Components.size(); i++) Components[i]->Run();
110 if(DoNormalize) Normalize();
117 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 void FGFCS::SetThrottleCmd(int engineNum, double setting)
123 if (engineNum < (int)ThrottlePos.size()) {
125 for (ctr=0;ctr<ThrottleCmd.size();ctr++) ThrottleCmd[ctr] = setting;
127 ThrottleCmd[engineNum] = setting;
130 cerr << "Throttle " << engineNum << " does not exist! " << ThrottleCmd.size()
131 << " engines exist, but attempted throttle command is for engine "
132 << engineNum << endl;
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 void FGFCS::SetThrottlePos(int engineNum, double setting)
142 if (engineNum < (int)ThrottlePos.size()) {
144 for (ctr=0;ctr<ThrottlePos.size();ctr++) ThrottlePos[ctr] = setting;
146 ThrottlePos[engineNum] = setting;
149 cerr << "Throttle " << engineNum << " does not exist! " << ThrottlePos.size()
150 << " engines exist, but attempted throttle position setting is for engine "
151 << engineNum << endl;
155 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 double FGFCS::GetThrottleCmd(int engineNum)
159 if (engineNum < (int)ThrottlePos.size()) {
161 cerr << "Cannot get throttle value for ALL engines" << endl;
163 return ThrottleCmd[engineNum];
166 cerr << "Throttle " << engineNum << " does not exist! " << ThrottleCmd.size()
167 << " engines exist, but throttle setting for engine " << engineNum
168 << " is selected" << endl;
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 double FGFCS::GetThrottlePos(int engineNum)
177 if (engineNum < (int)ThrottlePos.size()) {
179 cerr << "Cannot get throttle value for ALL engines" << endl;
181 return ThrottlePos[engineNum];
184 cerr << "Throttle " << engineNum << " does not exist! " << ThrottlePos.size()
185 << " engines exist, but attempted throttle position setting is for engine "
186 << engineNum << endl;
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 void FGFCS::SetMixtureCmd(int engineNum, double setting)
197 if (engineNum < (int)ThrottlePos.size()) {
199 for (ctr=0;ctr<MixtureCmd.size();ctr++) MixtureCmd[ctr] = setting;
201 MixtureCmd[engineNum] = setting;
206 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 void FGFCS::SetMixturePos(int engineNum, double setting)
212 if (engineNum < (int)ThrottlePos.size()) {
214 for (ctr=0;ctr<=MixtureCmd.size();ctr++) MixturePos[ctr] = MixtureCmd[ctr];
216 MixturePos[engineNum] = setting;
221 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 void FGFCS::SetPropAdvanceCmd(int engineNum, double setting)
227 if (engineNum < (int)ThrottlePos.size()) {
229 for (ctr=0;ctr<PropAdvanceCmd.size();ctr++) PropAdvanceCmd[ctr] = setting;
231 PropAdvanceCmd[engineNum] = setting;
236 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 void FGFCS::SetPropAdvance(int engineNum, double setting)
242 if (engineNum < (int)ThrottlePos.size()) {
244 for (ctr=0;ctr<=PropAdvanceCmd.size();ctr++) PropAdvance[ctr] = PropAdvanceCmd[ctr];
246 PropAdvance[engineNum] = setting;
251 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 bool FGFCS::Load(FGConfigFile* AC_cfg)
258 Name = Name + ":" + AC_cfg->GetValue("NAME");
259 if (debug_lvl > 0) cout << " Control System Name: " << Name << endl;
260 if( AC_cfg->GetValue("NORMALIZE") == "FALSE") {
262 cout << " Automatic Control Surface Normalization Disabled" << endl;
264 AC_cfg->GetNextConfigLine();
265 while ((token = AC_cfg->GetValue()) != string("/FLIGHT_CONTROL")) {
266 if (token == "COMPONENT") {
267 token = AC_cfg->GetValue("TYPE");
268 if (debug_lvl > 0) cout << endl << " Loading Component \""
269 << AC_cfg->GetValue("NAME")
270 << "\" of type: " << token << endl;
271 if ((token == "LAG_FILTER") ||
272 (token == "LEAD_LAG_FILTER") ||
273 (token == "SECOND_ORDER_FILTER") ||
274 (token == "WASHOUT_FILTER") ||
275 (token == "INTEGRATOR") ) {
276 Components.push_back(new FGFilter(this, AC_cfg));
277 } else if ((token == "PURE_GAIN") ||
278 (token == "SCHEDULED_GAIN") ||
279 (token == "AEROSURFACE_SCALE") ) {
281 Components.push_back(new FGGain(this, AC_cfg));
283 } else if (token == "SUMMER") {
284 Components.push_back(new FGSummer(this, AC_cfg));
285 } else if (token == "DEADBAND") {
286 Components.push_back(new FGDeadBand(this, AC_cfg));
287 } else if (token == "GRADIENT") {
288 Components.push_back(new FGGradient(this, AC_cfg));
289 } else if (token == "SWITCH") {
290 Components.push_back(new FGSwitch(this, AC_cfg));
291 } else if (token == "KINEMAT") {
292 Components.push_back(new FGKinemat(this, AC_cfg));
294 cerr << "Unknown token [" << token << "] in FCS portion of config file" << endl;
297 AC_cfg->GetNextConfigLine();
300 //collect information for normalizing control surfaces
302 for(i=0;i<Components.size();i++) {
304 if(Components[i]->GetType() == "AEROSURFACE_SCALE"
305 || Components[i]->GetType() == "KINEMAT" ) {
306 if( Components[i]->GetOutputIdx() == FG_ELEVATOR_POS ) {
308 } else if ( Components[i]->GetOutputIdx() == FG_LEFT_AILERON_POS
309 || Components[i]->GetOutputIdx() == FG_AILERON_POS ) {
310 ToNormalize[iNDaL]=i;
311 } else if ( Components[i]->GetOutputIdx() == FG_RIGHT_AILERON_POS ) {
312 ToNormalize[iNDaR]=i;
313 } else if ( Components[i]->GetOutputIdx() == FG_RUDDER_POS ) {
315 } else if ( Components[i]->GetOutputIdx() == FG_SPDBRAKE_POS ) {
316 ToNormalize[iNDsb]=i;
317 } else if ( Components[i]->GetOutputIdx() == FG_SPOILERS_POS ) {
318 ToNormalize[iNDsp]=i;
319 } else if ( Components[i]->GetOutputIdx() == FG_FLAPS_POS ) {
328 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 double FGFCS::GetComponentOutput(eParam idx) {
331 return Components[idx]->GetOutput();
334 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 string FGFCS::GetComponentName(int idx) {
337 return Components[idx]->GetName();
340 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 double FGFCS::GetBrake(FGLGear::BrakeGroup bg)
345 case FGLGear::bgLeft:
347 case FGLGear::bgRight:
349 case FGLGear::bgCenter:
352 cerr << "GetBrake asked to return a bogus brake value" << endl;
357 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 string FGFCS::GetComponentStrings(void)
362 string CompStrings = "";
363 bool firstime = true;
365 for (comp = 0; comp < Components.size(); comp++) {
366 if (firstime) firstime = false;
367 else CompStrings += ", ";
369 CompStrings += Components[comp]->GetName();
375 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377 string FGFCS::GetComponentValues(void)
380 string CompValues = "";
382 bool firstime = true;
384 for (comp = 0; comp < Components.size(); comp++) {
385 if (firstime) firstime = false;
386 else CompValues += ", ";
388 sprintf(buffer, "%9.6f", Components[comp]->GetOutput());
389 CompValues += string(buffer);
395 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 void FGFCS::AddThrottle(void)
399 ThrottleCmd.push_back(0.0);
400 ThrottlePos.push_back(0.0);
401 MixtureCmd.push_back(0.0); // assume throttle and mixture are coupled
402 MixturePos.push_back(0.0);
403 PropAdvanceCmd.push_back(0.0); // assume throttle and prop pitch are coupled
404 PropAdvance.push_back(0.0);
407 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 void FGFCS::Normalize(void) {
410 if( ToNormalize[iNDe] > -1 )
411 DePosN = Components[ToNormalize[iNDe]]->GetOutputPct();
413 if( ToNormalize[iNDaL] > -1 )
414 DaLPosN = Components[ToNormalize[iNDaL]]->GetOutputPct();
416 if( ToNormalize[iNDaR] > -1 )
417 DaRPosN = Components[ToNormalize[iNDaR]]->GetOutputPct();
419 if( ToNormalize[iNDr] > -1 )
420 DrPosN = Components[ToNormalize[iNDr]]->GetOutputPct();
422 if( ToNormalize[iNDsb] > -1 )
423 DsbPosN = Components[ToNormalize[iNDsb]]->GetOutputPct();
425 if( ToNormalize[iNDsp] > -1 )
426 DspPosN = Components[ToNormalize[iNDsp]]->GetOutputPct();
428 if( ToNormalize[iNDf] > -1 )
429 DfPosN = Components[ToNormalize[iNDf]]->GetOutputPct();
433 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434 // The bitmasked value choices are as follows:
435 // unset: In this case (the default) JSBSim would only print
436 // out the normally expected messages, essentially echoing
437 // the config files as they are read. If the environment
438 // variable is not set, debug_lvl is set to 1 internally
439 // 0: This requests JSBSim not to output any messages
441 // 1: This value explicity requests the normal JSBSim
443 // 2: This value asks for a message to be printed out when
444 // a class is instantiated
445 // 4: When this value is set, a message is displayed when a
446 // FGModel object executes its Run() method
447 // 8: When this value is set, various runtime state variables
448 // are printed out periodically
449 // 16: When set various parameters are sanity checked and
450 // a message is printed out when they go out of bounds
452 void FGFCS::Debug(int from)
454 if (debug_lvl <= 0) return;
456 if (debug_lvl & 1) { // Standard console startup message output
457 if (from == 0) { // Constructor
461 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
462 if (from == 0) cout << "Instantiated: FGFCS" << endl;
463 if (from == 1) cout << "Destroyed: FGFCS" << endl;
465 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
467 if (debug_lvl & 8 ) { // Runtime state variables
469 if (debug_lvl & 16) { // Sanity checking
471 if (debug_lvl & 64) {
472 if (from == 0) { // Constructor
473 cout << IdSrc << endl;
474 cout << IdHdr << endl;