]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGFCS.cpp
New version of JSBSim, a big rewrite.
[flightgear.git] / src / FDM / JSBSim / models / FGFCS.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGFCS.cpp 
4  Author:       Jon Berndt
5  Date started: 12/12/98
6  Purpose:      Model the flight controls
7  Called by:    FDMExec
8
9  ------------- Copyright (C) 1999  Jon S. Berndt (jon@jsbsim.org) -------------
10
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU Lesser General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15
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 Lesser General Public License for more
19  details.
20
21  You should have received a copy of the GNU Lesser 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.
24
25  Further information about the GNU Lesser General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class models the flight controls for a specific airplane
31
32 HISTORY
33 --------------------------------------------------------------------------------
34 12/12/98   JSB   Created
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include <fstream>
41 #include <sstream>
42 #include <iomanip>
43
44 #include "FGFCS.h"
45 #include "FGFDMExec.h"
46 #include "FGGroundReactions.h"
47 #include "input_output/FGPropertyManager.h"
48
49 #include "models/flight_control/FGFilter.h"
50 #include "models/flight_control/FGDeadBand.h"
51 #include "models/flight_control/FGGain.h"
52 #include "models/flight_control/FGPID.h"
53 #include "models/flight_control/FGSwitch.h"
54 #include "models/flight_control/FGSummer.h"
55 #include "models/flight_control/FGKinemat.h"
56 #include "models/flight_control/FGFCSFunction.h"
57 #include "models/flight_control/FGSensor.h"
58 #include "models/flight_control/FGActuator.h"
59 #include "models/flight_control/FGAccelerometer.h"
60 #include "models/flight_control/FGMagnetometer.h"
61 #include "models/flight_control/FGGyro.h"
62
63 using namespace std;
64
65 namespace JSBSim {
66
67 static const char *IdSrc = "$Id: FGFCS.cpp,v 1.76 2011/08/14 20:15:56 jberndt Exp $";
68 static const char *IdHdr = ID_FCS;
69
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 CLASS IMPLEMENTATION
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73
74 FGFCS::FGFCS(FGFDMExec* fdmex) : FGModel(fdmex)
75 {
76   int i;
77   Name = "FGFCS";
78
79   DaCmd = DeCmd = DrCmd = DsCmd = DfCmd = DsbCmd = DspCmd = 0;
80   PTrimCmd = YTrimCmd = RTrimCmd = 0.0;
81   GearCmd = GearPos = 1; // default to gear down
82   BrakePos.resize(FGLGear::bgNumBrakeGroups);
83   TailhookPos = WingFoldPos = 0.0; 
84
85   bind();
86   for (i=0;i<NForms;i++) {
87     DePos[i] = DaLPos[i] = DaRPos[i] = DrPos[i] = 0.0;
88     DfPos[i] = DsbPos[i] = DspPos[i] = 0.0;
89   }
90
91   Debug(0);
92 }
93
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
96 FGFCS::~FGFCS()
97 {
98   ThrottleCmd.clear();
99   ThrottlePos.clear();
100   MixtureCmd.clear();
101   MixturePos.clear();
102   PropAdvanceCmd.clear();
103   PropAdvance.clear();
104   SteerPosDeg.clear();
105   PropFeatherCmd.clear();
106   PropFeather.clear();
107
108   unsigned int i;
109
110   for (i=0;i<APComponents.size();i++) delete APComponents[i];
111   APComponents.clear();
112   for (i=0;i<FCSComponents.size();i++) delete FCSComponents[i];
113   FCSComponents.clear();
114   for (i=0;i<Systems.size();i++) delete Systems[i];
115   Systems.clear();
116
117
118   Debug(1);
119 }
120
121 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122
123 bool FGFCS::InitModel(void)
124 {
125   unsigned int i;
126
127   for (i=0; i<ThrottlePos.size(); i++) ThrottlePos[i] = 0.0;
128   for (i=0; i<MixturePos.size(); i++) MixturePos[i] = 0.0;
129   for (i=0; i<ThrottleCmd.size(); i++) ThrottleCmd[i] = 0.0;
130   for (i=0; i<MixtureCmd.size(); i++) MixtureCmd[i] = 0.0;
131   for (i=0; i<PropAdvance.size(); i++) PropAdvance[i] = 0.0;
132   for (i=0; i<PropFeather.size(); i++) PropFeather[i] = 0.0;
133
134   DaCmd = DeCmd = DrCmd = DsCmd = DfCmd = DsbCmd = DspCmd = 0;
135   PTrimCmd = YTrimCmd = RTrimCmd = 0.0;
136   TailhookPos = WingFoldPos = 0.0;
137
138   for (i=0;i<NForms;i++) {
139     DePos[i] = DaLPos[i] = DaRPos[i] = DrPos[i] = 0.0;
140     DfPos[i] = DsbPos[i] = DspPos[i] = 0.0;
141   }
142
143   for (unsigned int i=0; i<Systems.size(); i++) {
144     if (Systems[i]->GetType() == "LAG" ||
145         Systems[i]->GetType() == "LEAD_LAG" ||
146         Systems[i]->GetType() == "WASHOUT" ||
147         Systems[i]->GetType() == "SECOND_ORDER_FILTER" ||
148         Systems[i]->GetType() == "INTEGRATOR")
149     {
150       ((FGFilter*)Systems[i])->ResetPastStates();
151     } else if (Systems[i]->GetType() == "PID" ) {
152       ((FGPID*)Systems[i])->ResetPastStates();
153     }
154   }
155
156   for (unsigned int i=0; i<FCSComponents.size(); i++) {
157     if (FCSComponents[i]->GetType() == "LAG" ||
158         FCSComponents[i]->GetType() == "LEAD_LAG" ||
159         FCSComponents[i]->GetType() == "WASHOUT" ||
160         FCSComponents[i]->GetType() == "SECOND_ORDER_FILTER" ||
161         FCSComponents[i]->GetType() == "INTEGRATOR")
162     {
163       ((FGFilter*)FCSComponents[i])->ResetPastStates();
164     } else if (FCSComponents[i]->GetType() == "PID" ) {
165       ((FGPID*)FCSComponents[i])->ResetPastStates();
166     }
167   }
168
169   for (unsigned int i=0; i<APComponents.size(); i++) {
170     if (APComponents[i]->GetType() == "LAG" ||
171         APComponents[i]->GetType() == "LEAD_LAG" ||
172         APComponents[i]->GetType() == "WASHOUT" ||
173         APComponents[i]->GetType() == "SECOND_ORDER_FILTER" ||
174         APComponents[i]->GetType() == "INTEGRATOR")
175     {
176       ((FGFilter*)APComponents[i])->ResetPastStates();
177     } else if (APComponents[i]->GetType() == "PID" ) {
178       ((FGPID*)APComponents[i])->ResetPastStates();
179     }
180   }
181
182   return true;
183 }
184
185 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 // Notes: In this logic the default engine commands are set. This is simply a
187 // sort of safe-mode method in case the user has not defined control laws for
188 // throttle, mixture, and prop-advance. The throttle, mixture, and prop advance
189 // positions are set equal to the respective commands. Any control logic that is
190 // actually present in the flight_control or autopilot section will override
191 // these simple assignments.
192
193 bool FGFCS::Run(bool Holding)
194 {
195   unsigned int i;
196
197   if (FGModel::Run(Holding)) return true; // fast exit if nothing to do
198   if (Holding) return false;
199
200   RunPreFunctions();
201
202   for (i=0; i<ThrottlePos.size(); i++) ThrottlePos[i] = ThrottleCmd[i];
203   for (i=0; i<MixturePos.size(); i++) MixturePos[i] = MixtureCmd[i];
204   for (i=0; i<PropAdvance.size(); i++) PropAdvance[i] = PropAdvanceCmd[i];
205   for (i=0; i<PropFeather.size(); i++) PropFeather[i] = PropFeatherCmd[i];
206
207   // Set the default steering angle
208   for (i=0; i<SteerPosDeg.size(); i++) {
209     FGLGear* gear = FDMExec->GetGroundReactions()->GetGearUnit(i);
210     SteerPosDeg[i] = gear->GetDefaultSteerAngle( GetDsCmd() );
211   }
212
213   // Execute Systems in order
214   for (i=0; i<Systems.size(); i++) Systems[i]->Run();
215
216   // Execute Autopilot
217   for (i=0; i<APComponents.size(); i++) APComponents[i]->Run();
218
219   // Execute Flight Control System
220   for (i=0; i<FCSComponents.size(); i++) FCSComponents[i]->Run();
221
222   RunPostFunctions();
223
224   return false;
225 }
226
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228
229 void FGFCS::SetDaLPos( int form , double pos )
230 {
231   switch(form) {
232   case ofRad:
233     DaLPos[ofRad] = pos;
234     DaLPos[ofDeg] = pos*radtodeg;
235     break;
236   case ofDeg:
237     DaLPos[ofRad] = pos*degtorad;
238     DaLPos[ofDeg] = pos;
239     break;
240   case ofNorm:
241     DaLPos[ofNorm] = pos;
242   }
243   DaLPos[ofMag] = fabs(DaLPos[ofRad]);
244 }
245
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247
248 void FGFCS::SetDaRPos( int form , double pos )
249 {
250   switch(form) {
251   case ofRad:
252     DaRPos[ofRad] = pos;
253     DaRPos[ofDeg] = pos*radtodeg;
254     break;
255   case ofDeg:
256     DaRPos[ofRad] = pos*degtorad;
257     DaRPos[ofDeg] = pos;
258     break;
259   case ofNorm:
260     DaRPos[ofNorm] = pos;
261   }
262   DaRPos[ofMag] = fabs(DaRPos[ofRad]);
263 }
264
265 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266
267 void FGFCS::SetDePos( int form , double pos )
268 {
269   switch(form) {
270   case ofRad:
271     DePos[ofRad] = pos;
272     DePos[ofDeg] = pos*radtodeg;
273     break;
274   case ofDeg:
275     DePos[ofRad] = pos*degtorad;
276     DePos[ofDeg] = pos;
277     break;
278   case ofNorm:
279     DePos[ofNorm] = pos;
280   }
281   DePos[ofMag] = fabs(DePos[ofRad]);
282 }
283
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285
286 void FGFCS::SetDrPos( int form , double pos )
287 {
288   switch(form) {
289   case ofRad:
290     DrPos[ofRad] = pos;
291     DrPos[ofDeg] = pos*radtodeg;
292     break;
293   case ofDeg:
294     DrPos[ofRad] = pos*degtorad;
295     DrPos[ofDeg] = pos;
296     break;
297   case ofNorm:
298     DrPos[ofNorm] = pos;
299   }
300   DrPos[ofMag] = fabs(DrPos[ofRad]);
301 }
302
303 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304
305 void FGFCS::SetDfPos( int form , double pos )
306 {
307   switch(form) {
308   case ofRad:
309     DfPos[ofRad] = pos;
310     DfPos[ofDeg] = pos*radtodeg;
311     break;
312   case ofDeg:
313     DfPos[ofRad] = pos*degtorad;
314     DfPos[ofDeg] = pos;
315     break;
316   case ofNorm:
317     DfPos[ofNorm] = pos;
318   }
319   DfPos[ofMag] = fabs(DfPos[ofRad]);
320 }
321
322 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323
324 void FGFCS::SetDsbPos( int form , double pos )
325 {
326   switch(form) {
327   case ofRad:
328     DsbPos[ofRad] = pos;
329     DsbPos[ofDeg] = pos*radtodeg;
330     break;
331   case ofDeg:
332     DsbPos[ofRad] = pos*degtorad;
333     DsbPos[ofDeg] = pos;
334     break;
335   case ofNorm:
336     DsbPos[ofNorm] = pos;
337   }
338   DsbPos[ofMag] = fabs(DsbPos[ofRad]);
339 }
340
341 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342
343 void FGFCS::SetDspPos( int form , double pos )
344 {
345   switch(form) {
346   case ofRad:
347     DspPos[ofRad] = pos;
348     DspPos[ofDeg] = pos*radtodeg;
349     break;
350   case ofDeg:
351     DspPos[ofRad] = pos*degtorad;
352     DspPos[ofDeg] = pos;
353     break;
354   case ofNorm:
355     DspPos[ofNorm] = pos;
356   }
357   DspPos[ofMag] = fabs(DspPos[ofRad]);
358 }
359
360 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361
362 void FGFCS::SetThrottleCmd(int engineNum, double setting)
363 {
364   unsigned int ctr;
365
366   if (engineNum < (int)ThrottlePos.size()) {
367     if (engineNum < 0) {
368       for (ctr=0;ctr<ThrottleCmd.size();ctr++) ThrottleCmd[ctr] = setting;
369     } else {
370       ThrottleCmd[engineNum] = setting;
371     }
372   } else {
373     cerr << "Throttle " << engineNum << " does not exist! " << ThrottleCmd.size()
374          << " engines exist, but attempted throttle command is for engine "
375          << engineNum << endl;
376   }
377 }
378
379 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380
381 void FGFCS::SetThrottlePos(int engineNum, double setting)
382 {
383   unsigned int ctr;
384
385   if (engineNum < (int)ThrottlePos.size()) {
386     if (engineNum < 0) {
387       for (ctr=0;ctr<ThrottlePos.size();ctr++) ThrottlePos[ctr] = setting;
388     } else {
389       ThrottlePos[engineNum] = setting;
390     }
391   } else {
392     cerr << "Throttle " << engineNum << " does not exist! " << ThrottlePos.size()
393          << " engines exist, but attempted throttle position setting is for engine "
394          << engineNum << endl;
395   }
396 }
397
398 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399
400 double FGFCS::GetThrottleCmd(int engineNum) const
401 {
402   if (engineNum < (int)ThrottlePos.size()) {
403     if (engineNum < 0) {
404        cerr << "Cannot get throttle value for ALL engines" << endl;
405     } else {
406       return ThrottleCmd[engineNum];
407     }
408   } else {
409     cerr << "Throttle " << engineNum << " does not exist! " << ThrottleCmd.size()
410          << " engines exist, but throttle setting for engine " << engineNum
411          << " is selected" << endl;
412   }
413   return 0.0;
414 }
415
416 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417
418 double FGFCS::GetThrottlePos(int engineNum) const
419 {
420   if (engineNum < (int)ThrottlePos.size()) {
421     if (engineNum < 0) {
422        cerr << "Cannot get throttle value for ALL engines" << endl;
423     } else {
424       return ThrottlePos[engineNum];
425     }
426   } else {
427     cerr << "Throttle " << engineNum << " does not exist! " << ThrottlePos.size()
428          << " engines exist, but attempted throttle position setting is for engine "
429          << engineNum << endl;
430   }
431   return 0.0;
432 }
433
434 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435
436 void FGFCS::SetMixtureCmd(int engineNum, double setting)
437 {
438   unsigned int ctr;
439
440   if (engineNum < (int)ThrottlePos.size()) {
441     if (engineNum < 0) {
442       for (ctr=0;ctr<MixtureCmd.size();ctr++) MixtureCmd[ctr] = setting;
443     } else {
444       MixtureCmd[engineNum] = setting;
445     }
446   }
447 }
448
449 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450
451 void FGFCS::SetMixturePos(int engineNum, double setting)
452 {
453   unsigned int ctr;
454
455   if (engineNum < (int)ThrottlePos.size()) {
456     if (engineNum < 0) {
457       for (ctr=0;ctr<MixtureCmd.size();ctr++) MixturePos[ctr] = MixtureCmd[ctr];
458     } else {
459       MixturePos[engineNum] = setting;
460     }
461   }
462 }
463
464 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465
466 void FGFCS::SetPropAdvanceCmd(int engineNum, double setting)
467 {
468   unsigned int ctr;
469
470   if (engineNum < (int)ThrottlePos.size()) {
471     if (engineNum < 0) {
472       for (ctr=0;ctr<PropAdvanceCmd.size();ctr++) PropAdvanceCmd[ctr] = setting;
473     } else {
474       PropAdvanceCmd[engineNum] = setting;
475     }
476   }
477 }
478
479 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
480
481 void FGFCS::SetPropAdvance(int engineNum, double setting)
482 {
483   unsigned int ctr;
484
485   if (engineNum < (int)ThrottlePos.size()) {
486     if (engineNum < 0) {
487       for (ctr=0;ctr<PropAdvanceCmd.size();ctr++) PropAdvance[ctr] = PropAdvanceCmd[ctr];
488     } else {
489       PropAdvance[engineNum] = setting;
490     }
491   }
492 }
493
494 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
495
496 void FGFCS::SetFeatherCmd(int engineNum, bool setting)
497 {
498   unsigned int ctr;
499
500   if (engineNum < (int)ThrottlePos.size()) {
501     if (engineNum < 0) {
502       for (ctr=0;ctr<PropFeatherCmd.size();ctr++) PropFeatherCmd[ctr] = setting;
503     } else {
504       PropFeatherCmd[engineNum] = setting;
505     }
506   }
507 }
508
509 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
510
511 void FGFCS::SetPropFeather(int engineNum, bool setting)
512 {
513   unsigned int ctr;
514
515   if (engineNum < (int)ThrottlePos.size()) {
516     if (engineNum < 0) {
517       for (ctr=0;ctr<PropFeatherCmd.size();ctr++) PropFeather[ctr] = PropFeatherCmd[ctr];
518     } else {
519       PropFeather[engineNum] = setting;
520     }
521   }
522 }
523
524 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525
526 bool FGFCS::Load(Element* el, SystemType systype)
527 {
528   string name, file, fname="", interface_property_string, parent_name;
529   vector <FGFCSComponent*> *Components;
530   Element *component_element;
531   Element *channel_element;
532
533   Components=0;
534
535 // ToDo: The handling of name and file attributes could be improved, here,
536 //       considering that a name can be in the external file, as well.
537
538   name = el->GetAttributeValue("name");
539
540   if (name.empty() || !el->GetAttributeValue("file").empty()) {
541     fname = el->GetAttributeValue("file");
542     if (systype == stSystem) {
543       file = FindSystemFullPathname(fname);
544     } else { 
545       file = FDMExec->GetFullAircraftPath() + "/" + fname + ".xml";
546     }
547     if (fname.empty()) {
548       cerr << "FCS, Autopilot, or system does not appear to be defined inline nor in a file" << endl;
549       return false;
550     } else {
551       document = LoadXMLDocument(file);
552       if (!document) {
553         cerr << "Error loading file " << file << endl;
554         return false;
555       }
556       name = document->GetAttributeValue("name");
557     }
558   } else {
559     document = el;
560   }
561
562   if (document->GetName() == "autopilot") {
563     Components = &APComponents;
564     Name = "Autopilot: " + document->GetAttributeValue("name");
565   } else if (document->GetName() == "flight_control") {
566     Components = &FCSComponents;
567     Name = "FCS: " + document->GetAttributeValue("name");
568   } else if (document->GetName() == "system") {
569     Components = &Systems;
570     Name = "System: " + document->GetAttributeValue("name");
571   }
572   Debug(2);
573
574   if (document->GetName() == "flight_control") bindModel();
575
576   FGModel::Load(document); // Load interface properties from document
577
578   // After reading interface properties in a file, read properties in the local
579   // flight_control, autopilot, or system element. This allows general-purpose
580   // systems to be defined in a file, with overrides or initial loaded constants
581   // supplied in the relevant element of the aircraft configuration file.
582
583   Element* property_element = 0;
584
585   if (!fname.empty()) {
586     property_element = el->FindElement("property");
587     if (property_element && debug_lvl > 0) cout << endl << "    Overriding properties" << endl << endl;
588     while (property_element) {
589       double value=0.0;
590       if ( ! property_element->GetAttributeValue("value").empty())
591         value = property_element->GetAttributeValueAsNumber("value");
592
593       interface_property_string = property_element->GetDataLine();
594       if (PropertyManager->HasNode(interface_property_string)) {
595         FGPropertyManager* node = PropertyManager->GetNode(interface_property_string);
596         if (debug_lvl > 0)
597           cout << "      " << "Overriding value for property " << interface_property_string
598                << " (old value: " << node->getDoubleValue() << "  new value: " << value << ")" << endl;
599         node->setDoubleValue(value);
600       } else {
601         interface_properties.push_back(new double(value));
602         PropertyManager->Tie(interface_property_string, interface_properties.back());
603         if (debug_lvl > 0)
604           cout << "      " << interface_property_string << " (initial value: " << value << ")" << endl;
605       }
606       
607       property_element = el->FindNextElement("property");
608     }
609   }
610
611   channel_element = document->FindElement("channel");
612   while (channel_element) {
613   
614     if (debug_lvl > 0)
615       cout << endl << highint << fgblue << "    Channel " 
616          << normint << channel_element->GetAttributeValue("name") << reset << endl;
617   
618     component_element = channel_element->GetElement();
619     while (component_element) {
620       try {
621         if ((component_element->GetName() == string("lag_filter")) ||
622             (component_element->GetName() == string("lead_lag_filter")) ||
623             (component_element->GetName() == string("washout_filter")) ||
624             (component_element->GetName() == string("second_order_filter")) ||
625             (component_element->GetName() == string("integrator")) )
626         {
627           Components->push_back(new FGFilter(this, component_element));
628         } else if ((component_element->GetName() == string("pure_gain")) ||
629                    (component_element->GetName() == string("scheduled_gain")) ||
630                    (component_element->GetName() == string("aerosurface_scale")))
631         {
632           Components->push_back(new FGGain(this, component_element));
633         } else if (component_element->GetName() == string("summer")) {
634           Components->push_back(new FGSummer(this, component_element));
635         } else if (component_element->GetName() == string("deadband")) {
636           Components->push_back(new FGDeadBand(this, component_element));
637         } else if (component_element->GetName() == string("switch")) {
638           Components->push_back(new FGSwitch(this, component_element));
639         } else if (component_element->GetName() == string("kinematic")) {
640           Components->push_back(new FGKinemat(this, component_element));
641         } else if (component_element->GetName() == string("fcs_function")) {
642           Components->push_back(new FGFCSFunction(this, component_element));
643         } else if (component_element->GetName() == string("pid")) {
644           Components->push_back(new FGPID(this, component_element));
645         } else if (component_element->GetName() == string("actuator")) {
646           Components->push_back(new FGActuator(this, component_element));
647         } else if (component_element->GetName() == string("sensor")) {
648           Components->push_back(new FGSensor(this, component_element));
649         } else if (component_element->GetName() == string("accelerometer")) {
650           Components->push_back(new FGAccelerometer(this, component_element));
651         } else if (component_element->GetName() == string("magnetometer")) {
652           Components->push_back(new FGMagnetometer(this, component_element));
653         } else if (component_element->GetName() == string("gyro")) {
654           Components->push_back(new FGGyro(this, component_element));
655         } else {
656           cerr << "Unknown FCS component: " << component_element->GetName() << endl;
657         }
658       } catch(string s) {
659         cerr << highint << fgred << endl << "  " << s << endl;
660         cerr << reset << endl;
661         return false;
662       }
663       component_element = channel_element->GetNextElement();
664     }
665     channel_element = document->FindNextElement("channel");
666   }
667
668   ResetParser();
669
670   return true;
671 }
672
673 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674
675 double FGFCS::GetBrake(FGLGear::BrakeGroup bg)
676 {
677   return BrakePos[bg];
678 }
679
680 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681
682 string FGFCS::FindSystemFullPathname(const string& sysfilename)
683 {
684   string fullpath, localpath;
685   string system_filename = sysfilename;
686   string systemPath = FDMExec->GetSystemsPath();
687   string aircraftPath = FDMExec->GetFullAircraftPath();
688   ifstream system_file;
689
690   fullpath = systemPath + "/";
691   localpath = aircraftPath + "/Systems/";
692
693   if (system_filename.length() <=4 || system_filename.substr(system_filename.length()-4, 4) != ".xml") {
694     system_filename.append(".xml");
695   }
696
697   system_file.open(string(localpath + system_filename).c_str());
698   if ( !system_file.is_open()) {
699     system_file.open(string(fullpath + system_filename).c_str());
700       if ( !system_file.is_open()) {
701         cerr << " Could not open system file: " << system_filename << " in path "
702              << fullpath << " or " << localpath << endl;
703         return string("");
704       } else {
705         return string(fullpath + system_filename);
706       }
707   }
708   return string(localpath + system_filename);
709 }
710
711 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
712
713 ifstream* FGFCS::FindSystemFile(const string& sysfilename)
714 {
715   string fullpath, localpath;
716   string system_filename = sysfilename;
717   string systemPath = FDMExec->GetSystemsPath();
718   string aircraftPath = FDMExec->GetFullAircraftPath();
719   ifstream* system_file = new ifstream();
720
721   fullpath = systemPath + "/";
722   localpath = aircraftPath + "/Systems/";
723
724   if (system_filename.substr(system_filename.length()-4, 4) != ".xml") {
725     system_filename.append(".xml");
726   }
727
728   system_file->open(string(localpath + system_filename).c_str());
729   if ( !system_file->is_open()) {
730     system_file->open(string(fullpath + system_filename).c_str());
731       if ( !system_file->is_open()) {
732         cerr << " Could not open system file: " << system_filename << " in path "
733              << fullpath << " or " << localpath << endl;
734       }
735   }
736   return system_file;
737 }
738
739 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
740
741 string FGFCS::GetComponentStrings(const string& delimiter) const
742 {
743   unsigned int comp;
744   string CompStrings = "";
745   bool firstime = true;
746   int total_count=0;
747
748   for (unsigned int i=0; i<Systems.size(); i++) {
749     if (firstime) firstime = false;
750     else          CompStrings += delimiter;
751
752     CompStrings += Systems[i]->GetName();
753     total_count++;
754   }
755
756   for (comp = 0; comp < APComponents.size(); comp++)
757   {
758     if (firstime) firstime = false;
759     else          CompStrings += delimiter;
760
761     CompStrings += APComponents[comp]->GetName();
762     total_count++;
763   }
764
765   for (comp = 0; comp < FCSComponents.size(); comp++) {
766     if (firstime) firstime = false;
767     else          CompStrings += delimiter;
768
769     CompStrings += FCSComponents[comp]->GetName();
770     total_count++;
771   }
772
773   return CompStrings;
774 }
775
776 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777
778 string FGFCS::GetComponentValues(const string& delimiter) const
779 {
780   std::ostringstream buf;
781
782   unsigned int comp;
783   bool firstime = true;
784   int total_count=0;
785
786   for (unsigned int i=0; i<Systems.size(); i++) {
787     if (firstime) firstime = false;
788     else          buf << delimiter;
789
790     buf << setprecision(9) << Systems[i]->GetOutput();
791     total_count++;
792   }
793
794   for (comp = 0; comp < APComponents.size(); comp++) {
795     if (firstime) firstime = false;
796     else          buf << delimiter;
797
798     buf << setprecision(9) << APComponents[comp]->GetOutput();
799     total_count++;
800   }
801
802   for (comp = 0; comp < FCSComponents.size(); comp++) {
803     if (firstime) firstime = false;
804     else          buf << delimiter;
805
806     buf << setprecision(9) << FCSComponents[comp]->GetOutput();
807     total_count++;
808   }
809
810   return buf.str();
811 }
812
813 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814
815 void FGFCS::AddThrottle(void)
816 {
817   ThrottleCmd.push_back(0.0);
818   ThrottlePos.push_back(0.0);
819   MixtureCmd.push_back(0.0);     // assume throttle and mixture are coupled
820   MixturePos.push_back(0.0);
821   PropAdvanceCmd.push_back(0.0); // assume throttle and prop pitch are coupled
822   PropAdvance.push_back(0.0);
823   PropFeatherCmd.push_back(false);
824   PropFeather.push_back(false);
825
826   unsigned int num = (unsigned int)ThrottleCmd.size()-1;
827   bindThrottle(num);
828 }
829
830 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
831
832 void FGFCS::AddGear(unsigned int NumGear)
833 {
834   SteerPosDeg.clear();
835   for (unsigned int i=0; i<NumGear; i++) SteerPosDeg.push_back(0.0);
836 }
837
838 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
839
840 double FGFCS::GetDt(void)
841 {
842   return FDMExec->GetDeltaT()*rate;
843 }
844
845 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
846
847 void FGFCS::bind(void)
848 {
849   PropertyManager->Tie("fcs/aileron-cmd-norm", this, &FGFCS::GetDaCmd, &FGFCS::SetDaCmd);
850   PropertyManager->Tie("fcs/elevator-cmd-norm", this, &FGFCS::GetDeCmd, &FGFCS::SetDeCmd);
851   PropertyManager->Tie("fcs/rudder-cmd-norm", this, &FGFCS::GetDrCmd, &FGFCS::SetDrCmd);
852   PropertyManager->Tie("fcs/flap-cmd-norm", this, &FGFCS::GetDfCmd, &FGFCS::SetDfCmd);
853   PropertyManager->Tie("fcs/speedbrake-cmd-norm", this, &FGFCS::GetDsbCmd, &FGFCS::SetDsbCmd);
854   PropertyManager->Tie("fcs/spoiler-cmd-norm", this, &FGFCS::GetDspCmd, &FGFCS::SetDspCmd);
855   PropertyManager->Tie("fcs/pitch-trim-cmd-norm", this, &FGFCS::GetPitchTrimCmd, &FGFCS::SetPitchTrimCmd);
856   PropertyManager->Tie("fcs/roll-trim-cmd-norm", this, &FGFCS::GetRollTrimCmd, &FGFCS::SetRollTrimCmd);
857   PropertyManager->Tie("fcs/yaw-trim-cmd-norm", this, &FGFCS::GetYawTrimCmd, &FGFCS::SetYawTrimCmd);
858
859   PropertyManager->Tie("fcs/left-aileron-pos-rad", this, ofRad, &FGFCS::GetDaLPos, &FGFCS::SetDaLPos);
860   PropertyManager->Tie("fcs/left-aileron-pos-deg", this, ofDeg, &FGFCS::GetDaLPos, &FGFCS::SetDaLPos);
861   PropertyManager->Tie("fcs/left-aileron-pos-norm", this, ofNorm, &FGFCS::GetDaLPos, &FGFCS::SetDaLPos);
862   PropertyManager->Tie("fcs/mag-left-aileron-pos-rad", this, ofMag, &FGFCS::GetDaLPos);
863
864   PropertyManager->Tie("fcs/right-aileron-pos-rad", this, ofRad, &FGFCS::GetDaRPos, &FGFCS::SetDaRPos);
865   PropertyManager->Tie("fcs/right-aileron-pos-deg", this, ofDeg, &FGFCS::GetDaRPos, &FGFCS::SetDaRPos);
866   PropertyManager->Tie("fcs/right-aileron-pos-norm", this, ofNorm, &FGFCS::GetDaRPos, &FGFCS::SetDaRPos);
867   PropertyManager->Tie("fcs/mag-right-aileron-pos-rad", this, ofMag, &FGFCS::GetDaRPos);
868
869   PropertyManager->Tie("fcs/elevator-pos-rad", this, ofRad, &FGFCS::GetDePos, &FGFCS::SetDePos);
870   PropertyManager->Tie("fcs/elevator-pos-deg", this, ofDeg, &FGFCS::GetDePos, &FGFCS::SetDePos);
871   PropertyManager->Tie("fcs/elevator-pos-norm", this, ofNorm, &FGFCS::GetDePos, &FGFCS::SetDePos);
872   PropertyManager->Tie("fcs/mag-elevator-pos-rad", this, ofMag, &FGFCS::GetDePos);
873
874   PropertyManager->Tie("fcs/rudder-pos-rad", this,ofRad, &FGFCS::GetDrPos, &FGFCS::SetDrPos);
875   PropertyManager->Tie("fcs/rudder-pos-deg", this,ofDeg, &FGFCS::GetDrPos, &FGFCS::SetDrPos);
876   PropertyManager->Tie("fcs/rudder-pos-norm", this,ofNorm, &FGFCS::GetDrPos, &FGFCS::SetDrPos);
877   PropertyManager->Tie("fcs/mag-rudder-pos-rad", this,ofMag, &FGFCS::GetDrPos);
878
879   PropertyManager->Tie("fcs/flap-pos-rad", this,ofRad, &FGFCS::GetDfPos, &FGFCS::SetDfPos);
880   PropertyManager->Tie("fcs/flap-pos-deg", this,ofDeg, &FGFCS::GetDfPos, &FGFCS::SetDfPos);
881   PropertyManager->Tie("fcs/flap-pos-norm", this,ofNorm, &FGFCS::GetDfPos, &FGFCS::SetDfPos);
882
883   PropertyManager->Tie("fcs/speedbrake-pos-rad", this,ofRad, &FGFCS::GetDsbPos, &FGFCS::SetDsbPos);
884   PropertyManager->Tie("fcs/speedbrake-pos-deg", this,ofDeg, &FGFCS::GetDsbPos, &FGFCS::SetDsbPos);
885   PropertyManager->Tie("fcs/speedbrake-pos-norm", this,ofNorm, &FGFCS::GetDsbPos, &FGFCS::SetDsbPos);
886   PropertyManager->Tie("fcs/mag-speedbrake-pos-rad", this,ofMag, &FGFCS::GetDsbPos);
887
888   PropertyManager->Tie("fcs/spoiler-pos-rad", this, ofRad, &FGFCS::GetDspPos, &FGFCS::SetDspPos);
889   PropertyManager->Tie("fcs/spoiler-pos-deg", this, ofDeg, &FGFCS::GetDspPos, &FGFCS::SetDspPos);
890   PropertyManager->Tie("fcs/spoiler-pos-norm", this, ofNorm, &FGFCS::GetDspPos, &FGFCS::SetDspPos);
891   PropertyManager->Tie("fcs/mag-spoiler-pos-rad", this, ofMag, &FGFCS::GetDspPos);
892
893   PropertyManager->Tie("gear/gear-pos-norm", this, &FGFCS::GetGearPos, &FGFCS::SetGearPos);
894   PropertyManager->Tie("gear/gear-cmd-norm", this, &FGFCS::GetGearCmd, &FGFCS::SetGearCmd);
895   PropertyManager->Tie("fcs/left-brake-cmd-norm", this, &FGFCS::GetLBrake, &FGFCS::SetLBrake);
896   PropertyManager->Tie("fcs/right-brake-cmd-norm", this, &FGFCS::GetRBrake, &FGFCS::SetRBrake);
897   PropertyManager->Tie("fcs/center-brake-cmd-norm", this, &FGFCS::GetCBrake, &FGFCS::SetCBrake);
898   PropertyManager->Tie("fcs/steer-cmd-norm", this, &FGFCS::GetDsCmd, &FGFCS::SetDsCmd);
899
900   PropertyManager->Tie("gear/tailhook-pos-norm", this, &FGFCS::GetTailhookPos, &FGFCS::SetTailhookPos);
901   PropertyManager->Tie("fcs/wing-fold-pos-norm", this, &FGFCS::GetWingFoldPos, &FGFCS::SetWingFoldPos);
902 }
903
904 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905 // Technically, this function should probably bind propulsion type specific controls
906 // rather than mixture and prop-advance.
907
908 void FGFCS::bindThrottle(unsigned int num)
909 {
910   string tmp;
911
912   tmp = CreateIndexedPropertyName("fcs/throttle-cmd-norm", num);
913   PropertyManager->Tie( tmp.c_str(), this, num, &FGFCS::GetThrottleCmd,
914                                         &FGFCS::SetThrottleCmd);
915   tmp = CreateIndexedPropertyName("fcs/throttle-pos-norm", num);
916   PropertyManager->Tie( tmp.c_str(), this, num, &FGFCS::GetThrottlePos,
917                                         &FGFCS::SetThrottlePos);
918   tmp = CreateIndexedPropertyName("fcs/mixture-cmd-norm", num);
919   PropertyManager->Tie( tmp.c_str(), this, num, &FGFCS::GetMixtureCmd,
920                                         &FGFCS::SetMixtureCmd);
921   tmp = CreateIndexedPropertyName("fcs/mixture-pos-norm", num);
922   PropertyManager->Tie( tmp.c_str(), this, num, &FGFCS::GetMixturePos,
923                                         &FGFCS::SetMixturePos);
924   tmp = CreateIndexedPropertyName("fcs/advance-cmd-norm", num);
925   PropertyManager->Tie( tmp.c_str(), this, num, &FGFCS::GetPropAdvanceCmd,
926                                         &FGFCS::SetPropAdvanceCmd);
927   tmp = CreateIndexedPropertyName("fcs/advance-pos-norm", num);
928   PropertyManager->Tie( tmp.c_str(), this, num, &FGFCS::GetPropAdvance,
929                                         &FGFCS::SetPropAdvance);
930   tmp = CreateIndexedPropertyName("fcs/feather-cmd-norm", num);
931   PropertyManager->Tie( tmp.c_str(), this, num, &FGFCS::GetFeatherCmd,
932                                         &FGFCS::SetFeatherCmd);
933   tmp = CreateIndexedPropertyName("fcs/feather-pos-norm", num);
934   PropertyManager->Tie( tmp.c_str(), this, num, &FGFCS::GetPropFeather,
935                                         &FGFCS::SetPropFeather);
936 }
937
938 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
939
940 void FGFCS::bindModel(void)
941 {
942   unsigned int i;
943   string tmp;
944
945   for (i=0; i<SteerPosDeg.size(); i++) {
946     if (FDMExec->GetGroundReactions()->GetGearUnit(i)->GetSteerable()) {
947       tmp = CreateIndexedPropertyName("fcs/steer-pos-deg", i);
948       PropertyManager->Tie( tmp.c_str(), this, i, &FGFCS::GetSteerPosDeg, &FGFCS::SetSteerPosDeg);
949     }
950   }
951 }
952
953 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
954 //    The bitmasked value choices are as follows:
955 //    unset: In this case (the default) JSBSim would only print
956 //       out the normally expected messages, essentially echoing
957 //       the config files as they are read. If the environment
958 //       variable is not set, debug_lvl is set to 1 internally
959 //    0: This requests JSBSim not to output any messages
960 //       whatsoever.
961 //    1: This value explicity requests the normal JSBSim
962 //       startup messages
963 //    2: This value asks for a message to be printed out when
964 //       a class is instantiated
965 //    4: When this value is set, a message is displayed when a
966 //       FGModel object executes its Run() method
967 //    8: When this value is set, various runtime state variables
968 //       are printed out periodically
969 //    16: When set various parameters are sanity checked and
970 //       a message is printed out when they go out of bounds
971
972 void FGFCS::Debug(int from)
973 {
974   if (debug_lvl <= 0) return;
975
976   if (debug_lvl & 1) { // Standard console startup message output
977     if (from == 2) { // Loader
978       cout << endl << "  " << Name << endl;
979     }
980   }
981   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
982     if (from == 0) cout << "Instantiated: FGFCS" << endl;
983     if (from == 1) cout << "Destroyed:    FGFCS" << endl;
984   }
985   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
986   }
987   if (debug_lvl & 8 ) { // Runtime state variables
988   }
989   if (debug_lvl & 16) { // Sanity checking
990   }
991   if (debug_lvl & 64) {
992     if (from == 0) { // Constructor
993       cout << IdSrc << endl;
994       cout << IdHdr << endl;
995     }
996   }
997 }
998
999 }