FlightGear next
FGSensor.cpp
Go to the documentation of this file.
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGSensor.cpp
4 Author: Jon Berndt
5 Date started: 9 July 2005
6
7 ------------- Copyright (C) 2005 -------------
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
36%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37INCLUDES
38%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40#include "FGSensor.h"
42
43using namespace std;
44
45namespace JSBSim {
46
47/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48CLASS IMPLEMENTATION
49%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51
53{
54 // inputs are read from the base class constructor
55
56 bits = quantized = divisions = 0;
58 min = max = bias = gain = noise_variance = lag = drift_rate = drift = span = 0.0;
59 granularity = 0.0;
60 noise_type = 0;
61 fail_low = fail_high = fail_stuck = false;
62
63 Element* quantization_element = element->FindElement("quantization");
64 if ( quantization_element) {
65 if ( quantization_element->FindElement("bits") ) {
66 bits = (int)quantization_element->FindElementValueAsNumber("bits");
67 }
68 divisions = (1<<bits);
69 if ( quantization_element->FindElement("min") ) {
70 min = quantization_element->FindElementValueAsNumber("min");
71 }
72 if ( quantization_element->FindElement("max") ) {
73 max = quantization_element->FindElementValueAsNumber("max");
74 }
75 quant_property = quantization_element->GetAttributeValue("name");
76 span = max - min;
78 }
79 if ( element->FindElement("bias") ) {
80 bias = element->FindElementValueAsNumber("bias");
81 }
82 if ( element->FindElement("gain") ) {
83 gain = element->FindElementValueAsNumber("gain");
84 }
85 if ( element->FindElement("drift_rate") ) {
86 drift_rate = element->FindElementValueAsNumber("drift_rate");
87 }
88 if ( element->FindElement("lag") ) {
89 lag = element->FindElementValueAsNumber("lag");
90 double denom = 2.00 + dt*lag;
91 ca = dt*lag / denom;
92 cb = (2.00 - dt*lag) / denom;
93 }
94 if ( element->FindElement("noise") ) {
95 noise_variance = element->FindElementValueAsNumber("noise");
96 string variation = element->FindElement("noise")->GetAttributeValue("variation");
97 if (variation == "PERCENT") {
98 NoiseType = ePercent;
99 } else if (variation == "ABSOLUTE") {
101 } else {
103 cerr << "Unknown noise type in sensor: " << Name << endl;
104 cerr << " defaulting to PERCENT." << endl;
105 }
106 string distribution = element->FindElement("noise")->GetAttributeValue("distribution");
107 if (distribution == "UNIFORM") {
109 } else if (distribution == "GAUSSIAN") {
111 } else {
113 cerr << "Unknown random distribution type in sensor: " << Name << endl;
114 cerr << " defaulting to UNIFORM." << endl;
115 }
116 }
117
118 bind(element);
119
120 Debug(0);
121}
122
123//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124
126{
127 Debug(1);
128}
129
130//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131
138
139//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140
142{
143 Input = InputNodes[0]->getDoubleValue();
144
146
147 SetOutput();
148
149 return true;
150}
151
152//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153
155{
156 // Degrade signal as specified
157
158 if (!fail_stuck) {
159 Output = Input; // perfect sensor
160
161 if (lag != 0.0) Lag(); // models sensor lag and filter
162 if (noise_variance != 0.0) Noise(); // models noise
163 if (drift_rate != 0.0) Drift(); // models drift over time
164 if (gain != 0.0) Gain(); // models a finite gain
165 if (bias != 0.0) Bias(); // models a finite bias
166
167 if (delay != 0) Delay(); // models system signal transport latencies
168
169 if (fail_low) Output = -HUGE_VAL;
170 if (fail_high) Output = HUGE_VAL;
171
172 if (bits != 0) Quantize(); // models quantization degradation
173
174 Clip();
175 }
176}
177
178//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179
181{
182 double random_value=0.0;
183
184 if (DistributionType == eUniform) {
185 random_value = 2.0*(((double)rand()/(double)RAND_MAX) - 0.5);
186 } else {
187 random_value = GaussianRandomNumber();
188 }
189
190 switch( NoiseType ) {
191 case ePercent:
192 Output *= (1.0 + noise_variance*random_value);
193 break;
194
195 case eAbsolute:
196 Output += noise_variance*random_value;
197 break;
198 }
199}
200
201//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202
204{
205 Output += bias;
206}
207
208//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209
211{
212 Output *= gain;
213}
214
215//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216
218{
220 Output += drift;
221}
222
223//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224
226{
227 if (Output < min) Output = min;
228 if (Output > max) Output = max;
229 double portion = Output - min;
230 quantized = (int)(portion/granularity);
232}
233
234//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235
237{
238 // "Output" on the right side of the "=" is the current input
240
243}
244
245//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246
248{
249 string tmp = Name;
250
252
253 if (Name.find("/") == string::npos) {
254 tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
255 }
256 const string tmp_low = tmp + "/malfunction/fail_low";
257 const string tmp_high = tmp + "/malfunction/fail_high";
258 const string tmp_stuck = tmp + "/malfunction/fail_stuck";
259
263
264 if (!quant_property.empty()) {
265 if (quant_property.find("/") == string::npos) { // not found
266 string qprop = "fcs/" + PropertyManager->mkPropertyName(quant_property, true);
267 FGPropertyNode* node = PropertyManager->GetNode(qprop, true);
268 if (node->isTied()) {
269 cerr << el->ReadFrom()
270 << "Property " << tmp << " has already been successfully bound (late)." << endl;
271 throw("Failed to bind the property to an existing already tied node.");
272 }
273 else
274 PropertyManager->Tie(qprop, this, &FGSensor::GetQuantized);
275 }
276 }
277
278}
279
280//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281// The bitmasked value choices are as follows:
282// unset: In this case (the default) JSBSim would only print
283// out the normally expected messages, essentially echoing
284// the config files as they are read. If the environment
285// variable is not set, debug_lvl is set to 1 internally
286// 0: This requests JSBSim not to output any messages
287// whatsoever.
288// 1: This value explicity requests the normal JSBSim
289// startup messages
290// 2: This value asks for a message to be printed out when
291// a class is instantiated
292// 4: When this value is set, a message is displayed when a
293// FGModel object executes its Run() method
294// 8: When this value is set, various runtime state variables
295// are printed out periodically
296// 16: When set various parameters are sanity checked and
297// a message is printed out when they go out of bounds
298
299void FGSensor::Debug(int from)
300{
301 if (debug_lvl <= 0) return;
302
303 if (debug_lvl & 1) { // Standard console startup message output
304 if (from == 0) { // Constructor
305 if (!InputNodes.empty())
306 cout << " INPUT: " << InputNodes[0]->GetNameWithSign() << endl;
307 if (bits != 0) {
308 if (quant_property.empty())
309 cout << " Quantized output" << endl;
310 else
311 cout << " Quantized output (property: " << quant_property << ")" << endl;
312
313 cout << " Bits: " << bits << endl;
314 cout << " Min value: " << min << endl;
315 cout << " Max value: " << max << endl;
316 cout << " (span: " << span << ", granularity: " << granularity << ")" << endl;
317 }
318 if (bias != 0.0) cout << " Bias: " << bias << endl;
319 if (gain != 0.0) cout << " Gain: " << gain << endl;
320 if (drift_rate != 0) cout << " Sensor drift rate: " << drift_rate << endl;
321 if (lag != 0) cout << " Sensor lag: " << lag << endl;
322 if (noise_variance != 0) {
323 if (NoiseType == eAbsolute) {
324 cout << " Noise variance (absolute): " << noise_variance << endl;
325 } else if (NoiseType == ePercent) {
326 cout << " Noise variance (percent): " << noise_variance << endl;
327 } else {
328 cout << " Noise variance type is invalid" << endl;
329 }
330 if (DistributionType == eUniform) {
331 cout << " Random noise is uniformly distributed." << endl;
332 } else if (DistributionType == eGaussian) {
333 cout << " Random noise is gaussian distributed." << endl;
334 }
335 }
336 for (auto node: OutputNodes)
337 cout << " OUTPUT: " << node->getNameString() << endl;
338 }
339 }
340 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
341 if (from == 0) cout << "Instantiated: FGSensor" << endl;
342 if (from == 1) cout << "Destroyed: FGSensor" << endl;
343 }
344 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
345 }
346 if (debug_lvl & 8 ) { // Runtime state variables
347 }
348 if (debug_lvl & 16) { // Sanity checking
349 }
350 if (debug_lvl & 64) {
351 if (from == 0) { // Constructor
352 }
353 }
354}
355}
#define min(X, Y)
double FindElementValueAsNumber(const std::string &el="")
Searches for the named element and returns the data belonging to it as a number.
std::string ReadFrom(void) const
Return a string that contains a description of the location where the current XML element was read fr...
std::string GetAttributeValue(const std::string &key)
Retrieves an attribute.
Element * FindElement(const std::string &el="")
Searches for a specified element.
FGFCSComponent(FGFCS *fcs, Element *el)
Constructor.
std::vector< FGPropertyValue_ptr > InputNodes
virtual void ResetPastStates(void)
FGPropertyManager * PropertyManager
virtual void bind(Element *el)
virtual void SetOutput(void)
static double GaussianRandomNumber(void)
Class wrapper for property handling.
double PreviousInput
Definition FGSensor.h:160
std::string quant_property
Definition FGSensor.h:168
double GetFailLow(void) const
Definition FGSensor.h:137
virtual ~FGSensor()
Definition FGSensor.cpp:125
double drift_rate
Definition FGSensor.h:152
double cb
lag filter coefficient "a"
Definition FGSensor.h:158
void Noise(void)
Definition FGSensor.cpp:180
double granularity
Definition FGSensor.h:156
bool Run(void) override
Definition FGSensor.cpp:141
void Quantize(void)
Definition FGSensor.cpp:225
void bind(Element *el) override
Definition FGSensor.cpp:247
void SetFailStuck(double val)
Definition FGSensor.h:135
enum JSBSim::FGSensor::eDistributionType DistributionType
void SetFailLow(double val)
Definition FGSensor.h:133
double noise_variance
Definition FGSensor.h:154
double GetFailHigh(void) const
Definition FGSensor.h:138
enum JSBSim::FGSensor::eNoiseType NoiseType
double PreviousOutput
lag filter coefficient "b"
Definition FGSensor.h:159
FGSensor(FGFCS *fcs, Element *element)
Definition FGSensor.cpp:52
int GetQuantized(void) const
Definition FGSensor.h:140
double GetFailStuck(void) const
Definition FGSensor.h:139
void Gain(void)
Definition FGSensor.cpp:210
void Lag(void)
Definition FGSensor.cpp:236
void ResetPastStates(void) override
Definition FGSensor.cpp:132
void ProcessSensorSignal(void)
Definition FGSensor.cpp:154
void Bias(void)
Definition FGSensor.cpp:203
void SetFailHigh(double val)
Definition FGSensor.h:134
void Drift(void)
Definition FGSensor.cpp:217
short debug_lvl