From ec48f21b5e7c6a3a7a668d568665b836a02ebbc9 Mon Sep 17 00:00:00 2001 From: Kirill Isakov Date: Wed, 18 Nov 2020 00:57:01 +0600 Subject: [PATCH] Discard PM measurements which took too much time to produce --- firmware/main/dallas.cc | 2 +- firmware/main/measurement.cc | 8 ++++---- firmware/main/measurement.hh | 2 +- firmware/main/pms.cc | 33 ++++++++++++++++++++++----------- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/firmware/main/dallas.cc b/firmware/main/dallas.cc index eb15590..8b063ca 100644 --- a/firmware/main/dallas.cc +++ b/firmware/main/dallas.cc @@ -79,7 +79,7 @@ DS18B20_Info *TempSensor::searchTempSensor(const OneWireBus *const owb) { void TempSensor::runTempMeasurements(const DS18B20_Info *device, const TempSensor *const config) { int errCount = 0; - Measurement ms{.type = MeasurementType::MS_TEMPERATURE, + Measurement ms{.type = MeasurementType::Temperature, .sensor = config->name}; TickType_t lastWakeTime = xTaskGetTickCount(); diff --git a/firmware/main/measurement.cc b/firmware/main/measurement.cc index 3f07016..e76845c 100644 --- a/firmware/main/measurement.cc +++ b/firmware/main/measurement.cc @@ -47,10 +47,10 @@ void Measurement::set(const pms::ResponseSum &sum) { const char *Measurement::getType() const { switch (type) { - case MeasurementType::MS_TEMPERATURE: + case MeasurementType::Temperature: return "meas/temp"; - case MeasurementType::MS_PARTICULATES: + case MeasurementType::Particulates: return "meas/part"; default: @@ -61,13 +61,13 @@ const char *Measurement::getType() const { bool Measurement::formatMsg(char *const msg, const size_t len) const { switch (type) { - case MeasurementType::MS_TEMPERATURE: { + case MeasurementType::Temperature: { constexpr auto tpl = R"({"dev":"%s","time":%ld,"sens":"%s","temp":%f})"; snprintf(msg, len, tpl, appSettings.devName, time, sensor, temp); return true; } - case MeasurementType::MS_PARTICULATES: { + case MeasurementType::Particulates: { constexpr auto tpl = R"({"dev":"%s","time":%ld,"sens":"%s","std":{"pm1":%u,"pm2.5":%u,"pm10":%u},"atm":{"pm1":%u,"pm2.5":%u,"pm10":%u},"cnt":{"pm0.3":%u,"pm0.5":%u,"pm1":%u,"pm2.5":%u,"pm5":%u,"pm10":%u}})"; snprintf(msg, len, tpl, appSettings.devName, time, sensor, pm.std.pm1Mcg, diff --git a/firmware/main/measurement.hh b/firmware/main/measurement.hh index e718614..6662cf5 100644 --- a/firmware/main/measurement.hh +++ b/firmware/main/measurement.hh @@ -4,7 +4,7 @@ #include "utils.hh" #include -enum class MeasurementType { MS_TEMPERATURE, MS_PARTICULATES }; +enum class MeasurementType { Temperature, Particulates }; struct Measurement { MeasurementType type; diff --git a/firmware/main/pms.cc b/firmware/main/pms.cc index ae3676d..3146f19 100644 --- a/firmware/main/pms.cc +++ b/firmware/main/pms.cc @@ -51,8 +51,7 @@ void Response::swapBytes() { Response res; ResponseSum sum; - Measurement ms{.type = MeasurementType::MS_PARTICULATES, - .sensor = station.name}; + Measurement ms{.type = MeasurementType::Particulates, .sensor = station.name}; TickType_t lastWake = xTaskGetTickCount(); @@ -79,8 +78,15 @@ void Response::swapBytes() { } Timer execTime; + bool periodOverflow = false; for (int successful = 0; successful < CONFIG_PARTICULATE_MEASUREMENTS;) { + if (execTime.seconds() >= CONFIG_PARTICULATE_PERIOD_SECONDS) { + ESP_LOGE(logTag, "PM measurement took too much time"); + periodOverflow = true; + break; + } + const int received = station.readResponse(res, secToTicks(5)); if (received != sizeof(res)) { @@ -133,7 +139,6 @@ void Response::swapBytes() { if (!sendEach) { ms.time = getTimestamp(); - ms.set(sum); } sent = station.writeCommand(cmd::cmdSleep); @@ -141,12 +146,18 @@ void Response::swapBytes() { ESP_LOGE(logTag, "could not send sleep command"); } - if (!sendEach) { - ESP_LOGI(logTag, "avg PM: 1=%u, 2.5=%u, 10=%u", ms.pm.atm.pm1Mcg, - ms.pm.atm.pm2Mcg, ms.pm.atm.pm10Mcg); + if (periodOverflow) { + // measurement period overflow, skip next iteration + lastWake = xTaskGetTickCount(); + } else { + if (!sendEach) { + ms.set(sum); + ESP_LOGI(logTag, "avg PM: 1=%u, 2.5=%u, 10=%u", ms.pm.atm.pm1Mcg, + ms.pm.atm.pm2Mcg, ms.pm.atm.pm10Mcg); - if (!station.queue->putRetrying(ms)) { - ESP_LOGE(logTag, "could not queue averaged particulate measurement"); + if (!station.queue->putRetrying(ms)) { + ESP_LOGE(logTag, "could not queue averaged PM measurement"); + } } } @@ -182,10 +193,10 @@ void Station::start(Queue &msQueue) { ESP_ERROR_CHECK( uart_set_pin(port, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); - std::string taskName{"pm_"}; - taskName.append(name); + char buf[32]; + snprintf(buf, sizeof(buf), "pm_%s", name); - xTaskCreate(collectionTask, taskName.c_str(), KiB(2), this, 4, nullptr); + xTaskCreate(collectionTask, buf, KiB(2), this, 4, nullptr); } void ResponseSum::addMeasurement(const Response &resp) {