]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGFCSComponent.cpp
sync. with JSBSim v. 2.0
[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 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 General Public License for more
17  details.
18
19  You should have received a copy of the GNU 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 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   IsOutput   = clip = false;
58   string input, clip_string;
59
60   PropertyManager = fcs->GetPropertyManager();
61   Type = element->GetAttributeValue("type"); // Old, deprecated format
62   if (Type.empty()) {
63     if        (element->GetName() == string("lag_filter")) {
64       Type = "LAG_FILTER";
65     } else if (element->GetName() == string("lead_lag_filter")) {
66       Type = "LEAD_LAG_FILTER";
67     } else if (element->GetName() == string("washout_filter")) {
68       Type = "WASHOUT_FILTER";
69     } else if (element->GetName() == string("second_order_filter")) {
70       Type = "SECOND_ORDER_FILTER";
71     } else if (element->GetName() == string("integrator")) {
72       Type = "INTEGRATOR";
73     } else if (element->GetName() == string("summer")) {
74       Type = "SUMMER";
75     } else if (element->GetName() == string("pure_gain")) {
76       Type = "PURE_GAIN";
77     } else if (element->GetName() == string("scheduled_gain")) {
78       Type = "SCHEDULED_GAIN";
79     } else if (element->GetName() == string("aerosurface_scale")) {
80       Type = "AEROSURFACE_SCALE";
81     } else if (element->GetName() == string("switch")) {
82       Type = "SWITCH";
83     } else if (element->GetName() == string("kinematic")) {
84       Type = "KINEMATIC";
85     } else if (element->GetName() == string("deadband")) {
86       Type = "DEADBAND";
87     } else if (element->GetName() == string("fcs_function")) {
88       Type = "FCS_FUNCTION";
89     } else if (element->GetName() == string("sensor")) {
90       Type = "SENSOR";
91     } else { // illegal component in this channel
92       Type = "UNKNOWN";
93     }
94   }
95
96   Name = element->GetAttributeValue("name");
97
98   input_element = element->FindElement("input");
99   while (input_element) {
100     input = input_element->GetDataLine();
101     if (input[0] == '-') {
102       InputSigns.push_back(-1.0);
103       input.erase(0,1);
104     } else {
105       InputSigns.push_back( 1.0);
106     }
107     InputNodes.push_back( resolveSymbol(input) );
108     input_element = element->FindNextElement("input");
109   }
110
111   if (element->FindElement("output")) {
112     IsOutput = true;
113     OutputNode = PropertyManager->GetNode( element->FindElementValue("output") );
114     if (!OutputNode) {
115       cerr << endl << "  Unable to process property: " << element->FindElementValue("output") << endl;
116       throw(string("Invalid output property name in flight control definition"));
117     }
118   }
119
120   clip_el = element->FindElement("clipto");
121   if (clip_el) {
122     clip_string = clip_el->FindElementValue("min");
123     if (clip_string.find_first_not_of("+-.0123456789") != string::npos) { // it's a property
124       ClipMinPropertyNode = PropertyManager->GetNode( clip_string );
125     } else {
126       clipmin = clip_el->FindElementValueAsNumber("min");
127     }
128     clip_string = clip_el->FindElementValue("max");
129     if (clip_string.find_first_not_of("+-.0123456789") != string::npos) { // it's a property
130       ClipMaxPropertyNode = PropertyManager->GetNode( clip_string );
131     } else {
132       clipmax = clip_el->FindElementValueAsNumber("max");
133     }
134     clip = true;
135   }
136
137   Debug(0);
138 }
139
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141
142 FGFCSComponent::~FGFCSComponent()
143 {
144 //  string tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
145 //  PropertyManager->Untie( tmp);
146
147   Debug(1);
148 }
149
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152 void FGFCSComponent::SetOutput(void)
153 {
154   OutputNode->setDoubleValue(Output);
155 }
156
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158
159 bool FGFCSComponent::Run(void)
160 {
161   return true;
162 }
163
164 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165
166 void FGFCSComponent::Clip(void)
167 {
168   if (clip) {
169     if (ClipMinPropertyNode != 0) clipmin = ClipMinPropertyNode->getDoubleValue();
170     if (ClipMaxPropertyNode != 0) clipmax = ClipMaxPropertyNode->getDoubleValue();
171     if (Output > clipmax)      Output = clipmax;
172     else if (Output < clipmin) Output = clipmin;
173   }
174 }
175
176 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177
178 FGPropertyManager* FGFCSComponent::resolveSymbol(string token)
179 {
180   string prop;
181   FGPropertyManager* tmp = PropertyManager->GetNode(token,false);
182   if (!tmp) {
183     if (token.find("/") == token.npos) prop = "model/" + token;
184     cerr << "Creating new property " << prop << endl;
185     tmp = PropertyManager->GetNode(token,true);
186   }
187   return tmp;
188 }
189
190
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192
193 void FGFCSComponent::bind(void)
194 {
195   string tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
196   PropertyManager->Tie( tmp, this, &FGFCSComponent::GetOutput);
197 }
198
199 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 //    The bitmasked value choices are as follows:
201 //    unset: In this case (the default) JSBSim would only print
202 //       out the normally expected messages, essentially echoing
203 //       the config files as they are read. If the environment
204 //       variable is not set, debug_lvl is set to 1 internally
205 //    0: This requests JSBSim not to output any messages
206 //       whatsoever.
207 //    1: This value explicity requests the normal JSBSim
208 //       startup messages
209 //    2: This value asks for a message to be printed out when
210 //       a class is instantiated
211 //    4: When this value is set, a message is displayed when a
212 //       FGModel object executes its Run() method
213 //    8: When this value is set, various runtime state variables
214 //       are printed out periodically
215 //    16: When set various parameters are sanity checked and
216 //       a message is printed out when they go out of bounds
217
218 void FGFCSComponent::Debug(int from)
219 {
220   if (debug_lvl <= 0) return;
221
222   if (debug_lvl & 1) { // Standard console startup message output
223     if (from == 0) {
224       cout << endl << "    Loading Component \"" << Name
225                    << "\" of type: " << Type << endl;
226     }
227   }
228   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
229     if (from == 0) cout << "Instantiated: FGFCSComponent" << endl;
230     if (from == 1) cout << "Destroyed:    FGFCSComponent" << endl;
231   }
232   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
233   }
234   if (debug_lvl & 8 ) { // Runtime state variables
235   }
236   if (debug_lvl & 16) { // Sanity checking
237   }
238   if (debug_lvl & 64) {
239     if (from == 0) { // Constructor
240       cout << IdSrc << endl;
241       cout << IdHdr << endl;
242     }
243   }
244 }
245 }