-
Notifications
You must be signed in to change notification settings - Fork 0
/
lidar_main.cpp
274 lines (228 loc) · 7.93 KB
/
lidar_main.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
/*
* RPLIDAR
* Simple Data Grabber Demo App
*
* Copyright (c) 2009 - 2014 RoboPeak Team
* http://www.robopeak.com
* Copyright (c) 2014 - 2019 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 "rplidar.h" //RPLIDAR standard sdk, all-in-one header
#define NEGATIVE_SPACE
//#undef NEGATIVE_SPACE
#ifdef NEGATIVE_SPACE
char CHAR1=' ';
char CHAR2='#';
#else
char CHAR1='#';
char CHAR2=' ';
#endif
#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(_word_size_t ms){
while (ms>=1000){
usleep(1000*1000);
ms-=1000;
};
if (ms!=0)
usleep(ms*1000);
}
#endif
using namespace rp::standalone::rplidar;
void print_usage(int argc, const char * argv[])
{
printf("Simple LIDAR data grabber for RPLIDAR.\n"
"Version: " RPLIDAR_SDK_VERSION "\n"
"Usage:\n"
"%s <com port> [baudrate]\n"
"The default baudrate is 115200(for A2) or 256000(for A3). Please refer to the datasheet for details.\n"
, argv[0]);
}
void plot_histogram(rplidar_response_measurement_node_t * nodes, size_t count)
{
const int BARCOUNT = 120;
const int MAXBARHEIGHT = 33;
const float ANGLESCALE = 360.0f/BARCOUNT;
float histogram[BARCOUNT];
for (int pos = 0; pos < _countof(histogram); ++pos) {
histogram[pos] = 0.0f;
}
float max_val = 0;
for (int pos =0 ; pos < (int)count; ++pos) {
int int_deg = (int)((nodes[pos].angle_q6_checkbit >> RPLIDAR_RESP_MEASUREMENT_ANGLE_SHIFT)/64.0f/ANGLESCALE);
if (int_deg >= BARCOUNT) int_deg = 0;
float cachedd = histogram[int_deg];
if (cachedd == 0.0f ) {
cachedd = nodes[pos].distance_q2/4.0f;
} else {
cachedd = (nodes[pos].distance_q2/4.0f + cachedd)/2.0f;
}
if (cachedd > max_val) max_val = cachedd;
histogram[int_deg] = cachedd;
}
for (int height = 0; height < MAXBARHEIGHT; ++height) {
float threshold_h = (MAXBARHEIGHT - height - 1) * (max_val/MAXBARHEIGHT);
for (int xpos = 0; xpos < BARCOUNT; ++xpos) {
if (histogram[xpos] >= threshold_h) {
putc(CHAR1, stdout);
}else {
putc(CHAR2, stdout);
}
}
printf("\n");
}
for (int xpos = 0; xpos < BARCOUNT; ++xpos) {
putc('-', stdout);
}
printf("\n");
}
u_result capture_and_display(RPlidarDriver * drv)
{
u_result ans;
rplidar_response_measurement_node_t nodes[8192];
size_t count = _countof(nodes);
printf("waiting for data...\n");
while (1) {
// fetch extactly one 0-360 degrees' scan
ans = drv->grabScanData(nodes, count);
if (IS_OK(ans) || ans == RESULT_OPERATION_TIMEOUT) {
drv->ascendScanData(nodes, count);
system("tput clear");
plot_histogram(nodes, count);
#if 0
printf("Do you want to see all the data? (y/n) ");
int key = getchar();
if (key == 'Y' || key == 'y') {
for (int pos = 0; pos < (int)count ; ++pos) {
printf("%s theta: %03.2f Dist: %08.2f \n",
(nodes[pos].sync_quality & RPLIDAR_RESP_MEASUREMENT_SYNCBIT) ?"S ":" ",
(nodes[pos].angle_q6_checkbit >> RPLIDAR_RESP_MEASUREMENT_ANGLE_SHIFT)/64.0f,
nodes[pos].distance_q2/4.0f);
}
}
#endif
} else {
printf("error code: %x\n", ans);
}
}
return ans;
}
int main(int argc, const char * argv[]) {
const char * opt_com_path = NULL;
_u32 opt_com_baudrate = 115200;
u_result op_result;
if (argc < 2) {
print_usage(argc, argv);
return -1;
}
opt_com_path = argv[1];
if (argc>2) opt_com_baudrate = strtoul(argv[2], NULL, 10);
// create the driver instance
RPlidarDriver * drv = RPlidarDriver::CreateDriver(DRIVER_TYPE_SERIALPORT);
if (!drv) {
fprintf(stderr, "insufficent memory, exit\n");
exit(-2);
}
rplidar_response_device_health_t healthinfo;
rplidar_response_device_info_t devinfo;
do {
// try to connect
if (IS_FAIL(drv->connect(opt_com_path, opt_com_baudrate))) {
fprintf(stderr, "Error, cannot bind to the specified serial port %s.\n"
, opt_com_path);
break;
}
// retrieving the device info
////////////////////////////////////////
op_result = drv->getDeviceInfo(devinfo);
if (IS_FAIL(op_result)) {
if (op_result == RESULT_OPERATION_TIMEOUT) {
// you can check the detailed failure reason
fprintf(stderr, "Error, operation time out.\n");
} else {
fprintf(stderr, "Error, unexpected error, code: %x\n", op_result);
// other unexpected result
}
break;
}
// print out the device serial number, firmware and hardware version number..
printf("RPLIDAR S/N: ");
for (int pos = 0; pos < 16 ;++pos) {
printf("%02X", devinfo.serialnum[pos]);
}
printf("\n"
"Version: "RPLIDAR_SDK_VERSION"\n"
"Firmware Ver: %d.%02d\n"
"Hardware Rev: %d\n"
, devinfo.firmware_version>>8
, devinfo.firmware_version & 0xFF
, (int)devinfo.hardware_version);
// check the device health
////////////////////////////////////////
op_result = drv->getHealth(healthinfo);
if (IS_OK(op_result)) { // the macro IS_OK is the preperred way to judge whether the operation is succeed.
printf("RPLidar health status : ");
switch (healthinfo.status) {
case RPLIDAR_STATUS_OK:
printf("OK.");
break;
case RPLIDAR_STATUS_WARNING:
printf("Warning.");
break;
case RPLIDAR_STATUS_ERROR:
printf("Error.");
break;
}
printf(" (errorcode: %d)\n", healthinfo.error_code);
} else {
fprintf(stderr, "Error, cannot retrieve the lidar health code: %x\n", op_result);
break;
}
if (healthinfo.status == RPLIDAR_STATUS_ERROR) {
fprintf(stderr, "Error, rplidar internal error detected. Please reboot the device to retry.\n");
// enable the following code if you want rplidar to be reboot by software
// drv->reset();
break;
}
drv->startMotor();
// take only one 360 deg scan and display the result as a histogram
////////////////////////////////////////////////////////////////////////////////
if (IS_FAIL(drv->startScan( 0,1 ))) // you can force rplidar to perform scan operation regardless whether the motor is rotating
{
fprintf(stderr, "Error, cannot start the scan operation.\n");
break;
}
if (IS_FAIL(capture_and_display(drv))) {
fprintf(stderr, "Error, cannot grab scan data.\n");
break;
}
} while(0);
drv->stop();
drv->stopMotor();
RPlidarDriver::DisposeDriver(drv);
return 0;
}