From c19b668a634a949ffc942a51176e316b76e48037 Mon Sep 17 00:00:00 2001 From: CAHEK7 Date: Fri, 26 Aug 2016 14:05:15 +0300 Subject: [PATCH 1/5] hold the same code in the base class --- BaseMQ.cpp | 5 +++++ BaseMQ.h | 3 ++- MQ2.cpp | 13 ++++--------- MQ2.h | 3 +-- MQ3.cpp | 9 ++------- MQ3.h | 3 +-- MQ4.cpp | 7 +------ MQ4.h | 3 +-- MQ5.cpp | 9 ++------- MQ5.h | 3 +-- MQ6.cpp | 7 +------ MQ6.h | 3 +-- MQ7.cpp | 7 +------ MQ7.h | 3 +-- MQ8.cpp | 7 +------ MQ8.h | 3 +-- MQ9.cpp | 11 +++-------- MQ9.h | 3 +-- 18 files changed, 30 insertions(+), 72 deletions(-) diff --git a/BaseMQ.cpp b/BaseMQ.cpp index c1daa38..3ac2fad 100644 --- a/BaseMQ.cpp +++ b/BaseMQ.cpp @@ -66,6 +66,11 @@ float BaseMQ::readRs() { return rs; } +int readScaled(float a, float b) { + float ratio = readRs()/_ro; + return pow(M_E, (log(ratio)-b)/a); +} + float BaseMQ::getRo() { return _ro; } diff --git a/BaseMQ.h b/BaseMQ.h index 6aaf1bc..d830d80 100644 --- a/BaseMQ.h +++ b/BaseMQ.h @@ -31,9 +31,10 @@ protected: uint8_t _pin; uint8_t _pinHeater; float readRs(); + int readScaled(float a, float b); float calculateResistance(int rawAdc); virtual float getRoInCleanAir() const = 0; virtual int getRL() const = 0; }; -#endif \ No newline at end of file +#endif diff --git a/MQ2.cpp b/MQ2.cpp index 6c0a29d..0b897de 100644 --- a/MQ2.cpp +++ b/MQ2.cpp @@ -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); } diff --git a/MQ2.h b/MQ2.h index a563c10..6f1e2bb 100644 --- a/MQ2.h +++ b/MQ2.h @@ -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_ \ No newline at end of file +#endif // MQ2_H_ diff --git a/MQ3.cpp b/MQ3.cpp index 2d4e321..71053b0 100644 --- a/MQ3.cpp +++ b/MQ3.cpp @@ -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; } diff --git a/MQ3.h b/MQ3.h index e796480..7bfc1fc 100644 --- a/MQ3.h +++ b/MQ3.h @@ -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_ \ No newline at end of file +#endif // MQ3_H_ diff --git a/MQ4.cpp b/MQ4.cpp index f377862..c63496e 100644 --- a/MQ4.cpp +++ b/MQ4.cpp @@ -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); } diff --git a/MQ4.h b/MQ4.h index a8b1f38..1555fd2 100644 --- a/MQ4.h +++ b/MQ4.h @@ -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_ \ No newline at end of file +#endif // MQ4_H_ diff --git a/MQ5.cpp b/MQ5.cpp index a462094..2ab0b1e 100644 --- a/MQ5.cpp +++ b/MQ5.cpp @@ -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); } diff --git a/MQ5.h b/MQ5.h index a2e3eda..30accb8 100644 --- a/MQ5.h +++ b/MQ5.h @@ -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_ \ No newline at end of file +#endif // MQ5_H_ diff --git a/MQ6.cpp b/MQ6.cpp index 05843fc..d60cb26 100644 --- a/MQ6.cpp +++ b/MQ6.cpp @@ -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); } diff --git a/MQ6.h b/MQ6.h index c474480..fb14bc6 100644 --- a/MQ6.h +++ b/MQ6.h @@ -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_ \ No newline at end of file +#endif // MQ6_H_ diff --git a/MQ7.cpp b/MQ7.cpp index dcb8563..abbc023 100644 --- a/MQ7.cpp +++ b/MQ7.cpp @@ -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); } diff --git a/MQ7.h b/MQ7.h index be281eb..cbda104 100644 --- a/MQ7.h +++ b/MQ7.h @@ -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_ \ No newline at end of file +#endif // MQ7_H_ diff --git a/MQ8.cpp b/MQ8.cpp index c2897d1..762e032 100644 --- a/MQ8.cpp +++ b/MQ8.cpp @@ -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); } diff --git a/MQ8.h b/MQ8.h index ceb18ea..fae336c 100644 --- a/MQ8.h +++ b/MQ8.h @@ -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_ \ No newline at end of file +#endif // MQ8_H_ diff --git a/MQ9.cpp b/MQ9.cpp index 95dd875..efc80ca 100644 --- a/MQ9.cpp +++ b/MQ9.cpp @@ -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); } diff --git a/MQ9.h b/MQ9.h index 4a4b28f..b375598 100644 --- a/MQ9.h +++ b/MQ9.h @@ -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_ \ No newline at end of file +#endif // MQ9_H_ From 7bf7d9df09856676305f8e3214add695dad4838d Mon Sep 17 00:00:00 2001 From: CAHEK7 Date: Fri, 26 Aug 2016 14:06:39 +0300 Subject: [PATCH 2/5] use base class methods --- BaseMQ.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/BaseMQ.cpp b/BaseMQ.cpp index 3ac2fad..5c91101 100644 --- a/BaseMQ.cpp +++ b/BaseMQ.cpp @@ -26,8 +26,7 @@ void BaseMQ::calibrate() { } ro = ro/MQ_SAMPLE_TIMES; ro = ro/getRoInCleanAir(); - _ro = ro; - _stateCalibrate = true; + calibrate (ro); } void BaseMQ::heaterPwrHigh() { @@ -67,7 +66,7 @@ float BaseMQ::readRs() { } int readScaled(float a, float b) { - float ratio = readRs()/_ro; + float ratio = readRatio(); return pow(M_E, (log(ratio)-b)/a); } @@ -80,7 +79,7 @@ bool BaseMQ::isCalibrated() { } float BaseMQ::readRatio() { - return readRs()/_ro; + return readRs()/getRo(); } bool BaseMQ::heatingCompleted() { From d948cdb265aa0c82a7b9ef3575cadad95912188c Mon Sep 17 00:00:00 2001 From: CAHEK7 Date: Fri, 26 Aug 2016 14:09:24 +0300 Subject: [PATCH 3/5] actually pow(M_E, x) is exp(x) --- BaseMQ.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BaseMQ.cpp b/BaseMQ.cpp index 5c91101..7a7f0c7 100644 --- a/BaseMQ.cpp +++ b/BaseMQ.cpp @@ -67,7 +67,7 @@ float BaseMQ::readRs() { int readScaled(float a, float b) { float ratio = readRatio(); - return pow(M_E, (log(ratio)-b)/a); + return exp((log(ratio)-b)/a); } float BaseMQ::getRo() { From 7881a1c58f459adda159808328a3c06bb88662b1 Mon Sep 17 00:00:00 2001 From: CAHEK7 Date: Fri, 26 Aug 2016 14:26:07 +0300 Subject: [PATCH 4/5] replace return value to 'unsigned long' since all the functions return that type, not 'int' (type size and signedness mismatch) --- BaseMQ.cpp | 2 +- BaseMQ.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/BaseMQ.cpp b/BaseMQ.cpp index 7a7f0c7..d289a1b 100644 --- a/BaseMQ.cpp +++ b/BaseMQ.cpp @@ -65,7 +65,7 @@ float BaseMQ::readRs() { return rs; } -int readScaled(float a, float b) { +unsigned long readScaled(float a, float b) { float ratio = readRatio(); return exp((log(ratio)-b)/a); } diff --git a/BaseMQ.h b/BaseMQ.h index d830d80..2093b18 100644 --- a/BaseMQ.h +++ b/BaseMQ.h @@ -31,7 +31,7 @@ protected: uint8_t _pin; uint8_t _pinHeater; float readRs(); - int readScaled(float a, float b); + unsigned long readScaled(float a, float b); float calculateResistance(int rawAdc); virtual float getRoInCleanAir() const = 0; virtual int getRL() const = 0; From 94d34c6e4af42c2aefb9b878d259b77f99c9a5bb Mon Sep 17 00:00:00 2001 From: CAHEK7 Date: Fri, 26 Aug 2016 14:43:01 +0300 Subject: [PATCH 5/5] make proper class declaration --- BaseMQ.cpp | 32 ++++++++++++-------------------- BaseMQ.h | 31 +++++++++++++++++++------------ 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/BaseMQ.cpp b/BaseMQ.cpp index d289a1b..2395a7a 100644 --- a/BaseMQ.cpp +++ b/BaseMQ.cpp @@ -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; } // калибровка датчика @@ -48,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)); @@ -65,31 +65,23 @@ float BaseMQ::readRs() { return rs; } -unsigned long readScaled(float a, float b) { +unsigned long BaseMQ::readScaled(float a, float b) const { float ratio = readRatio(); return exp((log(ratio)-b)/a); } -float BaseMQ::getRo() { - return _ro; -} - -bool BaseMQ::isCalibrated() { - return _stateCalibrate; -} - -float BaseMQ::readRatio() { +float BaseMQ::readRatio() const { return readRs()/getRo(); } -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 diff --git a/BaseMQ.h b/BaseMQ.h index 2093b18..d80cab1 100644 --- a/BaseMQ.h +++ b/BaseMQ.h @@ -16,25 +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(); - unsigned long readScaled(float a, float b); - float calculateResistance(int rawAdc); - virtual float getRoInCleanAir() const = 0; - virtual int getRL() const = 0; + float readRs() const; + float calculateResistance(int rawAdc) const; }; #endif