From f938d94bf2a2e8900ef11f7fa92674273f6efbec Mon Sep 17 00:00:00 2001 From: Chi Huu Huynh <73843190+Chi-EEE@users.noreply.github.com> Date: Sun, 8 Oct 2023 10:36:27 +0100 Subject: [PATCH] Use unique_ptr --- backend/src/RPLidar.cpp | 9 ++++----- backend/src/RPLidar.h | 4 ++-- backend/src/main.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/backend/src/RPLidar.cpp b/backend/src/RPLidar.cpp index a9fefe27..6ea96d90 100644 --- a/backend/src/RPLidar.cpp +++ b/backend/src/RPLidar.cpp @@ -399,7 +399,7 @@ Measure _process_scan(const std::vector &raw) return measurementData; } -Measure _process_express_scan(std::shared_ptr data, float newAngle, int trame) +Measure _process_express_scan(std::unique_ptr &data, float newAngle, int trame) { Measure measurementData; @@ -461,12 +461,12 @@ std::function RPLidar::iter_measures(ScanType scanType, int maxBufMea if (this->express_data == nullptr) { spdlog::debug("reading first time bytes"); - this->express_data = std::make_shared(ExpressPacket(this->_read_response(dsize))); + this->express_data = std::make_unique(ExpressPacket(this->_read_response(dsize))); } - this->express_old_data = this->express_data; + this->express_old_data = std::move(this->express_data); spdlog::debug("set old_data with start_angle {}", this->express_old_data->start_angle); - this->express_data = std::make_shared(ExpressPacket(this->_read_response(dsize))); + this->express_data = std::make_unique(ExpressPacket(this->_read_response(dsize))); spdlog::debug("set new_data with start_angle {}", this->express_data->start_angle); } this->express_trame++; @@ -477,7 +477,6 @@ std::function RPLidar::iter_measures(ScanType scanType, int maxBufMea Measure measure = _process_express_scan(this->express_old_data, this->express_data->start_angle, this->express_trame); return measure; } - std::cout << "return null"; } }; diff --git a/backend/src/RPLidar.h b/backend/src/RPLidar.h index e92ec238..01c0089a 100644 --- a/backend/src/RPLidar.h +++ b/backend/src/RPLidar.h @@ -143,8 +143,8 @@ class RPLidar bool motor_running = false; ScanInfo scanning = {false, 0, ScanType::NORMAL}; int express_trame = 32; - std::shared_ptr express_data = nullptr; - std::shared_ptr express_old_data = nullptr; + std::unique_ptr express_data = nullptr; + std::unique_ptr express_old_data = nullptr; }; #endif \ No newline at end of file diff --git a/backend/src/main.cpp b/backend/src/main.cpp index 1f457ad2..2ecf7872 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -7,4 +7,30 @@ int main() { spdlog::set_level(spdlog::level::info); + auto lidar = RPLidar("COM3"); + + auto info = lidar.get_info(); + std::cout << fmt::format("model: {}, firmware: ({}, {}), hardware: {}, serialnumber: {}\n", info.model, info.firmware.first, info.firmware.second, info.hardware, info.serialNumber); + + auto health = lidar.get_health(); + std::cout << fmt::format("({}, {})\n", health.status, health.errorCode); + + std::function()> scanGenerator = lidar.iter_scans(); + for (int i = 0; i < 10; i++) + { + std::vector scan = scanGenerator(); + std::cout << "Got " << scan.size() << " Measures!\n"; + for (const Measure &measure : scan) + { + // Access individual measurements in the scan + bool newScan = measure.newScan; + int quality = measure.quality; + float angle = measure.angle; + float distance = measure.distance; + } + } + + lidar.stop(); + lidar.stop_motor(); + lidar.disconnect(); } \ No newline at end of file