]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGFCSComponent.cpp
Sync. with JSBSim CVS (header cleanups).
[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("actuator")) {
93     Type = "ACTUATOR";
94   } else { // illegal component in this channel
95     Type = "UNKNOWN";
96   }
97
98   Name = element->GetAttributeValue("name");
99
100   FGPropertyManager *tmp=0;
101
102   input_element = element->FindElement("input");
103   while (input_element) {
104     input = input_element->GetDataLine();
105     if (input[0] == '-') {
106       InputSigns.push_back(-1.0);
107       input.erase(0,1);
108     } else {
109       InputSigns.push_back( 1.0);
110     }
111     tmp = PropertyManager->GetNode(input);
112     if (tmp) {
113       InputNodes.push_back( tmp );
114     } else {
115       cerr << fgred << "  In component: " << Name << " unknown property "
116            << input << " referenced. Aborting" << reset << endl;
117       exit(-1);
118     }
119     input_element = element->FindNextElement("input");
120   }
121
122   if (element->FindElement("output")) {
123     IsOutput = true;
124     OutputNode = PropertyManager->GetNode( element->FindElementValue("output"), true );
125     if (!OutputNode) {
126       cerr << endl << "  Unable to process property: " << element->FindElementValue("output") << endl;
127       throw(string("Invalid output property name in flight control definition"));
128     }
129   }
130
131   clip_el = element->FindElement("clipto");
132   if (clip_el) {
133     clip_string = clip_el->FindElementValue("min");
134     if (clip_string.find_first_not_of("+-.0123456789") != string::npos) { // it's a property
135       if (clip_string[0] == '-') {
136         clipMinSign = -1.0;
137         clip_string.erase(0,1);
138       }
139       ClipMinPropertyNode = PropertyManager->GetNode( clip_string );
140     } else {
141       clipmin = clip_el->FindElementValueAsNumber("min");
142     }
143     clip_string = clip_el->FindElementValue("max");
144     if (clip_string.find_first_not_of("+-.0123456789") != string::npos) { // it's a property
145       if (clip_string[0] == '-') {
146         clipMaxSign = -1.0;
147         clip_string.erase(0,1);
148       }
149       ClipMaxPropertyNode = PropertyManager->GetNode( clip_string );
150     } else {
151       clipmax = clip_el->FindElementValueAsNumber("max");
152     }
153     clip = true;
154   }
155
156   Debug(0);
157 }
158
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
161 FGFCSComponent::~FGFCSComponent()
162 {
163   Debug(1);
164 }
165
166 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167
168 void FGFCSComponent::SetOutput(void)
169 {
170   OutputNode->setDoubleValue(Output);
171 }
172
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174
175 bool FGFCSComponent::Run(void)
176 {
177   return true;
178 }
179
180 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181
182 void FGFCSComponent::Clip(void)
183 {
184   if (clip) {
185     if (ClipMinPropertyNode != 0) clipmin = clipMinSign*ClipMinPropertyNode->getDoubleValue();
186     if (ClipMaxPropertyNode != 0) clipmax = clipMaxSign*ClipMaxPropertyNode->getDoubleValue();
187     if (Output > clipmax)      Output = clipmax;
188     else if (Output < clipmin) Output = clipmin;
189   }
190 }
191
192 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 //
194 // The old way of naming FCS components allowed upper or lower case, spaces, etc.
195 // but then the names were modified to fit into a property name heirarchy. This
196 // was confusing (it wasn't done intentionally - it was a carryover from the early
197 // design). We now support the direct naming of properties in the FCS component
198 // name attribute. The old way is supported in code at this time, but deprecated.
199
200 void FGFCSComponent::bind(void)
201 {
202   string tmp;
203   if (Name.find("/") == string::npos) {
204     tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
205   } else {
206     tmp = Name;
207   }
208   PropertyManager->Tie( tmp, this, &FGFCSComponent::GetOutput);
209 }
210
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 //    The bitmasked value choices are as follows:
213 //    unset: In this case (the default) JSBSim would only print
214 //       out the normally expected messages, essentially echoing
215 //       the config files as they are read. If the environment
216 //       variable is not set, debug_lvl is set to 1 internally
217 //    0: This requests JSBSim not to output any messages
218 //       whatsoever.
219 //    1: This value explicity requests the normal JSBSim
220 //       startup messages
221 //    2: This value asks for a message to be printed out when
222 //       a class is instantiated
223 //    4: When this value is set, a message is displayed when a
224 //       FGModel object executes its Run() method
225 //    8: When this value is set, various runtime state variables
226 //       are printed out periodically
227 //    16: When set various parameters are sanity checked and
228 //       a message is printed out when they go out of bounds
229
230 void FGFCSComponent::Debug(int from)
231 {
232   string propsign="";
233
234   if (debug_lvl <= 0) return;
235
236   if (debug_lvl & 1) { // Standard console startup message output
237     if (from == 0) {
238       cout << endl << "    Loading Component \"" << Name
239                    << "\" of type: " << Type << endl;
240
241       if (clip) {
242         if (ClipMinPropertyNode != 0L) {
243           if (clipMinSign < 0.0) propsign="-";
244           cout << "      Minimum limit: " << propsign << ClipMinPropertyNode->GetName() << endl;
245           propsign="";
246         } else {
247           cout << "      Minimum limit: " << clipmin << endl;
248         }
249         if (ClipMaxPropertyNode != 0L) {
250           if (clipMaxSign < 0.0) propsign="-";
251           cout << "      Maximum limit: " << propsign << ClipMaxPropertyNode->GetName() << endl;
252           propsign="";
253         } else {
254           cout << "      Maximum limit: " << clipmax << endl;
255         }
256       }  
257     }
258   }
259   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
260     if (from == 0) cout << "Instantiated: FGFCSComponent" << endl;
261     if (from == 1) cout << "Destroyed:    FGFCSComponent" << endl;
262   }
263   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
264   }
265   if (debug_lvl & 8 ) { // Runtime state variables
266   }
267   if (debug_lvl & 16) { // Sanity checking
268   }
269   if (debug_lvl & 64) {
270     if (from == 0) { // Constructor
271       cout << IdSrc << endl;
272       cout << IdHdr << endl;
273     }
274   }
275 }
276 }