Browse Source

添加算法

storage-in-realtime
zhaohe 12 months ago
parent
commit
98e1740b87
  1. 45
      appsrc/appcomponents/algo/dvalue_computer.cpp
  2. 11
      appsrc/appcomponents/algo/dvalue_computer.hpp
  3. 43
      appsrc/appcomponents/algo/moving_average_filter.hpp
  4. 2
      appsrc/service/hardware/device_ctrl_service.hpp
  5. 22
      appsrc/service/hardware/disinfectant_weight_update_service.cpp
  6. 52
      appsrc/service/hardware/disinfectant_weight_update_service.hpp
  7. 12
      appsrc/service/main_control_service.cpp

45
appsrc/appcomponents/algo/dvalue_computer.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;
}

11
appsrc/appcomponents/algo/dvalue_computer.hpp

@ -0,0 +1,11 @@
#pragma once
#include <stdio.h>
namespace iflytop {
class DValueComputer {
public:
static float computeDValue(float coefficient, float h2o2ppm);
};
} // namespace iflytop

43
appsrc/appcomponents/algo/moving_average_filter.hpp

@ -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

2
appsrc/service/hardware/device_ctrl_service.hpp

@ -30,7 +30,7 @@ using namespace std;
using namespace core;
class DeviceIoControlService : public enable_shared_from_this<DeviceIoControlService> {
ENABLE_LOGGER(DeviceIoControlService);
THISCLASS(DeviceIoControlService);
private:
shared_ptr<GConfig> m_config;

22
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);
}
}

52
appsrc/service/hardware/disinfectant_weight_update_service.hpp

@ -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

12
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);

Loading…
Cancel
Save