You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.5 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// main.cpp - 程序入口(模块化重构版)
// 模块: config / json_utils / filter / io_utils / lidar_utils / count / detect / tcp_server
#include <chrono>
#include <ctime>
#include <iostream>
#include <thread>
#include <vector>
#ifdef _WIN32
#include <winsock2.h>
#endif
#include "config.h"
#include "common.h"
#include "io_utils.h"
#include "lidar_utils.h"
#include "detect.h"
#include "tcp_server.h"
// ==================== 日志目录清理:启动时 + 每天 01:00 ====================
static void start_daily_cleanup()
{
clean_expired_log_dirs(); // 启动清理一次
std::thread([]() {
for (;;)
{
time_t raw = std::time(nullptr);
struct tm* t = std::localtime(&raw);
std::tm next = *t;
next.tm_hour = 1;
next.tm_min = 0;
next.tm_sec = 0;
time_t next_t = std::mktime(&next);
if (next_t <= raw) next_t += 86400; // 已过今日 01:00 → 明日
std::this_thread::sleep_for(std::chrono::seconds(next_t - raw));
clean_expired_log_dirs();
}
}).detach();
}
// ==================== exe 模式:自动做一次截面检测 ====================
// 启动统一后台扫描,采 Collect_Duration_S 秒后自动停止,做截面检测并保存到
// data/日期/时间_ 目录(无 task_id)。
static int run_exe_check(pavo2::pavo2_driver& driver)
{
std::cout << "\n[exe] 自动检测开始: 采集 " << Collect_Duration_S
<< " 秒 ..." << std::endl;
ScanSession scan;
scan.reset();
if (!scan.start(driver))
{
std::cerr << "[错误] 启动扫描会话失败!" << std::endl;
return -1;
}
std::this_thread::sleep_for(std::chrono::seconds(Collect_Duration_S));
scan.stop();
std::vector<CountPoint> pts = scan.snapshot();
scan.reset();
std::cout << "[exe] 采集结束, 点数=" << pts.size() << std::endl;
std::string out_dir = make_task_dir("");
DetectResult res;
detect_analyze_slices(pts, out_dir, &res);
std::cout << "[exe] 检测完成: ";
if (res.valid)
{
std::cout << "start白色占比=" << res.start_fill_pct
<< "%, end白色占比=" << res.end_fill_pct << "%";
}
else
{
std::cout << "无有效结果";
}
std::cout << ", 输出目录=" << out_dir << std::endl;
return 0;
}
// ==================== 主入口 ====================
int main()
{
#ifdef _WIN32
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
std::cerr << "[错误] WSAStartup 失败!" << std::endl;
return -1;
}
#endif
load_config(); // 默认读取 ./config/config (JSON)
start_daily_cleanup();
std::cout << "==================== 配置文件内容 ====================" << std::endl;
std::cout << "雷达IP: " << Leida_IP << " : " << Leida_PORT << std::endl;
std::cout << "运行模式: " << Run_Mode << " (tcp=服务端, 其他=自动检测)" << std::endl;
std::cout << "TCP端口: " << Tcp_Port << std::endl;
std::cout << "采集时长: " << Collect_Duration_S << " 秒" << std::endl;
std::cout << "移动速度: " << Speed_Z << " cm/s (0=时间模式, >0=距离模式)"
<< std::endl;
std::cout << "Z范围: " << Z_RANGE_MM << " mm | Y范围: " << Y_RANGE_MM
<< " mm | 雷达频率: " << Lidar_Hz << " Hz" << std::endl;
std::cout << "z空隙插值模式: " << Z_Interpolate_Mode << std::endl;
std::cout << "日志保留天数: " << Log_Retain_Days << std::endl;
std::cout << "======================================================" << std::endl;
std::cout << "======================================================" << std::endl;
std::cout << " Pavo2 Lidar 雷达数据采集程序 (模块化)" << std::endl;
std::cout << "======================================================" << std::endl;
pavo2::pavo2_driver driver;
if (!connect_lidar(driver))
{
#ifdef _WIN32
WSACleanup();
#endif
return -1;
}
std::cout << "\n[等待] 等待雷达数据稳定(2秒)..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
int rc = 0;
if (Run_Mode == "tcp")
run_tcp_server(driver);
else
rc = run_exe_check(driver);
std::cout << "\n[关闭] 正在断开雷达连接..." << std::endl;
driver.pavo2_close();
#ifdef _WIN32
WSACleanup();
#endif
std::cout << "[完成] 程序运行结束。" << std::endl;
return rc;
}