1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGFCSComponent.cpp
7 ------------- Copyright (C) 2000 -------------
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19 You should have received a copy of the GNU Lesser General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA.
23 Further information about the GNU Lesser General Public License can also be found on
24 the world wide web at http://www.gnu.org.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
30 --------------------------------------------------------------------------------
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES, and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "FGFCSComponent.h"
41 #include "input_output/FGPropertyManager.h"
42 #include "input_output/FGXMLElement.h"
50 static const char *IdSrc = "$Id: FGFCSComponent.cpp,v 1.29 2010/09/07 00:40:03 jberndt Exp $";
51 static const char *IdHdr = ID_FCSCOMPONENT;
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 FGFCSComponent::FGFCSComponent(FGFCS* _fcs, Element* element) : fcs(_fcs)
59 Element *input_element, *clip_el;
60 Input = Output = clipmin = clipmax = 0.0;
63 ClipMinPropertyNode = ClipMaxPropertyNode = 0;
64 clipMinSign = clipMaxSign = 1.0;
65 IsOutput = clip = false;
66 string input, clip_string;
69 PropertyManager = fcs->GetPropertyManager();
70 if (element->GetName() == string("lag_filter")) {
72 } else if (element->GetName() == string("lead_lag_filter")) {
73 Type = "LEAD_LAG_FILTER";
74 } else if (element->GetName() == string("washout_filter")) {
75 Type = "WASHOUT_FILTER";
76 } else if (element->GetName() == string("second_order_filter")) {
77 Type = "SECOND_ORDER_FILTER";
78 } else if (element->GetName() == string("integrator")) {
80 } else if (element->GetName() == string("summer")) {
82 } else if (element->GetName() == string("pure_gain")) {
84 } else if (element->GetName() == string("scheduled_gain")) {
85 Type = "SCHEDULED_GAIN";
86 } else if (element->GetName() == string("aerosurface_scale")) {
87 Type = "AEROSURFACE_SCALE";
88 } else if (element->GetName() == string("switch")) {
90 } else if (element->GetName() == string("kinematic")) {
92 } else if (element->GetName() == string("deadband")) {
94 } else if (element->GetName() == string("fcs_function")) {
95 Type = "FCS_FUNCTION";
96 } else if (element->GetName() == string("pid")) {
98 } else if (element->GetName() == string("sensor")) {
100 } else if (element->GetName() == string("accelerometer")) {
101 Type = "ACCELEROMETER";
102 } else if (element->GetName() == string("magnetometer")) {
103 Type = "MAGNETOMETER";
104 } else if (element->GetName() == string("gyro")) {
106 } else if (element->GetName() == string("actuator")) {
108 } else { // illegal component in this channel
112 Name = element->GetAttributeValue("name");
114 FGPropertyManager *tmp=0;
116 input_element = element->FindElement("input");
117 while (input_element) {
118 input = input_element->GetDataLine();
119 if (input[0] == '-') {
120 InputSigns.push_back(-1.0);
123 InputSigns.push_back( 1.0);
125 if (PropertyManager->HasNode(input)) {
126 tmp = PropertyManager->GetNode(input);
129 // cerr << fgcyan << "In component: " + Name + " property "
130 // + input + " is initially undefined." << reset << endl;
132 InputNodes.push_back( tmp );
133 InputNames.push_back( input );
135 input_element = element->FindNextElement("input");
138 Element *out_elem = element->FindElement("output");
141 string output_node_name = out_elem->GetDataLine();
142 FGPropertyManager* OutputNode = PropertyManager->GetNode( output_node_name, true );
143 OutputNodes.push_back(OutputNode);
145 cerr << endl << " Unable to process property: " << output_node_name << endl;
146 throw(string("Invalid output property name in flight control definition"));
148 out_elem = element->FindNextElement("output");
151 Element* delay_elem = element->FindElement("delay");
153 delay = (unsigned int)delay_elem->GetDataAsNumber();
154 string delayType = delay_elem->GetAttributeValue("type");
155 if (delayType.length() > 0) {
156 if (delayType == "time") {
157 delay = (int)(delay / dt);
158 } else if (delayType == "frames") {
159 // no op. the delay type of "frames" is assumed and is the default.
161 cerr << "Unallowed delay type" << endl;
164 delay = (int)(delay / dt);
166 output_array.resize(delay);
167 for (int i=0; i<delay; i++) output_array[i] = 0.0;
170 clip_el = element->FindElement("clipto");
172 clip_string = clip_el->FindElementValue("min");
173 if (!is_number(clip_string)) { // it's a property
174 if (clip_string[0] == '-') {
176 clip_string.erase(0,1);
178 ClipMinPropertyNode = PropertyManager->GetNode( clip_string );
180 clipmin = clip_el->FindElementValueAsNumber("min");
182 clip_string = clip_el->FindElementValue("max");
183 if (!is_number(clip_string)) { // it's a property
184 if (clip_string[0] == '-') {
186 clip_string.erase(0,1);
188 ClipMaxPropertyNode = PropertyManager->GetNode( clip_string );
190 clipmax = clip_el->FindElementValueAsNumber("max");
198 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 FGFCSComponent::~FGFCSComponent()
205 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 void FGFCSComponent::SetOutput(void)
209 for (unsigned int i=0; i<OutputNodes.size(); i++) OutputNodes[i]->setDoubleValue(Output);
212 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 bool FGFCSComponent::Run(void)
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 void FGFCSComponent::Delay(void)
223 output_array[index] = Output;
224 if (index == delay-1) index = 0;
226 Output = output_array[index];
229 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231 void FGFCSComponent::Clip(void)
234 if (ClipMinPropertyNode != 0) clipmin = clipMinSign*ClipMinPropertyNode->getDoubleValue();
235 if (ClipMaxPropertyNode != 0) clipmax = clipMaxSign*ClipMaxPropertyNode->getDoubleValue();
236 if (Output > clipmax) Output = clipmax;
237 else if (Output < clipmin) Output = clipmin;
241 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 void FGFCSComponent::LateBind(void)
245 FGPropertyManager* node = 0L;
247 for (unsigned int i=0; i<InputNodes.size(); i++) {
248 if (!InputNodes[i]) {
249 if (PropertyManager->HasNode(InputNames[i])) {
250 node = PropertyManager->GetNode(InputNames[i]);
251 InputNodes[i] = node;
253 throw(InputNames[i]);
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 // The old way of naming FCS components allowed upper or lower case, spaces, etc.
262 // but then the names were modified to fit into a property name heirarchy. This
263 // was confusing (it wasn't done intentionally - it was a carryover from the early
264 // design). We now support the direct naming of properties in the FCS component
265 // name attribute. The old way is supported in code at this time, but deprecated.
267 void FGFCSComponent::bind(void)
270 if (Name.find("/") == string::npos) {
271 tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
275 PropertyManager->Tie( tmp, this, &FGFCSComponent::GetOutput);
278 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 // The bitmasked value choices are as follows:
280 // unset: In this case (the default) JSBSim would only print
281 // out the normally expected messages, essentially echoing
282 // the config files as they are read. If the environment
283 // variable is not set, debug_lvl is set to 1 internally
284 // 0: This requests JSBSim not to output any messages
286 // 1: This value explicity requests the normal JSBSim
288 // 2: This value asks for a message to be printed out when
289 // a class is instantiated
290 // 4: When this value is set, a message is displayed when a
291 // FGModel object executes its Run() method
292 // 8: When this value is set, various runtime state variables
293 // are printed out periodically
294 // 16: When set various parameters are sanity checked and
295 // a message is printed out when they go out of bounds
297 void FGFCSComponent::Debug(int from)
301 if (debug_lvl <= 0) return;
303 if (debug_lvl & 1) { // Standard console startup message output
305 cout << endl << " Loading Component \"" << Name
306 << "\" of type: " << Type << endl;
309 if (ClipMinPropertyNode != 0L) {
310 if (clipMinSign < 0.0) propsign="-";
311 cout << " Minimum limit: " << propsign << ClipMinPropertyNode->GetName() << endl;
314 cout << " Minimum limit: " << clipmin << endl;
316 if (ClipMaxPropertyNode != 0L) {
317 if (clipMaxSign < 0.0) propsign="-";
318 cout << " Maximum limit: " << propsign << ClipMaxPropertyNode->GetName() << endl;
321 cout << " Maximum limit: " << clipmax << endl;
324 if (delay > 0) cout <<" Frame delay: " << delay
325 << " frames (" << delay*dt << " sec)" << endl;
328 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
329 if (from == 0) cout << "Instantiated: FGFCSComponent" << endl;
330 if (from == 1) cout << "Destroyed: FGFCSComponent" << endl;
332 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
334 if (debug_lvl & 8 ) { // Runtime state variables
336 if (debug_lvl & 16) { // Sanity checking
338 if (debug_lvl & 64) {
339 if (from == 0) { // Constructor
340 cout << IdSrc << endl;
341 cout << IdHdr << endl;