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