![]() |
RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
|
Base class for the MQ-series gas sensors (MQ2, MQ7, ...). More...
#include <GasSensor.h>
Public Member Functions | |
| virtual void | CalculateInitialR0 () |
| Calibrates the sensor: computes and stores its clean-air resistance (R0). More... | |
| GasSensor () | |
| Builds a default gas sensor with zeroed calibration values and no ADC or digital pin. More... | |
| GasSensor (IAnalogDigitalConverterComponent *analogToDigitalConverter, const EComponentType &gasSensorType, const unsigned int &analogToDigitalConverterIndex, const unsigned int &digitalOutputPinNumber) | |
| Builds an analog gas sensor that reads through an ADC and also exposes a digital threshold pin. More... | |
| GasSensor (IAnalogDigitalConverterComponent *analogToDigitalConverter, const EComponentType &gasSensorType, const unsigned int &analogToDigitalConverterIndex) | |
| Builds an analog-only gas sensor that reads through an ADC (no digital threshold pin). More... | |
| GasSensor (const EComponentType &gasSensorType, const unsigned int &digitalOutputPinNumber) | |
| Builds a digital-only gas sensor that reports only its threshold-output pin (no ADC). More... | |
| virtual | ~GasSensor () |
| Destroys the gas sensor, turning it off first. More... | |
| virtual void | TurnOn () |
| Turns the sensor on. More... | |
| virtual void | TurnOff () |
| Turns the sensor off. More... | |
| virtual void | Initialize () |
| Initializes the sensor's datasheet values. More... | |
| int | ReadRawAnalogValue () const |
| Reads the raw analog value from the sensor's ADC channel. More... | |
| int | ReadDigitalValue () |
| Reads the sensor's digital threshold-output pin (1 when the configured gas threshold is exceeded). More... | |
| virtual double | ReadGasValue () const |
| Gets the most recently processed gas value. More... | |
| virtual void | ProcessRawValues () override |
| Reads the sensor and computes the current gas value (PPM) from the RS/R0 ratio using the datasheet's log-log curve. More... | |
| virtual std::string | ToString () const override |
| Serializes the sensor's current gas value (PPM), a rough percentage, and the raw reading to a string. More... | |
| unsigned int | GetAnalogToDigitalConverterIndex () const |
| Gets the ADC channel index this sensor reads from. More... | |
| unsigned int | GetDigitalOutputPinNumber () const |
| Gets the digital threshold-output pin (UINT_MAX if unused). More... | |
| double | GetInitialValueR0 () const |
| Gets the sensor's clean-air resistance (R0) from calibration. More... | |
| double | GetResistorValueRL () const |
| Gets the load resistor value (RL) from the sensor's datasheet. More... | |
| double | GetCurrentValueRS () const |
| Gets the sensor's current resistance (RS). More... | |
| const char * | GetDetectibleConcentration () const |
| Gets a human-readable detectable-concentration range for this sensor. More... | |
| EComponentType | GetGasSensorType () const |
| Gets this sensor's component type (which MQ sensor it is). More... | |
| const char * | GetGasSensorTypeAsString () const |
| Gets this sensor's type as a display string (e.g. More... | |
| const char * | GetGasSensorTypeAsString (const EComponentType &gasSensorType) const |
| Maps a gas-sensor component type to its display string (e.g. More... | |
| IAnalogDigitalConverterComponent * | GetAnalogDigitalConverter () const |
| Gets the ADC this sensor reads its analog value through. More... | |
| const GasData & | GetGasData () const |
| Gets the per-gas curve data (slope/intercept and concentration points) for this sensor. More... | |
Public Member Functions inherited from RumPi::BaseComponent | |
| BaseComponent () | |
| Builds a base component with an unknown type and an empty name. More... | |
| virtual | ~BaseComponent () |
| Virtual destructor so deleting a component through a BaseComponent* also cleans up the concrete subclass. More... | |
| virtual void | TurnOn ()=0 |
| Turns the component on (e.g. More... | |
| virtual void | TurnOff ()=0 |
| Turns the component off. More... | |
| virtual std::string | ToString () const =0 |
| Serializes the component's current readings to a string (usually JSON). More... | |
| virtual void | ProcessRawValues ()=0 |
| Reads and processes the component's latest raw values (called once per poll). More... | |
| virtual EComponentType | GetComponentType () const |
| Gets the component's type. More... | |
| const std::string & | GetName () const |
| Gets the component's unique per-instance name. More... | |
| void | SetName (const std::string &name) |
| Sets the component's unique per-instance name. More... | |
Static Public Attributes | |
| static constexpr double | GAS_SENSOR_VOLTAGE = 5.0 |
| The supply voltage the gas sensor runs on. More... | |
| static const int | CALIBRATION_SAMPLE_TIMES = 200 |
| The number of samples taken during the calibration phase. More... | |
| static const int | CALIBRATION_SAMPLE_INTERVAL = 250 |
| Milliseconds between samples during the calibration phase. More... | |
| static const int | READ_SAMPLE_INTERVAL = 250 |
| Milliseconds between samples during normal operation. More... | |
Protected Member Functions | |
| virtual double | CalculateR0 () |
| Samples the sensor many times in clean air, averages the reading, converts it to resistance (RS), and divides by the clean-air factor to get R0. More... | |
| virtual double | CalculateRS (const double &sensorValueInVolts) const |
| Computes the sensor's resistance (RS) from its output voltage, using the load resistor RL and supply voltage. More... | |
| 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 voltage. More... | |
| double | ConvertRawSensorValueToVolts (const double &rawGasSensorValue) const |
| Converts a raw analog reading to its equivalent voltage (using the sensor's supply voltage and the ADC's maximum value). More... | |
| double | ConvertVoltsToRawSensorValue (const double &volts) const |
| Converts a voltage back to its equivalent raw analog value (the inverse of ConvertRawSensorValueToVolts). More... | |
| double | GetAnalogToDigitalConverterMaxRawValue () const |
| Gets the ADC's full-scale raw count for scaling raw readings to volts (PCF8591 = 255 8-bit, MCP3008 = 1023 10-bit), falling back to the 10-bit convention when no ADC is wired. More... | |
| double | GetAnalogToDigitalConverterVoltage () const |
| Gets the ADC's reference voltage for scaling raw readings to volts (defined by the wired ADC, e.g. More... | |
Protected Attributes | |
| unsigned int | myAnalogToDigitalConverterIndex |
| The ADC channel index this sensor reads its analog value from. More... | |
| unsigned int | myDigitalOutputPinNumber |
| The digital output (threshold) pin, or UINT_MAX if unused. More... | |
| double | myInitialValueR0 |
| The sensor's resistance in clean air (R0), from calibration. More... | |
| double | myResistorValueRL |
| The load resistor value (RL) from the sensor's datasheet. More... | |
| double | myCurrentValueRS |
| The sensor's current resistance (RS). More... | |
| double | myCleanAirFactor |
| The clean-air factor (RS/R0 in clean air) from the datasheet. More... | |
| double | myRawGasSensorValue |
| The most recent raw analog reading. More... | |
| double | myCurrentGasValue |
| The most recent computed gas value (PPM). More... | |
| const char * | myDetectibleConcentration |
| A human-readable detectable-concentration range for this sensor. More... | |
| IAnalogDigitalConverterComponent * | myAnalogToDigitalConverter |
| The ADC this sensor reads its analog value through. More... | |
| GasData | myGasData |
| Per-gas curve data (slope/intercept) used to convert the RS/R0 ratio to PPM. More... | |
Protected Attributes inherited from RumPi::BaseComponent | |
| EComponentType | myComponentType |
| The kind of component this is (set by each concrete subclass). More... | |
| std::string | myName |
| A unique per-instance name, assigned when the component is added to the manager. More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from RumPi::BaseComponent | |
| static std::string | ComponentTypeToString (const EComponentType theComponentTypeToConvert) |
| Maps an EComponentType to its string name. More... | |
| static EComponentType | StringToComponentType (const std::string &theComponentTypeName) |
| Maps a type name back to its EComponentType (the inverse of ComponentTypeToString), so the client can turn the type carried on the wire back into a value it can look an icon up with. More... | |
Base class for the MQ-series gas sensors (MQ2, MQ7, ...).
An MQ sensor's value only reflects the approximate trend of gas concentration within a permissible error range - it does NOT represent an exact concentration. The R0/RL/RS/CleanAirFactor references come from the various MQ-X datasheets (https://www.mysensors.org/build/gas/).
| RumPi::GasSensor::GasSensor | ( | ) |
Builds a default gas sensor with zeroed calibration values and no ADC or digital pin.
Subclasses (e.g. MQ2) fill in their datasheet values via Initialize().
| RumPi::GasSensor::GasSensor | ( | IAnalogDigitalConverterComponent * | analogToDigitalConverter, |
| const EComponentType & | gasSensorType, | ||
| const unsigned int & | analogToDigitalConverterIndex, | ||
| const unsigned int & | digitalOutputPinNumber | ||
| ) |
Builds an analog gas sensor that reads through an ADC and also exposes a digital threshold pin.
| [in] | analogToDigitalConverter | The ADC the sensor reads its analog value through. |
| [in] | gasSensorType | The concrete sensor type (e.g. E_MQ2). |
| [in] | analogToDigitalConverterIndex | The ADC channel index to read. |
| [in] | digitalOutputPinNumber | The digital threshold-output pin. |
| RumPi::GasSensor::GasSensor | ( | IAnalogDigitalConverterComponent * | analogToDigitalConverter, |
| const EComponentType & | gasSensorType, | ||
| const unsigned int & | analogToDigitalConverterIndex | ||
| ) |
Builds an analog-only gas sensor that reads through an ADC (no digital threshold pin).
| [in] | analogToDigitalConverter | The ADC the sensor reads its analog value through. |
| [in] | gasSensorType | The concrete sensor type (e.g. E_MQ2). |
| [in] | analogToDigitalConverterIndex | The ADC channel index to read. |
| RumPi::GasSensor::GasSensor | ( | const EComponentType & | gasSensorType, |
| const unsigned int & | digitalOutputPinNumber | ||
| ) |
Builds a digital-only gas sensor that reports only its threshold-output pin (no ADC).
| [in] | gasSensorType | The concrete sensor type (e.g. E_MQ2). |
| [in] | digitalOutputPinNumber | The digital threshold-output pin. |
|
virtual |
Destroys the gas sensor, turning it off first.
|
virtual |
Calibrates the sensor: computes and stores its clean-air resistance (R0).
Run this once in clean air before reading, so the RS/R0 ratio has a valid baseline. Public so callers can (re)calibrate after warm-up.
Run this in clean air.
|
protectedvirtual |
Samples the sensor many times in clean air, averages the reading, converts it to resistance (RS), and divides by the clean-air factor to get R0.
Reimplemented in RumPi::MQ135.
|
protectedvirtual |
Computes the sensor's resistance (RS) from its output voltage, using the load resistor RL and supply voltage.
| [in] | sensorValueInVolts | The sensor's output voltage. |
|
protectedvirtual |
Computes the RS/R0 ratio (used to look up PPM on the datasheet curve) from the sensor's output voltage.
| [in] | sensorValueInVolts | The sensor's output voltage. |
|
protected |
Converts a raw analog reading to its equivalent voltage (using the sensor's supply voltage and the ADC's maximum value).
Converts a raw analog reading to its equivalent voltage (using the ADC's reference voltage and the ADC's maximum value).
| [in] | rawGasSensorValue | The raw analog reading. |
|
protected |
Converts a voltage back to its equivalent raw analog value (the inverse of ConvertRawSensorValueToVolts).
| [in] | volts | The voltage to convert. |
| IAnalogDigitalConverterComponent * RumPi::GasSensor::GetAnalogDigitalConverter | ( | ) | const |
Gets the ADC this sensor reads its analog value through.
| unsigned int RumPi::GasSensor::GetAnalogToDigitalConverterIndex | ( | ) | const |
Gets the ADC channel index this sensor reads from.
|
protected |
Gets the ADC's full-scale raw count for scaling raw readings to volts (PCF8591 = 255 8-bit, MCP3008 = 1023 10-bit), falling back to the 10-bit convention when no ADC is wired.
|
protected |
Gets the ADC's reference voltage for scaling raw readings to volts (defined by the wired ADC, e.g.
Gets the ADC's reference voltage for scaling raw readings to volts, falling back to the gas sensor's supply voltage when no ADC is wired.
the PCF8591's configured "adcVoltage"), falling back to the gas sensor's supply voltage when no ADC is wired.
| double RumPi::GasSensor::GetCurrentValueRS | ( | ) | const |
Gets the sensor's current resistance (RS).
| const char * RumPi::GasSensor::GetDetectibleConcentration | ( | ) | const |
Gets a human-readable detectable-concentration range for this sensor.
| unsigned int RumPi::GasSensor::GetDigitalOutputPinNumber | ( | ) | const |
Gets the digital threshold-output pin (UINT_MAX if unused).
| const GasData & RumPi::GasSensor::GetGasData | ( | ) | const |
Gets the per-gas curve data (slope/intercept and concentration points) for this sensor.
| EComponentType RumPi::GasSensor::GetGasSensorType | ( | ) | const |
Gets this sensor's component type (which MQ sensor it is).
| const char * RumPi::GasSensor::GetGasSensorTypeAsString | ( | ) | const |
Gets this sensor's type as a display string (e.g.
"MQ-2").
| const char * RumPi::GasSensor::GetGasSensorTypeAsString | ( | const EComponentType & | gasSensorType | ) | const |
Maps a gas-sensor component type to its display string (e.g.
E_MQ2 -> "MQ-2").
| [in] | gasSensorType | The gas-sensor type to convert. |
| double RumPi::GasSensor::GetInitialValueR0 | ( | ) | const |
Gets the sensor's clean-air resistance (R0) from calibration.
| double RumPi::GasSensor::GetResistorValueRL | ( | ) | const |
Gets the load resistor value (RL) from the sensor's datasheet.
|
virtual |
Initializes the sensor's datasheet values.
The base does nothing; each MQ subclass overrides this to set its RL, clean-air factor, detectable concentration, and gas curve.
Reimplemented in RumPi::MQ135, RumPi::MQ2, RumPi::MQ3, RumPi::MQ4, RumPi::MQ5, RumPi::MQ6, RumPi::MQ7, RumPi::MQ8, and RumPi::MQ9.
|
overridevirtual |
Reads the sensor and computes the current gas value (PPM) from the RS/R0 ratio using the datasheet's log-log curve.
Warns and reports zero if the gas curve slope is zero (which would divide by zero).
Implements RumPi::BaseComponent.
| int RumPi::GasSensor::ReadDigitalValue | ( | ) |
Reads the sensor's digital threshold-output pin (1 when the configured gas threshold is exceeded).
|
virtual |
Gets the most recently processed gas value.
MQ subclasses may override this to compute a gas-specific value.
Reimplemented in RumPi::MQ135, RumPi::MQ3, RumPi::MQ4, RumPi::MQ5, RumPi::MQ6, RumPi::MQ7, RumPi::MQ8, and RumPi::MQ9.
| int RumPi::GasSensor::ReadRawAnalogValue | ( | ) | const |
Reads the raw analog value from the sensor's ADC channel.
|
overridevirtual |
Serializes the sensor's current gas value (PPM), a rough percentage, and the raw reading to a string.
Implements RumPi::BaseComponent.
Reimplemented in RumPi::MQ135, RumPi::MQ3, RumPi::MQ4, RumPi::MQ5, RumPi::MQ6, RumPi::MQ7, RumPi::MQ8, and RumPi::MQ9.
|
virtual |
Turns the sensor off.
No active teardown is needed, so this is a no-op.
Implements RumPi::BaseComponent.
|
virtual |
Turns the sensor on.
If a digital threshold pin is configured, sets it as an input.
Implements RumPi::BaseComponent.
|
static |
Milliseconds between samples during the calibration phase.
|
static |
The number of samples taken during the calibration phase.
|
staticconstexpr |
The supply voltage the gas sensor runs on.
|
protected |
The ADC this sensor reads its analog value through.
|
protected |
The ADC channel index this sensor reads its analog value from.
|
protected |
The clean-air factor (RS/R0 in clean air) from the datasheet.
|
protected |
The most recent computed gas value (PPM).
|
protected |
The sensor's current resistance (RS).
|
protected |
A human-readable detectable-concentration range for this sensor.
|
protected |
The digital output (threshold) pin, or UINT_MAX if unused.
|
protected |
Per-gas curve data (slope/intercept) used to convert the RS/R0 ratio to PPM.
|
protected |
The sensor's resistance in clean air (R0), from calibration.
|
protected |
The most recent raw analog reading.
|
protected |
The load resistor value (RL) from the sensor's datasheet.
|
static |
Milliseconds between samples during normal operation.