Skip to content

Commit

Permalink
Add code for python rplidar
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Dec 9, 2023
1 parent 613973d commit fa4f7b3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 54 deletions.
112 changes: 67 additions & 45 deletions libraries/RPLidar/test/test.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,77 @@
#include <RPLidar.h>
//#include <RPLidar.h>
#include <memory>
#include <spdlog/spdlog.h>
#include <pybind11/embed.h>

using namespace rplidar;
int main()
{
spdlog::set_level(spdlog::level::debug);
//int run_native() {
// using namespace rplidar;
// spdlog::set_level(spdlog::level::debug);
//
// // auto lidar_result = RPLidar::create("/dev/ttyUSB0");
// auto lidar_result = RPLidar::create("COM3");
// if (!lidar_result.has_value())
// {
// return 0;
// }
// auto& lidar = std::move(lidar_result.value());
//
// auto info_result = lidar->get_info();
// if (!info_result.has_value())
// {
// std::cout << info_result.error();
// return 0;
// }
// auto& info = info_result.value();
// std::cout << fmt::format("model: {}, firmware: ({}, {}), hardware: {}, serialnumber: {}\n", info.model, info.firmware.first, info.firmware.second, info.hardware, info.serialNumber);
//
// auto health_result = lidar->get_health();
// if (!health_result.has_value())
// {
// std::cout << health_result.error();
// return 0;
// }
// auto& health = health_result.value();
// 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();
// return 0;
//}

void run_python() {
namespace py = pybind11;
py::scoped_interpreter guard{};

// auto lidar_result = RPLidar::create("/dev/ttyUSB0");
auto lidar_result = RPLidar::create("COM3");
if (!lidar_result.has_value())
{
return 0;
}
auto& lidar = std::move(lidar_result.value());
auto rplidar = py::module_::import("rplidar");
auto RPLidar = rplidar.attr("RPLidar");
auto lidar = RPLidar.call("COM3");

auto info_result = lidar->get_info();
if (!info_result.has_value())
{
std::cout << info_result.error();
return 0;
}
auto& info = info_result.value();
std::cout << fmt::format("model: {}, firmware: ({}, {}), hardware: {}, serialnumber: {}\n", info.model, info.firmware.first, info.firmware.second, info.hardware, info.serialNumber);
auto info = lidar.attr("get_info")();
py::print(info);

auto health_result = lidar->get_health();
if (!health_result.has_value())
{
std::cout << health_result.error();
return 0;
}
auto& health = health_result.value();
std::cout << fmt::format("({}, {})\n", health.status, health.errorCode);
auto health = lidar.attr("get_health")();
py::print(health);

// 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();
int main()
{
run_python();
return 1;
}
22 changes: 13 additions & 9 deletions libraries/RPLidar/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
add_requires("serial", "spdlog")
add_requires("tl_expected")

target("RPLidar")
set_kind("static")
add_files("src/*.cpp")
add_headerfiles("include/(**.h)")
add_headerfiles("include/(**.hpp)")
add_includedirs("include")
add_packages("serial", "spdlog")
add_packages("tl_expected")
add_requires("pybind11", "python", {configs = {headeronly = true}})

-- target("RPLidar")
-- set_kind("static")
-- add_files("src/*.cpp")
-- add_headerfiles("include/(**.h)")
-- add_headerfiles("include/(**.hpp)")
-- add_includedirs("include")
-- add_packages("serial", "spdlog")
-- add_packages("tl_expected")

target("test")
set_kind("binary")
add_files("test/*.cpp")
add_includedirs("include")
add_packages("serial", "spdlog")
add_packages("tl_expected")
add_deps("RPLidar")
-- add_deps("RPLidar")
add_packages("pybind11", "python")

0 comments on commit fa4f7b3

Please sign in to comment.