-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.cpp
286 lines (228 loc) · 10.4 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*****************************************************************************
* EAI TOF LIDAR DRIVER *
* Copyright (C) 2018 EAI TEAM chushuifurong618@eaibot.com. *
* *
* This file is part of EAI TOF LIDAR DRIVER. *
* *
* @file test.cpp *
* @brief Lidar test *
* Details. *
* *
* @author Tony.Yang *
* @email chushuifurong618@eaibot.com *
* @version 1.3.7(版本号) *
* @date chushuifurong618@eaibot.com *
* *
* *
*----------------------------------------------------------------------------*
* Remark : Description *
*----------------------------------------------------------------------------*
* Change History : *
* <Date> | <Version> | <Author> | <Description> *
*----------------------------------------------------------------------------*
* 2018/08/09 | 1.3.7 | Tony.Yang | Create file *
*----------------------------------------------------------------------------*
* *
*****************************************************************************/
#ifdef _WIN32
# pragma warning(disable: 4786)
#pragma comment(lib, "ydlidar_driver.lib")
#endif
#include <iostream>
#include <string>
#include <memory>
#include <Lidar/LIDARDevice.h>
#include <config.h>
#include <simpleini/SimpleIni.h>
using namespace std;
using namespace ydlidar;
std::vector<float> split(const std::string &s, char delim) {
std::vector<float> elems;
std::stringstream ss(s);
std::string number;
while(std::getline(ss, number, delim)) {
elems.push_back(atof(number.c_str()));
}
return elems;
}
void LaserScanCallback(const LaserScan& scan) {
std::cout<< "received scan size: "<< scan.ranges.size()<<std::endl;
std::cout<< "scan system time: "<< scan.system_time_stamp<<std::endl;
std::cout<< "scan self time: "<< scan.self_time_stamp<<std::endl;
std::cout<< "scan frequency: "<< 1000000000.0/scan.config.scan_time << "HZ"<<std::endl;
std::cout<< "lidar frequency: "<< scan.scan_frequency << "HZ"<<std::endl;
for(size_t i =0; i < scan.ranges.size(); i++) {
// current angle
double angle = scan.config.min_angle + i*scan.config.ang_increment;
//current distance
double distance = scan.ranges[i];
//current intensity
int intensity = scan.intensities[i];
UNUSED(angle);
UNUSED(distance);
UNUSED(intensity);
}
}
int main(int argc, char * argv[])
{
printf(" YDLIDAR C++ TEST\n");
ydlidar::init(argc, argv);
CSimpleIniA ini;
ini.SetUnicode();
LaserParamCfg cfg;
bool input = true;
std::string ini_file = "lidar.ini";
bool ini_exist = fileExists(ini_file.c_str());
if(ini_exist || argc > 1) {
if(argc > 1)
ini_file = (std::string)argv[1];
if(fileExists(ini_file.c_str())) {
SI_Error rc = ini.LoadFile(ini_file.c_str());
if(rc >= 0 ) {
input = false;
const char * pszValue = ini.GetValue("LIDAR", "serialPort", "");
cfg.serialPort = pszValue;
if(cfg.serialPort.empty()) {
input = true;
}
pszValue = ini.GetValue("LIDAR", "ignoreArray", "");
cfg.ignoreArray = split(pszValue,',');
cfg.serialBaudrate = ini.GetLongValue("LIDAR", "serialBaudrate", cfg.serialBaudrate);
cfg.sampleRate = ini.GetLongValue("LIDAR", "sampleRate", cfg.sampleRate);
cfg.scanFrequency = ini.GetLongValue("LIDAR", "scanFrequency", cfg.scanFrequency);
cfg.intensity = ini.GetBoolValue("LIDAR", "intensity", cfg.intensity);
cfg.autoReconnect = ini.GetBoolValue("LIDAR", "autoReconnect", cfg.autoReconnect);
cfg.exposure = ini.GetBoolValue("LIDAR", "exposure", cfg.exposure);
cfg.fixedResolution = ini.GetBoolValue("LIDAR", "fixedResolution", cfg.fixedResolution);
cfg.reversion = ini.GetBoolValue("LIDAR", "reversion", cfg.reversion);
cfg.heartBeat = ini.GetBoolValue("LIDAR", "heartBeat", cfg.heartBeat);
cfg.maxAngle = ini.GetDoubleValue("LIDAR", "maxAngle", cfg.maxAngle);
cfg.minAngle = ini.GetDoubleValue("LIDAR", "minAngle", cfg.minAngle);
cfg.maxRange = ini.GetDoubleValue("LIDAR", "maxRange", cfg.maxRange);
cfg.minRange = ini.GetDoubleValue("LIDAR", "minRange", cfg.minRange);
}
}
}
bool excep = false;
bool input_port =false;
try {
LIDAR ydlidar;
std::vector<string> ports = ydlidar.getLidarList();
if(ports.size() == 1) {
if(cfg.serialPort != ports[0]) {
std::string str;
printf("Radar[%s] detected, whether to select current radar(yes/no)?:", ports[0].c_str());
std::cin>>str;
for (size_t i=0; i <str.size(); i++)
str[i] = tolower(str[i]);
if(str.find("yes") != std::string::npos ||atoi(str.c_str()) == 1 ) {
cfg.serialPort = ports[0];
input_port = true;
}else{
return 0;
}
}
}
if(input) {
std::string port;
std::string baudrate;
std::string intensity;
if(ports.empty()) {
printf("Not radar delected, Please enter the radar port manually: ");
std::cin>>port;
cfg.serialPort = port;
input_port = true;
}
if(!input_port) {
int size = 0;
for(std::vector<string>::iterator it = ports.begin(); it != ports.end(); it++) {
printf("%d. %s\n", size, (*it).c_str());
size++;
}
select_port:
printf("Please select the lidar port:");
std::cin>>port;
if((size_t)atoi(port.c_str()) >= ports.size()) {
printf("Invalid serial number, Please re-select\n");
goto select_port;
}
cfg.serialPort = ports[atoi(port.c_str())];
}
std::vector<unsigned int> baud;
baud.push_back(115200);
baud.push_back(128000);
baud.push_back(153600);
baud.push_back(230400);
for( unsigned int i = 0; i < baud.size(); i ++) {
printf("%u. %u\n", i, baud[i]);
}
select_baud:
printf("Please select the lidar baud rate:");
std::cin>>baudrate;
if(atoi(baudrate.c_str()) >= 4) {
printf("Invalid serial number, Please re-select\n");
goto select_baud;
}
cfg.serialBaudrate = baud[atoi(baudrate.c_str())];
printf("0. false\n");
printf("1. true\n");
select_intensity:
printf("Please select the lidar intensity:");
std::cin>>intensity;
if(atoi(intensity.c_str()) >= 2) {
printf("Invalid serial number, Please re-select\n");
goto select_intensity;
}
cfg.intensity = atoi(intensity.c_str()) ==0?false:true;
}
ini.SetValue("LIDAR", "serialPort", cfg.serialPort.c_str());
ini.SetLongValue("LIDAR", "serialBaudrate", cfg.serialBaudrate);
ini.SetLongValue("LIDAR", "sampleRate", cfg.sampleRate);
ini.SetLongValue("LIDAR", "scanFrequency", cfg.scanFrequency);
ini.SetBoolValue("LIDAR", "intensity", cfg.intensity);
ini.SetBoolValue("LIDAR", "autoReconnect", cfg.autoReconnect);
ini.SetBoolValue("LIDAR", "exposure", cfg.exposure);
ini.SetBoolValue("LIDAR", "fixedResolution", cfg.fixedResolution);
ini.SetBoolValue("LIDAR", "reversion", cfg.reversion);
ini.SetBoolValue("LIDAR", "heartBeat", cfg.heartBeat);
ini.SetDoubleValue("LIDAR", "maxAngle", cfg.maxAngle);
ini.SetDoubleValue("LIDAR", "minAngle", cfg.minAngle);
ini.SetDoubleValue("LIDAR", "maxRange", cfg.maxRange);
ini.SetDoubleValue("LIDAR", "minRange", cfg.minRange);
std::cout<<"SDK Version: "<< SDK_VERSION<<std::endl;
std::cout <<"LIDAR Version: "<< YDLIDAR_VERSION <<std::endl;
ydlidar.RegisterLIDARDataCallback(&LaserScanCallback);
ydlidar.UpdateLidarParamCfg(cfg);
while(ydlidar::ok()){
try {
ydlidar.spinOnce();
}catch(TimeoutException& e) {
std::cout<< e.what()<<std::endl;
}catch(CorruptedDataException& e) {
std::cout<< e.what()<<std::endl;
}catch(DeviceInformationException& e) {
std::cout<< e.what()<<std::endl;
}catch(DeviceException& e) {
std::cerr<< e.what()<<std::endl;
excep = true;
break;
}
}
}catch(TimeoutException& e) {
std::cout<< e.what()<<std::endl;
excep = true;
}catch(CorruptedDataException& e) {
std::cout<< e.what()<<std::endl;
excep = true;
}catch(DeviceInformationException& e) {
std::cout<< e.what()<<std::endl;
excep = true;
}catch(DeviceException& e) {
std::cerr<< e.what()<<std::endl;
excep = true;
}
if(!excep) {
ini.SaveFile(ini_file.c_str());
}
return 0;
}