diff --git a/core/common/DriverInterface.h b/core/common/DriverInterface.h index 4aef988..a297efd 100644 --- a/core/common/DriverInterface.h +++ b/core/common/DriverInterface.h @@ -502,6 +502,12 @@ namespace ydlidar return RESULT_OK; } + //获取俯仰角值 + virtual bool getPitchAngle(float& pitch) { + UNUSED(pitch); + return false; + } + // 开始OTA升级 virtual bool ota() { return false; @@ -552,6 +558,7 @@ namespace ydlidar YDLIDAR_Tmini = 140, /**< Tmini LiDAR Model. */ YDLIDAR_TminiPro = 150, /**< Tmini Pro LiDAR Model. */ YDLIDAR_TminiPlus = 151, /**< Tmini Plus LiDAR Model. */ + YDLIDAR_TminiPlusSH = 152, //Tmini Plus 森合 YDLIDAR_SDM15 = 160, //SDM15单点雷达 YDLIDAR_SDM18, //DTS单点雷达 diff --git a/core/common/ydlidar_help.h b/core/common/ydlidar_help.h index c0f6c2d..5ac8719 100644 --- a/core/common/ydlidar_help.h +++ b/core/common/ydlidar_help.h @@ -80,6 +80,12 @@ namespace common { localTime->tm_hour, \ localTime->tm_min, \ localTime->tm_sec); +#define WIN_PRINT_TIME UNIX_PRINT_TIME +#ifdef _WIN32 + #define PRINT_TIME WIN_PRINT_TIME +#else + #define PRINT_TIME UNIX_PRINT_TIME +#endif //格式化字符串 #define FORMAT_STDOUT \ char buff[1024] = {0}; \ @@ -94,10 +100,7 @@ namespace common { inline void debug(char* fmt, ...) { printf(GREEN); //设置绿色 -#ifdef _WIN32 -#else - UNIX_PRINT_TIME -#endif + PRINT_TIME printf("[debug] "); FORMAT_STDOUT printf(COLOFF); //恢复默认颜色 @@ -107,10 +110,7 @@ inline void debug(char* fmt, ...) //常规 inline void info(char* fmt, ...) { -#ifdef _WIN32 -#else - UNIX_PRINT_TIME -#endif + PRINT_TIME printf("[info] "); FORMAT_STDOUT fflush(stdout); @@ -120,10 +120,7 @@ inline void info(char* fmt, ...) inline void warn(char* fmt, ...) { printf(YELLOW); //设置黄色 -#ifdef _WIN32 -#else - UNIX_PRINT_TIME -#endif + PRINT_TIME printf("[warn] "); FORMAT_STDOUT printf(COLOFF); //恢复默认颜色 @@ -134,10 +131,7 @@ inline void warn(char* fmt, ...) inline void error(char* fmt, ...) { printf(RED); //设置红色 -#ifdef _WIN32 -#else - UNIX_PRINT_TIME -#endif + PRINT_TIME printf("[error] "); FORMAT_STDOUT printf(COLOFF); //恢复默认颜色 @@ -150,10 +144,7 @@ inline void debugh(const uint8_t *data, int size) if (!data || !size) return; printf(GREEN); //设置绿色 -#ifdef _WIN32 -#else - UNIX_PRINT_TIME -#endif + PRINT_TIME printf("[debug] "); for (int i=0; i +#include #include #include #include +#include "CYdLidar.h" +#include "core/common/ydlidar_help.h" + using namespace std; using namespace ydlidar; +using namespace ydlidar::core::common; #if defined(_MSC_VER) #pragma comment(lib, "ydlidar_sdk.lib") #endif -/** - * @brief ydlidar test - * @param argc - * @param argv - * @return - * @par Flow chart - * Step1: instance CYdLidar.\n - * Step2: set paramters.\n - * Step3: initialize SDK and LiDAR.(::CYdLidar::initialize)\n - * Step4: Start the device scanning routine which runs on a separate thread and enable motor.(::CYdLidar::turnOn)\n - * Step5: Get the LiDAR Scan Data.(::CYdLidar::doProcessSimple)\n - * Step6: Stop the device scanning thread and disable motor.(::CYdLidar::turnOff)\n - * Step7: Uninitialize the SDK and Disconnect the LiDAR.(::CYdLidar::disconnecting)\n - */ -int main(int argc, char *argv[]) { - printf("__ ______ _ ___ ____ _ ____ \n"); - printf("\\ \\ / / _ \\| | |_ _| _ \\ / \\ | _ \\ \n"); - printf(" \\ V /| | | | | | || | | |/ _ \\ | |_) | \n"); - printf(" | | | |_| | |___ | || |_| / ___ \\| _ < \n"); - printf(" |_| |____/|_____|___|____/_/ \\_\\_| \\_\\ \n"); - printf("\n"); - fflush(stdout); - std::string port; - ydlidar::os_init(); +int main(int argc, char *argv[]) +{ + printLogo(); + os_init(); + std::string port; std::map ports = ydlidar::lidarPortList(); std::map::iterator it; @@ -111,75 +62,9 @@ int main(int argc, char *argv[]) { } int baudrate = 230400; - // std::map baudrateList; - // baudrateList[0] = 115200; - // baudrateList[1] = 128000; - // baudrateList[2] = 153600; - // baudrateList[3] = 230400; - // baudrateList[4] = 460800; - // baudrateList[5] = 512000; - - // printf("Baudrate:\n"); - - // for (std::map::iterator it = baudrateList.begin(); - // it != baudrateList.end(); it++) { - // printf("%d. %d\n", it->first, it->second); - // } - - // while (ydlidar::os_isOk()) { - // printf("Please select the lidar baudrate:"); - // std::string number; - // std::cin >> number; - - // if ((size_t)atoi(number.c_str()) > baudrateList.size()) { - // continue; - // } - - // baudrate = baudrateList[atoi(number.c_str())]; - // break; - // } - - if (!ydlidar::os_isOk()) { - return 0; - } - bool isSingleChannel = false; - // std::string input_channel; - // printf("Whether the Lidar is one-way communication[yes/no]:"); - // std::cin >> input_channel; - // std::transform(input_channel.begin(), input_channel.end(), - // input_channel.begin(), - // [](unsigned char c) { - // return std::tolower(c); // correct - // }); - - // if (input_channel.find("y") != std::string::npos) { - // isSingleChannel = true; - // } - - // if (!ydlidar::os_isOk()) { - // return 0; - // } - - std::string input_frequency; - float frequency = 10.0; - // while (ydlidar::os_isOk() && !isSingleChannel) { - // printf("Please enter the lidar scan frequency[5-12]:"); - // std::cin >> input_frequency; - // frequency = atof(input_frequency.c_str()); - // if (frequency <= 12 && frequency >= 5.0) { - // break; - // } - // fprintf(stderr, - // "Invalid scan frequency,The scanning frequency range is 5 to 12 HZ, Please re-enter.\n"); - // } - - if (!ydlidar::os_isOk()) { - return 0; - } - CYdLidar laser; //////////////////////string property///////////////// /// lidar port @@ -193,7 +78,7 @@ int main(int argc, char *argv[]) { //////////////////////int property///////////////// /// lidar baudrate laser.setlidaropt(LidarPropSerialBaudrate, &baudrate, sizeof(int)); - /// tof lidar + //TYPE_TRIANGLE(Tmini Pro\Tmini Plus),TYPE_TOF(Tmini Plus(森合)) int optval = TYPE_TRIANGLE; laser.setlidaropt(LidarPropLidarType, &optval, sizeof(int)); /// device type @@ -251,41 +136,50 @@ int main(int argc, char *argv[]) { laser.enableSunNoise(false); bool ret = laser.initialize(); - if (!ret) { - fprintf(stderr, "Fail to initialize %s\n", laser.DescribeError()); - fflush(stderr); + if (!ret) + { + error("Fail to initialize %s", laser.DescribeError()); return -1; } + //获取俯仰角值 + float pitch = .0f; + if (!laser.getPitchAngle(pitch)) + { + warn("Fail to get pitch angle"); + } + else + { + info("Pitch angle [%.02f]°", pitch); + } + ret = laser.turnOn(); - if (!ret) { - fprintf(stderr, "Fail to start %s\n", laser.DescribeError()); - fflush(stderr); + if (!ret) + { + error("Fail to start %s", laser.DescribeError()); return -1; } LaserScan scan; while (ydlidar::os_isOk()) { - if (laser.doProcessSimple(scan)) - { - printf("[%u] points [%.02f(%.02f)]Hz\n", - (unsigned int)scan.points.size(), - scan.scanFreq, - 1.0 / scan.config.scan_time); - for (size_t i = 0; i < scan.points.size(); ++i) - { - const LaserPoint &p = scan.points.at(i); - printf("%d a %.01f r %.04f i %.0f\n", - i, p.angle * 180.0 / M_PI, p.range, p.intensity); - } - fflush(stdout); - } - else - { - fprintf(stderr, "Failed to get Lidar Data\n"); - fflush(stderr); - } + if (laser.doProcessSimple(scan)) + { + info("[%u] points [%.02f(%.02f)]Hz", + (unsigned int)scan.points.size(), + scan.scanFreq, + 1.0 / scan.config.scan_time); + // for (size_t i = 0; i < scan.points.size(); ++i) + // { + // const LaserPoint &p = scan.points.at(i); + // info("%d a %.01f r %.04f i %.0f", + // i, p.angle * 180.0 / M_PI, p.range, p.intensity); + // } + } + else + { + error("Failed to get Lidar Data"); + } } laser.turnOff(); diff --git a/src/CYdLidar.cpp b/src/CYdLidar.cpp index 06522ab..6a3c4f8 100644 --- a/src/CYdLidar.cpp +++ b/src/CYdLidar.cpp @@ -945,6 +945,13 @@ void CYdLidar::setAutoIntensity(bool yes) m_AutoIntensity = yes; } +bool CYdLidar::getPitchAngle(float& pitch) +{ + if (lidarPtr) + return lidarPtr->getPitchAngle(pitch); + return false; +} + bool CYdLidar::ota() { if (lidarPtr) diff --git a/src/CYdLidar.h b/src/CYdLidar.h index d64025e..67a75cb 100644 --- a/src/CYdLidar.h +++ b/src/CYdLidar.h @@ -178,6 +178,8 @@ class YDLIDAR_API CYdLidar { bool getDeviceInfo(std::vector& dis); //设置是否自动识别强度(启用时会占用一定时间) void setAutoIntensity(bool yes=false); + //获取俯仰角值(目前仅针对Tmini Plus(森合)) + bool getPitchAngle(float& pitch); //启用调试 void setEnableDebug(bool yes) {m_Debug = yes;} diff --git a/src/YDlidarDriver.cpp b/src/YDlidarDriver.cpp index 23348fa..09fa8d5 100644 --- a/src/YDlidarDriver.cpp +++ b/src/YDlidarDriver.cpp @@ -2768,4 +2768,40 @@ namespace ydlidar return RESULT_OK; } +bool YDlidarDriver::getPitchAngle(float& pitch) +{ + if (!m_isConnected || m_SingleChannel) + return false; + + result_t ret = RESULT_OK; + ScopedLocker l(_cmd_lock); + ret = sendCommand(LIDAR_CMD_GETPITCH); + if (!IS_OK(ret)) + return false; + u_int32_t timeout = TIMEOUT_300; + uint32_t st = getms(); + uint32_t wt = 0; + while ((wt = getms() - st) <= timeout) + { + lidar_ans_header head = {0}; + ret = waitResponseHeader(&head, timeout); + if (!IS_OK(ret)) + return ret; + if (head.type != LIDAR_ANS_TYPE_PITCH) + continue; + if (head.size < 4) //数据大小为4字节 + return false; + ret = waitForData(head.size, timeout); + if (!IS_OK(ret)) + return ret; + + int32_t p = 0; + getData(reinterpret_cast(&p), 4); + pitch = p / 100.0f; //缩小100倍 + return true; + } + + return false; +} + } diff --git a/src/YDlidarDriver.h b/src/YDlidarDriver.h index 7d571bd..29f966d 100644 --- a/src/YDlidarDriver.h +++ b/src/YDlidarDriver.h @@ -584,6 +584,8 @@ class YDlidarDriver : public DriverInterface { uint32_t timeout = DEFAULT_TIMEOUT / 2); //解析点云数据并判断带不带强度信息 virtual result_t getIntensityFlag(); + //获取俯仰角值 + virtual bool getPitchAngle(float& pitch); private: /// package sample bytes