#ifndef CRSLIDARDEVICE_H #define CRSLIDARDEVICE_H #include "IRsLidarDevice.h" // SDK 内部以 uint8_t* 调用 recvfrom(),但 C++ 中 char*/unsigned char*/signed char* // 是三种独立类型,MSVC 拒绝 uint8_t*(=unsigned char*) → char* 的隐式转换。 // 此重载在 SDK 头文件之前定义,利用重载决议的精确匹配规则拦截调用并内部转型。 #ifdef _WIN32 #include #pragma comment(lib, "ws2_32.lib") inline int recvfrom(SOCKET s, unsigned char* buf, int len, int flags, sockaddr* from, int* fromlen) { return ::recvfrom(s, reinterpret_cast(buf), len, flags, from, fromlen); } #endif // SDK 依赖 std::array / int16_t / uint16_t,但其内部缺少对应的 #include, // 在 Linux 上通过 libstdc++ 间接包含可用,MSVC STL 不会间接拉入,需在此处显式引入。 #include #include // SDK 头文件全部放在私有层,不污染公共接口 #include #include #include #include #include #include #include #include #include #include #include using namespace robosense::lidar; class CRsLidarDevice : public IRsLidarDevice { public: CRsLidarDevice(); ~CRsLidarDevice(); int InitDevice() override; int OpenDevice(const RsLidarConfig& config) override; int CloseDevice() override; bool IsOpened() const override; int Start() override; int Stop() override; bool IsRunning() const override; int SetPointCloudCallback(PointCloudCallback callback) override; int SetRawCloudCallback(RawCloudCallback callback) override; int SetPacketCallback(PacketCallback callback) override; int SetExceptionCallback(ExceptionCallback callback) override; std::string GetVersion() override; uint64_t GetDroppedFrameCount() const override; size_t GetStuffedQueueDepth() const override; uint64_t GetExceptionCount(int errCode) const override; RsCallbackStats GetCallbackLatencyStats() const override; size_t GetStuffedQueuePeak() const override; private: // SDK 内部类型别名 using SdkCloudMsg = PointCloudT; using SdkCloudPtr = std::shared_ptr; void processCloudThread(); /// 输出 5s 窗口诊断日志并重置窗口字段;仅当距 lastLogTime ≥5s 时实际输出 void emitDiagnosticLogIfDue(std::chrono::steady_clock::time_point& lastLogTime); SdkCloudPtr getFreeCloud(); void putStuffedCloud(SdkCloudPtr msg); /// 将 SDK Packet 转为 RsPacketInfo 后转发给用户 void onPacket(const Packet& pkt); /// 将 SDK Error 转为 RsExceptionInfo 后转发给用户 void onException(const Error& code); /// RsLidarConfig → RSDriverParam static RSDriverParam toDriverParam(const RsLidarConfig& config); bool m_bOpened = false; std::atomic m_bRunning{false}; std::atomic m_bStopProcess{false}; PointCloudCallback m_pointCloudCallback; RawCloudCallback m_rawCloudCallback; PacketCallback m_packetCallback; ExceptionCallback m_exceptionCallback; std::mutex m_callbackMutex; std::unique_ptr> m_pDriver; std::thread m_processThread; SyncQueue m_freeQueue; SyncQueue m_stuffedQueue; RSDriverParam m_param; // 丢帧监控 std::atomic m_droppedFrameCount{0}; std::atomic m_totalPushCount{0}; // 诊断统计(每 5s 滚动窗口) // bucket: 0=MSOPTIMEOUT(0x40) 1=PKTBUFOVERFLOW(0x48) 2=CLOUDOVERFLOW(0x49) 3=WRONGMSOPLEN(0x42) 4=other static constexpr size_t EXC_BUCKET_COUNT = 5; std::array, EXC_BUCKET_COUNT> m_exceptionCounts{}; std::atomic m_cbMaxUs{0}; std::atomic m_cbAccumUs{0}; std::atomic m_cbCount{0}; std::atomic m_qPeak{0}; /// 将 SDK ErrCode 映射到统计桶下标,未知返回 4 (other) static size_t errCodeToBucket(int errCode); }; #endif // CRSLIDARDEVICE_H