// V2 PublishGate —— 与 Python `portcv/service/publish_gate.py` 同输入/同输出/同向量。 // // 目标契约(docs/plc_interface.md §3A / §6A): // * PLC 结果区 state 只写 0/1/2,255 只存在于内部 Decision; // * 有效(0/1/2 + stable + conf>=min_conf)→ 状态 + RESULT_VALID + STABLE; // * 内部 255 / 低置信度 / 未稳定 / 帧超时 → 保持 last stable,清两个标志; // * cmd=2 复位 → 0 + flags 0/0,清 last stable; // * 冷启动 → 0 + flags 0/0(不是“已确认 0”)。 // // 本类只做输出映射,不接 evidence_v2;P1a 先用合成 Decision 验证契约。 #pragma once #include #include #include "portcv/schema.hpp" namespace portcv { /// 与 Python `DEFAULT_MIN_CONFIDENCE` 一致。 inline constexpr double kPublishMinConfidence = 0.45; /// PublishGate 的输出;字段对应 PLC 结果区前三个字。 struct PublishResult { uint16_t state = 0; bool result_valid = false; bool stable = false; uint16_t confidence = 0; ///< 千分比 0..1000 std::optional last_stable{}; bool perception_uncertain = false; ///< P0-2:true 时 result_valid 必须为 false bool operator==(const PublishResult& other) const { return state == other.state && result_valid == other.result_valid && stable == other.stable && confidence == other.confidence && last_stable == other.last_stable && perception_uncertain == other.perception_uncertain; } bool operator!=(const PublishResult& other) const { return !(*this == other); } }; /// 纯函数版:`last_stable` 是上一稳定值,不是本帧内部状态;有效时才更新。 /// 优先级:reset > frame_timeout > 有效结果 > 保持。 PublishResult evaluate_publish_gate(const Decision& decision, bool frame_timeout = false, bool reset = false, std::optional last_stable = std::nullopt, double min_conf = kPublishMinConfidence, bool perception_uncertain = false); /// 有状态版:内部保存 last stable;线程内使用,非线程安全。 class PublishGate { public: explicit PublishGate(double min_conf = kPublishMinConfidence); PublishResult apply(const Decision& decision, bool frame_timeout = false, bool reset = false, bool perception_uncertain = false); void reset() { last_stable_.reset(); } /// PLC `min_conf` 覆盖(千分比 → 0..1);非法值忽略。 void set_min_conf(double value) { if (value >= 0.0 && value <= 1.0) { min_conf_ = value; } } double min_conf() const { return min_conf_; } std::optional last_stable() const { return last_stable_; } private: double min_conf_ = kPublishMinConfidence; std::optional last_stable_{}; }; } // namespace portcv