JSBSim Flight Dynamics Model  1.1.11 (13 Feb 2022)
An Open Source Flight Dynamics and Control Software Library in C++
FGSensor.cpp
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 
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
28 
29 HISTORY
30 --------------------------------------------------------------------------------
31 
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES, and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 
40 #include "FGSensor.h"
41 #include "input_output/FGXMLElement.h"
42 
43 using namespace std;
44 
45 namespace JSBSim {
46 
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50 
51 
52 FGSensor::FGSensor(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
53 {
54  // inputs are read from the base class constructor
55 
56  bits = quantized = divisions = 0;
57  PreviousInput = PreviousOutput = 0.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;
77  granularity = span/divisions;
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") {
100  NoiseType = eAbsolute;
101  } else {
102  NoiseType = ePercent;
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") {
108  DistributionType = eUniform;
109  } else if (distribution == "GAUSSIAN") {
110  DistributionType = eGaussian;
111  } else {
112  DistributionType = eUniform;
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 
125 FGSensor::~FGSensor()
126 {
127  Debug(1);
128 }
129 
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 
132 void FGSensor::ResetPastStates(void)
133 {
134  FGFCSComponent::ResetPastStates();
135 
136  PreviousOutput = PreviousInput = Output = 0.0;
137 }
138 
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 
141 bool FGSensor::Run(void)
142 {
143  Input = InputNodes[0]->getDoubleValue();
144 
145  ProcessSensorSignal();
146 
147  SetOutput();
148 
149  return true;
150 }
151 
152 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 
154 void FGSensor::ProcessSensorSignal(void)
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 
180 void FGSensor::Noise(void)
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 
203 void FGSensor::Bias(void)
204 {
205  Output += bias;
206 }
207 
208 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 
210 void FGSensor::Gain(void)
211 {
212  Output *= gain;
213 }
214 
215 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 
217 void FGSensor::Drift(void)
218 {
219  drift += drift_rate*dt;
220  Output += drift;
221 }
222 
223 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 
225 void FGSensor::Quantize(void)
226 {
227  if (Output < min) Output = min;
228  if (Output > max) Output = max;
229  double portion = Output - min;
230  quantized = (int)(portion/granularity);
231  Output = quantized*granularity + min;
232 }
233 
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 
236 void FGSensor::Lag(void)
237 {
238  // "Output" on the right side of the "=" is the current input
239  Output = ca * (Output + PreviousInput) + PreviousOutput * cb;
240 
241  PreviousOutput = Output;
242  PreviousInput = Input;
243 }
244 
245 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 
247 void FGSensor::bind(Element* el)
248 {
249  string tmp = Name;
250 
251  FGFCSComponent::bind(el);
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 
260  PropertyManager->Tie( tmp_low, this, &FGSensor::GetFailLow, &FGSensor::SetFailLow);
261  PropertyManager->Tie( tmp_high, this, &FGSensor::GetFailHigh, &FGSensor::SetFailHigh);
262  PropertyManager->Tie( tmp_stuck, this, &FGSensor::GetFailStuck, &FGSensor::SetFailStuck);
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 
299 void 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 }
JSBSim::FGSensor::PreviousOutput
double PreviousOutput
lag filter coefficient "b"
Definition: FGSensor.h:159
JSBSim::FGPropertyManager::mkPropertyName
std::string mkPropertyName(std::string name, bool lowercase)
Property-ify a name replaces spaces with '-' and, optionally, makes name all lower case.
Definition: FGPropertyManager.cpp:64
JSBSim::FGPropertyManager::Tie
void Tie(const std::string &name, T *pointer)
Tie a property to an external variable.
Definition: FGPropertyManager.h:449
JSBSim::FGSensor::cb
double cb
lag filter coefficient "a"
Definition: FGSensor.h:158