Loading [MathJax]/extensions/tex2jax.js
JSBSim Flight Dynamics Model 1.2.2 (22 Mar 2025)
An Open Source Flight Dynamics and Control Software Library in C++
All Classes Functions Variables Enumerations Enumerator Friends Pages
FGSwitch.cpp
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
26FUNCTIONAL DESCRIPTION
27--------------------------------------------------------------------------------
28
29HISTORY
30--------------------------------------------------------------------------------
31
32%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33COMMENTS, REFERENCES, and NOTES
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36The switch component is defined as follows (see the API documentation for more
37information):
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
58Also, see the header file (FGSwitch.h) for further details.
59
60%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61INCLUDES
62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63
64#include "FGSwitch.h"
65#include "models/FGFCS.h"
66#include "math/FGCondition.h"
67#include "math/FGRealValue.h"
68
69using namespace std;
70
71namespace JSBSim {
72
73/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74CLASS IMPLEMENTATION
75%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76
77FGSwitch::FGSwitch(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
78{
79 string value;
80 Test *current_test;
81 auto PropertyManager = fcs->GetPropertyManager();
82
83 bind(element, PropertyManager.get()); // Bind() this component here in case it is used in its own
84 // definition for a sample-and-hold
85 Element* test_element = element->FindElement("default");
86 if (test_element) {
87 current_test = new Test;
88 value = test_element->GetAttributeValue("value");
89 current_test->setTestValue(value, Name, PropertyManager, test_element);
90 current_test->Default = true;
91 auto output_value = current_test->OutputValue.ptr();
92 if (delay > 0 && dynamic_cast<FGRealValue*>(output_value)) { // If there is a delay
93 double v = output_value->GetValue();
94 for (unsigned int i=0; i<delay-1; i++) { // Initialize the delay buffer to the default value
95 output_array[i] = v; // for the switch if that value is a number.
96 }
97 }
98 tests.push_back(current_test);
99 }
100
101 test_element = element->FindElement("test");
102 while (test_element) {
103 current_test = new Test;
104 current_test->condition = new FGCondition(test_element, PropertyManager);
105 value = test_element->GetAttributeValue("value");
106 current_test->setTestValue(value, Name, PropertyManager, test_element);
107 tests.push_back(current_test);
108 test_element = element->FindNextElement("test");
109 }
110
111 Debug(0);
112}
113
114//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
117{
118 for (auto test: tests) {
119 delete test->condition;
120 delete test;
121 }
122
123 Debug(1);
124}
125
126//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127
128bool FGSwitch::Run(void )
129{
130 bool pass = false;
131 double default_output=0.0;
132
133 // To detect errors early, make sure all conditions and values can be
134 // evaluated in the first time step.
135 if (!initialized) {
136 initialized = true;
137 VerifyProperties();
138 }
139
140 for (auto test: tests) {
141 if (test->Default) {
142 default_output = test->OutputValue->GetValue();
143 } else {
144 pass = test->condition->Evaluate();
145 }
146
147 if (pass) {
148 Output = test->OutputValue->GetValue();
149 break;
150 }
151 }
152
153 if (!pass) Output = default_output;
154
155 if (delay != 0) Delay();
156 Clip();
157 SetOutput();
158
159 return true;
160}
161
162//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163
164void FGSwitch::VerifyProperties(void)
165{
166 for (auto test: tests) {
167 if (!test->Default) {
168 test->condition->Evaluate();
169 }
170 test->OutputValue->GetValue();
171 }
172}
173
174//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175// The bitmasked value choices are as follows:
176// unset: In this case (the default) JSBSim would only print
177// out the normally expected messages, essentially echoing
178// the config files as they are read. If the environment
179// variable is not set, debug_lvl is set to 1 internally
180// 0: This requests JSBSim not to output any messages
181// whatsoever.
182// 1: This value explicity requests the normal JSBSim
183// startup messages
184// 2: This value asks for a message to be printed out when
185// a class is instantiated
186// 4: When this value is set, a message is displayed when a
187// FGModel object executes its Run() method
188// 8: When this value is set, various runtime state variables
189// are printed out periodically
190// 16: When set various parameters are sanity checked and
191// a message is printed out when they go out of bounds
192
193void FGSwitch::Debug(int from)
194{
195 if (debug_lvl <= 0) return;
196
197 if (debug_lvl & 1) { // Standard console startup message output
198 if (from == 0) { // Constructor
199 unsigned int i = 0;
200 for (auto test: tests) {
201 if (test->Default) {
202 cout << " Switch default value is: " << test->GetOutputName();
203 } else {
204 cout << " Switch takes test " << i << " value (" << test->GetOutputName() << ")" << endl;
205
206 test->condition->PrintCondition(" ");
207 }
208 cout << endl;
209 ++i;
210 }
211 for (auto node: OutputNodes)
212 cout << " OUTPUT: " << node->getNameString() << endl;
213 }
214 }
215 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
216 if (from == 0) cout << "Instantiated: FGSwitch" << endl;
217 if (from == 1) cout << "Destroyed: FGSwitch" << endl;
218 }
219 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
220 }
221 if (debug_lvl & 8 ) { // Runtime state variables
222 }
223 if (debug_lvl & 16) { // Sanity checking
224 }
225 if (debug_lvl & 64) {
226 if (from == 0) { // Constructor
227 }
228 }
229}
230
231} //namespace JSBSim
Element * FindElement(const std::string &el="")
Searches for a specified element.
std::string GetAttributeValue(const std::string &key)
Retrieves an attribute.
Element * FindNextElement(const std::string &el="")
Searches for the next element as specified.
Encapsulates a condition, which is used in parts of JSBSim including switches.
Definition FGCondition.h:65
Base class for JSBSim Flight Control System Components.
Encapsulates the Flight Control System (FCS) functionality.
Definition FGFCS.h:189
Represents a real value.
Definition FGRealValue.h:58
FGSwitch(FGFCS *fcs, Element *element)
Constructor.
Definition FGSwitch.cpp:77
~FGSwitch()
Destructor.
Definition FGSwitch.cpp:116
bool Run(void) override
Executes the switch logic.
Definition FGSwitch.cpp:128