TIAN-LEIDA-JIANCE/include/unitree_driver.h

124 lines
4.2 KiB
C
Raw Normal View History

#pragma once
#include <string>
#include <vector>
#include <cstdint>
#include <memory>
// 前向声明
namespace unitree_lidar_sdk {
class UnitreeLidarReader;
}
struct ULPoint {
float x, y, z, intensity;
float timestamp; // 对应SDK中的time字段
uint32_t ring; // 对应SDK中的ring字段
};
// 裂缝信息结构体
struct CrackInfo {
float start_x, start_y, start_z; // 裂缝起点
float end_x, end_y, end_z; // 裂缝终点
float length; // 裂缝长度
float depth; // 裂缝深度
float width; // 裂缝宽度
bool is_critical; // 是否为严重裂缝
uint32_t point_count; // 相关点数
};
// 遮挡物信息结构体
struct OcclusionInfo {
float center_x = 0.0f, center_y = 0.0f, center_z = 0.0f; // 遮挡物位置
float distance = 0.0f; // 距离雷达的距离
float size = 0.0f; // 遮挡物大小
float width = 0.0f, height = 0.0f; // 遮挡物尺寸
bool is_blocking = false; // 是否严重遮挡
uint32_t point_count = 0; // 遮挡物点数
};
// 预警级别枚举
enum class AlertLevel {
NORMAL = 0, // 正常
WARNING = 1, // 警告
CRITICAL = 2 // 严重
};
// 检测结果结构体
struct DetectionResult {
std::vector<CrackInfo> cracks; // 检测到的裂缝
std::vector<OcclusionInfo> occlusions; // 检测到的遮挡物
AlertLevel alert_level = AlertLevel::NORMAL; // 当前预警级别
std::string alert_message = ""; // 预警消息
uint32_t total_points = 0; // 总点数
float scan_coverage = 1.0f; // 扫描覆盖率
// 构造函数确保正确初始化
DetectionResult()
: alert_level(AlertLevel::NORMAL)
, alert_message("")
, total_points(0)
, scan_coverage(1.0f)
{}
};
// 坡面分析器类
class SlopeAnalyzer {
private:
struct Config {
float crack_threshold = 100.0f; // 裂缝阈值100米
float min_crack_length = 100.0f; // 最小裂缝长度100米
float occlusion_threshold = 0.20f; // 遮挡阈值改为20厘米
float alert_distance = 0.20f; // 预警距离20厘米
float severe_alert_distance = 0.15f; // 严重预警距离15厘米
float critical_distance = 0.05f; // 关键距离5厘米
float scan_width = 16.0f; // 扫描宽度16米
float height_diff_threshold = 0.1f; // 高度差阈值10厘米
bool enable_voice_alert = true; // 启用语音预警
int voice_repeat_count = 3; // 语音重复3次
} config_;
// 历史数据用于趋势分析
std::vector<DetectionResult> history_;
static const size_t MAX_HISTORY = 10;
public:
SlopeAnalyzer();
// 设置检测参数
void setConfig(float crack_threshold, float min_crack_length,
float occlusion_threshold, float alert_distance);
// 分析点云数据
DetectionResult analyze(const std::vector<ULPoint>& points);
// 获取历史检测结果
const std::vector<DetectionResult>& getHistory() const { return history_; }
private:
// 检测裂缝
std::vector<CrackInfo> detectCracks(const std::vector<ULPoint>& points);
// 检测遮挡物
std::vector<OcclusionInfo> detectOcclusions(const std::vector<ULPoint>& points);
// 计算预警级别
AlertLevel calculateAlertLevel(const std::vector<CrackInfo>& cracks,
const std::vector<OcclusionInfo>& occlusions);
// 生成预警消息
std::string generateAlertMessage(const DetectionResult& result);
};
class UnitreeDriver {
private:
unitree_lidar_sdk::UnitreeLidarReader* lreader;
public:
UnitreeDriver() : lreader(nullptr) {} // 构造函数初始化
~UnitreeDriver() { close(); } // 析构函数确保资源释放
// 串口:/dev/ttyACM0 或 /dev/ttyUSB0以太网可用 "udp://ip:port" 自己扩展
bool open(const std::string& port, int baudrate);
bool readFrame(std::vector<ULPoint>& out_points); // 读取一帧点云返回false表示暂时无数据
void close();
};