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.27 2009/10/24 22:59:30 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 tmp = PropertyManager->GetNode(input);
127 InputNodes.push_back( tmp );
129 cerr << fgred << " In component: " << Name << " unknown property "
130 << input << " referenced. Aborting" << reset << endl;
133 input_element = element->FindNextElement("input");
136 Element *out_elem = element->FindElement("output");
139 string output_node_name = out_elem->GetDataLine();
140 FGPropertyManager* OutputNode = PropertyManager->GetNode( output_node_name, true );
141 OutputNodes.push_back(OutputNode);
143 cerr << endl << " Unable to process property: " << output_node_name << endl;
144 throw(string("Invalid output property name in flight control definition"));
146 out_elem = element->FindNextElement("output");
149 Element* delay_elem = element->FindElement("delay");
151 delay = (unsigned int)delay_elem->GetDataAsNumber();
152 string delayType = delay_elem->GetAttributeValue("type");
153 if (delayType.length() > 0) {
154 if (delayType == "time") {
155 delay = (int)(delay / dt);
156 } else if (delayType == "frames") {
157 // no op. the delay type of "frames" is assumed and is the default.
159 cerr << "Unallowed delay type" << endl;
162 delay = (int)(delay / dt);
164 output_array.resize(delay);
165 for (int i=0; i<delay; i++) output_array[i] = 0.0;
168 clip_el = element->FindElement("clipto");
170 clip_string = clip_el->FindElementValue("min");
171 if (!is_number(clip_string)) { // it's a property
172 if (clip_string[0] == '-') {
174 clip_string.erase(0,1);
176 ClipMinPropertyNode = PropertyManager->GetNode( clip_string );
178 clipmin = clip_el->FindElementValueAsNumber("min");
180 clip_string = clip_el->FindElementValue("max");
181 if (!is_number(clip_string)) { // it's a property
182 if (clip_string[0] == '-') {
184 clip_string.erase(0,1);
186 ClipMaxPropertyNode = PropertyManager->GetNode( clip_string );
188 clipmax = clip_el->FindElementValueAsNumber("max");
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 FGFCSComponent::~FGFCSComponent()
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 void FGFCSComponent::SetOutput(void)
207 for (unsigned int i=0; i<OutputNodes.size(); i++) OutputNodes[i]->setDoubleValue(Output);
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 bool FGFCSComponent::Run(void)
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 void FGFCSComponent::Delay(void)
221 output_array[index] = Output;
222 if (index == delay-1) index = 0;
224 Output = output_array[index];
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 void FGFCSComponent::Clip(void)
232 if (ClipMinPropertyNode != 0) clipmin = clipMinSign*ClipMinPropertyNode->getDoubleValue();
233 if (ClipMaxPropertyNode != 0) clipmax = clipMaxSign*ClipMaxPropertyNode->getDoubleValue();
234 if (Output > clipmax) Output = clipmax;
235 else if (Output < clipmin) Output = clipmin;
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 // The old way of naming FCS components allowed upper or lower case, spaces, etc.
242 // but then the names were modified to fit into a property name heirarchy. This
243 // was confusing (it wasn't done intentionally - it was a carryover from the early
244 // design). We now support the direct naming of properties in the FCS component
245 // name attribute. The old way is supported in code at this time, but deprecated.
247 void FGFCSComponent::bind(void)
250 if (Name.find("/") == string::npos) {
251 tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
255 PropertyManager->Tie( tmp, this, &FGFCSComponent::GetOutput);
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 // The bitmasked value choices are as follows:
260 // unset: In this case (the default) JSBSim would only print
261 // out the normally expected messages, essentially echoing
262 // the config files as they are read. If the environment
263 // variable is not set, debug_lvl is set to 1 internally
264 // 0: This requests JSBSim not to output any messages
266 // 1: This value explicity requests the normal JSBSim
268 // 2: This value asks for a message to be printed out when
269 // a class is instantiated
270 // 4: When this value is set, a message is displayed when a
271 // FGModel object executes its Run() method
272 // 8: When this value is set, various runtime state variables
273 // are printed out periodically
274 // 16: When set various parameters are sanity checked and
275 // a message is printed out when they go out of bounds
277 void FGFCSComponent::Debug(int from)
281 if (debug_lvl <= 0) return;
283 if (debug_lvl & 1) { // Standard console startup message output
285 cout << endl << " Loading Component \"" << Name
286 << "\" of type: " << Type << endl;
289 if (ClipMinPropertyNode != 0L) {
290 if (clipMinSign < 0.0) propsign="-";
291 cout << " Minimum limit: " << propsign << ClipMinPropertyNode->GetName() << endl;
294 cout << " Minimum limit: " << clipmin << endl;
296 if (ClipMaxPropertyNode != 0L) {
297 if (clipMaxSign < 0.0) propsign="-";
298 cout << " Maximum limit: " << propsign << ClipMaxPropertyNode->GetName() << endl;
301 cout << " Maximum limit: " << clipmax << endl;
304 if (delay > 0) cout <<" Frame delay: " << delay
305 << " frames (" << delay*dt << " sec)" << endl;
308 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
309 if (from == 0) cout << "Instantiated: FGFCSComponent" << endl;
310 if (from == 1) cout << "Destroyed: FGFCSComponent" << endl;
312 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
314 if (debug_lvl & 8 ) { // Runtime state variables
316 if (debug_lvl & 16) { // Sanity checking
318 if (debug_lvl & 64) {
319 if (from == 0) { // Constructor
320 cout << IdSrc << endl;
321 cout << IdHdr << endl;