7 changed files with 184 additions and 3 deletions
-
45appsrc/appcomponents/algo/dvalue_computer.cpp
-
11appsrc/appcomponents/algo/dvalue_computer.hpp
-
43appsrc/appcomponents/algo/moving_average_filter.hpp
-
2appsrc/service/hardware/device_ctrl_service.hpp
-
22appsrc/service/hardware/disinfectant_weight_update_service.cpp
-
52appsrc/service/hardware/disinfectant_weight_update_service.hpp
-
12appsrc/service/main_control_service.cpp
@ -0,0 +1,45 @@ |
|||
#include "dvalue_computer.hpp"
|
|||
|
|||
#include <cmath>
|
|||
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; |
|||
} |
@ -0,0 +1,11 @@ |
|||
|
|||
#pragma once
|
|||
#include <stdio.h>
|
|||
|
|||
namespace iflytop { |
|||
|
|||
class DValueComputer { |
|||
public: |
|||
static float computeDValue(float coefficient, float h2o2ppm); |
|||
}; |
|||
} // namespace iflytop
|
@ -0,0 +1,43 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
|
|||
class MovingAverageFilter { |
|||
size_t size_; |
|||
float sum_ = 0; |
|||
std::list<float> 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
|
@ -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); |
|||
} |
|||
} |
@ -0,0 +1,52 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
//
|
|||
#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<DisinfectantWeightUpdateService> { |
|||
THISCLASS(DisinfectantWeightUpdateService); |
|||
|
|||
unique_ptr<Thread> updateThread; |
|||
shared_ptr<DeviceIoControlService> deviceIoControlService; |
|||
|
|||
MovingAverageFilter filter = {20}; |
|||
float weightCache = 0; |
|||
|
|||
public: |
|||
void initialize(); |
|||
float getWeight(); |
|||
|
|||
public: |
|||
void updateWeightThread(); |
|||
}; |
|||
|
|||
}; // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue