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 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 General Public License for more
19 You should have received a copy of the GNU 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 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)
55 dt = fcs->GetState()->Getdt();
57 // inputs are read from the base class constructor
59 dt = fcs->GetState()->Getdt();
61 bits = quantized = divisions = 0;
62 PreviousInput = PreviousOutput = 0.0;
63 min = max = bias = noise_variance = lag = drift_rate = drift = span = 0.0;
66 fail_low = fail_high = fail_stuck = false;
68 Element* quantization_element = element->FindElement("quantization");
69 if ( quantization_element) {
70 if ( quantization_element->FindElement("bits") ) {
71 bits = (int)quantization_element->FindElementValueAsNumber("bits");
73 divisions = (1<<bits);
74 if ( quantization_element->FindElement("min") ) {
75 min = quantization_element->FindElementValueAsNumber("min");
77 if ( quantization_element->FindElement("max") ) {
78 max = quantization_element->FindElementValueAsNumber("max");
81 granularity = span/divisions;
83 if ( element->FindElement("bias") ) {
84 bias = element->FindElementValueAsNumber("bias");
86 if ( element->FindElement("drift_rate") ) {
87 drift_rate = element->FindElementValueAsNumber("drift_rate");
89 if ( element->FindElement("lag") ) {
90 lag = element->FindElementValueAsNumber("lag");
91 denom = 2.00 + dt*lag;
93 cb = (2.00 - dt*lag) / denom;
95 if ( element->FindElement("noise") ) {
96 noise_variance = element->FindElementValueAsNumber("noise");
97 string variation = element->FindElement("noise")->GetAttributeValue("variation");
98 if (variation == "PERCENT") {
100 } else if (variation == "ABSOLUTE") {
101 NoiseType = eAbsolute;
103 NoiseType = ePercent;
104 cerr << "Unknown noise type in sensor: " << Name << endl;
105 cerr << " defaulting to PERCENT." << endl;
109 FGFCSComponent::bind();
115 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 FGSensor::~FGSensor()
122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 bool FGSensor::Run(void )
126 Input = InputNodes[0]->getDoubleValue() * InputSigns[0];
128 Output = Input; // perfect sensor
130 // Degrade signal as specified
133 Output = PreviousOutput;
137 if (lag != 0.0) Lag(); // models sensor lag
138 if (noise_variance != 0.0) Noise(); // models noise
139 if (drift_rate != 0.0) Drift(); // models drift over time
140 if (bias != 0.0) Bias(); // models a finite bias
142 if (fail_low) Output = -HUGE_VAL;
143 if (fail_high) Output = HUGE_VAL;
145 if (bits != 0) Quantize(); // models quantization degradation
146 // if (delay != 0.0) Delay(); // models system signal transport latencies
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 void FGSensor::Noise(void)
155 double random_value = ((double)rand()/(double)RAND_MAX) - 0.5;
157 switch( NoiseType ) {
159 Output *= (1.0 + noise_variance*random_value);
163 Output += noise_variance*random_value;
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 void FGSensor::Bias(void)
175 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 void FGSensor::Drift(void)
179 drift += drift_rate*dt;
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 void FGSensor::Quantize(void)
187 if (Output < min) Output = min;
188 if (Output > max) Output = max;
189 double portion = Output - min;
190 quantized = (int)(portion/granularity);
191 Output = quantized*granularity + min;
194 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196 void FGSensor::Lag(void)
198 // "Output" on the right side of the "=" is the current frame input
199 Output = ca * (Output + PreviousInput) + PreviousOutput * cb;
201 PreviousOutput = Output;
202 PreviousInput = Input;
205 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 void FGSensor::bind(void)
209 string tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
210 const string tmp_low = tmp + "/malfunction/fail_low";
211 const string tmp_high = tmp + "/malfunction/fail_high";
212 const string tmp_stuck = tmp + "/malfunction/fail_stuck";
214 PropertyManager->Tie( tmp_low, this, &FGSensor::GetFailLow, &FGSensor::SetFailLow);
215 PropertyManager->Tie( tmp_high, this, &FGSensor::GetFailHigh, &FGSensor::SetFailHigh);
216 PropertyManager->Tie( tmp_stuck, this, &FGSensor::GetFailStuck, &FGSensor::SetFailStuck);
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 // The bitmasked value choices are as follows:
221 // unset: In this case (the default) JSBSim would only print
222 // out the normally expected messages, essentially echoing
223 // the config files as they are read. If the environment
224 // variable is not set, debug_lvl is set to 1 internally
225 // 0: This requests JSBSim not to output any messages
227 // 1: This value explicity requests the normal JSBSim
229 // 2: This value asks for a message to be printed out when
230 // a class is instantiated
231 // 4: When this value is set, a message is displayed when a
232 // FGModel object executes its Run() method
233 // 8: When this value is set, various runtime state variables
234 // are printed out periodically
235 // 16: When set various parameters are sanity checked and
236 // a message is printed out when they go out of bounds
238 void FGSensor::Debug(int from)
240 if (debug_lvl <= 0) return;
242 if (debug_lvl & 1) { // Standard console startup message output
243 if (from == 0) { // Constructor
247 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
248 if (from == 0) cout << "Instantiated: FGSensor" << endl;
249 if (from == 1) cout << "Destroyed: FGSensor" << endl;
251 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
253 if (debug_lvl & 8 ) { // Runtime state variables
255 if (debug_lvl & 16) { // Sanity checking
257 if (debug_lvl & 64) {
258 if (from == 0) { // Constructor
259 cout << IdSrc << endl;
260 cout << IdHdr << endl;