From a98d77e0c3694197bf8cfc57cd4dd28a56b353bc Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Sat, 2 Nov 2024 11:02:36 +0700 Subject: [PATCH] slr pm2.5 correction implementation --- src/PMS/PMS.cpp | 30 ++++++++++++++++++++++++++++++ src/PMS/PMS.h | 1 + src/PMS/PMS5003.cpp | 4 ++++ src/PMS/PMS5003.h | 1 + src/PMS/PMS5003T.cpp | 4 ++++ src/PMS/PMS5003T.h | 1 + 6 files changed, 41 insertions(+) diff --git a/src/PMS/PMS.cpp b/src/PMS/PMS.cpp index 88f17bb..feeb4b4 100644 --- a/src/PMS/PMS.cpp +++ b/src/PMS/PMS.cpp @@ -314,6 +314,36 @@ int PMSBase::pm25ToAQI(int pm02) { return 500; } + +/** + * @brief SLR correction for PM2.5 + * + * Reference: https://www.airgradient.com/blog/low-readings-from-pms5003/ + * + * @param pm25 PM2.5 raw value + * @param pm003Count PM0.3 count + * @param scalingFactor Scaling factor + * @param intercept Intercept + * @return float Calibrated PM2.5 value + */ +float PMSBase::slrCorrection(float pm25, float pm003Count, float scalingFactor, float intercept) { + float calibrated; + + float lowCalibrated = (scalingFactor * pm003Count) + intercept; + if (lowCalibrated < 31) { + calibrated = lowCalibrated; + } else { + calibrated = pm25; + } + + // No negative value for pm2.5 + if (calibrated < 0) { + return 0.0; + } + + return calibrated; +} + /** * @brief Correction PM2.5 * diff --git a/src/PMS/PMS.h b/src/PMS/PMS.h index 460a87a..c363c69 100644 --- a/src/PMS/PMS.h +++ b/src/PMS/PMS.h @@ -39,6 +39,7 @@ public: uint8_t getErrorCode(void); int pm25ToAQI(int pm02); + float slrCorrection(float pm25, float pm003Count, float scalingFactor, float intercept); float compensate(float pm25, float humidity); private: diff --git a/src/PMS/PMS5003.cpp b/src/PMS/PMS5003.cpp index 974bb18..d20c4e9 100644 --- a/src/PMS/PMS5003.cpp +++ b/src/PMS/PMS5003.cpp @@ -172,6 +172,10 @@ int PMS5003::getPm10ParticleCount(void) { return pms.getCount10(); } */ int PMS5003::convertPm25ToUsAqi(int pm25) { return pms.pm25ToAQI(pm25); } +float PMS5003::slrCorrection(float pm25, float pm003Count, float scalingFactor, float intercept) { + return pms.slrCorrection(pm25, pm003Count, scalingFactor, intercept); +} + /** * @brief Correct PM2.5 * diff --git a/src/PMS/PMS5003.h b/src/PMS/PMS5003.h index 594964e..bd2cf7b 100644 --- a/src/PMS/PMS5003.h +++ b/src/PMS/PMS5003.h @@ -42,6 +42,7 @@ public: int getPm10ParticleCount(void); int convertPm25ToUsAqi(int pm25); + float slrCorrection(float pm25, float pm003Count, float scalingFactor, float intercept); float compensate(float pm25, float humidity); int getFirmwareVersion(void); uint8_t getErrorCode(void); diff --git a/src/PMS/PMS5003T.cpp b/src/PMS/PMS5003T.cpp index 27d6313..e50d58a 100644 --- a/src/PMS/PMS5003T.cpp +++ b/src/PMS/PMS5003T.cpp @@ -205,6 +205,10 @@ float PMS5003T::getRelativeHumidity(void) { return pms.getHum() / 10.0f; } +float PMS5003T::slrCorrection(float pm25, float pm003Count, float scalingFactor, float intercept) { + return pms.slrCorrection(pm25, pm003Count, scalingFactor, intercept); +} + /** * @brief Correct PM2.5 * diff --git a/src/PMS/PMS5003T.h b/src/PMS/PMS5003T.h index 73017e7..4f09e8b 100644 --- a/src/PMS/PMS5003T.h +++ b/src/PMS/PMS5003T.h @@ -45,6 +45,7 @@ public: int convertPm25ToUsAqi(int pm25); float getTemperature(void); float getRelativeHumidity(void); + float slrCorrection(float pm25, float pm003Count, float scalingFactor, float intercept); float compensate(float pm25, float humidity); int getFirmwareVersion(void); uint8_t getErrorCode(void);