| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Header: FGSensor.h |
| 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 |
| 11 | Software Foundation; either version 2 of the License, or (at your option) any |
| 12 | later 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 |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 21 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | Further information about the GNU Lesser General Public License can also be |
| 24 | found on the world wide web at http://www.gnu.org. |
| 25 | |
| 26 | HISTORY |
| 27 | -------------------------------------------------------------------------------- |
| 28 | |
| 29 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 30 | SENTRY |
| 31 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 32 | |
| 33 | #ifndef FGSENSOR_H |
| 34 | #define FGSENSOR_H |
| 35 | |
| 36 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 37 | INCLUDES |
| 38 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 39 | |
| 40 | #include <memory> |
| 41 | |
| 42 | #include "FGFCSComponent.h" |
| 43 | |
| 44 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 45 | FORWARD DECLARATIONS |
| 46 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 47 | |
| 48 | namespace JSBSim { |
| 49 | |
| 50 | class FGFCS; |
| 51 | class Element; |
| 52 | |
| 53 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 54 | CLASS DOCUMENTATION |
| 55 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 56 | |
| 57 | /** Encapsulates a Sensor component for the flight control system. |
| 58 | |
| 59 | Syntax: |
| 60 | |
| 61 | @code |
| 62 | <sensor name="name"> |
| 63 | <input> property </input> |
| 64 | <lag> number </lag> |
| 65 | <noise [variation="PERCENT|ABSOLUTE"] [distribution="UNIFORM|GAUSSIAN"]> number </noise> |
| 66 | <quantization name="name"> |
| 67 | <bits> number </bits> |
| 68 | <min> number </min> |
| 69 | <max> number </max> |
| 70 | </quantization> |
| 71 | <drift_rate> number </drift_rate> |
| 72 | <gain> number </gain> |
| 73 | <bias> number </bias> |
| 74 | <delay [type="time|frames"]> number < /delay> |
| 75 | </sensor> |
| 76 | @endcode |
| 77 | |
| 78 | Example: |
| 79 | |
| 80 | @code |
| 81 | <sensor name="aero/sensor/qbar"> |
| 82 | <input> aero/qbar </input> |
| 83 | <lag> 0.5 </lag> |
| 84 | <noise variation="PERCENT"> 2 </noise> |
| 85 | <quantization name="aero/sensor/quantized/qbar"> |
| 86 | <bits> 12 </bits> |
| 87 | <min> 0 </min> |
| 88 | <max> 400 </max> |
| 89 | </quantization> |
| 90 | <bias> 0.5 </bias> |
| 91 | </sensor> |
| 92 | @endcode |
| 93 | |
| 94 | The only required element in the sensor definition is the input element. In that |
| 95 | case, no degradation would be modeled, and the output would simply be the input. |
| 96 | |
| 97 | Noise can be Gaussian or uniform, and the noise can be applied as a factor |
| 98 | (PERCENT) or additively (ABSOLUTE). The noise that can be applied at each frame |
| 99 | of the simulation execution is calculated as a random factor times a noise value |
| 100 | that is specified in the config file. When the noise distribution type is |
| 101 | Gaussian, the random number can be between roughly -3 and +3 for a span of six |
| 102 | sigma. When the distribution type is UNIFORM, the random value can be between |
| 103 | -1.0 and +1.0. This random value is multiplied against the specified noise to |
| 104 | arrive at a random noise value for the frame. If the noise type is PERCENT, then |
| 105 | random noise value is added to one, and that sum is then multiplied against the |
| 106 | input signal for the sensor. In this case, the specified noise value in the |
| 107 | config file would be expected to actually be a percent value, such as 0.05 (for |
| 108 | a 5% variance). If the noise type is ABSOLUTE, then the random noise value |
| 109 | specified in the config file is understood to be an absolute value of noise to |
| 110 | be added to the input signal instead of being added to 1.0 and having that sum |
| 111 | be multiplied against the input signal as in the PERCENT type. For the ABSOLUTE |
| 112 | noise case, the noise number specified in the config file could be any number. |
| 113 | |
| 114 | If the type is ABSOLUTE, then the noise number times the random number is added |
| 115 | to the input signal instead of being multiplied against it as with the PERCENT |
| 116 | type of noise. |
| 117 | |
| 118 | The delay element can specify a frame delay. The integer number provided is the |
| 119 | number of frames to delay the output signal. |
| 120 | |
| 121 | @author Jon S. Berndt |
| 122 | @version $Revision: 1.24 $ |
| 123 | */ |
| 124 | |
| 125 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 126 | CLASS DECLARATION |
| 127 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 128 | |
| 129 | class FGSensor : public FGFCSComponent |
| 130 | { |
| 131 | public: |
| 132 | FGSensor(FGFCS* fcs, Element* element); |
| 133 | virtual ~FGSensor(); |
| 134 | |
| 135 | void SetFailLow(double val) {if (val > 0.0) fail_low = true; else fail_low = false;} |
| 136 | void SetFailHigh(double val) {if (val > 0.0) fail_high = true; else fail_high = false;} |
| 137 | void SetFailStuck(double val) {if (val > 0.0) fail_stuck = true; else fail_stuck = false;} |
| 138 | |
| 139 | double GetFailLow(void) const {if (fail_low) return 1.0; else return 0.0;} |
| 140 | double GetFailHigh(void) const {if (fail_high) return 1.0; else return 0.0;} |
| 141 | double GetFailStuck(void) const {if (fail_stuck) return 1.0; else return 0.0;} |
| 142 | int GetQuantized(void) const {return quantized;} |
| 143 | |
| 144 | bool Run (void) override; |
| 145 | void ResetPastStates(void) override; |
| 146 | |
| 147 | protected: |
| 148 | enum eNoiseType {ePercent=0, eAbsolute} NoiseType; |
| 149 | enum eDistributionType {eUniform=0, eGaussian} DistributionType; |
| 150 | double min, max; |
| 151 | double span; |
| 152 | double bias; |
| 153 | double gain; |
| 154 | double drift_rate; |
| 155 | double drift; |
| 156 | double noise_variance; |
| 157 | double lag; |
| 158 | double granularity; |
| 159 | double ca; /// lag filter coefficient "a" |
| 160 | double cb; /// lag filter coefficient "b" |
| 161 | double PreviousOutput; |
| 162 | double PreviousInput; |
| 163 | int noise_type; |
| 164 | int bits; |
| 165 | int quantized; |
| 166 | int divisions; |
| 167 | bool fail_low; |
| 168 | bool fail_high; |
| 169 | bool fail_stuck; |
| 170 | std::string quant_property; |
| 171 | |
| 172 | void ProcessSensorSignal(void); |
| 173 | void Noise(void); |
| 174 | void Bias(void); |
| 175 | void Drift(void); |
| 176 | void Quantize(void); |
| 177 | void Lag(void); |
| 178 | void Gain(void); |
| 179 | |
| 180 | void bind(Element* el) override; |
| 181 | |
| 182 | private: |
| 183 | std::shared_ptr<RandomNumberGenerator> generator; |
| 184 | void Debug(int from) override; |
| 185 | }; |
| 186 | } |
| 187 | #endif |