|
| 1 | +#include "CYdLidar.h" |
| 2 | +#include <iostream> |
| 3 | +#include <string> |
| 4 | +#include <algorithm> |
| 5 | +#include <cctype> |
| 6 | +#include "core/base/timer.h" |
| 7 | + |
| 8 | +using namespace std; |
| 9 | +using namespace ydlidar; |
| 10 | + |
| 11 | +#if defined(_MSC_VER) |
| 12 | +#pragma comment(lib, "ydlidar_sdk.lib") |
| 13 | +#endif |
| 14 | + |
| 15 | +int main(int argc, char *argv[]) |
| 16 | +{ |
| 17 | + printf("__ ______ _ ___ ____ _ ____ \n"); |
| 18 | + printf("\\ \\ / / _ \\| | |_ _| _ \\ / \\ | _ \\ \n"); |
| 19 | + printf(" \\ V /| | | | | | || | | |/ _ \\ | |_) | \n"); |
| 20 | + printf(" | | | |_| | |___ | || |_| / ___ \\| _ < \n"); |
| 21 | + printf(" |_| |____/|_____|___|____/_/ \\_\\_| \\_\\ \n"); |
| 22 | + printf("\n"); |
| 23 | + fflush(stdout); |
| 24 | + |
| 25 | + ydlidar::os_init(); |
| 26 | + |
| 27 | + bool ret = false; |
| 28 | + CYdLidar lidarGs; //GS2雷达 |
| 29 | + { |
| 30 | + bool isSingleChannel = false; |
| 31 | + float frequency = 8.0; |
| 32 | + std::string port = "/dev/ttyUSB0"; |
| 33 | + int baudrate = 921600; |
| 34 | + //////////////////////string property///////////////// |
| 35 | + /// lidar port |
| 36 | + lidarGs.setlidaropt(LidarPropSerialPort, port.c_str(), port.size()); |
| 37 | + /// lidar baudrate |
| 38 | + lidarGs.setlidaropt(LidarPropSerialBaudrate, &baudrate, sizeof(int)); |
| 39 | + /// gs lidar |
| 40 | + int optval = TYPE_GS; |
| 41 | + lidarGs.setlidaropt(LidarPropLidarType, &optval, sizeof(int)); |
| 42 | + /// device type |
| 43 | + optval = YDLIDAR_TYPE_SERIAL; |
| 44 | + lidarGs.setlidaropt(LidarPropDeviceType, &optval, sizeof(int)); |
| 45 | + /// sample rate |
| 46 | + optval = 4; |
| 47 | + lidarGs.setlidaropt(LidarPropSampleRate, &optval, sizeof(int)); |
| 48 | + /// abnormal count |
| 49 | + optval = 4; |
| 50 | + lidarGs.setlidaropt(LidarPropAbnormalCheckCount, &optval, sizeof(int)); |
| 51 | + /// Intenstiy bit count |
| 52 | + optval = 8; |
| 53 | + lidarGs.setlidaropt(LidarPropIntenstiyBit, &optval, sizeof(int)); |
| 54 | + //////////////////////bool property///////////////// |
| 55 | + /// fixed angle resolution |
| 56 | + bool b_optvalue = false; |
| 57 | + lidarGs.setlidaropt(LidarPropFixedResolution, &b_optvalue, sizeof(bool)); |
| 58 | + /// rotate 180 |
| 59 | + lidarGs.setlidaropt(LidarPropReversion, &b_optvalue, sizeof(bool)); |
| 60 | + /// Counterclockwise |
| 61 | + lidarGs.setlidaropt(LidarPropInverted, &b_optvalue, sizeof(bool)); |
| 62 | + b_optvalue = true; |
| 63 | + lidarGs.setlidaropt(LidarPropAutoReconnect, &b_optvalue, sizeof(bool)); |
| 64 | + /// one-way communication |
| 65 | + lidarGs.setlidaropt(LidarPropSingleChannel, &isSingleChannel, sizeof(bool)); |
| 66 | + /// intensity |
| 67 | + b_optvalue = true; |
| 68 | + lidarGs.setlidaropt(LidarPropIntenstiy, &b_optvalue, sizeof(bool)); |
| 69 | + /// Motor DTR |
| 70 | + b_optvalue = true; |
| 71 | + lidarGs.setlidaropt(LidarPropSupportMotorDtrCtrl, &b_optvalue, sizeof(bool)); |
| 72 | + /// HeartBeat |
| 73 | + b_optvalue = false; |
| 74 | + lidarGs.setlidaropt(LidarPropSupportHeartBeat, &b_optvalue, sizeof(bool)); |
| 75 | + //////////////////////float property///////////////// |
| 76 | + /// unit: ° |
| 77 | + float f_optvalue = 180.0f; |
| 78 | + lidarGs.setlidaropt(LidarPropMaxAngle, &f_optvalue, sizeof(float)); |
| 79 | + f_optvalue = -180.0f; |
| 80 | + lidarGs.setlidaropt(LidarPropMinAngle, &f_optvalue, sizeof(float)); |
| 81 | + /// unit: m |
| 82 | + f_optvalue = 1.f; |
| 83 | + lidarGs.setlidaropt(LidarPropMaxRange, &f_optvalue, sizeof(float)); |
| 84 | + f_optvalue = 0.025f; |
| 85 | + lidarGs.setlidaropt(LidarPropMinRange, &f_optvalue, sizeof(float)); |
| 86 | + /// unit: Hz |
| 87 | + lidarGs.setlidaropt(LidarPropScanFrequency, &frequency, sizeof(float)); |
| 88 | + |
| 89 | + //雷达初始化 |
| 90 | + ret = lidarGs.initialize(); |
| 91 | + if (!ret) |
| 92 | + { |
| 93 | + fprintf(stderr, "Fail to initialize %s\n", lidarGs.DescribeError()); |
| 94 | + fflush(stderr); |
| 95 | + return -1; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + CYdLidar lidarS2; //S2雷达 |
| 100 | + { |
| 101 | + bool isSingleChannel = false; |
| 102 | + float frequency = 8.0; |
| 103 | + std::string port = "/dev/ttyUSB1"; |
| 104 | + int baudrate = 115200; |
| 105 | + //////////////////////string property///////////////// |
| 106 | + /// lidar port |
| 107 | + lidarS2.setlidaropt(LidarPropSerialPort, port.c_str(), port.size()); |
| 108 | + //////////////////////int property///////////////// |
| 109 | + /// lidar baudrate |
| 110 | + lidarS2.setlidaropt(LidarPropSerialBaudrate, &baudrate, sizeof(int)); |
| 111 | + /// tof lidar |
| 112 | + int optval = TYPE_TRIANGLE; |
| 113 | + lidarS2.setlidaropt(LidarPropLidarType, &optval, sizeof(int)); |
| 114 | + /// device type |
| 115 | + optval = YDLIDAR_TYPE_SERIAL; |
| 116 | + lidarS2.setlidaropt(LidarPropDeviceType, &optval, sizeof(int)); |
| 117 | + /// sample rate |
| 118 | + optval = isSingleChannel ? 3 : 4; |
| 119 | + lidarS2.setlidaropt(LidarPropSampleRate, &optval, sizeof(int)); |
| 120 | + /// abnormal count |
| 121 | + optval = 4; |
| 122 | + lidarS2.setlidaropt(LidarPropAbnormalCheckCount, &optval, sizeof(int)); |
| 123 | + /// Intenstiy bit count |
| 124 | + optval = 10; |
| 125 | + lidarS2.setlidaropt(LidarPropIntenstiyBit, &optval, sizeof(int)); |
| 126 | + //////////////////////bool property///////////////// |
| 127 | + /// fixed angle resolution |
| 128 | + bool b_optvalue = true; |
| 129 | + lidarS2.setlidaropt(LidarPropFixedResolution, &b_optvalue, sizeof(bool)); |
| 130 | + b_optvalue = false; |
| 131 | + /// rotate 180 |
| 132 | + lidarS2.setlidaropt(LidarPropReversion, &b_optvalue, sizeof(bool)); |
| 133 | + /// Counterclockwise |
| 134 | + lidarS2.setlidaropt(LidarPropInverted, &b_optvalue, sizeof(bool)); |
| 135 | + b_optvalue = true; |
| 136 | + lidarS2.setlidaropt(LidarPropAutoReconnect, &b_optvalue, sizeof(bool)); |
| 137 | + /// one-way communication |
| 138 | + lidarS2.setlidaropt(LidarPropSingleChannel, &isSingleChannel, sizeof(bool)); |
| 139 | + /// intensity |
| 140 | + b_optvalue = true; |
| 141 | + lidarS2.setlidaropt(LidarPropIntenstiy, &b_optvalue, sizeof(bool)); |
| 142 | + /// Motor DTR |
| 143 | + b_optvalue = false; |
| 144 | + lidarS2.setlidaropt(LidarPropSupportMotorDtrCtrl, &b_optvalue, sizeof(bool)); |
| 145 | + /// HeartBeat |
| 146 | + b_optvalue = false; |
| 147 | + lidarS2.setlidaropt(LidarPropSupportHeartBeat, &b_optvalue, sizeof(bool)); |
| 148 | + //////////////////////float property///////////////// |
| 149 | + /// unit: ° |
| 150 | + float f_optvalue = 180.0f; |
| 151 | + lidarS2.setlidaropt(LidarPropMaxAngle, &f_optvalue, sizeof(float)); |
| 152 | + f_optvalue = -180.0f; |
| 153 | + lidarS2.setlidaropt(LidarPropMinAngle, &f_optvalue, sizeof(float)); |
| 154 | + /// unit: m |
| 155 | + f_optvalue = 64.f; |
| 156 | + lidarS2.setlidaropt(LidarPropMaxRange, &f_optvalue, sizeof(float)); |
| 157 | + f_optvalue = 0.05f; |
| 158 | + lidarS2.setlidaropt(LidarPropMinRange, &f_optvalue, sizeof(float)); |
| 159 | + /// unit: Hz |
| 160 | + lidarS2.setlidaropt(LidarPropScanFrequency, &frequency, sizeof(float)); |
| 161 | + ret = lidarS2.initialize(); |
| 162 | + if (!ret) |
| 163 | + { |
| 164 | + fprintf(stderr, "Fail to initialize %s\n", lidarS2.DescribeError()); |
| 165 | + fflush(stderr); |
| 166 | + return -1; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + LaserScan scanGs; //GS2点云数据 |
| 171 | + LaserScan scanS2; //S2雷达点云数据 |
| 172 | + while (ydlidar::os_isOk()) |
| 173 | + { |
| 174 | + //启动S2 |
| 175 | + ret = lidarS2.turnOn(); |
| 176 | + if (!ret) |
| 177 | + { |
| 178 | + fprintf(stderr, "Fail to turn on S2 %s\n", lidarS2.DescribeError()); |
| 179 | + fflush(stderr); |
| 180 | + return -1; |
| 181 | + } |
| 182 | + //启动GS2 |
| 183 | + ret = lidarGs.turnOn(); |
| 184 | + if (!ret) |
| 185 | + { |
| 186 | + fprintf(stderr, "Fail to turn on GS2 %s\n", lidarGs.DescribeError()); |
| 187 | + fflush(stderr); |
| 188 | + return -1; |
| 189 | + } |
| 190 | + //启动后运行5秒然后停止扫描 |
| 191 | + uint64_t t = getms(); |
| 192 | + while (getms() - t < 5000) |
| 193 | + { |
| 194 | + //获取GS2点云数据 |
| 195 | + if (lidarGs.doProcessSimple(scanGs)) |
| 196 | + { |
| 197 | + printf("[%lu] points in [0x%016lX] module num [%d] env flag [0x%04X]\n", |
| 198 | + scanGs.points.size(), |
| 199 | + scanGs.stamp, |
| 200 | + scanGs.moduleNum, |
| 201 | + scanGs.envFlag); |
| 202 | + fflush(stdout); |
| 203 | + } |
| 204 | + else |
| 205 | + { |
| 206 | + fprintf(stderr, "Failed to get Lidar GS2 Data\n"); |
| 207 | + fflush(stderr); |
| 208 | + } |
| 209 | + //获取S2点云数据 |
| 210 | + if (lidarS2.doProcessSimple(scanS2)) |
| 211 | + { |
| 212 | + printf("[%u] points inc [%f]\n", |
| 213 | + (unsigned int)scanS2.points.size(), |
| 214 | + scanS2.config.angle_increment); |
| 215 | + fflush(stdout); |
| 216 | + } |
| 217 | + else |
| 218 | + { |
| 219 | + fprintf(stderr, "Failed to get Lidar S2 Data\n"); |
| 220 | + fflush(stderr); |
| 221 | + static int s_errorCount = 0; |
| 222 | + if (s_errorCount++ > 10) |
| 223 | + return -1; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + //停止S2 |
| 228 | + lidarS2.turnOff(); |
| 229 | + //停止GS2 |
| 230 | + lidarGs.turnOff(); |
| 231 | + } |
| 232 | + |
| 233 | + lidarGs.turnOff(); |
| 234 | + lidarGs.disconnecting(); |
| 235 | + |
| 236 | + return 0; |
| 237 | +} |
0 commit comments