-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·286 lines (241 loc) · 8.55 KB
/
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
275
276
277
278
279
280
281
282
283
284
285
286
/*
* RPLIDAR
* Ultra Simple Data Grabber Demo App
*
* Copyright (c) 2009 - 2014 RoboPeak Team
* http://www.robopeak.com
* Copyright (c) 2014 - 2018 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 <cmath>
#include "rplidar.h" //RPLIDAR standard sdk, all-in-one header
#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;
bool checkRPLIDARHealth(RPlidarDriver * drv)
{
u_result op_result;
rplidar_response_device_health_t healthinfo;
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 : %d\n", healthinfo.status);
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();
return false;
} else {
return true;
}
} else {
fprintf(stderr, "Error, cannot retrieve the lidar health code: %x\n", op_result);
return false;
}
}
#include <signal.h>
bool ctrl_c_pressed;
void ctrlc(int)
{
ctrl_c_pressed = true;
}
int main(int argc, const char * argv[]) {
const char * opt_com_path = NULL;
_u32 baudrateArray[2] = {115200, 256000};
_u32 opt_com_baudrate = 0;
u_result op_result;
//additional variables for our own purposes
float myAngle;
float myDistance;
float expDistance;
float frontDistance;
float rightDistance;
float leftDistance;
//variables for changing dimension
float sidelength = 2150.0;
float shortsidelength = 1200;
bool useArgcBaudrate = false;
printf("Ultra simple LIDAR data grabber for RPLIDAR.\n"
"Version: "RPLIDAR_SDK_VERSION"\n");
// read serial port from the command line...
if (argc>1) opt_com_path = argv[1]; // or set to a fixed value: e.g. "com3"
// read baud rate from the command line if specified...
if (argc>2)
{
opt_com_baudrate = strtoul(argv[2], NULL, 10);
useArgcBaudrate = true;
}
if (!opt_com_path) {
#ifdef _WIN32
// use default com port
opt_com_path = "\\\\.\\com3";
#else
opt_com_path = "/dev/ttyUSB0";
#endif
}
// create the driver instance
RPlidarDriver * drv = RPlidarDriver::CreateDriver(DRIVER_TYPE_SERIALPORT);
if (!drv) {
fprintf(stderr, "insufficent memory, exit\n");
exit(-2);
}
rplidar_response_device_info_t devinfo;
bool connectSuccess = false;
// make connection...
if(useArgcBaudrate)
{
if(!drv)
drv = RPlidarDriver::CreateDriver(DRIVER_TYPE_SERIALPORT);
if (IS_OK(drv->connect(opt_com_path, opt_com_baudrate)))
{
op_result = drv->getDeviceInfo(devinfo);
if (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)
{
if(!drv)
drv = RPlidarDriver::CreateDriver(DRIVER_TYPE_SERIALPORT);
if(IS_OK(drv->connect(opt_com_path, baudrateArray[i])))
{
op_result = drv->getDeviceInfo(devinfo);
if (IS_OK(op_result))
{
connectSuccess = true;
break;
}
else
{
delete drv;
drv = NULL;
}
}
}
}
if (!connectSuccess) {
fprintf(stderr, "Error, cannot bind to the specified serial port %s.\n"
, opt_com_path);
goto on_finished;
}
// 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"
"Firmware Ver: %d.%02d\n"
"Hardware Rev: %d\n"
, devinfo.firmware_version>>8
, devinfo.firmware_version & 0xFF
, (int)devinfo.hardware_version);
// check health...
if (!checkRPLIDARHealth(drv)) {
goto on_finished;
}
signal(SIGINT, ctrlc);
drv->startMotor();
// start scan...
drv->startScan(0,1);
// fetech result and print it out...
while (1) {
rplidar_response_measurement_node_t nodes[8192];
size_t count = _countof(nodes);
op_result = drv->grabScanData(nodes, count);
if (IS_OK(op_result)) {
drv->ascendScanData(nodes, count);
printf("\n\nstarting scan\n\n=====\n");
for (int pos = 0; pos < (int)count ; ++pos) {
myAngle = (nodes[pos].angle_q6_checkbit >> RPLIDAR_RESP_MEASUREMENT_ANGLE_SHIFT)/64.0f;
myDistance = nodes[pos].distance_q2/4.0f;
//front
if(myAngle >= 0.0 && myAngle <= 90.0){
frontDistance = sidelength/cos(myAngle * M_PI/180.0); //convert from angles to radians
}
else if(myAngle >= 270.0 && myAngle <= 360.0){
frontDistance = sidelength/cos((360.0-myAngle) * M_PI/180.0);
}
else{ frontDistance = 999999;}
//right
if(myAngle >= 0.0 && myAngle <= 90.0){
rightDistance = (shortsidelength)/cos((90.0 - myAngle) * M_PI/180.0);
}
else{ rightDistance = 999999;}
//left
if(myAngle >= 270 && myAngle <= 360.0){
leftDistance = (shortsidelength)/cos((90.0-(360.0-myAngle))*M_PI/180.0);
}
else{ leftDistance = 999999;}
//get smallest
if( frontDistance < rightDistance){
expDistance = frontDistance;
}
else{ expDistance = rightDistance;}
if( leftDistance < expDistance){
expDistance = leftDistance;
}
if( (myAngle >= 0.0 && myAngle <= 85.0) || (myAngle >= 275 && myAngle <= 360)){
if( abs(expDistance - myDistance) > 250.0){
printf("object detected at angle %03.2f and distance %f\n", myAngle, myDistance);
printf("expected distance at angle %03.2f is %f mm\n", myAngle, expDistance);
printf("actual distance at angle %03.2f is %f mm\n", myAngle, myDistance);
printf("Front Distance %03.2f, left distance, %03.2f, right distance %03.2f \n", frontDistance, leftDistance, rightDistance);
printf("--------------------------------\n");
}
}
}
}
if (ctrl_c_pressed){
break;
}
}
drv->stop();
drv->stopMotor();
// done!
on_finished:
RPlidarDriver::DisposeDriver(drv);
drv = NULL;
return 0;
}