Skip to content

Commit

Permalink
Use unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Oct 8, 2023
1 parent b9cd6ca commit f938d94
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
9 changes: 4 additions & 5 deletions backend/src/RPLidar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ Measure _process_scan(const std::vector<uint8_t> &raw)
return measurementData;
}

Measure _process_express_scan(std::shared_ptr<ExpressPacket> data, float newAngle, int trame)
Measure _process_express_scan(std::unique_ptr<ExpressPacket> &data, float newAngle, int trame)
{
Measure measurementData;

Expand Down Expand Up @@ -461,12 +461,12 @@ std::function<Measure()> 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>(ExpressPacket(this->_read_response(dsize)));
this->express_data = std::make_unique<ExpressPacket>(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>(ExpressPacket(this->_read_response(dsize)));
this->express_data = std::make_unique<ExpressPacket>(ExpressPacket(this->_read_response(dsize)));
spdlog::debug("set new_data with start_angle {}", this->express_data->start_angle);
}
this->express_trame++;
Expand All @@ -477,7 +477,6 @@ std::function<Measure()> 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";
}
};

Expand Down
4 changes: 2 additions & 2 deletions backend/src/RPLidar.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ class RPLidar
bool motor_running = false;
ScanInfo scanning = {false, 0, ScanType::NORMAL};
int express_trame = 32;
std::shared_ptr<ExpressPacket> express_data = nullptr;
std::shared_ptr<ExpressPacket> express_old_data = nullptr;
std::unique_ptr<ExpressPacket> express_data = nullptr;
std::unique_ptr<ExpressPacket> express_old_data = nullptr;
};

#endif
26 changes: 26 additions & 0 deletions backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<Measure>()> scanGenerator = lidar.iter_scans();
for (int i = 0; i < 10; i++)
{
std::vector<Measure> 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();
}

0 comments on commit f938d94

Please sign in to comment.