diff --git a/config.cpp b/config.cpp index 99587a6..f269ef7 100644 --- a/config.cpp +++ b/config.cpp @@ -209,6 +209,8 @@ bool load_config(const std::string& path) // 2. 全局段: server / log if (json_get_string(text, "server", "mode", s)) new_cfg.server.mode = s; if (json_get_number(text, "server", "port", d)) new_cfg.server.port = (uint16_t)d; + if (json_get_bool(text, "server", "connect_on_demand", bv)) + new_cfg.server.connect_on_demand = bv; if (json_get_bool(text, "log", "enable", bv)) new_cfg.log.enable = bv; if (json_get_number(text, "log", "retain_days", d)) diff --git a/config.h b/config.h index 8fd8f3d..cab4f6e 100644 --- a/config.h +++ b/config.h @@ -135,6 +135,10 @@ struct AppConfig { std::string mode = "exe"; // exe=直接检测一次 / tcp=TCP服务端 / http=HTTP服务端 uint16_t port = 9601; // TCP/HTTP监听端口 + // 按需连接(默认关): 关=程序启动即连接雷达并保持(空闲期SDK仍持续收/解析数据, + // 单核占用约100%); 开=收到 start 才连接, 扫描结束后断开, 空闲期不收任何数据。 + // 代价: start 响应多约 1~3 秒(建连+SDK线程启动)。 + bool connect_on_demand = false; } server; // ---------- log: 日志(全局) ---------- diff --git a/count.cpp b/count.cpp index 1da8745..6933253 100644 --- a/count.cpp +++ b/count.cpp @@ -242,13 +242,16 @@ static void trim_tail_runs(std::vector& image, int w, int h, // ==================== 点集 → 图像 ==================== std::vector points_to_image(const std::vector& points, double* area_out, - int* out_w, int* out_h) + int* out_w, int* out_h, + double* z_min_out, + const double* z_min_forced) { const int MIN_DIM = 8; // 最小尺寸保护 const int MAX_DIM = 20000; // 单边最大尺寸保护(防止z过大导致内存爆炸) if (area_out) *area_out = 0.0; if (out_w) *out_w = 0; if (out_h) *out_h = 0; + if (z_min_out) *z_min_out = 0.0; if (points.empty()) return std::vector((size_t)MIN_DIM * MIN_DIM, 0); // 宽: radar_cfg().filter.polygon 多边形 X 最大最小值直接构成 (1mm=1px) @@ -266,13 +269,25 @@ std::vector points_to_image(const std::vector& points, double spanX = polyXMax - polyXMin; if (spanX <= 0.0) spanX = 1.0; - // 高: z 最大值直接构成 (Z=运动方向, 从0开始递增, 1单位=1px) - double maxZ = 0.0; + // 高: z 轴有点范围 z_max - z_min (Z=运动方向, 1单位=1px) + // 只保留 z 轴有点的部分, 裁掉下方的空白区域。 + // z_min_forced 非空时以指定值为起点(layer 图与全点图共用同一 z 范围对齐)。 + double minZ = 0.0; + if (z_min_forced) + minZ = *z_min_forced; + else + { + minZ = points.front().z_val; + for (const auto& p : points) minZ = std::min(minZ, p.z_val); + } + double maxZ = minZ; for (const auto& p : points) maxZ = std::max(maxZ, p.z_val); - if (maxZ <= 0.0) maxZ = 1.0; + double spanZ = maxZ - minZ; + if (spanZ <= 0.0) spanZ = 1.0; + if (z_min_out) *z_min_out = minZ; int imageWidth = (int)std::lround(spanX); - int imageHeight = (int)std::lround(maxZ); + int imageHeight = (int)std::lround(spanZ); if (imageWidth < MIN_DIM) imageWidth = MIN_DIM; if (imageHeight < MIN_DIM) imageHeight = MIN_DIM; if (imageWidth > MAX_DIM) imageWidth = MAX_DIM; @@ -281,9 +296,9 @@ std::vector points_to_image(const std::vector& points, if (out_h) *out_h = imageHeight; std::vector image((size_t)imageWidth * imageHeight, 0); // 0=白底 - // X坐标相对多边形左边界偏移到0;Z从0开始 + // X坐标相对多边形左边界偏移到0;Z相对有点范围的下边界偏移到0 double scaleX = (double)imageWidth / spanX; // ≈1: 宽直接由多边形X跨度构成 - double scaleY = (double)imageHeight / maxZ; // ≈1: 高直接由z最大值构成 + double scaleY = (double)imageHeight / spanZ; // ≈1: 高直接由z有点范围构成 double xOffset = -polyXMin; // 优化:Step1 先按1x1网格标记点位置(重复点只写一次), @@ -295,7 +310,7 @@ std::vector points_to_image(const std::vector& points, for (const auto& p : points) { int px = (int)((p.x_mm + xOffset) * scaleX); - int py = (int)(p.z_val * scaleY); + int py = (int)((p.z_val - minZ) * scaleY); if (px < 0 || px >= imageWidth || py < 0 || py >= imageHeight) continue; size_t idx = (size_t)py * imageWidth + px; if (image[idx] == 0) // 未标记过才记录 @@ -333,7 +348,9 @@ std::vector points_to_image(const std::vector& points, for (size_t i = 0; i < image.size(); i++) if (image[i] != 0) totalPixels++; - double pixelToAreaRatio = spanX * maxZ / (double)(imageWidth * imageHeight); + // 每像素实际面积: (spanX/宽)×(spanZ/高)。必须用 spanZ(z有点范围)而非 maxZ —— + // 图像高度已改为 spanZ, 若沿用 maxZ, z 不从 0 开始的数据面积会虚大 maxZ/spanZ 倍 + double pixelToAreaRatio = spanX * spanZ / (double)(imageWidth * imageHeight); if (area_out) *area_out = totalPixels * pixelToAreaRatio; return image; } @@ -693,6 +710,10 @@ int calc_item_count(const std::vector& points, const CountParams& bo if (points.empty() || box.l <= 0 || box.w <= 0 || box.h <= 0 || box.maxPerLayer <= 0) return 0; + // 全点 z 范围下界: layer 图与 count_points.jpg 共用同一 z 范围, 宽高完全一致 + double all_zmin = points.front().z_val; + for (const auto& p : points) all_zmin = std::min(all_zmin, p.z_val); + const bool use_half_max = (radar_cfg().count.layer_algorithm == "HalfMax"); const bool use_kde_peak = (radar_cfg().count.layer_algorithm == "KdePeak"); // 分层容差(三个算法统一): 基准 + 每层递增 @@ -811,7 +832,8 @@ int calc_item_count(const std::vector& points, const CountParams& bo { double area = 0.0; int img_w = 0, img_h = 0; - std::vector img = points_to_image(it->second, &area, &img_w, &img_h); + std::vector img = points_to_image(it->second, &area, &img_w, &img_h, + nullptr, &all_zmin); layer_area[h] = area; layer_img[h] = std::move(img); layer_img_size[h] = std::make_pair(img_w, img_h); @@ -911,7 +933,15 @@ void save_count_files_async(const std::string& radar_id, double all_area = 0.0; int all_w = 0, all_h = 0; std::vector all_img = points_to_image(points, &all_area, &all_w, &all_h); - if (save_jpg_gray(out_dir + "/count_points.jpg", all_w, all_h, all_img)) + // 全黑图(所有点被过滤光/点集为空)不落盘 —— 与 count_layer.jpg 行为一致: + // 没有有效内容就不生成, 而不是存一张黑图。 + auto has_ink = [](const std::vector& img) + { + for (uint8_t v : img) if (v) return true; + return false; + }; + if (has_ink(all_img) && + save_jpg_gray(out_dir + "/count_points.jpg", all_w, all_h, all_img)) std::cout << "[count] 点云图已保存: " << out_dir << "/count_points.jpg (" << all_w << "x" << all_h << ", X-Z平面, 面积≈" << std::fixed << std::setprecision(1) @@ -920,7 +950,8 @@ void save_count_files_async(const std::string& radar_id, // 图片2:被计数层(最高有效层)图像 if (top_lv > 0 && !top_layer_img.empty() && top_img_w > 0 && top_img_h > 0) { - if (save_jpg_gray(out_dir + "/count_layer.jpg", top_img_w, top_img_h, top_layer_img)) + if (has_ink(top_layer_img) && + save_jpg_gray(out_dir + "/count_layer.jpg", top_img_w, top_img_h, top_layer_img)) std::cout << "[count] 有效层图已保存: " << out_dir << "/count_layer.jpg (" << top_img_w << "x" << top_img_h << ", 层" << top_lv << ", 面积≈" << std::fixed diff --git a/count.h b/count.h index 5117d01..82d42f4 100644 --- a/count.h +++ b/count.h @@ -13,13 +13,18 @@ int sparse_density_filter(std::vector& points); // 点集 → X-Z平面白底图像(0=白,255=黑点) + 换算面积。 -// 宽: cfg.filter.polygon 多边形X跨度(1mm=1px); 高: z最大值(1单位=1px); +// 宽: cfg.filter.polygon 多边形X跨度(1mm=1px); +// 高: z轴有点范围 z_max - z_min(1单位=1px) —— 只保留 z 轴有点的部分, 不含下方空白。 // 点先按1x1网格标记再膨胀 cfg.count.point_dilate×cfg.count.point_dilate。 // area_out 返回换算实际面积(mm^2); out_w/out_h 返回实际图像尺寸。 +// z_min_out : 返回图像对应的 z 起点(图内第0行 = z_min_out)。 +// z_min_forced: 非空时强制以该 z 为图像起点(如 layer 图与全点图使用同一范围对齐)。 std::vector points_to_image(const std::vector& points, double* area_out = nullptr, int* out_w = nullptr, - int* out_h = nullptr); + int* out_h = nullptr, + double* z_min_out = nullptr, + const double* z_min_forced = nullptr); // z轴空隙插值:雷达断流/瞬间丢包导致的 z 空白带,仅在停止采集后调用一次。 // 原理:同一圈(scan包)点 z 相同,正常圈间隔≈normal_gap; diff --git a/crash_handler.cpp b/crash_handler.cpp index 01cdf4c..9a72c14 100644 --- a/crash_handler.cpp +++ b/crash_handler.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -635,13 +636,13 @@ void crash_milestone(const char* fmt, ...) std::strncpy(g_milestone, buf, sizeof(g_milestone) - 1); g_milestone[sizeof(g_milestone) - 1] = '\0'; } - // 同时进控制台与 run.log(便于对照时间线) + // 同时进控制台与 run.log(便于对照时间线)。 + // 时间戳由日志镜像(TeeBuf)统一加在行首, 这里不再自带, 避免一行两个时间。 char line[600]; - char ts[32]; - now_str(ts, sizeof(ts)); - snprintf(line, sizeof(line), "[%s][里程碑] %s\n", ts, buf); - std::fwrite(line, 1, std::strlen(line), stdout); - fflush(stdout); + snprintf(line, sizeof(line), "[里程碑] %s\n", buf); + // 走 std::cout(经日志镜像 TeeBuf)而非 fwrite(stdout): 否则该行不会写进 run.log + std::cout << line; + std::cout.flush(); } void crash_log(const char* fmt, ...) diff --git a/io_utils.cpp b/io_utils.cpp index 4d542fa..d3c84b4 100644 --- a/io_utils.cpp +++ b/io_utils.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -211,6 +212,52 @@ void save_pcd_ascii(const std::string& filename, f.close(); } +bool load_pcd_ascii(const std::string& path, + std::vector& xs, + std::vector& ys, + std::vector& zs, + std::string& err) +{ + std::ifstream f(path.c_str()); + if (!f.is_open()) + { + err = "cannot open file: " + path; + return false; + } + std::string line; + bool in_data = false; + while (std::getline(f, line)) + { + if (!in_data) + { + std::string t = trim_str(line); + if (t.empty() || t[0] == '#') continue; + // 头部只做必要校验: 遇到 DATA 行进入数据段, 只支持 ascii + if (t.rfind("DATA", 0) == 0) + { + if (t.find("ascii") == std::string::npos) + { + err = "only DATA ascii supported, got: " + t; + return false; + } + in_data = true; + } + continue; + } + std::istringstream ss(line); + double x, y, z; + if (ss >> x >> y >> z) // 残缺行/空行忽略 + { + xs.push_back(x); + ys.push_back(y); + zs.push_back(z); + } + } + if (!in_data) { err = "no DATA ascii section: " + path; return false; } + if (xs.empty()) { err = "no points in file: " + path; return false; } + return true; +} + bool save_jpg_gray(const std::string& filename, int width, int height, const std::vector& pixels, int quality) { @@ -287,6 +334,26 @@ namespace std::string day_; }; + // 当前时刻 HH-MM-SS-SSS(本地时间,含毫秒) + static std::string log_stamp() + { + using namespace std::chrono; + auto now = system_clock::now(); + time_t raw = system_clock::to_time_t(now); + int ms = (int)(duration_cast(now.time_since_epoch()).count() % 1000); + if (ms < 0) ms += 1000; + struct tm tmv; +#ifdef _WIN32 + localtime_s(&tmv, &raw); +#else + localtime_r(&raw, &tmv); +#endif + char buf[24]; + std::snprintf(buf, sizeof(buf), "%02d-%02d-%02d-%03d", + tmv.tm_hour, tmv.tm_min, tmv.tm_sec, ms); + return buf; + } + // 同时写控制台(原 rdbuf)与 run.log 的 streambuf,按行落盘 class TeeBuf : public std::streambuf { @@ -330,12 +397,33 @@ namespace } void out_locked(const char* data, std::size_t n) { - if (console_) console_->sputn(data, (std::streamsize)n); - RunLog::instance().write(data, n); + // 每行开头加时间戳 HH-MM-SS-SSS(控制台与 run.log 同步生效)。 + // 多线程打印时各行为原子整行(TeeBuf 持锁),时间戳取写入时刻。 + std::string stamped; + stamped.reserve(n + 32); + std::size_t i = 0; + while (i < n) + { + if (line_start_) + { + stamped += log_stamp(); + stamped += ' '; + line_start_ = false; + } + std::size_t j = i; + while (j < n && data[j] != '\n') ++j; + if (j < n) ++j; // 含换行符 + stamped.append(data + i, j - i); + if (j > i && data[j - 1] == '\n') line_start_ = true; + i = j; + } + if (console_) console_->sputn(stamped.data(), (std::streamsize)stamped.size()); + RunLog::instance().write(stamped.data(), stamped.size()); } std::streambuf* console_; std::string buf_; std::mutex mtx_; + bool line_start_ = true; // 上一输出是否止于行尾 }; static TeeBuf* g_cout_tee = nullptr; diff --git a/io_utils.h b/io_utils.h index e9ce960..4d01adc 100644 --- a/io_utils.h +++ b/io_utils.h @@ -37,6 +37,14 @@ void save_pcd_ascii(const std::string& filename, const std::vector& ys, const std::vector& zs); +// 读取 ASCII PCD 点云(本工程 save_pcd_ascii 保存的格式,FIELDS x y z / DATA ascii)。 +// 返回 true 时 xs/ys/zs 填充了点;失败时 err 给出原因(文件打不开/非ascii/无点)。 +bool load_pcd_ascii(const std::string& path, + std::vector& xs, + std::vector& ys, + std::vector& zs, + std::string& err); + // 保存灰度 JPG(灰度转RGB后压缩为JPEG,quality=90) bool save_jpg_gray(const std::string& filename, int width, int height, const std::vector& pixels, int quality = 90); diff --git a/lidar_utils.cpp b/lidar_utils.cpp index 415c616..e6eb6e9 100644 --- a/lidar_utils.cpp +++ b/lidar_utils.cpp @@ -52,6 +52,42 @@ bool connect_lidar(pavo2::pavo2_driver& driver, const RadarConfig& rc) return true; } +// ==================== 按需连接 (server.connect_on_demand=true 时使用) ==================== +// 背景: SDK 在 Connect 成功的瞬间就启动接收线程并永远运行(pavo2_net.cpp), +// 即使不 start 扫描也会持续收+解析雷达数据流(50Hz×4500点), 空闲期占满一个核。 +// 按需连接模式下: 收到 start 才建连, 扫描结束立即断开, 空闲期零 CPU 占用。 +// 代价: start 响应多约 1~3 秒(建连+SDK线程启动+设备参数下发)。 + +// 确保节点已连接; 未连接则现场建立连接(持有 io_mtx 与后台重连线程互斥)。 +// 返回 true=已连接(可能是本次新建的)。 +bool ensure_radar_connected(RadarNode* n) +{ + if (!n || !n->enabled.load()) return false; + if (n->connected.load()) return true; + std::lock_guard lk(n->io_mtx); + if (n->connected.load()) return true; // 双检: 拿锁期间可能已被连上 + std::shared_ptr rc = cfg.find(n->id); + if (!rc) return false; + RadarConfig copy = *rc; // 拷贝, 防配置重载时引用失效 + if (connect_lidar(*n->driver, copy)) + { + n->connected = true; + return true; + } + return false; +} + +// 断开节点连接并复位状态(按需连接模式下扫描取完结果后调用)。 +// 采集进行中绝不关连接(调用方保证已 session.stop, 此处再防御一次)。 +void close_radar_connection(RadarNode* n) +{ + if (!n) return; + if (n->session.running()) return; + std::lock_guard lk(n->io_mtx); + n->driver->pavo2_close(); + n->connected = false; +} + // ==================== 扫描辅助 ==================== int drain_stale_scans(pavo2::pavo2_driver& driver, int max_ms) { @@ -572,8 +608,9 @@ void RadarManager::connect_loop(RadarNode* n) } } } - else if (n->enabled.load()) + else if (n->enabled.load() && !cfg.server.connect_on_demand) { + // 按需连接模式: 空闲期不主动连接, 由 start 命令现场建连(ensure_radar_connected) std::shared_ptr rc = cfg.find(n->id); if (rc) { diff --git a/lidar_utils.h b/lidar_utils.h index 79eb3d0..da4d4d1 100644 --- a/lidar_utils.h +++ b/lidar_utils.h @@ -127,6 +127,13 @@ struct RadarNode std::mutex io_mtx; // open/close/enable_data 串行化 }; +// ==================== 按需连接 (server.connect_on_demand) ==================== +// ensure: 未连接则现场建连(阻塞, 约1~3秒); 已连接直接返回 true。 +// close : 断开连接。采集运行中调用会被忽略(防御)。 +// tcp/http 模式: start 前建连, 结果取完后断开; exe 模式: 启动时建连一次。 +bool ensure_radar_connected(RadarNode* n); +void close_radar_connection(RadarNode* n); + class RadarManager { public: diff --git a/main.cpp b/main.cpp index 7e0b9ac..cec1901 100644 --- a/main.cpp +++ b/main.cpp @@ -266,6 +266,9 @@ int main() // exe 模式:用默认雷达(配置第一个),最多等 10 秒连上 std::string id = radar_mgr.default_id(); RadarNode* node = radar_mgr.find(id); + // 按需连接模式下后台线程不主动建连, exe 模式没有 start 命令, 这里现场连一次 + if (node && !node->connected.load() && cfg.server.connect_on_demand) + ensure_radar_connected(node); if (!node || !radar_mgr.wait_connected(id, 10000)) { std::cerr << "[错误] 默认雷达 " << id << " 未连接,无法自动检测。" << std::endl; diff --git a/main.exe b/main.exe index c5eb014..7e13ad6 100644 Binary files a/main.exe and b/main.exe differ diff --git a/simiRadar_20261107.zip b/simiRadar_20261107.zip new file mode 100644 index 0000000..dbabfd0 Binary files /dev/null and b/simiRadar_20261107.zip differ diff --git a/simiRadar_20261107_v2.zip b/simiRadar_20261107_v2.zip new file mode 100644 index 0000000..bd4d785 Binary files /dev/null and b/simiRadar_20261107_v2.zip differ diff --git a/simiRadar_20261107_v3.zip b/simiRadar_20261107_v3.zip new file mode 100644 index 0000000..b5d4de4 Binary files /dev/null and b/simiRadar_20261107_v3.zip differ diff --git a/tcp_server.cpp b/tcp_server.cpp index 2075562..dffd744 100644 --- a/tcp_server.cpp +++ b/tcp_server.cpp @@ -185,6 +185,9 @@ namespace node->session.stop(); // 等待采集线程退出(≤100ms轮询粒度) *pts = node->session.snapshot(); node->session.reset(); // 清空点集,为下一次 start 准备 + // 按需连接: 结果已到手, 立即断开, 空闲期 SDK 不再收/解析数据(省一个核) + if (cfg.server.connect_on_demand) + close_radar_connection(node); } return true; } @@ -203,6 +206,8 @@ namespace { node->session.stop(); node->session.reset(); + // 注意: 此函数唯一调用点是 handle_scan_start 的强制重启, 之后必然 + // 立即 session.start —— 这里绝不能关连接, 保留现场复用。 } return true; } @@ -227,7 +232,9 @@ namespace total = calc_item_count(work, snap.box, &detail, &top_img, &tw, &th, &tlv, &tarea); } - std::cout << "[TCP][" << snap.radar << "] count结束: task_id=" << snap.task_id + std::cout << "[TCP][" << snap.radar << "] " + << (snap.function == "count_pcd" ? "count_pcd完成" : "count结束") + << ": task_id=" << snap.task_id << ", 点数=" << pts.size() << ", 结果=" << total << std::endl; std::ostringstream r; @@ -244,6 +251,77 @@ namespace total, tlv, tarea, &pts); } + // ==================== count_pcd: 离线重算 ==================== + // 输入已有 pcd 路径 + box 参数, 走与在线 count 完全相同的后续逻辑: + // 稀疏过滤 → z空隙插值 → 分层计数 → 保存 count_data.pcd/图片/log.txt + // pcd 的 y 是高度(= floor_height - y_mm), 读回时按此还原。 + // 请求: {"function":"count_pcd","pcd":"D:/x/count_data.pcd", + // "box":{"l":..,"w":..,"h":..,"maxPerLayer":..},"radar":"radar1","task_id":"t1"} + // 不触碰雷达连接与扫描会话, 可在扫描进行中调用。 + static void handle_count_pcd(const std::string& msg, SOCKET client, + std::string* resp, const std::string& radar_id) + { + std::string pcd_path; + json_get_string(msg, "pcd", pcd_path); + std::string task_id; + json_get_string(msg, "task_id", task_id); + + CountParams box; + json_get_string(msg, "task_id", box.task_id); + json_get_number(msg, "l", box.l); + json_get_number(msg, "w", box.w); + json_get_number(msg, "h", box.h); + double d = 0.0; + if (json_get_number(msg, "maxPerLayer", d) || + json_get_number(msg, "max", d)) + box.maxPerLayer = (int)d; + + if (pcd_path.empty()) + { + send_reply(client, resp, "{\"code\":400,\"message\":\"missing pcd path\"}"); + return; + } + if (!(box.l > 0 && box.w > 0 && box.h > 0 && box.maxPerLayer > 0)) + { + send_reply(client, resp, + "{\"code\":400,\"message\":\"invalid box (l/w/h/maxPerLayer required)\"}"); + return; + } + + std::vector xs, ys, zs; + std::string err; + if (!load_pcd_ascii(pcd_path, xs, ys, zs, err)) + { + std::cerr << "[TCP][" << radar_id << "] count_pcd 读取失败: " << err << std::endl; + send_reply(client, resp, "{\"code\":400,\"message\":\"pcd load failed\"}"); + return; + } + + // pcd 的 y=高度 → 还原为 y_mm = floor_height - 高度 (与保存时互逆) + std::vector pts; + pts.reserve(xs.size()); + double floor_h = radar_cfg().count.floor_height; + for (std::size_t i = 0; i < xs.size(); ++i) + { + CountPoint p; + p.x_mm = xs[i]; + p.y_mm = floor_h - ys[i]; + p.z_val = zs[i]; + pts.push_back(p); + } + + std::cout << "[TCP][" << radar_id << "] count_pcd: " << pcd_path + << " (" << pts.size() << " 点)" << std::endl; + + SessionState snap; + snap.active = true; + snap.radar = radar_id; + snap.function = "count_pcd"; + snap.box = box; + snap.task_id = task_id; + finish_count(snap, pts, client, resp); // 与在线 count 完全同一条计算/保存路径 + } + static void finish_check(const SessionState& snap, const std::vector& pts, SOCKET target, std::string* resp = nullptr) @@ -369,7 +447,9 @@ namespace ack << ",\"scanTime\":" << (int)scan_time; ack << ",\"radar\":\"" << radar_id << "\"}"; - if (!node->connected.load()) + // 未连接时: 按需连接模式下现场建连(约1~3秒); 常驻模式或建连失败才拒绝 + if (!node->connected.load() && + !(cfg.server.connect_on_demand && ensure_radar_connected(node))) { std::string fail = "radar not connected: " + radar_id; std::cerr << "[TCP][" << radar_id << "] " << fail << std::endl; @@ -495,7 +575,9 @@ namespace std::cout << "[TCP][" << radar_id << "] 收到ConfigureSpeed: 采集" << seconds << "s 标定变速剖面(每帧Y最大值) ..." << std::endl; - if (!node->connected.load()) + // 按需连接: 标定前现场建连, 结束(无论成败)后断开 + if (!node->connected.load() && + !(cfg.server.connect_on_demand && ensure_radar_connected(node))) { std::string fail = "radar not connected: " + radar_id; std::cerr << "[TCP][" << radar_id << "] " << fail << std::endl; @@ -511,6 +593,8 @@ namespace std::cout << "[TCP][" << radar_id << "] 扫描运行中被ConfigureSpeed中断" << std::endl; rows = collect_speed_profile(*node->driver, seconds, err); } + if (cfg.server.connect_on_demand) + close_radar_connection(node); // 标定结束即断开 if (rows < 0) { std::cerr << "[TCP][" << radar_id << "] ConfigureSpeed失败: " << err << std::endl; @@ -559,6 +643,11 @@ namespace const std::string radar_id = node->id; RadarScope scope(radar_id); // 本请求全程使用该雷达的配置 + if (fn == "count_pcd") + { + handle_count_pcd(msg, client, resp, radar_id); + return true; + } if (fn == "ConfigureSpeed" || fn == "configurespeed") { handle_configure_speed(msg, client, resp, radar_id, node);