]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGSensor.cpp
d2f9243d92a49147fc03524deba9a2391d233af3
[flightgear.git] / src / FDM / JSBSim / models / flight_control / 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 "FGSensor.h"
41
42 namespace JSBSim {
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_SENSOR;
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51
52 FGSensor::FGSensor(FGFCS* fcs, Element* element) : FGFCSComponent(fcs, element)
53 {
54   double denom;
55
56   // inputs are read from the base class constructor
57
58   bits = quantized = divisions = 0;
59   PreviousInput = PreviousOutput = 0.0;
60   min = max = bias = gain = noise_variance = lag = drift_rate = drift = span = 0.0;
61   granularity = 0.0;
62   noise_type = 0;
63   fail_low = fail_high = fail_stuck = false;
64
65   Element* quantization_element = element->FindElement("quantization");
66   if ( quantization_element) {
67     if ( quantization_element->FindElement("bits") ) {
68       bits = (int)quantization_element->FindElementValueAsNumber("bits");
69     }
70     divisions = (1<<bits);
71     if ( quantization_element->FindElement("min") ) {
72       min = quantization_element->FindElementValueAsNumber("min");
73     }
74     if ( quantization_element->FindElement("max") ) {
75       max = quantization_element->FindElementValueAsNumber("max");
76     }
77     quant_property = quantization_element->GetAttributeValue("name");
78     span = max - min;
79     granularity = span/divisions;
80   }
81   if ( element->FindElement("bias") ) {
82     bias = element->FindElementValueAsNumber("bias");
83   }
84   if ( element->FindElement("gain") ) {
85     gain = element->FindElementValueAsNumber("gain");
86   }
87   if ( element->FindElement("drift_rate") ) {
88     drift_rate = element->FindElementValueAsNumber("drift_rate");
89   }
90   if ( element->FindElement("lag") ) {
91     lag = element->FindElementValueAsNumber("lag");
92     denom = 2.00 + dt*lag;
93     ca = dt*lag / denom;
94     cb = (2.00 - dt*lag) / denom;
95   }
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;
103     } else {
104       NoiseType = ePercent;
105       cerr << "Unknown noise type in sensor: " << Name << endl;
106       cerr << "  defaulting to PERCENT." << endl;
107     }
108     string distribution = element->FindElement("noise")->GetAttributeValue("distribution");
109     if (distribution == "UNIFORM") {
110       DistributionType = eUniform;
111     } else if (distribution == "GAUSSIAN") {
112       DistributionType = eGaussian;
113     } else {
114       DistributionType = eUniform;
115       cerr << "Unknown random distribution type in sensor: " << Name << endl;
116       cerr << "  defaulting to UNIFORM." << endl;
117     }
118   }
119
120   FGFCSComponent::bind();
121   bind();
122
123   Debug(0);
124 }
125
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127
128 FGSensor::~FGSensor()
129 {
130   Debug(1);
131 }
132
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134
135 bool FGSensor::Run(void)
136 {
137   Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
138
139   ProcessSensorSignal();
140
141   return true;
142 }
143
144 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145
146 void FGSensor::ProcessSensorSignal(void)
147 {
148   Output = Input; // perfect sensor
149
150   // Degrade signal as specified
151
152   if (fail_stuck) {
153     Output = PreviousOutput;
154   } else {
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
160
161     if (delay != 0)            Delay();     // models system signal transport latencies
162
163     if (fail_low)  Output = -HUGE_VAL;
164     if (fail_high) Output =  HUGE_VAL;
165
166     if (bits != 0)             Quantize();  // models quantization degradation
167
168     Clip();
169   }
170 }
171
172 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173
174 void FGSensor::Noise(void)
175 {
176   double random_value=0.0;
177
178   if (DistributionType == eUniform) {
179     random_value = ((double)rand()/(double)RAND_MAX) - 0.5;
180   } else {
181     random_value = GaussianRandomNumber();
182   }
183
184   switch( NoiseType ) {
185   case ePercent:
186     Output *= (1.0 + noise_variance*random_value);
187     break;
188
189   case eAbsolute:
190     Output += noise_variance*random_value;
191     break;
192   }
193 }
194
195 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196
197 void FGSensor::Bias(void)
198 {
199   Output += bias;
200 }
201
202 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203
204 void FGSensor::Gain(void)
205 {
206   Output *= gain;
207 }
208
209 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210
211 void FGSensor::Drift(void)
212 {
213   drift += drift_rate*dt;
214   Output += drift;
215 }
216
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218
219 void FGSensor::Quantize(void)
220 {
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;
226 }
227
228 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229
230 void FGSensor::Lag(void)
231 {
232   // "Output" on the right side of the "=" is the current input
233   Output = ca * (Output + PreviousInput) + PreviousOutput * cb;
234
235   PreviousOutput = Output;
236   PreviousInput  = Input;
237 }
238
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240
241 void FGSensor::bind(void)
242 {
243   string tmp = Name;
244   if (Name.find("/") == string::npos) {
245     tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
246   }
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";
250
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);
254   
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);
259     }
260   }
261
262 }
263
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
271 //       whatsoever.
272 //    1: This value explicity requests the normal JSBSim
273 //       startup messages
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
282
283 void FGSensor::Debug(int from)
284 {
285   if (debug_lvl <= 0) return;
286
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;
292         else
293           cout << "      INPUT: " << InputNodes[0]->getName() << endl;
294       }
295       if (bits != 0) {
296         if (quant_property.empty())
297           cout << "      Quantized output" << endl;
298         else
299           cout << "      Quantized output (property: " << quant_property << ")" << endl;
300
301         cout << "        Bits: " << bits << endl;
302         cout << "        Min value: " << min << endl;
303         cout << "        Max value: " << max << endl;
304         cout << "          (span: " << span << ", granularity: " << granularity << ")" << endl;
305       }
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;
315         } else {
316           cout << "      Noise variance type is invalid" << endl;
317         }
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;
322         }
323       }
324       if (IsOutput) {
325         for (unsigned int i=0; i<OutputNodes.size(); i++)
326           cout << "      OUTPUT: " << OutputNodes[i]->getName() << endl;
327       }
328     }
329   }
330   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
331     if (from == 0) cout << "Instantiated: FGSensor" << endl;
332     if (from == 1) cout << "Destroyed:    FGSensor" << endl;
333   }
334   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
335   }
336   if (debug_lvl & 8 ) { // Runtime state variables
337   }
338   if (debug_lvl & 16) { // Sanity checking
339   }
340   if (debug_lvl & 64) {
341     if (from == 0) { // Constructor
342       cout << IdSrc << endl;
343       cout << IdHdr << endl;
344     }
345   }
346 }
347 }