19#ifndef _USE_MATH_DEFINES
20#define _USE_MATH_DEFINES
25#define DEG2RAD M_PI/180.0
31using namespace rp::standalone::rplidar;
38 u_result result = m_drv->startMotor();
39 if (result != RESULT_OK)
47 if (m_pwm_val > 0 && m_pwm_val < 1023)
49 result = m_drv->setMotorPWM(m_pwm_val);
50 if (result != RESULT_OK)
60 yCError(
RP_LIDAR3) <<
"Invalid motor pwm request " << m_pwm_val <<
". It should be a value between 0 and 1023.";
67bool RpLidar3::startScan()
69 RplidarScanMode current_scan_mode;
70 std::vector<RplidarScanMode> scanModes;
71 u_result op_result =
m_drv->getAllSupportedScanModes(scanModes);
73 for (
size_t i = 0; i < scanModes.size(); i++)
76 <<
", mode:" << scanModes[i].scan_mode
77 <<
", max distance:" << scanModes[i].max_distance
78 <<
", us per sample:" << scanModes[i].us_per_sample
79 <<
", samples/s:" << 1.0 / scanModes[i].us_per_sample * 1000000;
86 _u16 selectedScanMode = _u16(-1);
87 for (std::vector<RplidarScanMode>::iterator iter = scanModes.begin(); iter != scanModes.end(); iter++)
91 selectedScanMode = iter->id;
96 if (selectedScanMode == _u16(-1))
99 for (std::vector<RplidarScanMode>::iterator iter = scanModes.begin(); iter != scanModes.end(); iter++)
101 yCInfo(
RP_LIDAR3,
"\t%s: max_distance: %.1f m, Point number: %.1fK", iter->scan_mode, iter->max_distance, (1000 / iter->us_per_sample));
103 op_result = RESULT_OPERATION_FAIL;
107 op_result =
m_drv->startScanExpress(
false , selectedScanMode, 0, ¤t_scan_mode);
111 if (op_result != RESULT_OK)
113 handleError(op_result);
135 bool br = config.
check(
"GENERAL");
141 if (general_config.
check(
"serial_port") ==
false) {
yCError(
RP_LIDAR3) <<
"Missing serial_port param in GENERAL group";
return false; }
142 if (general_config.
check(
"serial_baudrate") ==
false) {
yCError(
RP_LIDAR3) <<
"Missing serial_baudrate param in GENERAL group";
return false; }
143 if (general_config.
check(
"sample_buffer_life") ==
false) {
yCError(
RP_LIDAR3) <<
"Missing sample_buffer_life param in GENERAL group";
return false; }
144 if (general_config.
check(
"thread_period"))
146 double thread_period = general_config.
find(
"thread_period").
asInt32() / 1000.0;
150 baudrate = general_config.
find(
"serial_baudrate").
asInt32();
158 if (rplidar_config.
check(
"motor_pwm"))
162 if (rplidar_config.
check(
"express_mode"))
166 if (rplidar_config.
check(
"scan_mode"))
170 if (rplidar_config.
check(
"force_scan"))
182 m_drv = RPlidarDriver::CreateDriver(rp::standalone::rplidar::DRIVER_TYPE_SERIALPORT);
192 RPlidarDriver::DisposeDriver(
m_drv);
196 if (!deviceinfo())
return false;
197 if (!startMotor())
return false;
198 if (!startScan())
return false;
205 PeriodicThread::start();
212 PeriodicThread::stop();
216 RPlidarDriver::DisposeDriver(
m_drv);
223 std::lock_guard<std::mutex> guard(
m_mutex);
231 std::lock_guard<std::mutex> guard(
m_mutex);
239 std::lock_guard<std::mutex> guard(
m_mutex);
246 std::lock_guard<std::mutex> guard(
m_mutex);
258#define _countof(_Array) (int)(sizeof(_Array) / sizeof (_Array[0]))
281 if (op_result != RESULT_OK)
284 handleError(op_result);
290 if (op_result != RESULT_OK)
293 handleError(op_result);
302 m_laser_data[i] = std::numeric_limits<double>::infinity();
310 for (
size_t i = 0; i < count; ++i)
313 double distance = (
m_nodes[i].dist_mm_q2 / 1000.f / (1 << 2));
314 double angle = (
m_nodes[i].angle_z_q14 * 90.f / (1 << 14));
315 double quality = (
m_nodes[i].quality >> RPLIDAR_RESP_MEASUREMENT_QUALITY_SHIFT);
316 angle = (360 - angle);
329 if (quality == 0 || distance == 0)
331 distance = std::numeric_limits<double>::infinity();
359void RpLidar3::handleError(u_result error)
363 case RESULT_FAIL_BIT:
366 case RESULT_ALREADY_DONE:
369 case RESULT_INVALID_DATA:
372 case RESULT_OPERATION_FAIL:
375 case RESULT_OPERATION_TIMEOUT:
378 case RESULT_OPERATION_STOP:
381 case RESULT_OPERATION_NOT_SUPPORT:
384 case RESULT_FORMAT_NOT_SUPPORT:
387 case RESULT_INSUFFICIENT_MEMORY:
393bool RpLidar3::deviceinfo()
398 rplidar_response_device_info_t info;
399 std::string serialNumber;
401 result =
m_drv->getDeviceInfo(info);
402 if (result != RESULT_OK)
409 for (
unsigned char i : info.serialnum)
417 "\nSerial Number:" + serialNumber;
rpLidar2: The device driver for the RP2 lidar
rplidar_response_measurement_node_hq_t * m_nodes
bool acquireDataFromHW() override final
This method should be implemented by the user, and contain the logic to grab data from the hardware.
void run() override
Loop function.
void threadRelease() override
Release method.
bool setDistanceRange(double min, double max) override
set the device detection range.
bool threadInit() override
Initialization method.
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
bool close() override
Close the DeviceDriver.
bool setScanLimits(double min, double max) override
set the scan angular range.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool setHorizontalResolution(double step) override
get the angular step between two measurements (if available)
virtual bool updateLidarData()
This utility method calls in sequence: grabDataFromHW(), updateTimestamp and applyLimitsOnLaserData()...
yarp::dev::IRangefinder2D::Device_status m_device_status
bool parseConfiguration(yarp::os::Searchable &config)
yarp::sig::Vector m_laser_data
bool setPeriod(double period)
Set the (new) period of the thread.
void step()
Call this to "step" the thread rather than starting it.
A base class for nested structures that can be searched.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual std::string asString() const
Get string value.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
std::string to_string(IntegerType x)
const yarp::os::LogComponent & RP_LIDAR3()