forked from tysik/obstacle_detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobstacle_extractor.cpp
483 lines (386 loc) · 16.1 KB
/
obstacle_extractor.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2017, Poznan University of Technology
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Poznan University of Technology nor the names
* of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Author: Mateusz Przybyla
*/
#include "processing_lidar_objects/obstacle_extractor.h"
#include "processing_lidar_objects/utilities/figure_fitting.h"
#include "processing_lidar_objects/utilities/math_utilities.h"
using namespace std;
using namespace processing_lidar_objects;
ObstacleExtractor::ObstacleExtractor(ros::NodeHandle& nh, ros::NodeHandle& nh_local) : nh_(nh), nh_local_(nh_local) {
p_active_ = false;
params_srv_ = nh_local_.advertiseService("params", &ObstacleExtractor::updateParams, this);
initialize();
}
ObstacleExtractor::~ObstacleExtractor() {
nh_local_.deleteParam("active");
nh_local_.deleteParam("use_scan");
nh_local_.deleteParam("use_pcl");
nh_local_.deleteParam("use_split_and_merge");
nh_local_.deleteParam("circles_from_visibles");
nh_local_.deleteParam("discard_converted_segments");
nh_local_.deleteParam("transform_coordinates");
nh_local_.deleteParam("min_group_points");
nh_local_.deleteParam("max_group_distance");
nh_local_.deleteParam("distance_proportion");
nh_local_.deleteParam("max_split_distance");
nh_local_.deleteParam("max_merge_separation");
nh_local_.deleteParam("max_merge_spread");
nh_local_.deleteParam("max_circle_radius");
nh_local_.deleteParam("radius_enlargement");
nh_local_.deleteParam("min_x_limit");
nh_local_.deleteParam("max_x_limit");
nh_local_.deleteParam("min_y_limit");
nh_local_.deleteParam("max_y_limit");
nh_local_.deleteParam("frame_id");
}
bool ObstacleExtractor::updateParams(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res) {
bool prev_active = p_active_;
nh_local_.param<bool>("active", p_active_, true);
nh_local_.param<bool>("use_scan", p_use_scan_, false);
nh_local_.param<bool>("use_pcl", p_use_pcl_, true);
nh_local_.param<bool>("use_split_and_merge", p_use_split_and_merge_, true);
nh_local_.param<bool>("circles_from_visibles", p_circles_from_visibles_, true);
nh_local_.param<bool>("discard_converted_segments", p_discard_converted_segments_, true);
nh_local_.param<bool>("transform_coordinates", p_transform_coordinates_, true);
nh_local_.param<int>("min_group_points", p_min_group_points_, 5);
nh_local_.param<double>("max_group_distance", p_max_group_distance_, 0.1);
nh_local_.param<double>("distance_proportion", p_distance_proportion_, 0.00628);
nh_local_.param<double>("max_split_distance", p_max_split_distance_, 0.2);
nh_local_.param<double>("max_merge_separation", p_max_merge_separation_, 0.2);
nh_local_.param<double>("max_merge_spread", p_max_merge_spread_, 0.2);
nh_local_.param<double>("max_circle_radius", p_max_circle_radius_, 0.6);
nh_local_.param<double>("radius_enlargement", p_radius_enlargement_, 0.25);
nh_local_.param<double>("min_x_limit", p_min_x_limit_, -10.0);
nh_local_.param<double>("max_x_limit", p_max_x_limit_, 10.0);
nh_local_.param<double>("min_y_limit", p_min_y_limit_, -10.0);
nh_local_.param<double>("max_y_limit", p_max_y_limit_, 10.0);
nh_local_.param<string>("frame_id", p_frame_id_, "map");
if (p_active_ != prev_active) {
if (p_active_) {
if (p_use_scan_)
scan_sub_ = nh_.subscribe("scan_transformed", 10, &ObstacleExtractor::scanCallback, this);
else if (p_use_pcl_)
pcl_sub_ = nh_.subscribe("pcl", 10, &ObstacleExtractor::pclCallback, this);
obstacles_pub_ = nh_.advertise<processing_lidar_objects::Obstacles>("raw_obstacles", 10);
}
else {
// Send empty message
processing_lidar_objects::ObstaclesPtr obstacles_msg(new processing_lidar_objects::Obstacles);
obstacles_msg->header.frame_id = p_frame_id_;
obstacles_msg->header.stamp = ros::Time::now();
obstacles_pub_.publish(obstacles_msg);
scan_sub_.shutdown();
pcl_sub_.shutdown();
obstacles_pub_.shutdown();
}
}
return true;
}
void ObstacleExtractor::scanCallback(const sensor_msgs::LaserScan::ConstPtr scan_msg) {
base_frame_id_ = scan_msg->header.frame_id;
stamp_ = scan_msg->header.stamp;
double phi = scan_msg->angle_min;
for (const float r : scan_msg->ranges) {
if (r >= scan_msg->range_min && r <= scan_msg->range_max)
input_points_.push_back(Point::fromPoolarCoords(r, phi));
phi += scan_msg->angle_increment;
}
processPoints();
}
void ObstacleExtractor::pclCallback(const sensor_msgs::PointCloud::ConstPtr pcl_msg) {
base_frame_id_ = pcl_msg->header.frame_id;
stamp_ = pcl_msg->header.stamp;
for (const geometry_msgs::Point32& point : pcl_msg->points)
input_points_.push_back(Point(point.x, point.y));
processPoints();
}
void ObstacleExtractor::processPoints() {
segments_.clear();
circles_.clear();
groupPoints(); // Grouping points simultaneously detects segments
mergeSegments();
detectCircles();
mergeCircles();
publishObstacles();
input_points_.clear();
}
void ObstacleExtractor::groupPoints() {
static double sin_dp = sin(2.0 * p_distance_proportion_);
PointSet point_set;
point_set.begin = input_points_.begin();
point_set.end = input_points_.begin();
point_set.num_points = 1;
point_set.is_visible = true;
for (PointIterator point = input_points_.begin()++; point != input_points_.end(); ++point) {
double range = (*point).length();
double distance = (*point - *point_set.end).length();
if (distance < p_max_group_distance_ + range * p_distance_proportion_) {
point_set.end = point;
point_set.num_points++;
}
else {
double prev_range = (*point_set.end).length();
// Heron's equation
double p = (range + prev_range + distance) / 2.0;
double S = sqrt(p * (p - range) * (p - prev_range) * (p - distance));
double sin_d = 2.0 * S / (range * prev_range); // Sine of angle between beams
// TODO: This condition can be fulfilled if the point are on the opposite sides
// of the scanner (angle = 180 deg). Needs another check.
if (abs(sin_d) < sin_dp && range < prev_range)
point_set.is_visible = false;
detectSegments(point_set);
// Begin new point set
point_set.begin = point;
point_set.end = point;
point_set.num_points = 1;
point_set.is_visible = (abs(sin_d) > sin_dp || range < prev_range);
}
}
detectSegments(point_set); // Check the last point set too!
}
void ObstacleExtractor::detectSegments(const PointSet& point_set) {
if (point_set.num_points < p_min_group_points_)
return;
Segment segment(*point_set.begin, *point_set.end); // Use Iterative End Point Fit
if (p_use_split_and_merge_)
segment = fitSegment(point_set);
PointIterator set_divider;
double max_distance = 0.0;
double distance = 0.0;
int split_index = 0; // Natural index of splitting point (counting from 1)
int point_index = 0; // Natural index of current point in the set
// Seek the point of division
for (PointIterator point = point_set.begin; point != point_set.end; ++point) {
++point_index;
if ((distance = segment.distanceTo(*point)) >= max_distance) {
double r = (*point).length();
if (distance > p_max_split_distance_ + r * p_distance_proportion_) {
max_distance = distance;
set_divider = point;
split_index = point_index;
}
}
}
// Split the set only if the sub-groups are not 'small'
if (max_distance > 0.0 && split_index > p_min_group_points_ && split_index < point_set.num_points - p_min_group_points_) {
set_divider = input_points_.insert(set_divider, *set_divider); // Clone the dividing point for each group
PointSet subset1, subset2;
subset1.begin = point_set.begin;
subset1.end = set_divider;
subset1.num_points = split_index;
subset1.is_visible = point_set.is_visible;
subset2.begin = ++set_divider;
subset2.end = point_set.end;
subset2.num_points = point_set.num_points - split_index;
subset2.is_visible = point_set.is_visible;
detectSegments(subset1);
detectSegments(subset2);
} else { // Add the segment
if (!p_use_split_and_merge_)
segment = fitSegment(point_set);
segments_.push_back(segment);
}
}
void ObstacleExtractor::mergeSegments() {
for (auto i = segments_.begin(); i != segments_.end(); ++i) {
for (auto j = i; j != segments_.end(); ++j) {
Segment merged_segment;
if (compareSegments(*i, *j, merged_segment)) {
auto temp_itr = segments_.insert(i, merged_segment);
segments_.erase(i);
segments_.erase(j);
i = --temp_itr; // Check the new segment against others
break;
}
}
}
}
bool ObstacleExtractor::compareSegments(const Segment& s1, const Segment& s2, Segment& merged_segment) {
if (&s1 == &s2)
return false;
// Segments must be provided counter-clockwise
if (s1.first_point.cross(s2.first_point) < 0.0)
return compareSegments(s2, s1, merged_segment);
if (checkSegmentsProximity(s1, s2)) {
vector<PointSet> point_sets;
point_sets.insert(point_sets.end(), s1.point_sets.begin(), s1.point_sets.end());
point_sets.insert(point_sets.end(), s2.point_sets.begin(), s2.point_sets.end());
Segment segment = fitSegment(point_sets);
if (checkSegmentsCollinearity(segment, s1, s2)) {
merged_segment = segment;
return true;
}
}
return false;
}
bool ObstacleExtractor::checkSegmentsProximity(const Segment& s1, const Segment& s2) {
return (s1.trueDistanceTo(s2.first_point) < p_max_merge_separation_ ||
s1.trueDistanceTo(s2.last_point) < p_max_merge_separation_ ||
s2.trueDistanceTo(s1.first_point) < p_max_merge_separation_ ||
s2.trueDistanceTo(s1.last_point) < p_max_merge_separation_);
}
bool ObstacleExtractor::checkSegmentsCollinearity(const Segment& segment, const Segment& s1, const Segment& s2) {
return (segment.distanceTo(s1.first_point) < p_max_merge_spread_ &&
segment.distanceTo(s1.last_point) < p_max_merge_spread_ &&
segment.distanceTo(s2.first_point) < p_max_merge_spread_ &&
segment.distanceTo(s2.last_point) < p_max_merge_spread_);
}
void ObstacleExtractor::detectCircles() {
for (auto segment = segments_.begin(); segment != segments_.end(); ++segment) {
if (p_circles_from_visibles_) {
bool segment_is_visible = true;
for (const PointSet& ps : segment->point_sets) {
if (!ps.is_visible) {
segment_is_visible = false;
break;
}
}
if (!segment_is_visible)
continue;
}
Circle circle(*segment);
circle.radius += p_radius_enlargement_;
if (circle.radius < p_max_circle_radius_) {
circles_.push_back(circle);
if (p_discard_converted_segments_) {
segment = segments_.erase(segment);
--segment;
}
}
}
}
void ObstacleExtractor::mergeCircles() {
for (auto i = circles_.begin(); i != circles_.end(); ++i) {
for (auto j = i; j != circles_.end(); ++j) {
Circle merged_circle;
if (compareCircles(*i, *j, merged_circle)) {
auto temp_itr = circles_.insert(i, merged_circle);
circles_.erase(i);
circles_.erase(j);
i = --temp_itr;
break;
}
}
}
}
bool ObstacleExtractor::compareCircles(const Circle& c1, const Circle& c2, Circle& merged_circle) {
if (&c1 == &c2)
return false;
// If circle c1 is fully inside c2 - merge and leave as c2
if (c2.radius - c1.radius >= (c2.center - c1.center).length()) {
merged_circle = c2;
return true;
}
// If circle c2 is fully inside c1 - merge and leave as c1
if (c1.radius - c2.radius >= (c2.center - c1.center).length()) {
merged_circle = c1;
return true;
}
// If circles intersect and are 'small' - merge
if (c1.radius + c2.radius >= (c2.center - c1.center).length()) {
Point center = c1.center + (c2.center - c1.center) * c1.radius / (c1.radius + c2.radius);
double radius = (c1.center - center).length() + c1.radius;
Circle circle(center, radius);
circle.radius += max(c1.radius, c2.radius);
if (circle.radius < p_max_circle_radius_) {
circle.point_sets.insert(circle.point_sets.end(), c1.point_sets.begin(), c1.point_sets.end());
circle.point_sets.insert(circle.point_sets.end(), c2.point_sets.begin(), c2.point_sets.end());
merged_circle = circle;
return true;
}
}
return false;
}
void ObstacleExtractor::publishObstacles() {
processing_lidar_objects::ObstaclesPtr obstacles_msg(new processing_lidar_objects::Obstacles);
obstacles_msg->header.stamp = stamp_;
if (p_transform_coordinates_) {
tf::StampedTransform transform;
geometry_msgs::TransformStamped geom_transform;
try {
tf_listener_.waitForTransform(p_frame_id_, base_frame_id_, stamp_, ros::Duration(0.1));
tf_listener_.lookupTransform(p_frame_id_, base_frame_id_, stamp_, transform);
tf::transformStampedTFToMsg(transform, geom_transform);
}
catch (tf::TransformException& ex) {
ROS_INFO_STREAM(ex.what());
return;
}
geometry_msgs::PointStamped geom_point;
geom_point.header = obstacles_msg->header;
for (Segment& s : segments_) {
geom_point.point.x = s.first_point.x;
geom_point.point.y = s.first_point.y;
tf2::doTransform(geom_point, geom_point, geom_transform);
s.first_point.x = geom_point.point.x;
s.first_point.y = geom_point.point.y;
geom_point.point.x = s.last_point.x;
geom_point.point.y = s.last_point.y;
tf2::doTransform(geom_point, geom_point, geom_transform);
s.last_point.x = geom_point.point.x;
s.last_point.y = geom_point.point.y;
}
for (Circle& c : circles_) {
geom_point.point.x = c.center.x;
geom_point.point.y = c.center.y;
tf2::doTransform(geom_point, geom_point, geom_transform);
c.center.x = geom_point.point.x;
c.center.y = geom_point.point.y;
}
obstacles_msg->header.frame_id = p_frame_id_;
}
else
obstacles_msg->header.frame_id = base_frame_id_;
for (const Segment& s : segments_) {
SegmentObstacle segment;
segment.first_point.x = s.first_point.x;
segment.first_point.y = s.first_point.y;
segment.last_point.x = s.last_point.x;
segment.last_point.y = s.last_point.y;
obstacles_msg->segments.push_back(segment);
}
for (const Circle& c : circles_) {
if (c.center.x > p_min_x_limit_ && c.center.x < p_max_x_limit_ &&
c.center.y > p_min_y_limit_ && c.center.y < p_max_y_limit_) {
CircleObstacle circle;
circle.center.x = c.center.x;
circle.center.y = c.center.y;
circle.velocity.x = 0.0;
circle.velocity.y = 0.0;
circle.radius = c.radius;
circle.true_radius = c.radius - p_radius_enlargement_;
obstacles_msg->circles.push_back(circle);
}
}
obstacles_pub_.publish(obstacles_msg);
}