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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_SENSOR;
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 FGSensor::FGSensor(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
56 // inputs are read from the base class constructor
58 bits = quantized = divisions = 0;
59 PreviousInput = PreviousOutput = 0.0;
60 min = max = bias = gain = noise_variance = lag = drift_rate = drift = span = 0.0;
63 fail_low = fail_high = fail_stuck = false;
65 Element* quantization_element = element->FindElement("quantization");
66 if ( quantization_element) {
67 if ( quantization_element->FindElement("bits") ) {
68 bits = (int)quantization_element->FindElementValueAsNumber("bits");
70 divisions = (1<<bits);
71 if ( quantization_element->FindElement("min") ) {
72 min = quantization_element->FindElementValueAsNumber("min");
74 if ( quantization_element->FindElement("max") ) {
75 max = quantization_element->FindElementValueAsNumber("max");
77 quant_property = quantization_element->GetAttributeValue("name");
79 granularity = span/divisions;
81 if ( element->FindElement("bias") ) {
82 bias = element->FindElementValueAsNumber("bias");
84 if ( element->FindElement("gain") ) {
85 gain = element->FindElementValueAsNumber("gain");
87 if ( element->FindElement("drift_rate") ) {
88 drift_rate = element->FindElementValueAsNumber("drift_rate");
90 if ( element->FindElement("lag") ) {
91 lag = element->FindElementValueAsNumber("lag");
92 denom = 2.00 + dt*lag;
94 cb = (2.00 - dt*lag) / denom;
96 if ( element->FindElement("noise") ) {
97 noise_variance = element->FindElementValueAsNumber("noise");
98 string variation = element->FindElement("noise")->GetAttributeValue("variation");
99 if (variation == "PERCENT") {
100 NoiseType = ePercent;
101 } else if (variation == "ABSOLUTE") {
102 NoiseType = eAbsolute;
104 NoiseType = ePercent;
105 cerr << "Unknown noise type in sensor: " << Name << endl;
106 cerr << " defaulting to PERCENT." << endl;
108 string distribution = element->FindElement("noise")->GetAttributeValue("distribution");
109 if (distribution == "UNIFORM") {
110 DistributionType = eUniform;
111 } else if (distribution == "GAUSSIAN") {
112 DistributionType = eGaussian;
114 DistributionType = eUniform;
115 cerr << "Unknown random distribution type in sensor: " << Name << endl;
116 cerr << " defaulting to UNIFORM." << endl;
120 FGFCSComponent::bind();
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 FGSensor::~FGSensor()
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 bool FGSensor::Run(void)
137 Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
139 ProcessSensorSignal();
144 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 void FGSensor::ProcessSensorSignal(void)
148 Output = Input; // perfect sensor
150 // Degrade signal as specified
153 Output = PreviousOutput;
155 if (lag != 0.0) Lag(); // models sensor lag and filter
156 if (noise_variance != 0.0) Noise(); // models noise
157 if (drift_rate != 0.0) Drift(); // models drift over time
158 if (gain != 0.0) Gain(); // models a finite gain
159 if (bias != 0.0) Bias(); // models a finite bias
161 if (delay != 0) Delay(); // models system signal transport latencies
163 if (fail_low) Output = -HUGE_VAL;
164 if (fail_high) Output = HUGE_VAL;
166 if (bits != 0) Quantize(); // models quantization degradation
172 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 void FGSensor::Noise(void)
176 double random_value=0.0;
178 if (DistributionType == eUniform) {
179 random_value = ((double)rand()/(double)RAND_MAX) - 0.5;
181 random_value = GaussianRandomNumber();
184 switch( NoiseType ) {
186 Output *= (1.0 + noise_variance*random_value);
190 Output += noise_variance*random_value;
195 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 void FGSensor::Bias(void)
202 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 void FGSensor::Gain(void)
209 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211 void FGSensor::Drift(void)
213 drift += drift_rate*dt;
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 void FGSensor::Quantize(void)
221 if (Output < min) Output = min;
222 if (Output > max) Output = max;
223 double portion = Output - min;
224 quantized = (int)(portion/granularity);
225 Output = quantized*granularity + min;
228 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 void FGSensor::Lag(void)
232 // "Output" on the right side of the "=" is the current input
233 Output = ca * (Output + PreviousInput) + PreviousOutput * cb;
235 PreviousOutput = Output;
236 PreviousInput = Input;
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 void FGSensor::bind(void)
244 if (Name.find("/") == string::npos) {
245 tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
247 const string tmp_low = tmp + "/malfunction/fail_low";
248 const string tmp_high = tmp + "/malfunction/fail_high";
249 const string tmp_stuck = tmp + "/malfunction/fail_stuck";
251 PropertyManager->Tie( tmp_low, this, &FGSensor::GetFailLow, &FGSensor::SetFailLow);
252 PropertyManager->Tie( tmp_high, this, &FGSensor::GetFailHigh, &FGSensor::SetFailHigh);
253 PropertyManager->Tie( tmp_stuck, this, &FGSensor::GetFailStuck, &FGSensor::SetFailStuck);
255 if (!quant_property.empty()) {
256 if (quant_property.find("/") == string::npos) { // not found
257 string qprop = "fcs/" + PropertyManager->mkPropertyName(quant_property, true);
258 PropertyManager->Tie(qprop, this, &FGSensor::GetQuantized);
264 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265 // The bitmasked value choices are as follows:
266 // unset: In this case (the default) JSBSim would only print
267 // out the normally expected messages, essentially echoing
268 // the config files as they are read. If the environment
269 // variable is not set, debug_lvl is set to 1 internally
270 // 0: This requests JSBSim not to output any messages
272 // 1: This value explicity requests the normal JSBSim
274 // 2: This value asks for a message to be printed out when
275 // a class is instantiated
276 // 4: When this value is set, a message is displayed when a
277 // FGModel object executes its Run() method
278 // 8: When this value is set, various runtime state variables
279 // are printed out periodically
280 // 16: When set various parameters are sanity checked and
281 // a message is printed out when they go out of bounds
283 void FGSensor::Debug(int from)
285 if (debug_lvl <= 0) return;
287 if (debug_lvl & 1) { // Standard console startup message output
288 if (from == 0) { // Constructor
289 if (InputSigns.size() > 0) {
290 if (InputSigns[0] < 0)
291 cout << " INPUT: -" << InputNodes[0]->getName() << endl;
293 cout << " INPUT: " << InputNodes[0]->getName() << endl;
296 if (quant_property.empty())
297 cout << " Quantized output" << endl;
299 cout << " Quantized output (property: " << quant_property << ")" << endl;
301 cout << " Bits: " << bits << endl;
302 cout << " Min value: " << min << endl;
303 cout << " Max value: " << max << endl;
304 cout << " (span: " << span << ", granularity: " << granularity << ")" << endl;
306 if (bias != 0.0) cout << " Bias: " << bias << endl;
307 if (gain != 0.0) cout << " Gain: " << gain << endl;
308 if (drift_rate != 0) cout << " Sensor drift rate: " << drift_rate << endl;
309 if (lag != 0) cout << " Sensor lag: " << lag << endl;
310 if (noise_variance != 0) {
311 if (NoiseType == eAbsolute) {
312 cout << " Noise variance (absolute): " << noise_variance << endl;
313 } else if (NoiseType == ePercent) {
314 cout << " Noise variance (percent): " << noise_variance << endl;
316 cout << " Noise variance type is invalid" << endl;
318 if (DistributionType == eUniform) {
319 cout << " Random noise is uniformly distributed." << endl;
320 } else if (DistributionType == eGaussian) {
321 cout << " Random noise is gaussian distributed." << endl;
325 for (unsigned int i=0; i<OutputNodes.size(); i++)
326 cout << " OUTPUT: " << OutputNodes[i]->getName() << endl;
330 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
331 if (from == 0) cout << "Instantiated: FGSensor" << endl;
332 if (from == 1) cout << "Destroyed: FGSensor" << endl;
334 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
336 if (debug_lvl & 8 ) { // Runtime state variables
338 if (debug_lvl & 16) { // Sanity checking
340 if (debug_lvl & 64) {
341 if (from == 0) { // Constructor
342 cout << IdSrc << endl;
343 cout << IdHdr << endl;