]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/flight_control/FGSensor.cpp
Sync w. JSBSim CVS (merge from PRE_OSG_PLIB_20061029 branch)
[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   dt = fcs->GetDt();
56
57   // inputs are read from the base class constructor
58
59   bits = quantized = divisions = 0;
60   PreviousInput = PreviousOutput = 0.0;
61   min = max = bias = noise_variance = lag = drift_rate = drift = span = 0.0;
62   granularity = 0.0;
63   noise_type = 0;
64   fail_low = fail_high = fail_stuck = false;
65
66   Element* quantization_element = element->FindElement("quantization");
67   if ( quantization_element) {
68     if ( quantization_element->FindElement("bits") ) {
69       bits = (int)quantization_element->FindElementValueAsNumber("bits");
70     }
71     divisions = (1<<bits);
72     if ( quantization_element->FindElement("min") ) {
73       min = quantization_element->FindElementValueAsNumber("min");
74     }
75     if ( quantization_element->FindElement("max") ) {
76       max = quantization_element->FindElementValueAsNumber("max");
77     }
78     quant_property = quantization_element->GetAttributeValue("name");
79     span = max - min;
80     granularity = span/divisions;
81   }
82   if ( element->FindElement("bias") ) {
83     bias = element->FindElementValueAsNumber("bias");
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     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   }
107
108   FGFCSComponent::bind();
109   bind();
110
111   Debug(0);
112 }
113
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
116 FGSensor::~FGSensor()
117 {
118   Debug(1);
119 }
120
121 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122
123 bool FGSensor::Run(void )
124 {
125   Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
126
127   Output = Input; // perfect sensor
128
129   // Degrade signal as specified
130
131   if (fail_stuck) {
132     Output = PreviousOutput;
133     return true;
134   }
135
136   if (lag != 0.0)            Lag();       // models sensor lag
137   if (noise_variance != 0.0) Noise();     // models noise
138   if (drift_rate != 0.0)     Drift();     // models drift over time
139   if (bias != 0.0)           Bias();      // models a finite bias
140
141   if (fail_low)  Output = -HUGE_VAL;
142   if (fail_high) Output =  HUGE_VAL;
143
144   if (bits != 0)             Quantize();  // models quantization degradation
145 //  if (delay != 0.0)          Delay();     // models system signal transport latencies
146
147   return true;
148 }
149
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152 void FGSensor::Noise(void)
153 {
154   double random_value = ((double)rand()/(double)RAND_MAX) - 0.5;
155
156   switch( NoiseType ) {
157   case ePercent:
158     Output *= (1.0 + noise_variance*random_value);
159     break;
160
161   case eAbsolute:
162     Output += noise_variance*random_value;
163     break;
164   }
165 }
166
167 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168
169 void FGSensor::Bias(void)
170 {
171   Output += bias;
172 }
173
174 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175
176 void FGSensor::Drift(void)
177 {
178   drift += drift_rate*dt;
179   Output += drift;
180 }
181
182 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183
184 void FGSensor::Quantize(void)
185 {
186   if (Output < min) Output = min;
187   if (Output > max) Output = max;
188   double portion = Output - min;
189   quantized = (int)(portion/granularity);
190   Output = quantized*granularity + min;
191 }
192
193 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194
195 void FGSensor::Lag(void)
196 {
197   // "Output" on the right side of the "=" is the current frame input
198   Output = ca * (Output + PreviousInput) + PreviousOutput * cb;
199
200   PreviousOutput = Output;
201   PreviousInput  = Input;
202 }
203
204 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205
206 void FGSensor::bind(void)
207 {
208   string tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
209   const string tmp_low = tmp + "/malfunction/fail_low";
210   const string tmp_high = tmp + "/malfunction/fail_high";
211   const string tmp_stuck = tmp + "/malfunction/fail_stuck";
212
213   PropertyManager->Tie( tmp_low, this, &FGSensor::GetFailLow, &FGSensor::SetFailLow);
214   PropertyManager->Tie( tmp_high, this, &FGSensor::GetFailHigh, &FGSensor::SetFailHigh);
215   PropertyManager->Tie( tmp_stuck, this, &FGSensor::GetFailStuck, &FGSensor::SetFailStuck);
216   
217   if (!quant_property.empty()) {
218     if (quant_property.find("/") == string::npos) { // not found
219       string qprop = "fcs/" + PropertyManager->mkPropertyName(quant_property, true);
220       PropertyManager->Tie(qprop, this, &FGSensor::GetQuantized);
221     }
222   }
223
224 }
225
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 //    The bitmasked value choices are as follows:
228 //    unset: In this case (the default) JSBSim would only print
229 //       out the normally expected messages, essentially echoing
230 //       the config files as they are read. If the environment
231 //       variable is not set, debug_lvl is set to 1 internally
232 //    0: This requests JSBSim not to output any messages
233 //       whatsoever.
234 //    1: This value explicity requests the normal JSBSim
235 //       startup messages
236 //    2: This value asks for a message to be printed out when
237 //       a class is instantiated
238 //    4: When this value is set, a message is displayed when a
239 //       FGModel object executes its Run() method
240 //    8: When this value is set, various runtime state variables
241 //       are printed out periodically
242 //    16: When set various parameters are sanity checked and
243 //       a message is printed out when they go out of bounds
244
245 void FGSensor::Debug(int from)
246 {
247   if (debug_lvl <= 0) return;
248
249   if (debug_lvl & 1) { // Standard console startup message output
250     if (from == 0) { // Constructor
251       if (InputSigns[0] < 0)
252         cout << "      INPUT: -" << InputNodes[0]->getName() << endl;
253       else
254         cout << "      INPUT: " << InputNodes[0]->getName() << endl;
255
256       if (IsOutput) cout << "      OUTPUT: " << OutputNode->getName() << endl;
257       if (bits != 0) {
258         if (quant_property.empty())
259           cout << "      Quantized output" << endl;
260         else
261           cout << "      Quantized output (property: " << quant_property << ")" << endl;
262
263         cout << "        Bits: " << bits << endl;
264         cout << "        Min value: " << min << endl;
265         cout << "        Max value: " << max << endl;
266         cout << "          (span: " << span << ", granularity: " << granularity << ")" << endl;
267       }
268       if (bias != 0.0) cout << "      Bias: " << bias << endl;
269       if (drift_rate != 0) cout << "      Sensor drift rate: " << drift_rate << endl;
270       if (lag != 0) cout << "      Sensor lag: " << lag << endl;
271       if (noise_variance != 0) {
272         if (NoiseType == eAbsolute) {
273           cout << "      Noise variance (absolute): " << noise_variance << endl;
274         } else if (NoiseType == ePercent) {
275           cout << "      Noise variance (percent): " << noise_variance << endl;
276         } else {
277           cout << "      Noise variance type is invalid" << endl;
278         }
279       }
280     }
281   }
282   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
283     if (from == 0) cout << "Instantiated: FGSensor" << endl;
284     if (from == 1) cout << "Destroyed:    FGSensor" << endl;
285   }
286   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
287   }
288   if (debug_lvl & 8 ) { // Runtime state variables
289   }
290   if (debug_lvl & 16) { // Sanity checking
291   }
292   if (debug_lvl & 64) {
293     if (from == 0) { // Constructor
294       cout << IdSrc << endl;
295       cout << IdHdr << endl;
296     }
297   }
298 }
299 }