RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
GasSensor.h
Go to the documentation of this file.
1#pragma once
2#include "../Common.h"
3#include "../Data/GasData.h"
4#include "BaseComponent.h"
6
7namespace RumPi
8{
18 class GasSensor : public BaseComponent
19 {
20 public:
22 static constexpr double GAS_SENSOR_VOLTAGE = 5.0;
23
25 static const int CALIBRATION_SAMPLE_TIMES = 200;
26
28 static const int CALIBRATION_SAMPLE_INTERVAL = 250;
29
31 static const int READ_SAMPLE_INTERVAL = 250;
32
33 protected:
36
39
42
45
48
51
54
57
60
63
66
80 virtual double CalculateR0();
81
96 virtual double CalculateRS(const double& sensorValueInVolts) const;
97
112 virtual double CalculateRSR0Ratio(const double& sensorValueInVolts) const;
113
129 double ConvertRawSensorValueToVolts(const double& rawGasSensorValue) const;
130
145 double ConvertVoltsToRawSensorValue(const double& volts) const;
146
161
176
177 public:
189 virtual void CalculateInitialR0();
190
202 GasSensor();
203
219 GasSensor(IAnalogDigitalConverterComponent* analogToDigitalConverter, const EComponentType& gasSensorType, const unsigned int& analogToDigitalConverterIndex, const unsigned int& digitalOutputPinNumber);
220
235 GasSensor(IAnalogDigitalConverterComponent* analogToDigitalConverter, const EComponentType& gasSensorType, const unsigned int& analogToDigitalConverterIndex);
236
250 GasSensor(const EComponentType& gasSensorType, const unsigned int& digitalOutputPinNumber);
251
263 virtual ~GasSensor();
264
275 virtual void TurnOn();
276
287 virtual void TurnOff();
288
300 virtual void Initialize();
301
314 int ReadRawAnalogValue() const;
315
328 int ReadDigitalValue();
329
342 virtual double ReadGasValue() const;
343
355 virtual void ProcessRawValues() override;
356
369 virtual std::string ToString() const override;
370
383 unsigned int GetAnalogToDigitalConverterIndex() const;
384
397 unsigned int GetDigitalOutputPinNumber() const;
398
411 double GetInitialValueR0() const;
412
425 double GetResistorValueRL() const;
426
439 double GetCurrentValueRS() const;
440
453 const char* GetDetectibleConcentration() const;
454
468
481 const char* GetGasSensorTypeAsString() const;
482
497 const char* GetGasSensorTypeAsString(const EComponentType& gasSensorType) const;
498
512
525 const GasData& GetGasData() const;
526
527 };
528} // namespace RumPi
Abstract base class for every component (sensor, actuator, etc.) the library manages.
Definition: BaseComponent.h:60
Gas data (its response curve and danger level) used by the MQ gas sensors.
Definition: GasData.h:40
Base class for the MQ-series gas sensors (MQ2, MQ7, ...).
Definition: GasSensor.h:19
unsigned int GetDigitalOutputPinNumber() const
Gets the digital threshold-output pin (UINT_MAX if unused).
Definition: GasSensor.cpp:581
const char * GetDetectibleConcentration() const
Gets a human-readable detectable-concentration range for this sensor.
Definition: GasSensor.cpp:649
unsigned int myAnalogToDigitalConverterIndex
The ADC channel index this sensor reads its analog value from.
Definition: GasSensor.h:35
double myResistorValueRL
The load resistor value (RL) from the sensor's datasheet.
Definition: GasSensor.h:44
virtual void ProcessRawValues() override
Reads the sensor and computes the current gas value (PPM) from the RS/R0 ratio using the datasheet's ...
Definition: GasSensor.cpp:324
int ReadRawAnalogValue() const
Reads the raw analog value from the sensor's ADC channel.
Definition: GasSensor.cpp:280
virtual double CalculateRSR0Ratio(const double &sensorValueInVolts) const
Computes the RS/R0 ratio (used to look up PPM on the datasheet curve) from the sensor's output voltag...
Definition: GasSensor.cpp:435
static const int CALIBRATION_SAMPLE_TIMES
The number of samples taken during the calibration phase.
Definition: GasSensor.h:25
const char * GetGasSensorTypeAsString() const
Gets this sensor's type as a display string (e.g.
Definition: GasSensor.cpp:683
double myInitialValueR0
The sensor's resistance in clean air (R0), from calibration.
Definition: GasSensor.h:41
static constexpr double GAS_SENSOR_VOLTAGE
The supply voltage the gas sensor runs on.
Definition: GasSensor.h:22
IAnalogDigitalConverterComponent * GetAnalogDigitalConverter() const
Gets the ADC this sensor reads its analog value through.
Definition: GasSensor.cpp:756
virtual std::string ToString() const override
Serializes the sensor's current gas value (PPM), a rough percentage, and the raw reading to a string.
Definition: GasSensor.cpp:541
double GetResistorValueRL() const
Gets the load resistor value (RL) from the sensor's datasheet.
Definition: GasSensor.cpp:615
const char * myDetectibleConcentration
A human-readable detectable-concentration range for this sensor.
Definition: GasSensor.h:59
double ConvertRawSensorValueToVolts(const double &rawGasSensorValue) const
Converts a raw analog reading to its equivalent voltage (using the sensor's supply voltage and the AD...
Definition: GasSensor.cpp:505
const GasData & GetGasData() const
Gets the per-gas curve data (slope/intercept and concentration points) for this sensor.
Definition: GasSensor.cpp:773
double myCurrentGasValue
The most recent computed gas value (PPM).
Definition: GasSensor.h:56
IAnalogDigitalConverterComponent * myAnalogToDigitalConverter
The ADC this sensor reads its analog value through.
Definition: GasSensor.h:62
double myRawGasSensorValue
The most recent raw analog reading.
Definition: GasSensor.h:53
double GetAnalogToDigitalConverterMaxRawValue() const
Gets the ADC's full-scale raw count for scaling raw readings to volts (PCF8591 = 255 8-bit,...
Definition: GasSensor.cpp:457
double ConvertVoltsToRawSensorValue(const double &volts) const
Converts a voltage back to its equivalent raw analog value (the inverse of ConvertRawSensorValueToVol...
Definition: GasSensor.cpp:524
virtual double CalculateRS(const double &sensorValueInVolts) const
Computes the sensor's resistance (RS) from its output voltage, using the load resistor RL and supply ...
Definition: GasSensor.cpp:252
EComponentType GetGasSensorType() const
Gets this sensor's component type (which MQ sensor it is).
Definition: GasSensor.cpp:666
GasData myGasData
Per-gas curve data (slope/intercept) used to convert the RS/R0 ratio to PPM.
Definition: GasSensor.h:65
virtual double CalculateR0()
Samples the sensor many times in clean air, averages the reading, converts it to resistance (RS),...
Definition: GasSensor.cpp:214
virtual void CalculateInitialR0()
Calibrates the sensor: computes and stores its clean-air resistance (R0).
Definition: GasSensor.cpp:195
virtual void TurnOn()
Turns the sensor on.
Definition: GasSensor.cpp:145
unsigned int GetAnalogToDigitalConverterIndex() const
Gets the ADC channel index this sensor reads from.
Definition: GasSensor.cpp:564
virtual double ReadGasValue() const
Gets the most recently processed gas value.
Definition: GasSensor.cpp:414
double myCleanAirFactor
The clean-air factor (RS/R0 in clean air) from the datasheet.
Definition: GasSensor.h:50
virtual void Initialize()
Initializes the sensor's datasheet values.
Definition: GasSensor.cpp:180
unsigned int myDigitalOutputPinNumber
The digital output (threshold) pin, or UINT_MAX if unused.
Definition: GasSensor.h:38
GasSensor()
Builds a default gas sensor with zeroed calibration values and no ADC or digital pin.
Definition: GasSensor.cpp:17
double GetCurrentValueRS() const
Gets the sensor's current resistance (RS).
Definition: GasSensor.cpp:632
virtual ~GasSensor()
Destroys the gas sensor, turning it off first.
Definition: GasSensor.cpp:130
static const int READ_SAMPLE_INTERVAL
Milliseconds between samples during normal operation.
Definition: GasSensor.h:31
virtual void TurnOff()
Turns the sensor off.
Definition: GasSensor.cpp:164
double myCurrentValueRS
The sensor's current resistance (RS).
Definition: GasSensor.h:47
static const int CALIBRATION_SAMPLE_INTERVAL
Milliseconds between samples during the calibration phase.
Definition: GasSensor.h:28
double GetInitialValueR0() const
Gets the sensor's clean-air resistance (R0) from calibration.
Definition: GasSensor.cpp:598
double GetAnalogToDigitalConverterVoltage() const
Gets the ADC's reference voltage for scaling raw readings to volts (defined by the wired ADC,...
Definition: GasSensor.cpp:480
int ReadDigitalValue()
Reads the sensor's digital threshold-output pin (1 when the configured gas threshold is exceeded).
Definition: GasSensor.cpp:302
Interface for analog-to-digital converter components (e.g.
Definition: AnalogDigitalConverterComponent.h:14
RumPi is the library's top-level facade.
Definition: AlertManager.h:6
EComponentType
Identifies each kind of component the library supports.
Definition: BaseComponent.h:14