// 公共数据结构 —— 与 Python 侧 portcv/schema.py 一一对应(字段名、语义、字宽都一致)。 // // 约定: // * 检测框使用像素坐标,原点左上角,y 向下增大; // * SceneState 的枚举值就是最终写给 PLC 的编码(0/1/2/3/255),改前先确认 PLC 侧; // * VisionSnapshot 是"视觉 → PLC"的唯一载体,Modbus/TCP 服务端只从它取数。 #pragma once #include #include #include #include #include #include #include "portcv/crc16.hpp" namespace portcv { /// 反馈给 PLC 的场景编码(与 Python SceneState 完全一致)。 enum class SceneState : int { NOT_ON_TRAIN = 0, ///< 集装箱不在车上 / 不是目标车(地面、堆场、卡车…) BED = 1, ///< 车斗(敞车,车上有箱) FLATBED = 2, ///< 平板车(平车,车上有箱) EMPTY = 3, ///< 空车(敞车,车斗内无箱;P0-Q9) UNKNOWN = 255, ///< 视觉未就绪 / 结果不可信 }; inline const char* scene_state_label(int state) { switch (state) { case 0: return "not_on_train"; case 1: return "bed"; case 2: return "flatbed"; case 3: return "empty"; default: return "unknown"; } } /// 反馈指令的取值(PLC → 视觉)。 enum : uint16_t { CMD_IDLE = 0, CMD_REQUEST = 1, CMD_RESET = 2, CMD_CLEAR_STATS = 3, }; /// VisionSnapshot::flags 的位定义(与 Python StatusBits 一致,不可随意改位)。 enum class StatusBits : uint16_t { READY = 1u << 0, ///< 模型已加载且完成预热 FRAME_OK = 1u << 1, ///< 最近一帧是新的(未超过 stale 时限) STABLE = 1u << 2, ///< 状态稳定,可用于联锁/自动落箱 LOW_FPS = 1u << 3, ///< 帧率低于下限 FRAME_TIMEOUT = 1u << 4, ///< 长时间拿不到帧(相机/网络异常) ON_DEMAND = 1u << 5, ///< 当前处于 PLC 请求触发的按需识别 RESULT_VALID = 1u << 6, ///< 置信度达到阈值,结果有效 PUBLISH_OK = 1u << 7, ///< 最近一次向 PLC 发布成功(服务端模式恒为 1) PERCEPTION_UNCERTAIN = 1u << 8, ///< 感知不确定;与 RESULT_VALID 互斥 MRC_REQUEST = 1u << 9, ///< 请求 PLC 进入最小风险状态 SENSOR_DEGRADED = 1u << 10, ///< 图像质量/传感器退化 CALIB_STALE = 1u << 11, ///< 标定过期或漂移 DATA_LOSS = 1u << 12, ///< 数据链中断(取帧超时/丢流) }; inline uint16_t bit_mask(StatusBits bit) { return static_cast(bit); } /// 一个检测框(像素坐标)。 struct Box { float x1 = 0.0f; float y1 = 0.0f; float x2 = 0.0f; float y2 = 0.0f; float score = 0.0f; int class_id = -1; std::string class_name; Box() = default; Box(float a, float b, float c, float d, float s = 0.0f, int cid = -1, std::string name = std::string()) : x1(a), y1(b), x2(c), y2(d), score(s), class_id(cid), class_name(std::move(name)) { normalize(); } void normalize() { if (x2 < x1) { float t = x1; x1 = x2; x2 = t; } if (y2 < y1) { float t = y1; y1 = y2; y2 = t; } } float width() const { return (x2 - x1) > 0.0f ? (x2 - x1) : 0.0f; } float height() const { return (y2 - y1) > 0.0f ? (y2 - y1) : 0.0f; } float area() const { return width() * height(); } float cx() const { return (x1 + x2) * 0.5f; } float cy() const { return (y1 + y2) * 0.5f; } float top() const { return y1 < y2 ? y1 : y2; } float bottom() const { return y1 > y2 ? y1 : y2; } }; /// 单帧观测(未经时序滤波)。 struct Observation { double ts = 0.0; int state = static_cast(SceneState::UNKNOWN); double confidence = 0.0; std::vector boxes; std::string reason; long frame_id = 0; bool is_valid() const { return state != static_cast(SceneState::UNKNOWN); } }; /// 时序滤波后的判决,可直接发布给 PLC。 struct Decision { int state = static_cast(SceneState::UNKNOWN); double confidence = 0.0; bool stable = false; uint32_t seq = 0; ///< 状态每变化一次 +1,PLC 用它判断"有新结果" double since_ts = 0.0; ///< 当前状态的起始时间(秒) std::string reason = "init"; bool changed = false; ///< 本次 update 是否发生了状态变化 const char* label() const { return scene_state_label(state); } }; // ------------------------------------------------------------------ 字/字节 /// 16bit 字数组 → 大端字节流。 inline std::vector words_to_bytes(const std::vector& words) { std::vector out(words.size() * 2, 0); for (std::size_t i = 0; i < words.size(); ++i) { out[i * 2] = static_cast((words[i] >> 8) & 0xFF); out[i * 2 + 1] = static_cast(words[i] & 0xFF); } return out; } /// 大端字节流 → 16bit 字数组(奇数长度时忽略最后一个字节,与 Python 行为一致)。 inline std::vector bytes_to_words(const uint8_t* data, std::size_t len) { std::vector out; out.reserve(len / 2); for (std::size_t i = 0; i + 1 < len; i += 2) { out.push_back(static_cast((static_cast(data[i]) << 8) | static_cast(data[i + 1]))); } return out; } /// 寄存器布局条目:字段名 + 16bit 字宽。 struct RegisterField { const char* name; int width; }; /// 视觉 → PLC 的结果快照(输入寄存器,FC4 只读;共 12 字,末字为 CRC)。 struct VisionSnapshot { uint16_t state = static_cast(SceneState::UNKNOWN); uint16_t confidence = 0; ///< 千分比 0..1000 uint16_t flags = 0; uint32_t seq = 0; ///< 32bit,状态变化计数 uint32_t ts_s = 0; ///< unix 秒(32bit) uint16_t heartbeat = 0; ///< 每个心跳周期 +1 uint16_t fps_x10 = 0; ///< 帧率 x10 uint16_t latency_ms = 0; ///< 单帧端到端耗时 uint16_t reserved = 0; /// 字段布局(与 Python VisionSnapshot.REGISTER_LAYOUT 逐项一致)。 static constexpr RegisterField kLayout[] = { {"state", 1}, {"confidence", 1}, {"flags", 1}, {"seq", 2}, {"ts_s", 2}, {"heartbeat", 1}, {"fps_x10", 1}, {"latency_ms", 1}, {"reserved", 1}, }; static constexpr int kFieldCount = 9; /// 布局字宽之和 + 1(CRC)。 static constexpr int register_count() { int n = 0; for (int i = 0; i < kFieldCount; ++i) { n += kLayout[i].width; } return n + 1; } std::vector to_registers() const { std::vector words = { state, confidence, flags, static_cast((seq >> 16) & 0xFFFF), static_cast(seq & 0xFFFF), static_cast((ts_s >> 16) & 0xFFFF), static_cast(ts_s & 0xFFFF), heartbeat, fps_x10, latency_ms, reserved, }; words.push_back(crc16_modbus(words_to_bytes(words))); return words; } /// 字数组 → 快照;verify=true 时校验 CRC(与 Python from_registers 一致)。 static VisionSnapshot from_registers(const std::vector& words, bool verify = false) { if (static_cast(words.size()) < register_count()) { throw std::invalid_argument("寄存器数量不足"); } if (verify) { std::vector payload(words.begin(), words.begin() + words.size() - 1); if (crc16_modbus(words_to_bytes(payload)) != words[words.size() - 1]) { throw std::invalid_argument("CRC 校验失败,数据可能不完整"); } } VisionSnapshot snap; snap.state = words[0]; snap.confidence = words[1]; snap.flags = words[2]; snap.seq = (static_cast(words[3]) << 16) | static_cast(words[4]); snap.ts_s = (static_cast(words[5]) << 16) | static_cast(words[6]); snap.heartbeat = words[7]; snap.fps_x10 = words[8]; snap.latency_ms = words[9]; snap.reserved = words[10]; return snap; } bool has_flag(StatusBits bit) const { return (flags & bit_mask(bit)) != 0; } void set_flag(StatusBits bit, bool on) { if (on) { flags = static_cast(flags | bit_mask(bit)); } else { flags = static_cast(flags & static_cast(~bit_mask(bit))); } } }; /// PLC → 视觉的指令块(保持寄存器 FC3/FC6/FC16;共 4 字)。 struct PlcCommand { uint16_t cmd = CMD_IDLE; uint16_t preset = 0; uint16_t min_conf = 0; ///< 千分比,0 = 用配置值 uint16_t reserved = 0; static constexpr int register_count() { return 4; } std::vector to_registers() const { return {cmd, preset, min_conf, reserved}; } static PlcCommand from_registers(const std::vector& words) { PlcCommand c; if (words.size() > 0) { c.cmd = words[0]; } if (words.size() > 1) { c.preset = words[1]; } if (words.size() > 2) { c.min_conf = words[2]; } if (words.size() > 3) { c.reserved = words[3]; } return c; } bool is_request() const { return cmd == CMD_REQUEST; } bool is_reset() const { return cmd == CMD_RESET; } }; } // namespace portcv