Merge pull request #3 from CAHEK7/cleanup

Various cleanups
This commit is contained in:
Igor89 2016-09-16 19:44:14 +03:00 committed by GitHub
commit 61dc0f9349
18 changed files with 59 additions and 103 deletions

View File

@ -1,14 +1,14 @@
#include "BaseMQ.h"
BaseMQ::BaseMQ(uint8_t pin) {
_pin = pin;
BaseMQ::BaseMQ(uint8_t pin) :
_pin (pin) {
}
BaseMQ::BaseMQ(uint8_t pin, uint8_t pinHeater) {
_pin = pin;
_pinHeater = pinHeater;
BaseMQ::BaseMQ(uint8_t pin, uint8_t pinHeater) :
_pin (pin),
_pinHeater (pinHeater)
{
pinMode(_pinHeater, OUTPUT);
_prMillis = 0;
}
// калибровка датчика
@ -26,8 +26,7 @@ void BaseMQ::calibrate() {
}
ro = ro/MQ_SAMPLE_TIMES;
ro = ro/getRoInCleanAir();
_ro = ro;
_stateCalibrate = true;
calibrate (ro);
}
void BaseMQ::heaterPwrHigh() {
@ -49,14 +48,14 @@ void BaseMQ::heaterPwrOff() {
}
// сопротивление датчика
float BaseMQ::calculateResistance(int rawAdc) {
float BaseMQ::calculateResistance(int rawAdc) const {
float vrl = rawAdc*(5.0 / 1023);
float rsAir = (5.0 - vrl)/vrl*getRL();
return rsAir;
}
// считывание датчика
float BaseMQ::readRs() {
float BaseMQ::readRs() const {
float rs = 0;
for (int i = 0; i < MQ_SAMPLE_TIMES; i++) {
rs += calculateResistance(analogRead(_pin));
@ -66,26 +65,23 @@ float BaseMQ::readRs() {
return rs;
}
float BaseMQ::getRo() {
return _ro;
unsigned long BaseMQ::readScaled(float a, float b) const {
float ratio = readRatio();
return exp((log(ratio)-b)/a);
}
bool BaseMQ::isCalibrated() {
return _stateCalibrate;
float BaseMQ::readRatio() const {
return readRs()/getRo();
}
float BaseMQ::readRatio() {
return readRs()/_ro;
}
bool BaseMQ::heatingCompleted() {
bool BaseMQ::heatingCompleted() const {
if ((_heater) && (!_cooler) && (millis() - _prMillis > 60000))
return true;
else
return false;
}
bool BaseMQ::coolanceCompleted() {
bool BaseMQ::coolanceCompleted() const {
if ((_heater) && (_cooler) && (millis() - _prMillis > 90000))
return true;
else

View File

@ -16,24 +16,32 @@ public:
void heaterPwrLow();
void heaterPwrOff();
void cycleHeat();
bool heatingCompleted();
bool coolanceCompleted();
bool isCalibrated();
bool atHeatCycleEnd();
float getRo();
float readRatio();
bool heatingCompleted() const;
bool coolanceCompleted() const;
float readRatio() const;
inline bool isCalibrated() const {
return _stateCalibrate;
};
inline float getRo() const {
return _ro;
};
protected:
unsigned long readScaled(float a, float b) const;
virtual float getRoInCleanAir() const = 0;
virtual int getRL() const = 0;
private:
bool _heater = false;
bool _cooler = false;
bool _stateCalibrate = false;
unsigned long _prMillis;
float _ro;
unsigned long _prMillis = 0;
float _ro = 1.0f;
uint8_t _pin;
uint8_t _pinHeater;
float readRs();
float calculateResistance(int rawAdc);
virtual float getRoInCleanAir() const = 0;
virtual int getRL() const = 0;
float readRs() const;
float calculateResistance(int rawAdc) const;
};
#endif
#endif

13
MQ2.cpp
View File

@ -9,22 +9,17 @@ MQ2::MQ2(uint8_t pin, uint8_t pinHeater)
}
unsigned long MQ2::readLPG() {
return readPpm(-0.45, 2.95);
return readScaled(-0.45, 2.95);
}
unsigned long MQ2::readMethane() {
return readPpm(-0.38, 3.21);
return readScaled(-0.38, 3.21);
}
unsigned long MQ2::readSmoke() {
return readPpm(-0.42, 3.54);
return readScaled(-0.42, 3.54);
}
unsigned long MQ2::readHydrogen() {
return readPpm(-0.48, 3.32);
}
int MQ2::readPpm(float a, float b) {
float ratio = readRs()/_ro;
return pow(M_E, (log(ratio)-b)/a);
return readScaled(-0.48, 3.32);
}

3
MQ2.h
View File

@ -12,11 +12,10 @@ public:
unsigned long readSmoke();
unsigned long readHydrogen();
private:
int readPpm(float a, float b);
// Резистор установленный на плату (кОм)
virtual int getRL() const { return 5; }
// коефициент чистого воздуха
virtual float getRoInCleanAir() const { return 9.83; }
};
#endif // MQ2_H_
#endif // MQ2_H_

View File

@ -9,14 +9,9 @@ MQ3::MQ3(uint8_t pin, uint8_t pinHeater)
}
float MQ3::readAlcoholMgL() {
return readMgL(-0.66, -0.62);
return readScaled(-0.66, -0.62);
}
float MQ3::readAlcoholPpm() {
return readMgL(-0.66, -0.62)*2.2;
}
float MQ3::readMgL(float a, float b) {
float ratio = readRs()/_ro;
return pow(M_E, (log(ratio)-b)/a);
return readScaled(-0.66, -0.62)*2.2;
}

3
MQ3.h
View File

@ -10,11 +10,10 @@ public:
float readAlcoholMgL();
float readAlcoholPpm();
private:
float readMgL(float a, float b);
// Резистор установленный на плату (кОм)
virtual int getRL() const { return 200; }
// коефициент чистого воздуха
virtual float getRoInCleanAir() const { return 60; }
};
#endif // MQ3_H_
#endif // MQ3_H_

View File

@ -9,10 +9,5 @@ MQ4::MQ4(uint8_t pin, uint8_t pinHeater)
}
unsigned long MQ4::readMethane() {
return readPpm(-0.36, 2.54);
}
int MQ4::readPpm(float a, float b) {
float ratio = readRs()/_ro;
return pow(M_E, (log(ratio)-b)/a);
return readScaled(-0.36, 2.54);
}

3
MQ4.h
View File

@ -9,11 +9,10 @@ public:
MQ4(uint8_t pin, uint8_t pinHeater);
unsigned long readMethane();
private:
int readPpm(float a, float b);
// Резистор установленный на плату (кОм)
virtual int getRL() const { return 20; }
// коефициент чистого воздуха
virtual float getRoInCleanAir() const { return 4.4; }
};
#endif // MQ4_H_
#endif // MQ4_H_

View File

@ -9,14 +9,9 @@ MQ5::MQ5(uint8_t pin, uint8_t pinHeater)
}
unsigned long MQ5::readLPG() {
return readPpm(-0.39, 1.73);
return readScaled(-0.39, 1.73);
}
unsigned long MQ5::readMethane() {
return readPpm(-0.38, 1.97);
}
int MQ5::readPpm(float a, float b) {
float ratio = readRs()/_ro;
return pow(M_E, (log(ratio)-b)/a);
return readScaled(-0.38, 1.97);
}

3
MQ5.h
View File

@ -10,11 +10,10 @@ public:
unsigned long readLPG();
unsigned long readMethane();
private:
int readPpm(float a, float b);
// Резистор установленный на плату (кОм)
virtual int getRL() const { return 20; }
// коефициент чистого воздуха
virtual float getRoInCleanAir() const { return 6.5; }
};
#endif // MQ5_H_
#endif // MQ5_H_

View File

@ -9,10 +9,5 @@ MQ6::MQ6(uint8_t pin, uint8_t pinHeater)
}
unsigned long MQ6::readLPG() {
return readPpm(-0.42, 2.91);
}
int MQ6::readPpm(float a, float b) {
float ratio = readRs()/_ro;
return pow(M_E, (log(ratio)-b)/a);
return readScaled(-0.42, 2.91);
}

3
MQ6.h
View File

@ -9,11 +9,10 @@ public:
MQ6(uint8_t pin, uint8_t pinHeater);
unsigned long readLPG();
private:
int readPpm(float a, float b);
// Резистор установленный на плату (кОм)
virtual int getRL() const { return 20; }
// коефициент чистого воздуха
virtual float getRoInCleanAir() const { return 10; }
};
#endif // MQ6_H_
#endif // MQ6_H_

View File

@ -9,10 +9,5 @@ MQ7::MQ7(uint8_t pin, uint8_t pinHeater)
}
unsigned long MQ7::readCarbonMonoxide() {
return readPpm(-0.77, 3.38);
}
int MQ7::readPpm(float a, float b) {
float ratio = readRs()/_ro;
return pow(M_E, (log(ratio)-b)/a);
return readScaled(-0.77, 3.38);
}

3
MQ7.h
View File

@ -9,11 +9,10 @@ public:
MQ7(uint8_t pin, uint8_t pinHeater);
unsigned long readCarbonMonoxide();
private:
int readPpm(float a, float b);
// Резистор установленный на плату (кОм)
virtual int getRL() const { return 10; }
// коефициент чистого воздуха
virtual float getRoInCleanAir() const { return 27; }
};
#endif // MQ7_H_
#endif // MQ7_H_

View File

@ -9,10 +9,5 @@ MQ8::MQ8(uint8_t pin, uint8_t pinHeater)
}
unsigned long MQ8::readHydrogen() {
return readPpm(-1.52, 10.49);
}
int MQ8::readPpm(float a, float b) {
float ratio = readRs()/_ro;
return pow(M_E, (log(ratio)-b)/a);
return readScaled(-1.52, 10.49);
}

3
MQ8.h
View File

@ -9,11 +9,10 @@ public:
MQ8(uint8_t pin, uint8_t pinHeater);
unsigned long readHydrogen();
private:
int readPpm(float a, float b);
// Резистор установленный на плату (кОм)
virtual int getRL() const { return 10; }
// коефициент чистого воздуха
virtual float getRoInCleanAir() const { return 70; }
};
#endif // MQ8_H_
#endif // MQ8_H_

11
MQ9.cpp
View File

@ -9,18 +9,13 @@ MQ9::MQ9(uint8_t pin, uint8_t pinHeater)
}
unsigned long MQ9::readLPG() {
return readPpm(-0.48, 3.33);
return readScaled(-0.48, 3.33);
}
unsigned long MQ9::readMethane() {
return readPpm(-0.38, 3.21);
return readScaled(-0.38, 3.21);
}
unsigned long MQ9::readCarbonMonoxide() {
return readPpm(-0.48, 3.10);
}
int MQ9::readPpm(float a, float b) {
float ratio = readRs()/_ro;
return pow(M_E, (log(ratio)-b)/a);
return readScaled(-0.48, 3.10);
}

3
MQ9.h
View File

@ -11,11 +11,10 @@ public:
unsigned long readMethane();
unsigned long readCarbonMonoxide();
private:
int readPpm(float a, float b);
// Резистор установленный на плату (кОм)
virtual int getRL() const { return 10; }
// коефициент чистого воздуха
virtual float getRoInCleanAir() const { return 9.8; }
};
#endif // MQ9_H_
#endif // MQ9_H_