Skip to content

Commit

Permalink
修改Python调用SDK库崩溃的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanyiaini committed Apr 13, 2023
1 parent 7482f5d commit 05b39d4
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 127 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(ydlidar_sdk C CXX)
# version
set(YDLIDAR_SDK_VERSION_MAJOR 1)
set(YDLIDAR_SDK_VERSION_MINOR 1)
set(YDLIDAR_SDK_VERSION_PATCH 6)
set(YDLIDAR_SDK_VERSION_PATCH 7)
set(YDLIDAR_SDK_VERSION ${YDLIDAR_SDK_VERSION_MAJOR}.${YDLIDAR_SDK_VERSION_MINOR}.${YDLIDAR_SDK_VERSION_PATCH})

##########################################################
Expand Down
26 changes: 13 additions & 13 deletions core/common/ydlidar_datatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@
#include <vector>
#include "ydlidar_def.h"

#define MAX_DEBUG_INDEX 14

/**
* @brief The Laser Debug struct
*/
typedef struct {
uint8_t W3F4CusMajor_W4F0CusMinor;
uint8_t W4F3Model_W3F0DebugInfTranVer;
uint8_t W3F4HardwareVer_W4F0FirewareMajor;
uint8_t W7F0FirewareMinor;
uint8_t W3F4BoradHardVer_W4F0Moth;
uint8_t W2F5Output2K4K5K_W5F0Date;
uint8_t W1F6GNoise_W1F5SNoise_W1F4MotorCtl_W4F0SnYear;
uint8_t W7F0SnNumH;
uint8_t W7F0SnNumL;
uint8_t W7F0Health;
uint8_t W3F4CusHardVer_W4F0CusSoftVer;
uint8_t W7F0LaserCurrent;
uint8_t MaxDebugIndex;
uint8_t cVer;
uint8_t debug2;
uint8_t hfVer;
uint8_t fVer;
uint8_t month;
uint8_t day;
uint8_t year;
uint8_t numH;
uint8_t numL;
uint8_t health;
uint8_t maxIndex;
} LaserDebug;


Expand Down
157 changes: 75 additions & 82 deletions core/common/ydlidar_help.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#pragma once
#include "DriverInterface.h"
#include <sstream>
#include <iomanip>

/**
* @brief ydlidar
Expand Down Expand Up @@ -778,10 +779,10 @@ inline bool isValidValue(uint8_t value) {
inline bool isVersionValid(const LaserDebug &info) {
bool ret = false;

if (isValidValue(info.W3F4CusMajor_W4F0CusMinor) &&
isValidValue(info.W4F3Model_W3F0DebugInfTranVer) &&
isValidValue(info.W3F4HardwareVer_W4F0FirewareMajor) &&
isValidValue(info.W3F4BoradHardVer_W4F0Moth)) {
if (isValidValue(info.cVer) &&
isValidValue(info.debug2) &&
isValidValue(info.hfVer) &&
isValidValue(info.month)) {
ret = true;
}

Expand All @@ -796,10 +797,10 @@ inline bool isVersionValid(const LaserDebug &info) {
inline bool isSerialNumbValid(const LaserDebug &info) {
bool ret = false;

if (isValidValue(info.W2F5Output2K4K5K_W5F0Date) &&
isValidValue(info.W1F6GNoise_W1F5SNoise_W1F4MotorCtl_W4F0SnYear) &&
isValidValue(info.W7F0SnNumH) &&
isValidValue(info.W7F0SnNumH)) {
if (isValidValue(info.day) &&
isValidValue(info.year) &&
isValidValue(info.numH) &&
isValidValue(info.numH)) {
ret = true;
}

Expand All @@ -811,114 +812,106 @@ inline bool isSerialNumbValid(const LaserDebug &info) {
* @param node LiDAR node_info information
* @param info LiDAR LaserDebug information
*/
inline void parsePackageNode(const node_info &node, LaserDebug &info) {
inline void parsePackageNode(const node_info &node, LaserDebug &info)
{
switch (node.index) {
case 0://scan frequency
case 0:
break;

case 1://W3F3CusHardVer_W4F0CusSoftVer;
info.W3F4CusMajor_W4F0CusMinor = node.debugInfo;
case 1:
info.cVer = node.debugInfo;
break;

case 2://W4F3Model_W3F0DebugInfTranVer
info.W4F3Model_W3F0DebugInfTranVer = node.debugInfo;
case 2:
info.debug2 = node.debugInfo;
break;

case 3://W3F4HardwareVer_W4F0FirewareMajor
case 3:
//健康信息
info.W7F0Health = node.debugInfo;
info.health = node.debugInfo;
break;

case 4://W7F0FirewareMinor
info.W3F4HardwareVer_W4F0FirewareMajor = node.debugInfo;
case 4:
info.hfVer = node.debugInfo;
break;
case 5://W3F4BoradHardVer_W4F0Moth
info.W7F0FirewareMinor = node.debugInfo;
case 5:
info.fVer = node.debugInfo;
break;

case 6://W2F5Output2K4K5K_W5F0Date
case 6:
break;
case 7://W1F6GNoise_W1F5SNoise_W1F4MotorCtl_W4F0SnYear
case 7:
break;
case 8://W7F0SnNumH
case 8:
break;

case 9://W7F0SnNumL
info.W1F6GNoise_W1F5SNoise_W1F4MotorCtl_W4F0SnYear =
node.debugInfo;
case 9:
info.year = node.debugInfo;
break;
case 10://W7F0Health
info.W3F4BoradHardVer_W4F0Moth = node.debugInfo;
case 10:
info.month = node.debugInfo;
break;
case 11://W3F4CusHardVer_W4F0CusSoftVer
info.W2F5Output2K4K5K_W5F0Date = node.debugInfo;
// info.W3F4CusHardVer_W4F0CusSoftVer = node.debugInfo;
case 11:
info.day = node.debugInfo;
break;

case 12://W7F0LaserCurrent
// info.W7F0LaserCurrent = node.debugInfo;
info.W7F0SnNumH = node.debugInfo;
case 12:
info.numH = node.debugInfo;
break;
case 13:
info.W7F0SnNumL = node.debugInfo;
info.numL = node.debugInfo;
break;

default:
break;
}

if (info.MaxDebugIndex > node.index) {
info.W3F4CusMajor_W4F0CusMinor = 0xff;
}
// if (info.MaxDebugIndex > node.index) {
// info.W3F4CusMajor_W4F0CusMinor = 0xff;
// }

if (static_cast<int>(node.index) > info.MaxDebugIndex && node.index < 100) {
info.MaxDebugIndex = static_cast<int>(node.index);
if (node.index > info.maxIndex) {
info.maxIndex = node.index;
}
}

// #include <iostream>
// #include <iomanip>
/**
* @brief convert LaserDebug information to device_info
* @param info LiDAR LaserDebug information
* @param value LiDAR Device information
* @return true if converted successfully, otherwise false.
*/
inline bool ParseLaserDebugInfo(const LaserDebug &info, device_info &value) {
inline bool ParseLaserDebugInfo(const LaserDebug &info, device_info &di)
{
bool ret = false;
uint8_t CustomVerMajor = (static_cast<uint8_t>
(info.W3F4HardwareVer_W4F0FirewareMajor & 0x0F));
uint8_t CustomVerMinor = static_cast<uint8_t>
(info.W7F0FirewareMinor);
uint8_t lidarmodel = (static_cast<uint8_t>(info.W4F3Model_W3F0DebugInfTranVer)
>> 3);
uint8_t hardwareVer = static_cast<uint8_t>
(info.W3F4HardwareVer_W4F0FirewareMajor) >> 4;

uint8_t Year = static_cast<uint8_t>
(info.W1F6GNoise_W1F5SNoise_W1F4MotorCtl_W4F0SnYear >> 2);
uint8_t Moth = static_cast<uint8_t>(info.W3F4BoradHardVer_W4F0Moth >> 3);
uint8_t Date = static_cast<uint8_t>(info.W2F5Output2K4K5K_W5F0Date >> 2);

uint16_t Number = ((static_cast<uint8_t>(info.W7F0SnNumH) << 7) |
static_cast<uint8_t>(info.W7F0SnNumL));

if (isVersionValid(info) && info.MaxDebugIndex > 0 && Year) {

if (isSerialNumbValid(info) && info.MaxDebugIndex > 8) {
value.firmware_version = (CustomVerMajor << 8 | CustomVerMinor);
value.hardware_version = hardwareVer;
value.model = lidarmodel;
uint32_t year = Year + 2020;
sprintf(reinterpret_cast<char *>(value.serialnum), "%04d", year);
sprintf(reinterpret_cast<char *>(value.serialnum + 4), "%02d", Moth);
sprintf(reinterpret_cast<char *>(value.serialnum + 6), "%02d", Date);
sprintf(reinterpret_cast<char *>(value.serialnum + 8), "%08d", Number);

for (int i = 0; i < 16; i++) {
value.serialnum[i] -= 48;
}

ret = true;
uint8_t CustomVerMajor = uint8_t(info.hfVer & 0x0F);
uint8_t CustomVerMinor = info.fVer;
uint8_t lidarmodel = uint8_t(info.debug2) >> 3;
uint8_t hardwareVer = uint8_t(info.hfVer) >> 4;

uint8_t Year = uint8_t(info.year >> 2);
uint8_t Moth = uint8_t(info.month >> 3);
uint8_t Date = uint8_t(info.day >> 2);
uint16_t Number = uint16_t(info.numH << 7) |
uint16_t(info.numL);

if (Moth && Date && Number)
{
di.firmware_version = uint16_t(CustomVerMajor << 8) |
uint16_t(CustomVerMinor);
di.hardware_version = hardwareVer;
di.model = lidarmodel;
std::stringstream ss;
ss << std::setw(4) << std::setfill('0') << int(Year + 2020);
ss << std::setw(2) << std::setfill('0') << int(Moth);
ss << std::setw(2) << std::setfill('0') << int(Date);
ss << std::setw(8) << std::setfill('0') << int(Number);
std::string sn(ss.str());
// printf("SN: %s %04u%02u%02u%08u\n", sn.c_str(), Year + 2020, Moth, Date, Number);
// 此处在Python调用中会导致缓存溢出
// sprintf(reinterpret_cast<char*>(di.serialnum),
// "%04u%02u%02u%08u", Year + 2020, Moth, Date, Number);
for (int i = 0; i < SDK_SNLEN && i < sn.size(); i++)
{
di.serialnum[i] = std::stoi(std::string(1, sn.at(i)));
}

ret = true;
}

return ret;
Expand Down
4 changes: 2 additions & 2 deletions core/common/ydlidar_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ struct device_info {
uint8_t model; ///< LiDAR model
uint16_t firmware_version; ///< firmware version
uint8_t hardware_version; ///< hardare version
uint8_t serialnum[16]; ///< serial number
} __attribute__((packed)) ;
uint8_t serialnum[SDK_SNLEN]; ///< serial number
} __attribute__((packed));
#define DEVICEINFOSIZE sizeof(device_info)

/// LiDAR Health Information
Expand Down
5 changes: 3 additions & 2 deletions python/examples/ydlidar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
port = "/dev/ydlidar";
for key, value in ports.items():
port = value;
print(port);
laser = ydlidar.CYdLidar();
laser.setlidaropt(ydlidar.LidarPropSerialPort, port);
laser.setlidaropt(ydlidar.LidarPropSerialBaudrate, 230400);
laser.setlidaropt(ydlidar.LidarPropSerialBaudrate, 128000);
laser.setlidaropt(ydlidar.LidarPropLidarType, ydlidar.TYPE_TRIANGLE);
laser.setlidaropt(ydlidar.LidarPropDeviceType, ydlidar.YDLIDAR_TYPE_SERIAL);
laser.setlidaropt(ydlidar.LidarPropScanFrequency, 10.0);
laser.setlidaropt(ydlidar.LidarPropSampleRate, 5);
laser.setlidaropt(ydlidar.LidarPropSingleChannel, False);
laser.setlidaropt(ydlidar.LidarPropSingleChannel, True);
laser.setlidaropt(ydlidar.LidarPropMaxAngle, 180.0);
laser.setlidaropt(ydlidar.LidarPropMinAngle, -180.0);
laser.setlidaropt(ydlidar.LidarPropMaxRange, 16.0);
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def build_extension(self, ext):

setup(
name='ydlidar',
version='1.0.2',
author='Tony',
version='1.1.7',
author='EAI',
author_email='[email protected]',
url='https://github.com/YDLIDAR/YDLidar-SDK',
description='YDLIDAR python SDK',
Expand Down
14 changes: 7 additions & 7 deletions src/CYdLidar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,6 @@ bool CYdLidar::turnOn()
printf("[YDLIDAR] Successed to start scan mode, Elapsed time %u ms\n", getms() - t);
fflush(stdout);

m_PointTime = lidarPtr->getPointTime();

t = getms();
//计算采样率
if (checkLidarAbnormal())
Expand Down Expand Up @@ -524,10 +522,11 @@ bool CYdLidar::turnOn()
m_FristNodeTime = getTime();
m_AllNode = 0;
m_PointTime = lidarPtr->getPointTime();
isScanning = true;
lidarPtr->setAutoReconnect(m_AutoReconnect);
printf("[YDLIDAR] Now lidar is scanning...\n");
fflush(stdout);

isScanning = true;
return true;
}

Expand All @@ -536,7 +535,7 @@ bool CYdLidar::turnOn()
-------------------------------------------------------------*/
bool CYdLidar::doProcessSimple(LaserScan &outscan)
{
// Boud?
//判断是否已启动扫描
if (!checkHardware())
{
delay(200 / m_ScanFrequency);
Expand Down Expand Up @@ -657,7 +656,7 @@ bool CYdLidar::doProcessSimple(LaserScan &outscan)
float range = 0.0;
float intensity = 0.0;
float angle = 0.0;
debug.MaxDebugIndex = 0;
debug.maxIndex = 0;

// printf("AngleOffset %f\n", m_AngleOffset);

Expand Down Expand Up @@ -764,7 +763,7 @@ bool CYdLidar::doProcessSimple(LaserScan &outscan)
parsePackageNode(global_nodes[i], debug);
if (global_nodes[i].error_package)
{
debug.MaxDebugIndex = 255;
debug.maxIndex = 255;
}
} //end for (int i = 0; i < count; i++)

Expand All @@ -777,6 +776,7 @@ bool CYdLidar::doProcessSimple(LaserScan &outscan)
handleVersionInfoByPackage(debug);
// resample sample rate
resample(scanfrequency, count, tim_scan_end, tim_scan_start);

return true;
}
else
Expand Down Expand Up @@ -963,7 +963,7 @@ void CYdLidar::handleVersionInfoByPackage(const LaserDebug &debug)
}

device_info info;
memset(&info, 0, sizeof(device_info));
memset(&info, 0, DEVICEINFOSIZE);

if (ParseLaserDebugInfo(debug, info))
{
Expand Down
Loading

0 comments on commit 05b39d4

Please sign in to comment.