6 #define _USE_MATH_DEFINES
27 #define DEG2RAD M_PI/180.0
34 maxsize = bufferSize + 1;
37 elems = (
byte *)calloc(maxsize,
sizeof(
byte));
49 info =
"Fake Laser device for test/debugging";
59 bool br = config.
check(
"GENERAL");
67 if (general_config.
check(
"max_angle") ==
false) {
yCError(
RPLIDAR) <<
"Missing max_angle param";
return false; }
68 if (general_config.
check(
"min_angle") ==
false) {
yCError(
RPLIDAR) <<
"Missing min_angle param";
return false; }
69 if (general_config.
check(
"resolution") ==
false) {
yCError(
RPLIDAR) <<
"Missing resolution param";
return false; }
81 bool bs = config.
check(
"SKIP");
87 size_t s_mins = mins.
size();
88 size_t s_maxs = mins.
size();
89 if (s_mins == s_maxs && s_maxs > 1 )
91 for (
size_t s = 1; s < s_maxs; s++)
96 if (range.
max >= 0 && range.
max <= 360 &&
97 range.
min >= 0 && range.
min <= 360 &&
114 if (fov >360) {
yCError(
RPLIDAR) <<
"max_angle - min_angle <= 360";
return false; }
125 bool ok = general_config.
check(
"Serial_Configuration");
128 yCError(
RPLIDAR,
"Cannot find configuration file for serial port communication!");
131 std::string serial_filename = general_config.
find(
"Serial_Configuration").
asString();
135 std::string serial_completefilename= rf.
findFileByName(serial_filename);
137 prop.
put(
"device",
"serialport");
141 yCError(
RPLIDAR,
"Unable to read from serial port configuration file");
155 yCError(
RPLIDAR,
"Error opening serial driver. Device not available");
167 bool b_health = HW_getHealth();
168 if (b_health ==
false)
172 b_health = HW_getHealth();
173 if (b_health ==
false)
180 yCInfo(
RPLIDAR,
"Sensor recovered from a previous error status");
188 return PeriodicThread::start();
193 PeriodicThread::stop();
211 std::lock_guard<std::mutex> guard(
mutex);
219 std::lock_guard<std::mutex> guard(
mutex);
227 std::lock_guard<std::mutex> guard(
mutex);
235 std::lock_guard<std::mutex> guard(
mutex);
243 std::lock_guard<std::mutex> guard(
mutex);
250 std::lock_guard<std::mutex> guard(
mutex);
257 std::lock_guard<std::mutex> guard(
mutex);
264 std::lock_guard<std::mutex> guard(
mutex);
272 std::lock_guard<std::mutex> guard(
mutex);
280 std::lock_guard<std::mutex> guard(
mutex);
288 for (
size_t i = 0; i < size; i++)
298 std::lock_guard<std::mutex> guard(
mutex);
318 bool RpLidar::HW_getInfo(std::string& s_info)
321 unsigned char cmd_arr[2];
328 unsigned char s[255];
336 if ((
unsigned char)s[0] != 0xA5 || (
unsigned char)s[1] != 0x5A || (
unsigned char)s[2] != 0x14 || (
unsigned char)s[6] != 0x04)
349 sprintf(
info,
"model %d fw_major %d fw_minor %d hardware %d serial number %c%c%c%c%c %c%c%c%c%c %c%c%c%c%c%c",
350 s[0], s[1], s[2], s[3],
351 s[4], s[5], s[6], s[7], s[8],
352 s[9], s[10], s[11], s[12], s[13],
353 s[14], s[15], s[16], s[17], s[18], s[19]);
358 bool RpLidar::HW_getHealth()
363 unsigned char cmd_arr[2];
370 unsigned char s[255];
379 if ((
unsigned char)s[0] != 0xA5 || (
unsigned char)s[1] != 0x5A || (
unsigned char)s[2] != 3 || (
unsigned char)s[6] != 0x06)
394 int code = s[1] << 8 | s[2];
399 else if (status == 1)
404 else if (status == 2)
413 bool RpLidar::HW_reset()
417 unsigned char cmd_arr[2];
426 bool RpLidar::HW_start()
432 unsigned char cmd_arr[2];
443 unsigned char s[255];
452 if ((
unsigned char)s[0] != 0xA5 || (
unsigned char)s[1] != 0x5A || (
unsigned char)s[2] != 0x05 ||
453 (
unsigned char)s[5] != 0x40 || (
unsigned char)s[6] != 0x81)
462 bool RpLidar::HW_stop()
466 unsigned char cmd_arr[2];
475 #define DEBUG_LOCKING 1
482 const int packet = 100;
483 std::lock_guard<std::mutex> guard(
mutex);
485 unsigned char buff[packet*3];
486 memset(buff, 0, packet*3);
489 static unsigned int total_r = 0;
490 unsigned int count = 0;
494 #ifdef DEBUG_BYTES_READ_1
497 buffer->write_elems(buff, r);
500 #ifdef DEBUG_BYTES_READ_2
507 while (
buffer->size() < (packet * 2) || r < packet);
509 unsigned char minibuff[15];
510 unsigned int ok_count = 0;
511 bool new_scan =
false;
514 buffer->select_elems(minibuff,15);
516 int start = (minibuff[0]) & 0x01;
517 int lock = (minibuff[0] >> 1) & 0x01;
518 int check = (minibuff[1] & 0x01);
520 int start_n1 = (minibuff[5]) & 0x01;
521 int lock_n1 = (minibuff[5] >> 1) & 0x01;
522 int start_n2 = (minibuff[10]) & 0x01;
523 int lock_n2 = (minibuff[10] >> 1) & 0x01;
524 int check_n1 = (minibuff[6] & 0x01);
525 int check_n2 = (minibuff[11] & 0x01);
527 int quality = (minibuff[0] >> 2);
528 int i_angle = ((minibuff[2] >> 1) << 8) | (minibuff[1]);
529 int i_distance = (minibuff[4] << 8) | (minibuff[3]);
534 yCError(
RPLIDAR) <<
"lock error 1 " <<
"previous ok" << ok_count <<
"total r" << total_r;
536 buffer->throw_away_elem();
542 if (start_n1 == lock_n1)
545 yCError(
RPLIDAR) <<
"lock error 2 " <<
"previous ok" << ok_count <<
"total r" << total_r;
547 buffer->throw_away_elem();
553 if (start_n2 == lock_n2)
556 yCError(
RPLIDAR) <<
"lock error 3 " <<
"previous ok" << ok_count <<
"total r" << total_r;
558 buffer->throw_away_elem();
564 if (
start == 1 && start_n1 == 1)
567 yCError(
RPLIDAR) <<
"lock error 4 " <<
"previous ok" << ok_count <<
"total r" << total_r;
569 buffer->throw_away_elem();
578 yCError(
RPLIDAR) <<
"checksum error 1" <<
"previous ok" << ok_count <<
"total r" << total_r;
580 buffer->throw_away_elem();
589 yCError(
RPLIDAR) <<
"checksum error 2" <<
"previous ok" << ok_count <<
"total r" << total_r;
591 buffer->throw_away_elem();
600 yCError(
RPLIDAR) <<
"checksum error 3" <<
"previous ok" << ok_count <<
"total r" << total_r;
602 buffer->throw_away_elem();
613 if (
start == 1 && new_scan ==
false)
618 else if (
start == 1 && new_scan ==
true)
624 double distance = i_distance / 4.0 / 1000;
625 double angle = i_angle / 64.0;
626 angle = (360 - angle) + 90;
638 distance = std::numeric_limits<double>::infinity();
664 if (angle > i.min && angle < i.max)
666 distance = std::numeric_limits<double>::infinity();
693 buffer->throw_away_elems(5);
726 std::lock_guard<std::mutex> guard(
mutex);
void run() override
Loop function.
yarp::sig::Vector laser_data
bool getDistanceRange(double &min, double &max) override
get the device detection range
bool close() override
Close the DeviceDriver.
bool do_not_clip_infinity_enable
std::vector< Range_t > range_skip_vector
bool threadInit() override
Initialization method.
bool getRawData(yarp::sig::Vector &data) override
Get the device measurements.
bool getHorizontalResolution(double &step) override
get the angular step between two measurments.
Device_status device_status
bool getScanRate(double &rate) override
get the scan rate (scans per seconds)
void threadRelease() override
Release method.
bool getDeviceInfo(std::string &device_info) override
get the device hardware charactestics
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
bool setScanLimits(double min, double max) override
set the scan angular range.
bool setDistanceRange(double min, double max) override
set the device detection range.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool getScanLimits(double &min, double &max) override
get the scan angular range.
bool getLaserMeasurement(std::vector< LaserMeasurementData > &data) override
Get the device measurements.
bool getDeviceStatus(Device_status &status) override
get the device status
bool setHorizontalResolution(double step) override
get the angular step between two measurments (if available)
bool view(T *&x)
Get an interface to the device driver.
virtual int flush()=0
Flushes the internal buffer.
virtual bool send(const yarp::os::Bottle &msg)=0
Sends a string of chars to the serial communications channel.
virtual int receiveBytes(unsigned char *bytes, const int size)=0
Gets an array of bytes (unsigned char) with size <= 'size' parameter.
bool close() override
Close the DeviceDriver.
bool isValid() const
Check if device is valid.
bool open(const std::string &txt)
Construct and configure a device by its common name.
A simple collection of objects that can be described and transmitted in a portable way.
size_type size() const
Gets the number of elements in the bottle.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
bool isRunning() const
Returns true when the thread is started, false otherwise.
bool start()
Call this to start the thread.
void step()
Call this to "step" the thread rather than starting it.
A class for storing options and configuration information.
bool fromConfigFile(const std::string &fname, bool wipe=true)
Interprets a file as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Helper class for finding config files and other external resources.
std::string findFileByName(const std::string &name)
Find the full path to a file.
A base class for nested structures that can be searched.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
static void delaySystem(double seconds)
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual std::string asString() const
Get string value.
void resize(size_t size) override
Resize the vector.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
double now()
Return the current time in seconds, relative to an arbitrary starting point.
const yarp::os::LogComponent & RPLIDAR()