| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- // 寄存器映射的展开与打印 —— 与 Python 侧 portcv/plc/registers.py 一致。
- //
- // 地址约定:地址 = 配置项 plc.modbus.input_addr / holding_addr + 表内偏移。
- // 西门子侧 MB_CLIENT/MB_MASTER 的 MB_DATA_ADDR 填起始地址,MB_DATA_LEN 填字数。
- #pragma once
- #include <cstdint>
- #include <string>
- #include <utility>
- #include <vector>
- #include "portcv/schema.hpp"
- namespace portcv {
- /// 打印用的一行:地址 / 字段 / 字宽 / 说明。
- struct LayoutRow {
- uint16_t address;
- std::string field;
- int width;
- std::string note;
- };
- /// flags 位定义表:名称 / 掩码 / 说明。
- struct FlagRow {
- std::string name;
- uint16_t mask;
- std::string note;
- };
- /// 输入寄存器字段偏移(含末尾 crc16)。
- inline std::vector<std::pair<std::string, int>> input_offsets() {
- std::vector<std::pair<std::string, int>> out;
- int offset = 0;
- for (int i = 0; i < VisionSnapshot::kFieldCount; ++i) {
- out.push_back({std::string(VisionSnapshot::kLayout[i].name), offset});
- offset += VisionSnapshot::kLayout[i].width;
- }
- out.push_back({"crc16", VisionSnapshot::register_count() - 1});
- return out;
- }
- /// 保持寄存器字段偏移。
- inline std::vector<std::pair<std::string, int>> holding_offsets() {
- return {{"cmd", 0}, {"preset", 1}, {"min_conf", 2}, {"reserved", 3}};
- }
- inline std::vector<FlagRow> flag_rows() {
- return {
- {"READY", bit_mask(StatusBits::READY), "模型已加载且完成预热"},
- {"FRAME_OK", bit_mask(StatusBits::FRAME_OK), "最近一帧是新的(未超过 stale 时限)"},
- {"STABLE", bit_mask(StatusBits::STABLE), "状态稳定,可用于联锁/自动落箱"},
- {"LOW_FPS", bit_mask(StatusBits::LOW_FPS), "帧率低于下限"},
- {"FRAME_TIMEOUT", bit_mask(StatusBits::FRAME_TIMEOUT), "长时间拿不到帧(相机/网络异常)"},
- {"ON_DEMAND", bit_mask(StatusBits::ON_DEMAND), "当前处于 PLC 请求触发的按需识别"},
- {"RESULT_VALID", bit_mask(StatusBits::RESULT_VALID), "置信度达到阈值,结果有效"},
- {"PUBLISH_OK", bit_mask(StatusBits::PUBLISH_OK), "最近一次向 PLC 发布成功(服务端模式恒为 1)"},
- {"PERCEPTION_UNCERTAIN", bit_mask(StatusBits::PERCEPTION_UNCERTAIN),
- "感知不确定;与 RESULT_VALID 互斥"},
- {"MRC_REQUEST", bit_mask(StatusBits::MRC_REQUEST), "请求 PLC 进入最小风险状态(整机执行)"},
- {"SENSOR_DEGRADED", bit_mask(StatusBits::SENSOR_DEGRADED), "图像质量/传感器退化"},
- {"CALIB_STALE", bit_mask(StatusBits::CALIB_STALE), "标定过期或漂移"},
- {"DATA_LOSS", bit_mask(StatusBits::DATA_LOSS), "数据链中断(取帧超时/丢流)"},
- };
- }
- inline const char* cmd_label(uint16_t cmd) {
- switch (cmd) {
- case CMD_IDLE: return "空闲";
- case CMD_REQUEST: return "请求识别(按需)";
- case CMD_RESET: return "复位(清状态/投票)";
- case CMD_CLEAR_STATS: return "清统计";
- default: return "保留";
- }
- }
- /// 输入寄存器全表(含 CRC 行)。注意:说明按**字**给出(2 字宽字段拆成 _hi/_lo 两行)。
- inline std::vector<LayoutRow> snapshot_rows(int base = 0) {
- static const char* word_notes[] = {
- "0=不在车上 1=车斗 2=平板车 255=不可信(未就绪/超时)",
- "千分比 0..1000",
- "bit0 就绪 bit1 帧有效 bit2 稳定 bit3 帧率低 bit4 取帧超时 bit5 按需识别 bit6 结果有效 bit7 发布正常 bit8 感知不确定 bit9 MRC 请求 bit10 传感器退化 bit11 标定过期 bit12 数据链中断",
- "结果序号(32bit 高字)",
- "结果序号(32bit 低字)",
- "unix 秒(32bit 高字)",
- "unix 秒(32bit 低字)",
- "心跳计数:持续变化=视觉存活;停滞=离线",
- "帧率 x10",
- "单帧端到端耗时(毫秒)",
- "预留(未使用,恒 0)",
- };
- std::vector<LayoutRow> rows;
- int word = 0;
- for (int i = 0; i < VisionSnapshot::kFieldCount; ++i) {
- const RegisterField& field = VisionSnapshot::kLayout[i];
- for (int half = 0; half < field.width; ++half, ++word) {
- const std::string name = field.width > 1
- ? std::string(field.name) + (half == 0 ? "_hi" : "_lo")
- : std::string(field.name);
- rows.push_back({static_cast<uint16_t>(base + word), name, 1, word_notes[word]});
- }
- }
- rows.push_back({static_cast<uint16_t>(base + VisionSnapshot::register_count() - 1), "crc16", 1,
- "CRC-16/MODBUS(前 11 个字按大端字节序的校验值)"});
- return rows;
- }
- /// 保持寄存器全表。
- inline std::vector<LayoutRow> command_rows(int base = 0) {
- return {
- {static_cast<uint16_t>(base + 0), "cmd", 1, "0=空闲 1=请求识别(按需) 2=复位(清状态/投票) 3=清统计"},
- {static_cast<uint16_t>(base + 1), "preset", 1, "相机/ROI 预设编号(0=默认)"},
- {static_cast<uint16_t>(base + 2), "min_conf", 1, "覆盖最小置信度(千分比,0=用配置文件里的值)"},
- {static_cast<uint16_t>(base + 3), "reserved", 1, "预留(未使用)"},
- };
- }
- /// 一行摘要,供启动日志打印(现场核对用)。
- inline std::string layout_summary(const std::string& host, uint16_t port, uint8_t unit_id,
- uint16_t input_addr, uint16_t holding_addr) {
- return host + ":" + std::to_string(port) + " unit=" + std::to_string(unit_id) +
- " input=" + std::to_string(input_addr) + "(" +
- std::to_string(VisionSnapshot::register_count()) + "字)" + " holding=" +
- std::to_string(holding_addr) + "(" + std::to_string(PlcCommand::register_count()) +
- "字)";
- }
- } // namespace portcv
|