From 98e1740b8730435fa40e4362f6c3bc79d9335ed0 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Fri, 16 Aug 2024 11:50:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- appsrc/appcomponents/algo/dvalue_computer.cpp | 45 +++++++++++++++++++ appsrc/appcomponents/algo/dvalue_computer.hpp | 11 +++++ .../appcomponents/algo/moving_average_filter.hpp | 43 ++++++++++++++++++ appsrc/service/hardware/device_ctrl_service.hpp | 2 +- .../disinfectant_weight_update_service.cpp | 22 +++++++++ .../disinfectant_weight_update_service.hpp | 52 ++++++++++++++++++++++ appsrc/service/main_control_service.cpp | 12 ++++- 7 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 appsrc/appcomponents/algo/dvalue_computer.cpp create mode 100644 appsrc/appcomponents/algo/dvalue_computer.hpp create mode 100644 appsrc/appcomponents/algo/moving_average_filter.hpp create mode 100644 appsrc/service/hardware/disinfectant_weight_update_service.cpp create mode 100644 appsrc/service/hardware/disinfectant_weight_update_service.hpp diff --git a/appsrc/appcomponents/algo/dvalue_computer.cpp b/appsrc/appcomponents/algo/dvalue_computer.cpp new file mode 100644 index 0000000..ca09546 --- /dev/null +++ b/appsrc/appcomponents/algo/dvalue_computer.cpp @@ -0,0 +1,45 @@ +#include "dvalue_computer.hpp" + +#include +using namespace iflytop; +using namespace std; + +static bool zfeq(float a, float b, float eps = 0.01) { + if (fabs(a - b) < eps) { + return true; + } + return false; +} + +float DValueComputer::computeDValue(float coefficient, float h2o2ppm) { + /** + * @brief + * + * D值的计算公式是根据美国竞品的数据记录计算得来的 + * + * 浓度小于150时,y=-0.5269X+97.868 + * 浓度大于150时,y=-0.1405X+40.369 + */ + + if (zfeq(h2o2ppm, 0)) { + return -1; + } + + float dvalue = 0; + + if (h2o2ppm < 150) { + // + dvalue = -0.5251 * h2o2ppm + 98.154; + } else if (h2o2ppm >= 150 && h2o2ppm < 240) { + // + dvalue = -0.125 * h2o2ppm + 38.913; + } else if (h2o2ppm >= 240) { + // + dvalue = -0.00596 * h2o2ppm + 10.3434; + } + if (dvalue < 2) { + dvalue = 2; + } + dvalue = dvalue * coefficient; + return dvalue; +} diff --git a/appsrc/appcomponents/algo/dvalue_computer.hpp b/appsrc/appcomponents/algo/dvalue_computer.hpp new file mode 100644 index 0000000..3961fe6 --- /dev/null +++ b/appsrc/appcomponents/algo/dvalue_computer.hpp @@ -0,0 +1,11 @@ + +#pragma once +#include + +namespace iflytop { + +class DValueComputer { + public: + static float computeDValue(float coefficient, float h2o2ppm); +}; +} // namespace iflytop \ No newline at end of file diff --git a/appsrc/appcomponents/algo/moving_average_filter.hpp b/appsrc/appcomponents/algo/moving_average_filter.hpp new file mode 100644 index 0000000..7e7246c --- /dev/null +++ b/appsrc/appcomponents/algo/moving_average_filter.hpp @@ -0,0 +1,43 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +namespace iflytop { +using namespace std; + +class MovingAverageFilter { + size_t size_; + float sum_ = 0; + std::list values_; + + public: + MovingAverageFilter(size_t size) : size_(size) { + if (size_ <= 0) { + throw std::invalid_argument("size must be greater than 0"); + } + } + + float filter(float value) { + if (values_.size() < size_) { + values_.push_back(value); + sum_ += value; + return sum_ / values_.size(); + } + + sum_ -= values_.front(); + values_.pop_front(); + values_.push_back(value); + sum_ += value; + return sum_ / size_; + } + + private: +}; +} // namespace iflytop diff --git a/appsrc/service/hardware/device_ctrl_service.hpp b/appsrc/service/hardware/device_ctrl_service.hpp index fa787eb..1bcfb26 100644 --- a/appsrc/service/hardware/device_ctrl_service.hpp +++ b/appsrc/service/hardware/device_ctrl_service.hpp @@ -30,7 +30,7 @@ using namespace std; using namespace core; class DeviceIoControlService : public enable_shared_from_this { - ENABLE_LOGGER(DeviceIoControlService); + THISCLASS(DeviceIoControlService); private: shared_ptr m_config; diff --git a/appsrc/service/hardware/disinfectant_weight_update_service.cpp b/appsrc/service/hardware/disinfectant_weight_update_service.cpp new file mode 100644 index 0000000..23acb65 --- /dev/null +++ b/appsrc/service/hardware/disinfectant_weight_update_service.cpp @@ -0,0 +1,22 @@ +#include "disinfectant_weight_update_service.hpp" + +using namespace iflytop; +using namespace core; +using namespace std; + +void DisinfectantWeightUpdateService::initialize() { // + GET_TO_SERVICE(deviceIoControlService); + updateThread.reset(new Thread("DisinfectantWeightUpdateService", [this]() { updateWeightThread(); })); +} + +float DisinfectantWeightUpdateService::getWeight() { // + return weightCache; +} +void DisinfectantWeightUpdateService::updateWeightThread() { + while (true) { + float weightCache = deviceIoControlService->DisinfectantVolume_readVal(); + weightCache = filter.filter(weightCache); + logger->info("weight {}", weightCache); + sleep(1000); + } +} \ No newline at end of file diff --git a/appsrc/service/hardware/disinfectant_weight_update_service.hpp b/appsrc/service/hardware/disinfectant_weight_update_service.hpp new file mode 100644 index 0000000..681a26d --- /dev/null +++ b/appsrc/service/hardware/disinfectant_weight_update_service.hpp @@ -0,0 +1,52 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +// +#include "baseservice/baseservice.hpp" +#include "device_ctrl_service.hpp" +// +#include "base/can_packet_dumper.hpp" +#include "base/h2o2_sensor_data_mgr.hpp" +// +#include "appcomponents/algo/moving_average_filter.hpp" +/** + * @brief + * + * service: DisinfectantWeightUpdateService + * + * 监听事件: + * 依赖状态: + * 依赖服务: + * 作用: + * + */ + +namespace iflytop { +using namespace std; +using namespace core; + +class DisinfectantWeightUpdateService : public enable_shared_from_this { + THISCLASS(DisinfectantWeightUpdateService); + + unique_ptr updateThread; + shared_ptr deviceIoControlService; + + MovingAverageFilter filter = {20}; + float weightCache = 0; + + public: + void initialize(); + float getWeight(); + + public: + void updateWeightThread(); +}; + +}; // namespace iflytop diff --git a/appsrc/service/main_control_service.cpp b/appsrc/service/main_control_service.cpp index 2ed908f..896920a 100644 --- a/appsrc/service/main_control_service.cpp +++ b/appsrc/service/main_control_service.cpp @@ -15,6 +15,9 @@ #include "service/app/disinfection_ctrl_service.hpp" #include "service/app/drain_liquid_service.hpp" #include "service/app/pipeline_pressure_control.hpp" +// +#include "hardware/device_ctrl_service.hpp" +#include "hardware/disinfectant_weight_update_service.hpp" using namespace iflytop; using namespace core; @@ -27,13 +30,18 @@ void MainControlService::dosystem(string order, bool dump) { } void MainControlService::initialize() { - // Base + // FrontEnd BUILD_AND_REG_SERRVICE(IflytopFrontEndService); BUILD_AND_REG_SERRVICE(FrontMsgProcesser); + // Data + ProjectPort::ins().initialize(); BUILD_AND_REG_SERRVICE(DBService); BUILD_AND_REG_SERRVICE(DeviceStateService); - BUILD_AND_REG_SERRVICE(PipelinePressureControl); + // Hardware + BUILD_AND_REG_SERRVICE(DeviceIoControlService); + BUILD_AND_REG_SERRVICE(DisinfectantWeightUpdateService); // + BUILD_AND_REG_SERRVICE(PipelinePressureControl); BUILD_AND_REG_SERRVICE(UDiskMgrService); // BUILD_AND_REG_SERRVICE(AuditMgrService);