JSBSim Flight Dynamics Model 1.3.0 (09 Apr 2026)
An Open Source Flight Dynamics and Control Software Library in C++
Loading...
Searching...
No Matches
FGSensor Class Reference

Detailed Description

Encapsulates a Sensor component for the flight control system.

Syntax:

<sensor name="name">
<input> property </input>
<lag> number </lag>
<noise [variation="PERCENT|ABSOLUTE"] [distribution="UNIFORM|GAUSSIAN"] > number </noise>
<quantization name="name">
<bits> number </bits>
<min> number </min>
<max> number </max>
</quantization>
<drift_rate> number </drift_rate>
<gain> number </gain>
<bias> number </bias>
<delay [type="time|frames"]> number < /delay>
</sensor>

Example:

<sensor name="aero/sensor/qbar">
<input> aero/qbar </input>
<lag> 0.5 </lag>
<noise variation="PERCENT"> 2 </noise>
<quantization name="aero/sensor/quantized/qbar">
<bits> 12 </bits>
<min> 0 </min>
<max> 400 </max>
</quantization>
<bias> 0.5 </bias>
</sensor>

The only required element in the sensor definition is the input element. In that case, no degradation would be modeled, and the output would simply be the input.

Noise can be Gaussian or uniform, and the noise can be applied as a factor (PERCENT) or additively (ABSOLUTE). The noise that can be applied at each frame of the simulation execution is calculated as a random factor times a noise value that is specified in the config file. When the noise distribution type is Gaussian, the random number can be between roughly -3 and +3 for a span of six sigma. When the distribution type is UNIFORM, the random value can be between -1.0 and +1.0. This random value is multiplied against the specified noise to arrive at a random noise value for the frame. If the noise type is PERCENT, then random noise value is added to one, and that sum is then multiplied against the input signal for the sensor. In this case, the specified noise value in the config file would be expected to actually be a percent value, such as 0.05 (for a 5% variance). If the noise type is ABSOLUTE, then the random noise value specified in the config file is understood to be an absolute value of noise to be added to the input signal instead of being added to 1.0 and having that sum be multiplied against the input signal as in the PERCENT type. For the ABSOLUTE noise case, the noise number specified in the config file could be any number.

If the type is ABSOLUTE, then the noise number times the random number is added to the input signal instead of being multiplied against it as with the PERCENT type of noise.

If a noise element is specified, then a property will be created, named name/randomseed if the name contains a path separator '/' or fcs/name/randomseed if the name does not contain a path separator. Which allows a user to specify a random seed to be used for the noise generation.

The delay element can specify a frame delay. The integer number provided is the number of frames to delay the output signal.

Author
Jon S. Berndt
Version
Revision
1.24

Definition at line 133 of file FGSensor.h.

#include <FGSensor.h>

+ Inheritance diagram for FGSensor:
+ Collaboration diagram for FGSensor:

Public Member Functions

 FGSensor (FGFCS *fcs, Element *element)
 
double GetFailHigh (void) const
 
double GetFailLow (void) const
 
double GetFailStuck (void) const
 
int GetNoiseRandomSeed (void) const
 
int GetQuantized (void) const
 
void ResetPastStates (void) override
 
bool Run (void) override
 
void SetFailHigh (double val)
 
void SetFailLow (double val)
 
void SetFailStuck (double val)
 
void SetNoiseRandomSeed (int seed)
 
- Public Member Functions inherited from FGFCSComponent
 FGFCSComponent (FGFCS *fcs, Element *el)
 Constructor.
 
virtual ~FGFCSComponent ()
 Destructor.
 
std::string GetName (void) const
 
double GetOutput (void) const
 
virtual double GetOutputPct (void) const
 
std::string GetType (void) const
 
virtual void SetOutput (void)
 
- Public Member Functions inherited from FGJSBBase
 FGJSBBase ()
 Constructor for FGJSBBase.
 
virtual ~FGJSBBase ()
 Destructor for FGJSBBase.
 
void disableHighLighting (void)
 Disables highlighting in the console output.
 

Protected Types

enum  eDistributionType { eUniform =0 , eGaussian }
 
enum  eNoiseType { ePercent =0 , eAbsolute }
 

Protected Member Functions

void Bias (void)
 
void bind (Element *el, FGPropertyManager *pm) override
 
void Drift (void)
 
void Gain (void)
 
void Lag (void)
 
void Noise (void)
 
void ProcessSensorSignal (void)
 
void Quantize (void)
 
- Protected Member Functions inherited from FGFCSComponent
void CheckInputNodes (size_t MinNodes, size_t MaxNodes, Element *el)
 
void Clip (void)
 
void Delay (void)
 

Protected Attributes

double bias
 
int bits
 
double ca
 
double cb
 lag filter coefficient "a"
 
enum JSBSim::FGSensor::eDistributionType DistributionType
 
int divisions
 
double drift
 
double drift_rate
 
bool fail_high
 
bool fail_low
 
bool fail_stuck
 
double gain
 
double granularity
 
double lag
 
double max
 
double min
 
int noise_type
 
double noise_variance
 
enum JSBSim::FGSensor::eNoiseType NoiseType
 
double PreviousInput
 
double PreviousOutput
 lag filter coefficient "b"
 
std::string quant_property
 
int quantized
 
double span
 
- Protected Attributes inherited from FGFCSComponent
bool clip
 
FGParameter_ptr ClipMax
 
FGParameter_ptr ClipMin
 
bool cyclic_clip
 
unsigned int delay
 
double delay_time
 
double dt
 
FGFCSfcs
 
int index
 
std::vector< FGPropertyValue_ptr > InitNodes
 
double Input
 
std::vector< FGPropertyValue_ptr > InputNodes
 
std::string Name
 
double Output
 
std::vector< double > output_array
 
std::vector< SGPropertyNode_ptr > OutputNodes
 
std::string Type
 

Additional Inherited Members

- Public Types inherited from FGJSBBase
enum  { eL = 1 , eM , eN }
 Moments L, M, N. More...
 
enum  { eP = 1 , eQ , eR }
 Rates P, Q, R. More...
 
enum  { eU = 1 , eV , eW }
 Velocities U, V, W. More...
 
enum  { eX = 1 , eY , eZ }
 Positions X, Y, Z. More...
 
enum  { ePhi = 1 , eTht , ePsi }
 Euler angles Phi, Theta, Psi. More...
 
enum  { eDrag = 1 , eSide , eLift }
 Stability axis forces, Drag, Side force, Lift. More...
 
enum  { eRoll = 1 , ePitch , eYaw }
 Local frame orientation Roll, Pitch, Yaw. More...
 
enum  { eNorth = 1 , eEast , eDown }
 Local frame position North, East, Down. More...
 
enum  { eLat = 1 , eLong , eRad }
 Locations Radius, Latitude, Longitude. More...
 
enum  {
  inNone = 0 , inDegrees , inRadians , inMeters ,
  inFeet
}
 Conversion specifiers. More...
 
- Static Public Member Functions inherited from FGJSBBase
static const std::string & GetVersion (void)
 Returns the version number of JSBSim.
 
static constexpr double KelvinToFahrenheit (double kelvin)
 Converts from degrees Kelvin to degrees Fahrenheit.
 
static constexpr double CelsiusToRankine (double celsius)
 Converts from degrees Celsius to degrees Rankine.
 
static constexpr double RankineToCelsius (double rankine)
 Converts from degrees Rankine to degrees Celsius.
 
static constexpr double KelvinToRankine (double kelvin)
 Converts from degrees Kelvin to degrees Rankine.
 
static constexpr double RankineToKelvin (double rankine)
 Converts from degrees Rankine to degrees Kelvin.
 
static constexpr double FahrenheitToCelsius (double fahrenheit)
 Converts from degrees Fahrenheit to degrees Celsius.
 
static constexpr double CelsiusToFahrenheit (double celsius)
 Converts from degrees Celsius to degrees Fahrenheit.
 
static constexpr double CelsiusToKelvin (double celsius)
 Converts from degrees Celsius to degrees Kelvin.
 
static constexpr double KelvinToCelsius (double kelvin)
 Converts from degrees Kelvin to degrees Celsius.
 
static constexpr double FeetToMeters (double measure)
 Converts from feet to meters.
 
static bool EqualToRoundoff (double a, double b)
 Finite precision comparison.
 
static bool EqualToRoundoff (float a, float b)
 Finite precision comparison.
 
static bool EqualToRoundoff (float a, double b)
 Finite precision comparison.
 
static bool EqualToRoundoff (double a, float b)
 Finite precision comparison.
 
static constexpr double Constrain (double min, double value, double max)
 Constrain a value between a minimum and a maximum value.
 
static constexpr double sign (double num)
 
- Static Public Attributes inherited from FGJSBBase
static char highint [5] = {27, '[', '1', 'm', '\0' }
 highlights text
 
static char halfint [5] = {27, '[', '2', 'm', '\0' }
 low intensity text
 
static char normint [6] = {27, '[', '2', '2', 'm', '\0' }
 normal intensity text
 
static char reset [5] = {27, '[', '0', 'm', '\0' }
 resets text properties
 
static char underon [5] = {27, '[', '4', 'm', '\0' }
 underlines text
 
static char underoff [6] = {27, '[', '2', '4', 'm', '\0' }
 underline off
 
static char fgblue [6] = {27, '[', '3', '4', 'm', '\0' }
 blue text
 
static char fgcyan [6] = {27, '[', '3', '6', 'm', '\0' }
 cyan text
 
static char fgred [6] = {27, '[', '3', '1', 'm', '\0' }
 red text
 
static char fggreen [6] = {27, '[', '3', '2', 'm', '\0' }
 green text
 
static char fgdef [6] = {27, '[', '3', '9', 'm', '\0' }
 default text
 
static short debug_lvl = 1
 
- Static Protected Member Functions inherited from FGJSBBase
static std::string CreateIndexedPropertyName (const std::string &Property, int index)
 
- Static Protected Attributes inherited from FGJSBBase
static constexpr double radtodeg = 180. / M_PI
 
static constexpr double degtorad = M_PI / 180.
 
static constexpr double hptoftlbssec = 550.0
 
static constexpr double psftoinhg = 0.014138
 
static constexpr double psftopa = 47.88
 
static constexpr double fttom = 0.3048
 
static constexpr double ktstofps = 1852./(3600*fttom)
 
static constexpr double fpstokts = 1.0 / ktstofps
 
static constexpr double inchtoft = 1.0/12.0
 
static constexpr double m3toft3 = 1.0/(fttom*fttom*fttom)
 
static constexpr double in3tom3 = inchtoft*inchtoft*inchtoft/m3toft3
 
static constexpr double inhgtopa = 3386.38
 
static constexpr double slugtolb = 32.174049
 Note that definition of lbtoslug by the inverse of slugtolb and not to a different constant you can also get from some tables will make lbtoslug*slugtolb == 1 up to the magnitude of roundoff.
 
static constexpr double lbtoslug = 1.0/slugtolb
 
static constexpr double kgtolb = 2.20462
 
static constexpr double kgtoslug = 0.06852168
 
static const std::string needed_cfg_version = "2.0"
 
static const std::string JSBSim_version = JSBSIM_VERSION " " __DATE__ " " __TIME__
 

Member Enumeration Documentation

◆ eDistributionType

enum eDistributionType
protected

Definition at line 156 of file FGSensor.h.

156{eUniform=0, eGaussian} DistributionType;

◆ eNoiseType

enum eNoiseType
protected

Definition at line 155 of file FGSensor.h.

155{ePercent=0, eAbsolute} NoiseType;

Constructor & Destructor Documentation

◆ FGSensor()

FGSensor ( FGFCS fcs,
Element element 
)

Definition at line 54 of file FGSensor.cpp.

55 : FGFCSComponent(fcs, element), generator(fcs->GetExec()->GetRandomGenerator())
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 = gain = 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("gain") ) {
86 gain = element->FindElementValueAsNumber("gain");
87 }
88 if ( element->FindElement("drift_rate") ) {
89 drift_rate = element->FindElementValueAsNumber("drift_rate");
90 }
91 if ( element->FindElement("lag") ) {
92 lag = element->FindElementValueAsNumber("lag");
93 double denom = 2.00 + dt*lag;
94 ca = dt*lag / denom;
95 cb = (2.00 - dt*lag) / denom;
96 }
97 if ( element->FindElement("noise") ) {
98 noise_variance = element->FindElementValueAsNumber("noise");
99 string variation = element->FindElement("noise")->GetAttributeValue("variation");
100 if (variation == "PERCENT") {
101 NoiseType = ePercent;
102 } else if (variation == "ABSOLUTE") {
103 NoiseType = eAbsolute;
104 } else {
105 NoiseType = ePercent;
106 FGLogging log(LogLevel::ERROR);
107 log << "Unknown noise type in sensor: " << Name
108 << "\n defaulting to PERCENT.\n";
109 }
110 string distribution = element->FindElement("noise")->GetAttributeValue("distribution");
111 if (distribution == "UNIFORM") {
112 DistributionType = eUniform;
113 } else if (distribution == "GAUSSIAN") {
114 DistributionType = eGaussian;
115 } else {
116 DistributionType = eUniform;
117 FGLogging log(LogLevel::ERROR);
118 log << "Unknown random distribution type in sensor: " << Name
119 << "\n defaulting to UNIFORM.\n";
120 }
121 }
122
123 bind(element, fcs->GetPropertyManager().get());
124
125 Debug(0);
126}
FGFCSComponent(FGFCS *fcs, Element *el)
Constructor.
double PreviousOutput
lag filter coefficient "b"
Definition FGSensor.h:168
double cb
lag filter coefficient "a"
Definition FGSensor.h:167

◆ ~FGSensor()

~FGSensor ( )
virtual

Definition at line 130 of file FGSensor.cpp.

131{
132 Debug(1);
133}

Member Function Documentation

◆ Bias()

void Bias ( void  )
protected

Definition at line 207 of file FGSensor.cpp.

208{
209 Output += bias;
210}

◆ bind()

void bind ( Element el,
FGPropertyManager *  pm 
)
overrideprotectedvirtual

Reimplemented from FGFCSComponent.

Definition at line 251 of file FGSensor.cpp.

252{
253 string tmp = Name;
254
255 FGFCSComponent::bind(el, PropertyManager);
256
257 if (Name.find("/") == string::npos) {
258 tmp = "fcs/" + PropertyManager->mkPropertyName(Name, true);
259 }
260 const string tmp_low = tmp + "/malfunction/fail_low";
261 const string tmp_high = tmp + "/malfunction/fail_high";
262 const string tmp_stuck = tmp + "/malfunction/fail_stuck";
263 const string tmp_randomseed = tmp + "/randomseed";
264
265 PropertyManager->Tie( tmp_low, this, &FGSensor::GetFailLow, &FGSensor::SetFailLow);
266 PropertyManager->Tie( tmp_high, this, &FGSensor::GetFailHigh, &FGSensor::SetFailHigh);
267 PropertyManager->Tie( tmp_stuck, this, &FGSensor::GetFailStuck, &FGSensor::SetFailStuck);
268 PropertyManager->Tie( tmp_randomseed, this, &FGSensor::GetNoiseRandomSeed, &FGSensor::SetNoiseRandomSeed);
269
270 if (!quant_property.empty()) {
271 if (quant_property.find("/") == string::npos) { // not found
272 string qprop = "fcs/" + PropertyManager->mkPropertyName(quant_property, true);
273 SGPropertyNode* node = PropertyManager->GetNode(qprop, true);
274 if (node->isTied()) {
275 XMLLogException err(el);
276 err << "Property " << tmp << " has already been successfully bound (late).\n";
277 throw err;
278 }
279 else
280 PropertyManager->Tie(qprop, this, &FGSensor::GetQuantized);
281 }
282 }
283}
A node in a property tree.
Definition props.hxx:747
bool isTied() const
Test whether this node is bound to an external data source.
Definition props.hxx:1349

◆ Drift()

void Drift ( void  )
protected

Definition at line 221 of file FGSensor.cpp.

222{
223 drift += drift_rate*dt;
224 Output += drift;
225}

◆ Gain()

void Gain ( void  )
protected

Definition at line 214 of file FGSensor.cpp.

215{
216 Output *= gain;
217}

◆ GetFailHigh()

double GetFailHigh ( void  ) const
inline

Definition at line 144 of file FGSensor.h.

144{if (fail_high) return 1.0; else return 0.0;}

◆ GetFailLow()

double GetFailLow ( void  ) const
inline

Definition at line 143 of file FGSensor.h.

143{if (fail_low) return 1.0; else return 0.0;}

◆ GetFailStuck()

double GetFailStuck ( void  ) const
inline

Definition at line 145 of file FGSensor.h.

145{if (fail_stuck) return 1.0; else return 0.0;}

◆ GetNoiseRandomSeed()

int GetNoiseRandomSeed ( void  ) const

Definition at line 295 of file FGSensor.cpp.

296{
297 if (RandomSeed)
298 return *RandomSeed;
299 else
300 return fcs->GetExec()->SRand();
301}

◆ GetQuantized()

int GetQuantized ( void  ) const
inline

Definition at line 146 of file FGSensor.h.

146{return quantized;}

◆ Lag()

void Lag ( void  )
protected

Definition at line 240 of file FGSensor.cpp.

241{
242 // "Output" on the right side of the "=" is the current input
243 Output = ca * (Output + PreviousInput) + PreviousOutput * cb;
244
245 PreviousOutput = Output;
246 PreviousInput = Input;
247}

◆ Noise()

void Noise ( void  )
protected

Definition at line 185 of file FGSensor.cpp.

186{
187 double random_value=0.0;
188
189 if (DistributionType == eUniform)
190 random_value = generator->GetUniformRandomNumber();
191 else
192 random_value = generator->GetNormalRandomNumber();
193
194 switch( NoiseType ) {
195 case ePercent:
196 Output *= (1.0 + noise_variance*random_value);
197 break;
198
199 case eAbsolute:
200 Output += noise_variance*random_value;
201 break;
202 }
203}

◆ ProcessSensorSignal()

void ProcessSensorSignal ( void  )
protected

Definition at line 159 of file FGSensor.cpp.

160{
161 // Degrade signal as specified
162
163 if (!fail_stuck) {
164 Output = Input; // perfect sensor
165
166 if (lag != 0.0) Lag(); // models sensor lag and filter
167 if (noise_variance != 0.0) Noise(); // models noise
168 if (drift_rate != 0.0) Drift(); // models drift over time
169 if (gain != 0.0) Gain(); // models a finite gain
170 if (bias != 0.0) Bias(); // models a finite bias
171
172 if (delay != 0) Delay(); // models system signal transport latencies
173
174 if (fail_low) Output = -HUGE_VAL;
175 if (fail_high) Output = HUGE_VAL;
176
177 if (bits != 0) Quantize(); // models quantization degradation
178
179 Clip();
180 }
181}

◆ Quantize()

void Quantize ( void  )
protected

Definition at line 229 of file FGSensor.cpp.

230{
231 if (Output < min) Output = min;
232 if (Output > max) Output = max;
233 double portion = Output - min;
234 quantized = (int)(portion/granularity);
235 Output = quantized*granularity + min;
236}

◆ ResetPastStates()

void ResetPastStates ( void  )
overridevirtual

Reimplemented from FGFCSComponent.

Definition at line 137 of file FGSensor.cpp.

138{
139 FGFCSComponent::ResetPastStates();
140
141 PreviousOutput = PreviousInput = Output = 0.0;
142}

◆ Run()

bool Run ( void  )
overridevirtual

Reimplemented from FGFCSComponent.

Definition at line 146 of file FGSensor.cpp.

147{
148 Input = InputNodes[0]->getDoubleValue();
149
150 ProcessSensorSignal();
151
152 SetOutput();
153
154 return true;
155}

◆ SetFailHigh()

void SetFailHigh ( double  val)
inline

Definition at line 140 of file FGSensor.h.

140{if (val > 0.0) fail_high = true; else fail_high = false;}

◆ SetFailLow()

void SetFailLow ( double  val)
inline

Definition at line 139 of file FGSensor.h.

139{if (val > 0.0) fail_low = true; else fail_low = false;}

◆ SetFailStuck()

void SetFailStuck ( double  val)
inline

Definition at line 141 of file FGSensor.h.

141{if (val > 0.0) fail_stuck = true; else fail_stuck = false;}

◆ SetNoiseRandomSeed()

void SetNoiseRandomSeed ( int  seed)

Definition at line 289 of file FGSensor.cpp.

290{
291 RandomSeed = sr;
292 generator = std::make_shared<RandomNumberGenerator>(*RandomSeed);
293}

Member Data Documentation

◆ bias

double bias
protected

Definition at line 159 of file FGSensor.h.

◆ bits

int bits
protected

Definition at line 171 of file FGSensor.h.

◆ ca

double ca
protected

Definition at line 166 of file FGSensor.h.

◆ cb

double cb
protected

lag filter coefficient "a"

Definition at line 167 of file FGSensor.h.

◆ divisions

int divisions
protected

Definition at line 173 of file FGSensor.h.

◆ drift

double drift
protected

Definition at line 162 of file FGSensor.h.

◆ drift_rate

double drift_rate
protected

Definition at line 161 of file FGSensor.h.

◆ fail_high

bool fail_high
protected

Definition at line 175 of file FGSensor.h.

◆ fail_low

bool fail_low
protected

Definition at line 174 of file FGSensor.h.

◆ fail_stuck

bool fail_stuck
protected

Definition at line 176 of file FGSensor.h.

◆ gain

double gain
protected

Definition at line 160 of file FGSensor.h.

◆ granularity

double granularity
protected

Definition at line 165 of file FGSensor.h.

◆ lag

double lag
protected

Definition at line 164 of file FGSensor.h.

◆ max

double max
protected

Definition at line 157 of file FGSensor.h.

◆ min

double min
protected

Definition at line 157 of file FGSensor.h.

◆ noise_type

int noise_type
protected

Definition at line 170 of file FGSensor.h.

◆ noise_variance

double noise_variance
protected

Definition at line 163 of file FGSensor.h.

◆ PreviousInput

double PreviousInput
protected

Definition at line 169 of file FGSensor.h.

◆ PreviousOutput

double PreviousOutput
protected

lag filter coefficient "b"

Definition at line 168 of file FGSensor.h.

◆ quant_property

std::string quant_property
protected

Definition at line 177 of file FGSensor.h.

◆ quantized

int quantized
protected

Definition at line 172 of file FGSensor.h.

◆ span

double span
protected

Definition at line 158 of file FGSensor.h.


The documentation for this class was generated from the following files: