1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 9 July 2005
7 ------------- Copyright (C) 2005 -------------
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
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
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.
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.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
30 --------------------------------------------------------------------------------
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES, and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41 #include "input_output/FGXMLElement.h"
49 static const char *IdSrc = "$Id: FGSensor.cpp,v 1.20 2009/10/24 22:59:30 jberndt Exp $";
50 static const char *IdHdr = ID_SENSOR;
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 FGSensor::FGSensor(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
61 // inputs are read from the base class constructor
63 bits = quantized = divisions = 0;
64 PreviousInput = PreviousOutput = 0.0;
65 min = max = bias = gain = noise_variance = lag = drift_rate = drift = span = 0.0;
68 fail_low = fail_high = fail_stuck = false;
70 Element* quantization_element = element->FindElement("quantization");
71 if ( quantization_element) {
72 if ( quantization_element->FindElement("bits") ) {
73 bits = (int)quantization_element->FindElementValueAsNumber("bits");
75 divisions = (1<<bits);
76 if ( quantization_element->FindElement("min") ) {
77 min = quantization_element->FindElementValueAsNumber("min");
79 if ( quantization_element->FindElement("max") ) {
80 max = quantization_element->FindElementValueAsNumber("max");
82 quant_property = quantization_element->GetAttributeValue("name");
84 granularity = span/divisions;
86 if ( element->FindElement("bias") ) {
87 bias = element->FindElementValueAsNumber("bias");
89 if ( element->FindElement("gain") ) {
90 gain = element->FindElementValueAsNumber("gain");
92 if ( element->FindElement("drift_rate") ) {
93 drift_rate = element->FindElementValueAsNumber("drift_rate");
95 if ( element->FindElement("lag") ) {
96 lag = element->FindElementValueAsNumber("lag");
97 denom = 2.00 + dt*lag;
99 cb = (2.00 - dt*lag) / denom;
101 if ( element->FindElement("noise") ) {
102 noise_variance = element->FindElementValueAsNumber("noise");
103 string variation = element->FindElement("noise")->GetAttributeValue("variation");
104 if (variation == "PERCENT") {
105 NoiseType = ePercent;
106 } else if (variation == "ABSOLUTE") {
107 NoiseType = eAbsolute;
109 NoiseType = ePercent;
110 cerr << "Unknown noise type in sensor: " << Name << endl;
111 cerr << " defaulting to PERCENT." << endl;
113 string distribution = element->FindElement("noise")->GetAttributeValue("distribution");
114 if (distribution == "UNIFORM") {
115 DistributionType = eUniform;
116 } else if (distribution == "GAUSSIAN") {
117 DistributionType = eGaussian;
119 DistributionType = eUniform;
120 cerr << "Unknown random distribution type in sensor: " << Name << endl;
121 cerr << " defaulting to UNIFORM." << endl;
125 FGFCSComponent::bind();
131 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 FGSensor::~FGSensor()
138 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 bool FGSensor::Run(void)
142 Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
144 ProcessSensorSignal();
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 void FGSensor::ProcessSensorSignal(void)
153 Output = Input; // perfect sensor
155 // Degrade signal as specified
158 Output = PreviousOutput;
160 if (lag != 0.0) Lag(); // models sensor lag and filter
161 if (noise_variance != 0.0) Noise(); // models noise
162 if (drift_rate != 0.0) Drift(); // models drift over time
163 if (gain != 0.0) Gain(); // models a finite gain
164 if (bias != 0.0) Bias(); // models a finite bias
166 if (delay != 0) Delay(); // models system signal transport latencies
168 if (fail_low) Output = -HUGE_VAL;
169 if (fail_high) Output = HUGE_VAL;
171 if (bits != 0) Quantize(); // models quantization degradation
177 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 void FGSensor::Noise(void)
181 double random_value=0.0;
183 if (DistributionType == eUniform) {
184 random_value = ((double)rand()/(double)RAND_MAX) - 0.5;
186 random_value = GaussianRandomNumber();
189 switch( NoiseType ) {
191 Output *= (1.0 + noise_variance*random_value);
195 Output += noise_variance*random_value;
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 void FGSensor::Bias(void)
207 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 void FGSensor::Gain(void)
214 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 void FGSensor::Drift(void)
218 drift += drift_rate*dt;
222 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 void FGSensor::Quantize(void)
226 if (Output < min) Output = min;
227 if (Output > max) Output = max;
228 double portion = Output - min;
229 quantized = (int)(portion/granularity);
230 Output = quantized*granularity + min;
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 void FGSensor::Lag(void)
237 // "Output" on the right side of the "=" is the current input
238 Output = ca * (Output + PreviousInput) + PreviousOutput * cb;
240 PreviousOutput = Output;
241 PreviousInput = Input;
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 void FGSensor::bind(void)
249 if (Name.find("/") == string::npos) {
250 tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
252 const string tmp_low = tmp + "/malfunction/fail_low";
253 const string tmp_high = tmp + "/malfunction/fail_high";
254 const string tmp_stuck = tmp + "/malfunction/fail_stuck";
256 PropertyManager->Tie( tmp_low, this, &FGSensor::GetFailLow, &FGSensor::SetFailLow);
257 PropertyManager->Tie( tmp_high, this, &FGSensor::GetFailHigh, &FGSensor::SetFailHigh);
258 PropertyManager->Tie( tmp_stuck, this, &FGSensor::GetFailStuck, &FGSensor::SetFailStuck);
260 if (!quant_property.empty()) {
261 if (quant_property.find("/") == string::npos) { // not found
262 string qprop = "fcs/" + PropertyManager->mkPropertyName(quant_property, true);
263 PropertyManager->Tie(qprop, this, &FGSensor::GetQuantized);
269 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 // The bitmasked value choices are as follows:
271 // unset: In this case (the default) JSBSim would only print
272 // out the normally expected messages, essentially echoing
273 // the config files as they are read. If the environment
274 // variable is not set, debug_lvl is set to 1 internally
275 // 0: This requests JSBSim not to output any messages
277 // 1: This value explicity requests the normal JSBSim
279 // 2: This value asks for a message to be printed out when
280 // a class is instantiated
281 // 4: When this value is set, a message is displayed when a
282 // FGModel object executes its Run() method
283 // 8: When this value is set, various runtime state variables
284 // are printed out periodically
285 // 16: When set various parameters are sanity checked and
286 // a message is printed out when they go out of bounds
288 void FGSensor::Debug(int from)
290 if (debug_lvl <= 0) return;
292 if (debug_lvl & 1) { // Standard console startup message output
293 if (from == 0) { // Constructor
294 if (InputSigns.size() > 0) {
295 if (InputSigns[0] < 0)
296 cout << " INPUT: -" << InputNodes[0]->getName() << endl;
298 cout << " INPUT: " << InputNodes[0]->getName() << endl;
301 if (quant_property.empty())
302 cout << " Quantized output" << endl;
304 cout << " Quantized output (property: " << quant_property << ")" << endl;
306 cout << " Bits: " << bits << endl;
307 cout << " Min value: " << min << endl;
308 cout << " Max value: " << max << endl;
309 cout << " (span: " << span << ", granularity: " << granularity << ")" << endl;
311 if (bias != 0.0) cout << " Bias: " << bias << endl;
312 if (gain != 0.0) cout << " Gain: " << gain << endl;
313 if (drift_rate != 0) cout << " Sensor drift rate: " << drift_rate << endl;
314 if (lag != 0) cout << " Sensor lag: " << lag << endl;
315 if (noise_variance != 0) {
316 if (NoiseType == eAbsolute) {
317 cout << " Noise variance (absolute): " << noise_variance << endl;
318 } else if (NoiseType == ePercent) {
319 cout << " Noise variance (percent): " << noise_variance << endl;
321 cout << " Noise variance type is invalid" << endl;
323 if (DistributionType == eUniform) {
324 cout << " Random noise is uniformly distributed." << endl;
325 } else if (DistributionType == eGaussian) {
326 cout << " Random noise is gaussian distributed." << endl;
330 for (unsigned int i=0; i<OutputNodes.size(); i++)
331 cout << " OUTPUT: " << OutputNodes[i]->getName() << endl;
335 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
336 if (from == 0) cout << "Instantiated: FGSensor" << endl;
337 if (from == 1) cout << "Destroyed: FGSensor" << endl;
339 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
341 if (debug_lvl & 8 ) { // Runtime state variables
343 if (debug_lvl & 16) { // Sanity checking
345 if (debug_lvl & 64) {
346 if (from == 0) { // Constructor
347 cout << IdSrc << endl;
348 cout << IdHdr << endl;