diff --git a/weigh/MsgHandler.cxx b/weigh/MsgHandler.cxx index 99cf63b..cf0136c 100644 --- a/weigh/MsgHandler.cxx +++ b/weigh/MsgHandler.cxx @@ -40,6 +40,13 @@ bool MsgHandler::SetEmptyWeight(const int& empty_weight) return true; } +bool MsgHandler::SetJitterWeight(const int& jitter_weight) +{ + this->jitter_weight = jitter_weight; + + return true; +} + int MsgHandler::SendDeviceMsg(const std::vector& data) { std::cout << "write to " << this->device << std::endl; diff --git a/weigh/MsgHandler.hpp b/weigh/MsgHandler.hpp index 7f7c842..9b8dafe 100644 --- a/weigh/MsgHandler.hpp +++ b/weigh/MsgHandler.hpp @@ -11,6 +11,7 @@ public: int fd; std::string device; int empty_weight; + int jitter_weight; std::vector m_WeightData; MsgHandler(); @@ -21,6 +22,7 @@ public: bool HandleDeviceMsg(T& msg); bool SetDevice(const std::string& device); bool SetEmptyWeight(const int& empty_weight); + bool SetJitterWeight(const int& jitter_weight); virtual bool OpenPort(const std::string& port, const std::string& baudrate); virtual int SendDeviceMsg(const std::vector& data); diff --git a/weigh/Publisher.cxx b/weigh/Publisher.cxx index cc498f7..99615b5 100644 --- a/weigh/Publisher.cxx +++ b/weigh/Publisher.cxx @@ -153,18 +153,18 @@ void PublisherApp::run(std::shared_ptr handler) { // 配置检测器 - 使用更合理的参数 WeightStabilityDetector::Config config; - config.jitter_threshold = 30.0f; // 30kg抖动阈值 - config.min_weight_threshold = handler->empty_weight; // 300kg开始检测 - config.empty_car_threshold = handler->empty_weight; // 默认300kg为空车 - config.fast_drop_threshold = 1000.0f; // 快速下降1吨认为车辆离开 - config.required_stable_count = 20; // 20次稳定即可 - config.min_jitter_count = 2; // 至少2次非增长 - config.timeout_extra = 3; // 超时额外次数 - config.window_size = 8; // 窗口大小8 - config.max_std_dev = 30.0f; // 最大标准差30kg + config.jitter_threshold = handler->jitter_weight; // 30kg抖动阈值 + config.min_weight_threshold = handler->empty_weight; // 300kg开始检测 + config.empty_car_threshold = handler->empty_weight; // 默认300kg为空车 + config.fast_drop_threshold = 1000.0f; // 快速下降1吨认为车辆离开 + config.required_stable_count = 24; // 20次稳定即可 + config.min_jitter_count = 4; // 至少4次非增长 + config.timeout_extra = 4; // 超时额外次数 + config.window_size = 8; // 窗口大小8 + config.max_std_dev = handler->jitter_weight; // 最大标准差30kg config.enable_debug_log = false; config.reset_window_on_vehicle_on = true; - config.leave_reset_delay_ms = 500; // 0.5秒后重置 + config.leave_reset_delay_ms = 500; // 0.5秒后重置 // 创建检测器 WeightStabilityDetector detector(config); @@ -241,7 +241,7 @@ void PublisherApp::run(std::shared_ptr handler) // 如果稳定,显示稳定重量 WeighingSystem::ScaleInfo info; - info.State() = 1; + info.State() = true; if (weight < handler->empty_weight) { info.HasVehicle() = 0; @@ -252,13 +252,13 @@ void PublisherApp::run(std::shared_ptr handler) } if (detector.isStable()) { - info.WeightOK() = 1; + info.WeightOK() = true; info.Value() = weight/1000; info.StableValue() = weight/1000; } else { - info.WeightOK() = 0; + info.WeightOK() = false; info.Value() = weight/1000; info.StableValue() = 0; } diff --git a/weigh/WeightStabilityDetector.cxx b/weigh/WeightStabilityDetector.cxx index 7d96faf..3d8da21 100644 --- a/weigh/WeightStabilityDetector.cxx +++ b/weigh/WeightStabilityDetector.cxx @@ -274,6 +274,9 @@ void WeightStabilityDetector::checkStateTransition(float weight_kg) { if (isVehicleLeaving(weight_kg) || checkFastDrop(weight_kg)) { transitionToState(VehicleState::VEHICLE_LEAVING); } + else if (isVehicleShaking(weight_kg)) { + transitionToState(VehicleState::VEHICLE_ON); + } break; } @@ -343,6 +346,18 @@ bool WeightStabilityDetector::isVehicleLeaving(float weight_kg) const { return weight_kg < config_.empty_car_threshold; } +bool WeightStabilityDetector::isVehicleShaking(float weight_kg) { + if (std::abs(weight_kg - previous_weight_) > config_.jitter_threshold) + { + is_leaving_state_ = false; // 清除离开状态标记 + stable_count_ = 0; // 重置稳定计数 + continuous_increase_count_ = 0; // 重置连续增长计数 + vehicle_weight_window_.clear(); // 重置重量窗口 + return true; + } + return false; +} + bool WeightStabilityDetector::checkFastDrop(float weight_kg) const { // 检查快速下降:当前重量远低于历史最高重量 if (max_weight_ > config_.min_weight_threshold) { diff --git a/weigh/WeightStabilityDetector.hpp b/weigh/WeightStabilityDetector.hpp index 5d04fe5..e6a0533 100644 --- a/weigh/WeightStabilityDetector.hpp +++ b/weigh/WeightStabilityDetector.hpp @@ -148,6 +148,7 @@ private: bool isJitterExceeded(float weight_diff) const; bool isVehicleOn(float weight_kg) const; bool isVehicleLeaving(float weight_kg) const; + bool isVehicleShaking(float weight_kg); bool checkFastDrop(float weight_kg) const; bool checkStableCondition() const; diff --git a/weigh/main.cxx b/weigh/main.cxx index 0e30b0c..19511be 100644 --- a/weigh/main.cxx +++ b/weigh/main.cxx @@ -79,6 +79,7 @@ int main(int argc, char** argv) const char* baudrate = "9600"; const char* device = "Keli"; int empty_weight = 300; + int jitter_weight = 30; for (int i = 1; i < argc; i++) { @@ -106,6 +107,10 @@ int main(int argc, char** argv) { empty_weight = atoi(argv[++i]); } + else if (strcmp(argv[i], "--jitter") == 0 && i + 1 < argc) + { + jitter_weight = atoi(argv[++i]); + } else if (strcmp(argv[i], "--version") == 0) { std::cout << "Vesrion: " << PROGRAM_VERSION << "\n"; @@ -121,6 +126,7 @@ int main(int argc, char** argv) << " --port Set port name (e.g., ttyS1)\n" << " --device Set device name (e.g., Toledo)\n" << " --empty Set empty weight (e.g., 300)\n" + << " --jitter Set jitter weight (e.g., 30)\n" << " --version Show software Version\n" << " --help Show this help message\n"; return EXIT_SUCCESS; @@ -135,6 +141,8 @@ int main(int argc, char** argv) << " --baudrate Set baudrate (e.g., 9600)\n" << " --port Set port name (e.g., ttyS1)\n" << " --device Set device name (e.g., Toledo)\n" + << " --empty Set empty weight (e.g., 300)\n" + << " --jitter Set jitter weight (e.g., 30)\n" << " --version Show software Version\n" << " --help Show this help message\n"; return EXIT_FAILURE; @@ -148,6 +156,7 @@ int main(int argc, char** argv) dev->OpenPort(port, baudrate); dev->SetDevice(device); dev->SetEmptyWeight(empty_weight); + dev->SetJitterWeight(jitter_weight); std::thread pub_thread(&PublisherApp::run, pub, dev);