]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGFCSComponent.cpp
Merge branch 'jsd/atmos' into topic/atmos-merge
[flightgear.git] / src / FDM / JSBSim / models / flight_control / FGFCSComponent.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGFCSComponent.cpp
4  Author:       Jon S. Berndt
5  Date started: 11/1999
6
7  ------------- Copyright (C) 2000 -------------
8
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
12  version.
13
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
17  details.
18
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.
22
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.
25
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
28
29 HISTORY
30 --------------------------------------------------------------------------------
31
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES,  and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include "FGFCSComponent.h"
41
42 namespace JSBSim {
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_FCSCOMPONENT;
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 FGFCSComponent::FGFCSComponent(FGFCS* _fcs, Element* element) : fcs(_fcs)
52 {
53   Element *input_element, *clip_el;
54   Input = Output = clipmin = clipmax = 0.0;
55   OutputNode = treenode = 0;
56   ClipMinPropertyNode = ClipMaxPropertyNode = 0;
57   clipMinSign = clipMaxSign = 1.0;
58   IsOutput   = clip = false;
59   string input, clip_string;
60
61   PropertyManager = fcs->GetPropertyManager();
62   if        (element->GetName() == string("lag_filter")) {
63     Type = "LAG_FILTER";
64   } else if (element->GetName() == string("lead_lag_filter")) {
65     Type = "LEAD_LAG_FILTER";
66   } else if (element->GetName() == string("washout_filter")) {
67     Type = "WASHOUT_FILTER";
68   } else if (element->GetName() == string("second_order_filter")) {
69     Type = "SECOND_ORDER_FILTER";
70   } else if (element->GetName() == string("integrator")) {
71     Type = "INTEGRATOR";
72   } else if (element->GetName() == string("summer")) {
73     Type = "SUMMER";
74   } else if (element->GetName() == string("pure_gain")) {
75     Type = "PURE_GAIN";
76   } else if (element->GetName() == string("scheduled_gain")) {
77     Type = "SCHEDULED_GAIN";
78   } else if (element->GetName() == string("aerosurface_scale")) {
79     Type = "AEROSURFACE_SCALE";
80   } else if (element->GetName() == string("switch")) {
81     Type = "SWITCH";
82   } else if (element->GetName() == string("kinematic")) {
83     Type = "KINEMATIC";
84   } else if (element->GetName() == string("deadband")) {
85     Type = "DEADBAND";
86   } else if (element->GetName() == string("fcs_function")) {
87     Type = "FCS_FUNCTION";
88   } else if (element->GetName() == string("pid")) {
89     Type = "PID";
90   } else if (element->GetName() == string("sensor")) {
91     Type = "SENSOR";
92   } else if (element->GetName() == string("accelerometer")) {
93     Type = "ACCELEROMETER";
94   } else if (element->GetName() == string("gyro")) {
95     Type = "GYRO";
96   } else if (element->GetName() == string("actuator")) {
97     Type = "ACTUATOR";
98   } else { // illegal component in this channel
99     Type = "UNKNOWN";
100   }
101
102   Name = element->GetAttributeValue("name");
103
104   FGPropertyManager *tmp=0;
105
106   input_element = element->FindElement("input");
107   while (input_element) {
108     input = input_element->GetDataLine();
109     if (input[0] == '-') {
110       InputSigns.push_back(-1.0);
111       input.erase(0,1);
112     } else {
113       InputSigns.push_back( 1.0);
114     }
115     tmp = PropertyManager->GetNode(input);
116     if (tmp) {
117       InputNodes.push_back( tmp );
118     } else {
119       cerr << fgred << "  In component: " << Name << " unknown property "
120            << input << " referenced. Aborting" << reset << endl;
121       exit(-1);
122     }
123     input_element = element->FindNextElement("input");
124   }
125
126   if (element->FindElement("output")) {
127     IsOutput = true;
128     OutputNode = PropertyManager->GetNode( element->FindElementValue("output"), true );
129     if (!OutputNode) {
130       cerr << endl << "  Unable to process property: " << element->FindElementValue("output") << endl;
131       throw(string("Invalid output property name in flight control definition"));
132     }
133   }
134
135   clip_el = element->FindElement("clipto");
136   if (clip_el) {
137     clip_string = clip_el->FindElementValue("min");
138     if (!is_number(clip_string)) { // it's a property
139       if (clip_string[0] == '-') {
140         clipMinSign = -1.0;
141         clip_string.erase(0,1);
142       }
143       ClipMinPropertyNode = PropertyManager->GetNode( clip_string );
144     } else {
145       clipmin = clip_el->FindElementValueAsNumber("min");
146     }
147     clip_string = clip_el->FindElementValue("max");
148     if (!is_number(clip_string)) { // it's a property
149       if (clip_string[0] == '-') {
150         clipMaxSign = -1.0;
151         clip_string.erase(0,1);
152       }
153       ClipMaxPropertyNode = PropertyManager->GetNode( clip_string );
154     } else {
155       clipmax = clip_el->FindElementValueAsNumber("max");
156     }
157     clip = true;
158   }
159
160   Debug(0);
161 }
162
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164
165 FGFCSComponent::~FGFCSComponent()
166 {
167   Debug(1);
168 }
169
170 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171
172 void FGFCSComponent::SetOutput(void)
173 {
174   OutputNode->setDoubleValue(Output);
175 }
176
177 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178
179 bool FGFCSComponent::Run(void)
180 {
181   return true;
182 }
183
184 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185
186 void FGFCSComponent::Clip(void)
187 {
188   if (clip) {
189     if (ClipMinPropertyNode != 0) clipmin = clipMinSign*ClipMinPropertyNode->getDoubleValue();
190     if (ClipMaxPropertyNode != 0) clipmax = clipMaxSign*ClipMaxPropertyNode->getDoubleValue();
191     if (Output > clipmax)      Output = clipmax;
192     else if (Output < clipmin) Output = clipmin;
193   }
194 }
195
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 //
198 // The old way of naming FCS components allowed upper or lower case, spaces, etc.
199 // but then the names were modified to fit into a property name heirarchy. This
200 // was confusing (it wasn't done intentionally - it was a carryover from the early
201 // design). We now support the direct naming of properties in the FCS component
202 // name attribute. The old way is supported in code at this time, but deprecated.
203
204 void FGFCSComponent::bind(void)
205 {
206   string tmp;
207   if (Name.find("/") == string::npos) {
208     tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
209   } else {
210     tmp = Name;
211   }
212   PropertyManager->Tie( tmp, this, &FGFCSComponent::GetOutput);
213 }
214
215 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 //    The bitmasked value choices are as follows:
217 //    unset: In this case (the default) JSBSim would only print
218 //       out the normally expected messages, essentially echoing
219 //       the config files as they are read. If the environment
220 //       variable is not set, debug_lvl is set to 1 internally
221 //    0: This requests JSBSim not to output any messages
222 //       whatsoever.
223 //    1: This value explicity requests the normal JSBSim
224 //       startup messages
225 //    2: This value asks for a message to be printed out when
226 //       a class is instantiated
227 //    4: When this value is set, a message is displayed when a
228 //       FGModel object executes its Run() method
229 //    8: When this value is set, various runtime state variables
230 //       are printed out periodically
231 //    16: When set various parameters are sanity checked and
232 //       a message is printed out when they go out of bounds
233
234 void FGFCSComponent::Debug(int from)
235 {
236   string propsign="";
237
238   if (debug_lvl <= 0) return;
239
240   if (debug_lvl & 1) { // Standard console startup message output
241     if (from == 0) {
242       cout << endl << "    Loading Component \"" << Name
243                    << "\" of type: " << Type << endl;
244
245       if (clip) {
246         if (ClipMinPropertyNode != 0L) {
247           if (clipMinSign < 0.0) propsign="-";
248           cout << "      Minimum limit: " << propsign << ClipMinPropertyNode->GetName() << endl;
249           propsign="";
250         } else {
251           cout << "      Minimum limit: " << clipmin << endl;
252         }
253         if (ClipMaxPropertyNode != 0L) {
254           if (clipMaxSign < 0.0) propsign="-";
255           cout << "      Maximum limit: " << propsign << ClipMaxPropertyNode->GetName() << endl;
256           propsign="";
257         } else {
258           cout << "      Maximum limit: " << clipmax << endl;
259         }
260       }  
261     }
262   }
263   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
264     if (from == 0) cout << "Instantiated: FGFCSComponent" << endl;
265     if (from == 1) cout << "Destroyed:    FGFCSComponent" << endl;
266   }
267   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
268   }
269   if (debug_lvl & 8 ) { // Runtime state variables
270   }
271   if (debug_lvl & 16) { // Sanity checking
272   }
273   if (debug_lvl & 64) {
274     if (from == 0) { // Constructor
275       cout << IdSrc << endl;
276       cout << IdHdr << endl;
277     }
278   }
279 }
280 }