JSBSim Flight Dynamics Model  1.0 (02 March 2017)
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 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 Lesser General Public License for more
17  details.
18 
19  You should have received a copy of the GNU Lesser 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 Lesser 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 <iostream>
41 #include <cstdlib>
42 
43 #include "FGSensor.h"
44 #include "input_output/FGXMLElement.h"
45 
46 using namespace std;
47 
48 namespace JSBSim {
49 
50 IDENT(IdSrc,"$Id: FGSensor.cpp,v 1.28 2015/07/12 19:34:08 bcoconni Exp $");
51 IDENT(IdHdr,ID_SENSOR);
52 
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 CLASS IMPLEMENTATION
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 
57 
58 FGSensor::FGSensor(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
59 {
60  // inputs are read from the base class constructor
61 
62  bits = quantized = divisions = 0;
63  PreviousInput = PreviousOutput = 0.0;
64  min = max = bias = gain = noise_variance = lag = drift_rate = drift = span = 0.0;
65  granularity = 0.0;
66  noise_type = 0;
67  fail_low = fail_high = fail_stuck = false;
68 
69  Element* quantization_element = element->FindElement("quantization");
70  if ( quantization_element) {
71  if ( quantization_element->FindElement("bits") ) {
72  bits = (int)quantization_element->FindElementValueAsNumber("bits");
73  }
74  divisions = (1<<bits);
75  if ( quantization_element->FindElement("min") ) {
76  min = quantization_element->FindElementValueAsNumber("min");
77  }
78  if ( quantization_element->FindElement("max") ) {
79  max = quantization_element->FindElementValueAsNumber("max");
80  }
81  quant_property = quantization_element->GetAttributeValue("name");
82  span = max - min;
83  granularity = span/divisions;
84  }
85  if ( element->FindElement("bias") ) {
86  bias = element->FindElementValueAsNumber("bias");
87  }
88  if ( element->FindElement("gain") ) {
89  gain = element->FindElementValueAsNumber("gain");
90  }
91  if ( element->FindElement("drift_rate") ) {
92  drift_rate = element->FindElementValueAsNumber("drift_rate");
93  }
94  if ( element->FindElement("lag") ) {
95  lag = element->FindElementValueAsNumber("lag");
96  double denom = 2.00 + dt*lag;
97  ca = dt*lag / denom;
98  cb = (2.00 - dt*lag) / denom;
99  }
100  if ( element->FindElement("noise") ) {
101  noise_variance = element->FindElementValueAsNumber("noise");
102  string variation = element->FindElement("noise")->GetAttributeValue("variation");
103  if (variation == "PERCENT") {
104  NoiseType = ePercent;
105  } else if (variation == "ABSOLUTE") {
106  NoiseType = eAbsolute;
107  } else {
108  NoiseType = ePercent;
109  cerr << "Unknown noise type in sensor: " << Name << endl;
110  cerr << " defaulting to PERCENT." << endl;
111  }
112  string distribution = element->FindElement("noise")->GetAttributeValue("distribution");
113  if (distribution == "UNIFORM") {
114  DistributionType = eUniform;
115  } else if (distribution == "GAUSSIAN") {
116  DistributionType = eGaussian;
117  } else {
118  DistributionType = eUniform;
119  cerr << "Unknown random distribution type in sensor: " << Name << endl;
120  cerr << " defaulting to UNIFORM." << endl;
121  }
122  }
123 
124  FGFCSComponent::bind();
125  bind();
126 
127  Debug(0);
128 }
129 
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 
132 FGSensor::~FGSensor()
133 {
134  Debug(1);
135 }
136 
137 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 
139 void FGSensor::ResetPastStates(void)
140 {
141  FGFCSComponent::ResetPastStates();
142 
143  PreviousOutput = PreviousInput = Output = 0.0;
144 }
145 
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 
148 bool FGSensor::Run(void)
149 {
150  Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
151 
152  ProcessSensorSignal();
153 
154  if (IsOutput) SetOutput();
155 
156  return true;
157 }
158 
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 
161 void FGSensor::ProcessSensorSignal(void)
162 {
163  Output = Input; // perfect sensor
164 
165  // Degrade signal as specified
166 
167  if (fail_stuck) {
168  Output = PreviousOutput;
169  } else {
170  if (lag != 0.0) Lag(); // models sensor lag and filter
171  if (noise_variance != 0.0) Noise(); // models noise
172  if (drift_rate != 0.0) Drift(); // models drift over time
173  if (gain != 0.0) Gain(); // models a finite gain
174  if (bias != 0.0) Bias(); // models a finite bias
175 
176  if (delay != 0) Delay(); // models system signal transport latencies
177 
178  if (fail_low) Output = -HUGE_VAL;
179  if (fail_high) Output = HUGE_VAL;
180 
181  if (bits != 0) Quantize(); // models quantization degradation
182 
183  Clip();
184  }
185 }
186 
187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 
189 void FGSensor::Noise(void)
190 {
191  double random_value=0.0;
192 
193  if (DistributionType == eUniform) {
194  random_value = 2.0*(((double)rand()/(double)RAND_MAX) - 0.5);
195  } else {
196  random_value = GaussianRandomNumber();
197  }
198 
199  switch( NoiseType ) {
200  case ePercent:
201  Output *= (1.0 + noise_variance*random_value);
202  break;
203 
204  case eAbsolute:
205  Output += noise_variance*random_value;
206  break;
207  }
208 }
209 
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211 
212 void FGSensor::Bias(void)
213 {
214  Output += bias;
215 }
216 
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 
219 void FGSensor::Gain(void)
220 {
221  Output *= gain;
222 }
223 
224 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 
226 void FGSensor::Drift(void)
227 {
228  drift += drift_rate*dt;
229  Output += drift;
230 }
231 
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 
234 void FGSensor::Quantize(void)
235 {
236  if (Output < min) Output = min;
237  if (Output > max) Output = max;
238  double portion = Output - min;
239  quantized = (int)(portion/granularity);
240  Output = quantized*granularity + min;
241 }
242 
243 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 
245 void FGSensor::Lag(void)
246 {
247  // "Output" on the right side of the "=" is the current input
248  Output = ca * (Output + PreviousInput) + PreviousOutput * cb;
249 
250  PreviousOutput = Output;
251  PreviousInput = Input;
252 }
253 
254 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 
256 void FGSensor::bind(void)
257 {
258  string tmp = Name;
259  if (Name.find("/") == string::npos) {
260  tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
261  }
262  const string tmp_low = tmp + "/malfunction/fail_low";
263  const string tmp_high = tmp + "/malfunction/fail_high";
264  const string tmp_stuck = tmp + "/malfunction/fail_stuck";
265 
266  PropertyManager->Tie( tmp_low, this, &FGSensor::GetFailLow, &FGSensor::SetFailLow);
267  PropertyManager->Tie( tmp_high, this, &FGSensor::GetFailHigh, &FGSensor::SetFailHigh);
268  PropertyManager->Tie( tmp_stuck, this, &FGSensor::GetFailStuck, &FGSensor::SetFailStuck);
269 
270  if (!quant_property.empty()) {
271  if (quant_property.find("/") == string::npos) { // not found
272  string qprop = "fcs/" + PropertyManager->mkPropertyName(quant_property, true);
273  PropertyManager->Tie(qprop, this, &FGSensor::GetQuantized);
274  }
275  }
276 
277 }
278 
279 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 // The bitmasked value choices are as follows:
281 // unset: In this case (the default) JSBSim would only print
282 // out the normally expected messages, essentially echoing
283 // the config files as they are read. If the environment
284 // variable is not set, debug_lvl is set to 1 internally
285 // 0: This requests JSBSim not to output any messages
286 // whatsoever.
287 // 1: This value explicity requests the normal JSBSim
288 // startup messages
289 // 2: This value asks for a message to be printed out when
290 // a class is instantiated
291 // 4: When this value is set, a message is displayed when a
292 // FGModel object executes its Run() method
293 // 8: When this value is set, various runtime state variables
294 // are printed out periodically
295 // 16: When set various parameters are sanity checked and
296 // a message is printed out when they go out of bounds
297 
298 void FGSensor::Debug(int from)
299 {
300  if (debug_lvl <= 0) return;
301 
302  if (debug_lvl & 1) { // Standard console startup message output
303  if (from == 0) { // Constructor
304  if (InputSigns.size() > 0) {
305  if (InputSigns[0] < 0)
306  cout << " INPUT: -" << InputNodes[0]->GetName() << endl;
307  else
308  cout << " INPUT: " << InputNodes[0]->GetName() << endl;
309  }
310  if (bits != 0) {
311  if (quant_property.empty())
312  cout << " Quantized output" << endl;
313  else
314  cout << " Quantized output (property: " << quant_property << ")" << endl;
315 
316  cout << " Bits: " << bits << endl;
317  cout << " Min value: " << min << endl;
318  cout << " Max value: " << max << endl;
319  cout << " (span: " << span << ", granularity: " << granularity << ")" << endl;
320  }
321  if (bias != 0.0) cout << " Bias: " << bias << endl;
322  if (gain != 0.0) cout << " Gain: " << gain << endl;
323  if (drift_rate != 0) cout << " Sensor drift rate: " << drift_rate << endl;
324  if (lag != 0) cout << " Sensor lag: " << lag << endl;
325  if (noise_variance != 0) {
326  if (NoiseType == eAbsolute) {
327  cout << " Noise variance (absolute): " << noise_variance << endl;
328  } else if (NoiseType == ePercent) {
329  cout << " Noise variance (percent): " << noise_variance << endl;
330  } else {
331  cout << " Noise variance type is invalid" << endl;
332  }
333  if (DistributionType == eUniform) {
334  cout << " Random noise is uniformly distributed." << endl;
335  } else if (DistributionType == eGaussian) {
336  cout << " Random noise is gaussian distributed." << endl;
337  }
338  }
339  if (IsOutput) {
340  for (unsigned int i=0; i<OutputNodes.size(); i++)
341  cout << " OUTPUT: " << OutputNodes[i]->getName() << endl;
342  }
343  }
344  }
345  if (debug_lvl & 2 ) { // Instantiation/Destruction notification
346  if (from == 0) cout << "Instantiated: FGSensor" << endl;
347  if (from == 1) cout << "Destroyed: FGSensor" << endl;
348  }
349  if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
350  }
351  if (debug_lvl & 8 ) { // Runtime state variables
352  }
353  if (debug_lvl & 16) { // Sanity checking
354  }
355  if (debug_lvl & 64) {
356  if (from == 0) { // Constructor
357  cout << IdSrc << endl;
358  cout << IdHdr << endl;
359  }
360  }
361 }
362 }
std::string mkPropertyName(std::string name, bool lowercase)
Property-ify a name replaces spaces with &#39;-&#39; and, optionally, makes name all lower case...
STL namespace.
void Tie(const std::string &name, bool *pointer, bool useDefault=true)
Tie a property to an external bool variable.