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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/*
* SLAMTEC LIDAR
* Ultra Simple Data Grabber Demo App
*
* Copyright (c) 2009 - 2014 RoboPeak Team
* http://www.robopeak.com
* Copyright (c) 2014 - 2020 Shanghai Slamtec Co., Ltd.
* http://www.slamtec.com
*
*/
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <cmath>
#include <iostream>
#include <fstream>
#include <filesystem>
#include "sl_lidar.h"
#include "sl_lidar_driver.h"
#ifndef _countof
#define _countof(_Array) (int)(sizeof(_Array) / sizeof(_Array[0]))
#endif
#ifdef _WIN32
#include <Windows.h>
#define delay(x) ::Sleep(x)
#else
#include <unistd.h>
static inline void delay(sl_word_size_t ms){
while (ms>=1000){
usleep(1000*1000);
ms-=1000;
};
if (ms!=0)
usleep(ms*1000);
}
#endif
using namespace sl;
void print_usage(int argc, const char * argv[])
{
printf("Usage:\n"
" For serial channel\n %s --channel --serial <com port> [baudrate]\n"
" The baudrate used by different models is as follows:\n"
" A1(115200),A2M7(256000),A2M8(115200),A2M12(256000),"
"A3(256000),S1(256000),S2(1000000),S3(1000000),C1(460800)\n"
" For udp channel\n %s --channel --udp <ipaddr> [port NO.]\n"
" The T1 default ipaddr is 192.168.11.2,and the port NO.is 8089. Please refer to the datasheet for details.\n"
, argv[0], argv[0]);
}
bool checkSLAMTECLIDARHealth(ILidarDriver * drv)
{
sl_result op_result;
sl_lidar_response_device_health_t healthinfo;
op_result = drv->getHealth(healthinfo);
if (SL_IS_OK(op_result)) { // the macro IS_OK is the preperred way to judge whether the operation is succeed.
printf("SLAMTEC Lidar health status : %d\n", healthinfo.status);
if (healthinfo.status == SL_LIDAR_STATUS_ERROR) {
fprintf(stderr, "Error, slamtec lidar internal error detected. Please reboot the device to retry.\n");
// enable the following code if you want slamtec lidar to be reboot by software
// drv->reset();
return false;
} else {
return true;
}
} else {
fprintf(stderr, "Error, cannot retrieve the lidar health code: %x\n", op_result);
return false;
}
}
bool ctrl_c_pressed;
void ctrlc(int)
{
ctrl_c_pressed = true;
}
int main(int argc, const char * argv[]) {
const char * opt_is_channel = NULL;
const char * opt_channel = NULL;
const char * opt_channel_param_first = NULL;
sl_u32 opt_channel_param_second = 0;
sl_u32 baudrateArray[2] = {115200, 256000};
sl_result op_result;
int opt_channel_type = CHANNEL_TYPE_SERIALPORT;
bool useArgcBaudrate = false;
IChannel* _channel;
std::ofstream outfile;
std::ofstream buf_file_o;
std::ifstream buf_file_i;
int distances[360];
int a, b;
printf("Ultra simple LIDAR data grabber for SLAMTEC LIDAR.\n"
"Version: %s\n", SL_LIDAR_SDK_VERSION);
if (argc>1)
{
opt_is_channel = argv[1];
}
else
{
print_usage(argc, argv);
return -1;
}
if(strcmp(opt_is_channel, "--channel")==0){
opt_channel = argv[2];
if(strcmp(opt_channel, "-s")==0||strcmp(opt_channel, "--serial")==0)
{
// read serial port from the command line...
opt_channel_param_first = argv[3];// or set to a fixed value: e.g. "com3"
// read baud rate from the command line if specified...
if (argc>4) opt_channel_param_second = strtoul(argv[4], NULL, 10);
useArgcBaudrate = true;
}
else if(strcmp(opt_channel, "-u")==0||strcmp(opt_channel, "--udp")==0)
{
// read ip addr from the command line...
opt_channel_param_first = argv[3];//or set to a fixed value: e.g. "192.168.11.2"
if (argc>4) opt_channel_param_second = strtoul(argv[4], NULL, 10);//e.g. "8089"
opt_channel_type = CHANNEL_TYPE_UDP;
}
else
{
print_usage(argc, argv);
return -1;
}
}
else
{
print_usage(argc, argv);
return -1;
}
if(opt_channel_type == CHANNEL_TYPE_SERIALPORT)
{
if (!opt_channel_param_first) {
#ifdef _WIN32
// use default com port
opt_channel_param_first = "\\\\.\\com3";
#elif __APPLE__
opt_channel_param_first = "/dev/tty.SLAB_USBtoUART";
#else
opt_channel_param_first = "/dev/ttyUSB0";
#endif
}
}
// create the driver instance
ILidarDriver * drv = *createLidarDriver();
if (!drv) {
fprintf(stderr, "insufficent memory, exit\n");
exit(-2);
}
sl_lidar_response_device_info_t devinfo;
bool connectSuccess = false;
if(opt_channel_type == CHANNEL_TYPE_SERIALPORT){
if(useArgcBaudrate){
_channel = (*createSerialPortChannel(opt_channel_param_first, opt_channel_param_second));
if (SL_IS_OK((drv)->connect(_channel))) {
op_result = drv->getDeviceInfo(devinfo);
if (SL_IS_OK(op_result))
{
connectSuccess = true;
}
else{
delete drv;
drv = NULL;
}
}
}
else{
size_t baudRateArraySize = (sizeof(baudrateArray))/ (sizeof(baudrateArray[0]));
for(size_t i = 0; i < baudRateArraySize; ++i)
{
_channel = (*createSerialPortChannel(opt_channel_param_first, baudrateArray[i]));
if (SL_IS_OK((drv)->connect(_channel))) {
op_result = drv->getDeviceInfo(devinfo);
if (SL_IS_OK(op_result))
{
connectSuccess = true;
break;
}
else{
delete drv;
drv = NULL;
}
}
}
}
}
else if(opt_channel_type == CHANNEL_TYPE_UDP){
_channel = *createUdpChannel(opt_channel_param_first, opt_channel_param_second);
if (SL_IS_OK((drv)->connect(_channel))) {
op_result = drv->getDeviceInfo(devinfo);
if (SL_IS_OK(op_result))
{
connectSuccess = true;
}
else{
delete drv;
drv = NULL;
}
}
}
if (!connectSuccess) {
(opt_channel_type == CHANNEL_TYPE_SERIALPORT)?
(fprintf(stderr, "Error, cannot bind to the specified serial port %s.\n"
, opt_channel_param_first)):(fprintf(stderr, "Error, cannot connect to the specified ip addr %s.\n"
, opt_channel_param_first));
goto on_finished;
}
// print out the device serial number, firmware and hardware version number..
printf("SLAMTEC LIDAR S/N: ");
for (int pos = 0; pos < 16 ;++pos) {
printf("%02X", devinfo.serialnum[pos]);
}
printf("\n"
"Firmware Ver: %d.%02d\n"
"Hardware Rev: %d\n"
, devinfo.firmware_version>>8
, devinfo.firmware_version & 0xFF
, (int)devinfo.hardware_version);
// check health...
if (!checkSLAMTECLIDARHealth(drv)) {
goto on_finished;
}
signal(SIGINT, ctrlc);
if(opt_channel_type == CHANNEL_TYPE_SERIALPORT)
drv->setMotorSpeed();
// start scan...
drv->startScan(0,1);
// fetech result and print it out...
while (1) {
sl_lidar_response_measurement_node_hq_t nodes[8192];
size_t count = _countof(nodes);
op_result = drv->grabScanDataHq(nodes, count);
if (SL_IS_OK(op_result)) {
drv->ascendScanData(nodes, count);
buf_file_o.open("distances_buf.txt");
for (int pos = 0; pos < (int)count ; ++pos) {
int angle = std::floor((nodes[pos].angle_z_q14 * 90.f) / 16384.f);
int dist = nodes[pos].dist_mm_q2/4.0f;
if (dist > 0.0) {
distances[angle] = dist;
buf_file_o << angle << " " << distances[angle] << "\n";
}
}
buf_file_o.close();
buf_file_i.open("distances_buf.txt");
outfile.open("distances.txt");
a = 0;
b = 0;
while (buf_file_i >> a >> b) {
outfile << a << " " << b << "\n";
printf("Angle: %i, Dist: %i\n", a, b);
}
buf_file_i.close();
outfile.close();
}
if (ctrl_c_pressed){
break;
}
}
drv->stop();
delay(200);
if(opt_channel_type == CHANNEL_TYPE_SERIALPORT)
drv->setMotorSpeed(0);
// done!
on_finished:
if(drv) {
delete drv;
drv = NULL;
}
return 0;
}
|