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.

110 lines
5.1 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.

#include "config.h"
#include "json_utils.h"
#include <iostream>
#include <fstream>
#include <sstream>
// ==================== 配置变量定义 ====================
std::string Leida_IP = "192.168.0.170";
uint16_t Leida_PORT = 2368;
int Collect_Duration_S = 6;
double Speed_Z = 20.0;
bool Log_Flag = true;
double Z_RANGE_MM = 50.0;
double Y_RANGE_MM = 80.0;
double Lidar_Hz = 50.0;
int Rect_Width_PX = 8;
double Rect_Length_Ratio = 1.5;
std::string Run_Mode = "exe";
uint16_t Tcp_Port = 9601;
double Floor_Height = 1600.0;
double Count_Base_Tolerance = 50.0;
double Count_Additional_Tolerance_Per_Level = 20.0;
int Point_Dilate = 8;
std::string Z_Interpolate_Mode = "none";
int Log_Retain_Days = 15;
int Tuo_Wei = 2;
int Qian_Chong = 2;
int Yuan_Duan = 1;
bool Jin_Duan = true;
std::vector<std::pair<double, double>> Filter_Polygon = {
{-650, 0}, {-650, 670}, {650, 670}, {650, 0}
};
int Filter_Poly_N = (int)Filter_Polygon.size();
// ==================== 配置加载(JSON + // 注释行) ====================
void load_config(const std::string& path)
{
std::ifstream fin(path);
if (!fin.is_open()) {
std::cerr << "[WARN] 未找到配置文件: " << path << std::endl;
exit(-1);
}
// 1. 剔除行首(忽略空白)以 // 开头的注释行,其余拼接为 JSON 文本
std::ostringstream json_oss;
std::string line;
while (std::getline(fin, line))
{
size_t b = line.find_first_not_of(" \t\r\n");
if (b != std::string::npos && line.compare(b, 2, "//") == 0)
continue; // 整行注释
json_oss << line << "\n";
}
fin.close();
std::string text = json_oss.str();
std::string s; double d; bool bv; std::vector<std::pair<double, double>> poly;
if (json_get_string(text, "Leida_IP", s)) Leida_IP = s;
if (json_get_number(text, "Leida_Port", d)) Leida_PORT = (uint16_t)d;
if (json_get_number(text, "Collect_Duration_S", d)) Collect_Duration_S = (int)d;
if (json_get_number(text, "Speed_Z", d)) Speed_Z = d;
if (json_get_bool(text, "Log_Flag", bv)) Log_Flag = bv;
if (json_get_number(text, "Z_Range_MM", d)) Z_RANGE_MM = d;
if (json_get_number(text, "Y_Range_MM", d)) Y_RANGE_MM = d;
if (json_get_number(text, "Lidar_Hz", d)) Lidar_Hz = d;
if (json_get_number(text, "Rect_Width_PX", d)) Rect_Width_PX = (int)d;
if (json_get_number(text, "Rect_Length_Ratio", d)) Rect_Length_Ratio = d;
if (json_get_string(text, "Run_Mode", s)) Run_Mode = s;
if (json_get_number(text, "Tcp_Port", d)) Tcp_Port = (uint16_t)d;
if (json_get_number(text, "Floor_Height", d)) Floor_Height = d;
if (json_get_number(text, "Count_Base_Tolerance", d)) { Count_Base_Tolerance = d; if (Count_Base_Tolerance < 0) Count_Base_Tolerance = 0; }
if (json_get_number(text, "Count_Additional_Tolerance_Per_Level", d)) { Count_Additional_Tolerance_Per_Level = d; if (Count_Additional_Tolerance_Per_Level < 0) Count_Additional_Tolerance_Per_Level = 0; }
if (json_get_number(text, "Point_Dilate", d)) { Point_Dilate = (int)d; if (Point_Dilate < 1) Point_Dilate = 1; if (Point_Dilate > 64) Point_Dilate = 64; }
if (json_get_string(text, "Z_Interpolate_Mode", s))
{
if (s == "CopyTranslation" || s == "AngleBucketInterpolation") Z_Interpolate_Mode = s;
else Z_Interpolate_Mode = "none";
}
if (json_get_number(text, "Log_Retain_Days", d)) { Log_Retain_Days = (int)d; if (Log_Retain_Days < 1) Log_Retain_Days = 1; }
if (json_get_number(text, "Tuo_Wei", d)) Tuo_Wei = (int)d;
if (json_get_number(text, "Qian_Chong", d)) Qian_Chong = (int)d;
if (json_get_number(text, "Yuan_Duan", d)) Yuan_Duan = (int)d;
if (json_get_bool(text, "Jin_Duan", bv)) Jin_Duan = bv;
if (json_get_point_array(text, "Filter_Polygon", poly) >= 3)
{
Filter_Polygon = poly;
Filter_Poly_N = (int)poly.size();
}
std::cout << "========== 配置已加载 ==========" << std::endl;
std::cout << "雷达: " << Leida_IP << ":" << Leida_PORT
<< " 帧率: " << Lidar_Hz << "Hz" << std::endl;
std::cout << "扫描默认时长: " << Collect_Duration_S << "s"
<< " Z速度: " << Speed_Z << "cm/s(0=时间)"
<< " 日志保留: " << Log_Retain_Days << "天" << std::endl;
std::cout << "运行模式: " << Run_Mode << (Run_Mode == "tcp" ? " (端口 " + std::to_string(Tcp_Port) + ")" : "") << std::endl;
std::cout << "分层冗余: 基准" << Count_Base_Tolerance
<< " + 每层" << Count_Additional_Tolerance_Per_Level
<< " 膨胀: " << Point_Dilate
<< " 插值模式: " << Z_Interpolate_Mode << std::endl;
std::cout << "过滤多边形(" << Filter_Poly_N << "点):";
for (const auto& p : Filter_Polygon)
std::cout << " (" << p.first << "," << p.second << ")";
std::cout << std::endl;
std::cout << "================================" << std::endl;
}