mirror of
https://github.com/miguel5612/MQSensorsLib.git
synced 2025-07-25 21:51:04 +03:00
Updated regression model selection
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
#include "MQUnifiedsensor.h"
|
||||
|
||||
MQUnifiedsensor::MQUnifiedsensor(String Placa, double Voltage_Resolution, int ADC_Bit_Resolution, int pin, String type) {
|
||||
MQUnifiedsensor::MQUnifiedsensor(String Placa, float Voltage_Resolution, int ADC_Bit_Resolution, int pin, String type) {
|
||||
this->_pin = pin;
|
||||
this->_type = type; //MQ-2, MQ-3 ... MQ-309A
|
||||
this->_placa = Placa;
|
||||
Placa.toCharArray(this->_placa, 20);
|
||||
type.toCharArray(this->_type, 6);
|
||||
//this->_type = type; //MQ-2, MQ-3 ... MQ-309A
|
||||
//this->_placa = Placa;
|
||||
this-> _VOLT_RESOLUTION = Voltage_Resolution;
|
||||
this-> _ADC_Bit_Resolution = ADC_Bit_Resolution;
|
||||
}
|
||||
@ -11,33 +13,48 @@ void MQUnifiedsensor::init()
|
||||
{
|
||||
pinMode(_pin, INPUT);
|
||||
}
|
||||
void MQUnifiedsensor::setA(double a) {
|
||||
void MQUnifiedsensor::setA(float a) {
|
||||
this->_a = a;
|
||||
}
|
||||
void MQUnifiedsensor::setB(double b) {
|
||||
void MQUnifiedsensor::setB(float b) {
|
||||
this->_b = b;
|
||||
}
|
||||
void MQUnifiedsensor::setR0(double R0) {
|
||||
void MQUnifiedsensor::setR0(float R0) {
|
||||
this->_R0 = R0;
|
||||
}
|
||||
void MQUnifiedsensor::setRL(double RL) {
|
||||
void MQUnifiedsensor::setRL(float RL) {
|
||||
this->_RL = RL;
|
||||
}
|
||||
void MQUnifiedsensor::setVoltResolution(double voltage_resolution)
|
||||
void MQUnifiedsensor::setVoltResolution(float voltage_resolution)
|
||||
{
|
||||
_VOLT_RESOLUTION = voltage_resolution;
|
||||
}
|
||||
void MQUnifiedsensor::setRegressionMethod(String regressionMethod)
|
||||
void MQUnifiedsensor::setRegressionMethod(int regressionMethod)
|
||||
{
|
||||
//this->_regressionMethod = regressionMethod;
|
||||
this->_regressionMethod = regressionMethod;
|
||||
}
|
||||
|
||||
double MQUnifiedsensor::getR0() {
|
||||
float MQUnifiedsensor::getR0() {
|
||||
return _R0;
|
||||
}
|
||||
double MQUnifiedsensor::getRL() {
|
||||
float MQUnifiedsensor::getRL() {
|
||||
return _RL;
|
||||
}
|
||||
float MQUnifiedsensor::getVoltResolution()
|
||||
{
|
||||
return _VOLT_RESOLUTION;
|
||||
}
|
||||
String MQUnifiedsensor::getRegressionMethod()
|
||||
{
|
||||
if(_regressionMethod == 1) return "Exponential";
|
||||
else return "Linear";
|
||||
}
|
||||
float MQUnifiedsensor::getA() {
|
||||
return _a;
|
||||
}
|
||||
float MQUnifiedsensor::getB() {
|
||||
return _b;
|
||||
}
|
||||
void MQUnifiedsensor::serialDebug(bool onSetup)
|
||||
{
|
||||
if(onSetup)
|
||||
@ -52,33 +69,33 @@ void MQUnifiedsensor::serialDebug(bool onSetup)
|
||||
Serial.println("Authors: Miguel A. Califa U - Yersson R. Carrillo A - Ghiordy F. Contreras C");
|
||||
Serial.println("Contributors: Andres A. Martinez - Juan A. Rodríguez - Mario A. Rodríguez O ");
|
||||
|
||||
Serial.println("Sensor: " + _type);
|
||||
Serial.print("Sensor: "); Serial.println(_type);
|
||||
Serial.print("Supply voltage: "); Serial.print(_VOLT_RESOLUTION); Serial.println(" VDC");
|
||||
Serial.print("ADC Resolution: "); Serial.print(_ADC_Bit_Resolution); Serial.println(" Bits");
|
||||
Serial.print("R0: "); Serial.print(_R0); Serial.println(" KΩ");
|
||||
Serial.print("RL: "); Serial.print(_RL); Serial.println(" KΩ");
|
||||
|
||||
Serial.print("Model: "); if(_regressionMethod == "Exponential") Serial.println("Exponential"); else Serial.println("Linear");
|
||||
Serial.print(_type + " -> " + "a: "); Serial.print(_a); Serial.print(" | b: "); Serial.println(_b);
|
||||
Serial.print(_type); Serial.print(" -> a: "); Serial.print(_a); Serial.print(" | b: "); Serial.println(_b);
|
||||
|
||||
Serial.println("Development board: " + _placa);
|
||||
Serial.print("Development board: "); Serial.println(_placa);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!_firstFlag)
|
||||
{
|
||||
Serial.println("| ********************************************************************" + _type + "*********************************************************************|");
|
||||
Serial.print("| ********************************************************************"); Serial.print(_type); Serial.println("*********************************************************************|");
|
||||
Serial.println("|ADC_In | Equation_V_ADC | Voltage_ADC | Equation_RS | Resistance_RS | EQ_Ratio | Ratio (RS/R0) | Equation_PPM | PPM |");
|
||||
_firstFlag = true; //Headers are printed
|
||||
}
|
||||
else
|
||||
{
|
||||
String eq = "";
|
||||
if(_regressionMethod == "Linear") eq = "ratio*a + b";
|
||||
if(_regressionMethod == "Exponential") eq = "a*ratio^b";
|
||||
Serial.print("|"); Serial.print(_adc); Serial.print("| v = ADC*"); Serial.print(_VOLT_RESOLUTION); Serial.print("/"); Serial.print(pow(2, _ADC_Bit_Resolution)); Serial.print(" | "); Serial.print(_sensor_volt);
|
||||
Serial.print(" | RS = ((" ); Serial.print(_VOLT_RESOLUTION ); Serial.print("*RL)/Voltage) - RL| "); Serial.print(_RS_Calc); Serial.print(" | Ratio = RS/R0| ");
|
||||
Serial.print(_ratio); Serial.print( " | " + eq + " | "); Serial.print(_PPM); Serial.println(" |");
|
||||
Serial.print(_ratio); Serial.print( " | ");
|
||||
if(_regressionMethod != 1) Serial.println("ratio*a + b");
|
||||
else Serial.println("a*ratio^b");
|
||||
Serial.print(" | "); Serial.print(_PPM); Serial.println(" |");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -86,11 +103,11 @@ void MQUnifiedsensor::update()
|
||||
{
|
||||
_sensor_volt = this->getVoltage();
|
||||
}
|
||||
float MQUnifiedsensor::calculatePPM(double ratio)
|
||||
float MQUnifiedsensor::validateEcuation(float ratioInput)
|
||||
{
|
||||
//Usage of this function: Unit test on ALgorithmTester example;
|
||||
if(_regressionMethod == "Exponential") _PPM= _a*pow(ratio, _b);
|
||||
if(_regressionMethod == "Linear") _PPM= _a*ratio + _b;
|
||||
if(_regressionMethod != 1) _PPM= _a*pow(ratioInput, _b);
|
||||
else _PPM= _a*ratioInput + _b;
|
||||
return _PPM;
|
||||
}
|
||||
float MQUnifiedsensor::readSensor()
|
||||
@ -100,8 +117,8 @@ float MQUnifiedsensor::readSensor()
|
||||
if(_RS_Calc < 0) _RS_Calc = 0; //No negative values accepted.
|
||||
_ratio = _RS_Calc / this->_R0; // Get ratio RS_gas/RS_air
|
||||
if(_ratio <= 0) _ratio = 0; //No negative values accepted or upper datasheet recomendation.
|
||||
if(_regressionMethod == "Exponential") _PPM= _a*pow(_ratio, _b);
|
||||
if(_regressionMethod == "Linear") _PPM= _a*_ratio + _b;
|
||||
if(_regressionMethod != 1) _PPM= _a*pow(_ratio, _b);
|
||||
else _PPM= _a*_ratio + _b;
|
||||
if(_PPM < 0) _PPM = 0; //No negative values accepted or upper datasheet recomendation.
|
||||
//if(_PPM > 10000) _PPM = 99999999; //No negative values accepted or upper datasheet recomendation.
|
||||
return _PPM;
|
||||
@ -127,11 +144,11 @@ float MQUnifiedsensor::calibrate(float ratioInCleanAir) {
|
||||
if(R0 < 0) R0 = 0; //No negative values accepted.
|
||||
return R0;
|
||||
}
|
||||
double MQUnifiedsensor::getVoltage(int read) {
|
||||
double voltage;
|
||||
float MQUnifiedsensor::getVoltage(int read) {
|
||||
float voltage;
|
||||
if(read)
|
||||
{
|
||||
double avg = 0.0;
|
||||
float avg = 0.0;
|
||||
for (int i = 0; i < retries; i ++) {
|
||||
_adc = analogRead(this->_pin);
|
||||
avg += _adc;
|
||||
@ -145,7 +162,7 @@ double MQUnifiedsensor::getVoltage(int read) {
|
||||
}
|
||||
return voltage;
|
||||
}
|
||||
double MQUnifiedsensor::stringToDouble(String & str)
|
||||
float MQUnifiedsensor::stringTofloat(String & str)
|
||||
{
|
||||
return atof( str.c_str() );
|
||||
}
|
@ -13,30 +13,34 @@
|
||||
class MQUnifiedsensor
|
||||
{
|
||||
public:
|
||||
MQUnifiedsensor(String Placa = "Arduino", double Voltage_Resolution = 5, int ADC_Bit_Resolution = 10, int pin = 1, String type = "CUSTOM MQ");
|
||||
MQUnifiedsensor(String Placa = "Arduino", float Voltage_Resolution = 5, int ADC_Bit_Resolution = 10, int pin = 1, String type = "CUSTOM MQ");
|
||||
|
||||
//Functions to set values
|
||||
void init();
|
||||
void update();
|
||||
void setR0(double R0 = 10);
|
||||
void setRL(double RL = 10);
|
||||
void setA(double a);
|
||||
void setB(double b);
|
||||
void setRegressionMethod(String regressionMethod);
|
||||
void setVoltResolution(double voltage_resolution = 5);
|
||||
void setR0(float R0 = 10);
|
||||
void setRL(float RL = 10);
|
||||
void setA(float a);
|
||||
void setB(float b);
|
||||
void setRegressionMethod(int regressionMethod);
|
||||
void setVoltResolution(float voltage_resolution = 5);
|
||||
void serialDebug(bool onSetup = false); //Show on serial port information about sensor
|
||||
|
||||
//user functions
|
||||
float calibrate(float ratioInCleanAir);
|
||||
float readSensor();
|
||||
float calculatePPM(double ratio = 0);
|
||||
float validateEcuation(float ratioInput = 0);
|
||||
|
||||
//get function for info
|
||||
double getR0();
|
||||
double getRL();
|
||||
double getVoltage(int read = true);
|
||||
float getA();
|
||||
float getB();
|
||||
float getR0();
|
||||
float getRL();
|
||||
float getVoltResolution();
|
||||
String getRegressionMethod();
|
||||
float getVoltage(int read = true);
|
||||
|
||||
double stringToDouble(String & str);
|
||||
float stringTofloat(String & str);
|
||||
|
||||
private:
|
||||
/************************Private vars************************************/
|
||||
@ -45,13 +49,13 @@ class MQUnifiedsensor
|
||||
byte _VOLT_RESOLUTION = 5.0; // if 3.3v use 3.3
|
||||
byte _RL = 10; //Value in KiloOhms
|
||||
byte _ADC_Bit_Resolution = 10;
|
||||
byte _regressionMethod = 1; // 1 -> Exponential || 2 -> Linear
|
||||
|
||||
double _adc, _a, _b, _sensor_volt;
|
||||
float _adc, _a, _b, _sensor_volt;
|
||||
float _R0, RS_air, _ratio, _PPM, _RS_Calc;
|
||||
|
||||
String _type;
|
||||
String _placa;
|
||||
String _regressionMethod;
|
||||
char _type[6];
|
||||
char _placa[20];
|
||||
};
|
||||
|
||||
#endif //MQUnifiedsensor_H
|
||||
|
Reference in New Issue
Block a user