Debian Codegraph @

src/FDM/JSBSim/models/flight_control/FGSwitch.cpp

raw symbols SCIP
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGSwitch.cpp
4 Author: Jon S. Berndt
5 Date started: 4/2000
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
11 Software Foundation; either version 2 of the License, or (at your option) any
12 later 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
20 with this program; if not, write to the Free Software Foundation, Inc., 59
21 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 Further information about the GNU Lesser General Public License can also be
24 found on 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 The switch component is defined as follows (see the API documentation for more
37 information):
38
39 @code
40 <switch name="switch1">
41 <default value="{property|value}"/>
42 <test logic="{AND|OR}" value="{property|value}">
43 {property} {conditional} {property|value}
44 <test logic="{AND|OR}">
45 {property} {conditional} {property|value}
46 ...
47 </test>
48 ...
49 </test>
50 <test logic="{AND|OR}" value="{property|value}">
51 {property} {conditional} {property|value}
52 ...
53 </test>
54 ...
55 </switch>
56 @endcode
57
58 Also, see the header file (FGSwitch.h) for further details.
59
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 INCLUDES
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63
64 #include "FGSwitch.h"
65 #include "math/FGCondition.h"
66
67 using namespace std;
68
69 namespace JSBSim {
70
71 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 CLASS IMPLEMENTATION
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
74
75 FGSwitch::FGSwitch(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
76 {
77 string value;
78 Test *current_test;
79
80 bind(element); // Bind() this component here in case it is used in its own
81 // definition for a sample-and-hold
82 Element* test_element = element->FindElement("default");
83 if (test_element) {
84 current_test = new Test;
85 value = test_element->GetAttributeValue("value");
86 current_test->setTestValue(value, Name, PropertyManager);
87 current_test->Default = true;
88 if (delay > 0 && is_number(trim(value))) { // If there is a delay, initialize the
89 double v = atof_locale_c(value);
90 for (unsigned int i=0; i<delay-1; i++) { // delay buffer to the default value
91 output_array[i] = v; // for the switch if that value is a number.
92 }
93 }
94 tests.push_back(current_test);
95 }
96
97 test_element = element->FindElement("test");
98 while (test_element) {
99 current_test = new Test;
100 current_test->condition = new FGCondition(test_element, PropertyManager);
101 value = test_element->GetAttributeValue("value");
102 current_test->setTestValue(value, Name, PropertyManager);
103 tests.push_back(current_test);
104 test_element = element->FindNextElement("test");
105 }
106
107 Debug(0);
108 }
109
110 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111
112 FGSwitch::~FGSwitch()
113 {
114 for (auto test: tests) {
115 delete test->condition;
116 delete test;
117 }
118
119 Debug(1);
120 }
121
122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123
124 bool FGSwitch::Run(void )
125 {
126 bool pass = false;
127 double default_output=0.0;
128
129 // To detect errors early, make sure all conditions and values can be
130 // evaluated in the first time step.
131 if (!initialized) {
132 initialized = true;
133 VerifyProperties();
134 }
135
136 for (auto test: tests) {
137 if (test->Default) {
138 default_output = test->OutputValue->GetValue();
139 } else {
140 pass = test->condition->Evaluate();
141 }
142
143 if (pass) {
144 Output = test->OutputValue->GetValue();
145 break;
146 }
147 }
148
149 if (!pass) Output = default_output;
150
151 if (delay != 0) Delay();
152 Clip();
153 SetOutput();
154
155 return true;
156 }
157
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159
160 void FGSwitch::VerifyProperties(void)
161 {
162 for (auto test: tests) {
163 if (!test->Default) {
164 test->condition->Evaluate();
165 }
166 test->OutputValue->GetValue();
167 }
168 }
169
170 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171 // The bitmasked value choices are as follows:
172 // unset: In this case (the default) JSBSim would only print
173 // out the normally expected messages, essentially echoing
174 // the config files as they are read. If the environment
175 // variable is not set, debug_lvl is set to 1 internally
176 // 0: This requests JSBSim not to output any messages
177 // whatsoever.
178 // 1: This value explicity requests the normal JSBSim
179 // startup messages
180 // 2: This value asks for a message to be printed out when
181 // a class is instantiated
182 // 4: When this value is set, a message is displayed when a
183 // FGModel object executes its Run() method
184 // 8: When this value is set, various runtime state variables
185 // are printed out periodically
186 // 16: When set various parameters are sanity checked and
187 // a message is printed out when they go out of bounds
188
189 void FGSwitch::Debug(int from)
190 {
191 if (debug_lvl <= 0) return;
192
193 if (debug_lvl & 1) { // Standard console startup message output
194 if (from == 0) { // Constructor
195 unsigned int i = 0;
196 for (auto test: tests) {
197 if (test->Default) {
198 cout << " Switch default value is: " << test->GetOutputName();
199 } else {
200 cout << " Switch takes test " << i << " value (" << test->GetOutputName() << ")" << endl;
201
202 test->condition->PrintCondition(" ");
203 }
204 cout << endl;
205 ++i;
206 }
207 for (auto node: OutputNodes)
208 cout << " OUTPUT: " << node->getNameString() << endl;
209 }
210 }
211 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
212 if (from == 0) cout << "Instantiated: FGSwitch" << endl;
213 if (from == 1) cout << "Destroyed: FGSwitch" << endl;
214 }
215 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
216 }
217 if (debug_lvl & 8 ) { // Runtime state variables
218 }
219 if (debug_lvl & 16) { // Sanity checking
220 }
221 if (debug_lvl & 64) {
222 if (from == 0) { // Constructor
223 }
224 }
225 }
226
227 } //namespace JSBSim
228