This repository was archived by the owner on Nov 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathpointcloud_octomap_updater.cpp
355 lines (312 loc) · 13.8 KB
/
pointcloud_octomap_updater.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
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2011, Willow Garage, Inc.
* 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 Willow Garage 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: Jon Binney, Ioan Sucan */
#include <cmath>
#include <moveit/pointcloud_octomap_updater/pointcloud_octomap_updater.h>
#include <moveit/occupancy_map_monitor/occupancy_map_monitor.h>
#include <message_filters/subscriber.h>
#include <sensor_msgs/point_cloud2_iterator.h>
#include <XmlRpcException.h>
namespace occupancy_map_monitor
{
PointCloudOctomapUpdater::PointCloudOctomapUpdater() : OccupancyMapUpdater("PointCloudUpdater"),
private_nh_("~"),
scale_(1.0),
padding_(0.0),
obstacle_range_(std::numeric_limits<double>::infinity()),
raytrace_range_(std::numeric_limits<double>::infinity()),
point_subsample_(1),
point_cloud_subscriber_(NULL),
point_cloud_filter_(NULL)
{
}
PointCloudOctomapUpdater::~PointCloudOctomapUpdater()
{
stopHelper();
}
bool PointCloudOctomapUpdater::setParams(XmlRpc::XmlRpcValue ¶ms)
{
try
{
if (!params.hasMember("point_cloud_topic"))
return false;
point_cloud_topic_ = static_cast<const std::string&>(params["point_cloud_topic"]);
readXmlParam(params, "max_range", &obstacle_range_);
readXmlParam(params, "obstacle_range", &obstacle_range_);
raytrace_range_ = 1.1*obstacle_range_;
readXmlParam(params, "raytrace_range", &raytrace_range_);
readXmlParam(params, "padding_offset", &padding_);
readXmlParam(params, "padding_scale", &scale_);
readXmlParam(params, "point_subsample", &point_subsample_);
if (params.hasMember("filtered_cloud_topic"))
filtered_cloud_topic_ = static_cast<const std::string&>(params["filtered_cloud_topic"]);
}
catch (XmlRpc::XmlRpcException &ex)
{
ROS_ERROR("XmlRpc Exception: %s", ex.getMessage().c_str());
return false;
}
return true;
}
bool PointCloudOctomapUpdater::initialize()
{
tf_ = monitor_->getTFClient();
shape_mask_.reset(new point_containment_filter::ShapeMask());
shape_mask_->setTransformCallback(boost::bind(&PointCloudOctomapUpdater::getShapeTransform, this, _1, _2));
if (!filtered_cloud_topic_.empty())
filtered_cloud_publisher_ = private_nh_.advertise<sensor_msgs::PointCloud2>(filtered_cloud_topic_, 10, false);
return true;
}
void PointCloudOctomapUpdater::start()
{
if (point_cloud_subscriber_)
return;
/* subscribe to point cloud topic using tf filter*/
point_cloud_subscriber_ = new message_filters::Subscriber<sensor_msgs::PointCloud2>(root_nh_, point_cloud_topic_, 5);
if (tf_ && !monitor_->getMapFrame().empty())
{
point_cloud_filter_ = new tf::MessageFilter<sensor_msgs::PointCloud2>(*point_cloud_subscriber_, *tf_, monitor_->getMapFrame(), 5);
point_cloud_filter_->registerCallback(boost::bind(&PointCloudOctomapUpdater::cloudMsgCallback, this, _1));
ROS_INFO("Listening to '%s' using message filter with target frame '%s'", point_cloud_topic_.c_str(), point_cloud_filter_->getTargetFramesString().c_str());
}
else
{
point_cloud_subscriber_->registerCallback(boost::bind(&PointCloudOctomapUpdater::cloudMsgCallback, this, _1));
ROS_INFO("Listening to '%s'", point_cloud_topic_.c_str());
}
}
void PointCloudOctomapUpdater::stopHelper()
{
delete point_cloud_filter_;
delete point_cloud_subscriber_;
}
void PointCloudOctomapUpdater::stop()
{
stopHelper();
point_cloud_filter_ = NULL;
point_cloud_subscriber_ = NULL;
}
ShapeHandle PointCloudOctomapUpdater::excludeShape(const shapes::ShapeConstPtr &shape)
{
ShapeHandle h = 0;
if (shape_mask_)
h = shape_mask_->addShape(shape, scale_, padding_);
else
ROS_ERROR("Shape filter not yet initialized!");
return h;
}
void PointCloudOctomapUpdater::forgetShape(ShapeHandle handle)
{
if (shape_mask_)
shape_mask_->removeShape(handle);
}
bool PointCloudOctomapUpdater::getShapeTransform(ShapeHandle h, Eigen::Affine3d &transform) const
{
ShapeTransformCache::const_iterator it = transform_cache_.find(h);
if (it == transform_cache_.end())
{
ROS_ERROR("Internal error. Shape filter handle %u not found", h);
return false;
}
transform = it->second;
return true;
}
void PointCloudOctomapUpdater::updateMask(const sensor_msgs::PointCloud2 &cloud, const Eigen::Vector3d &sensor_origin, std::vector<int> &mask)
{
}
void PointCloudOctomapUpdater::cloudMsgCallback(const sensor_msgs::PointCloud2::ConstPtr &cloud_msg)
{
ROS_DEBUG("Received a new point cloud message");
ros::WallTime start = ros::WallTime::now();
if (monitor_->getMapFrame().empty())
monitor_->setMapFrame(cloud_msg->header.frame_id);
/* get transform for cloud into map frame */
tf::StampedTransform map_H_sensor;
if (monitor_->getMapFrame() == cloud_msg->header.frame_id)
map_H_sensor.setIdentity();
else
{
if (tf_)
{
try
{
tf_->lookupTransform(monitor_->getMapFrame(), cloud_msg->header.frame_id, cloud_msg->header.stamp, map_H_sensor);
}
catch (tf::TransformException& ex)
{
ROS_ERROR_STREAM("Transform error of sensor data: " << ex.what() << "; quitting callback");
return;
}
}
else
return;
}
/* compute sensor origin in map frame */
const tf::Vector3 &sensor_origin_tf = map_H_sensor.getOrigin();
octomap::point3d sensor_origin(sensor_origin_tf.getX(), sensor_origin_tf.getY(), sensor_origin_tf.getZ());
Eigen::Vector3d sensor_origin_eigen(sensor_origin_tf.getX(), sensor_origin_tf.getY(), sensor_origin_tf.getZ());
if (!updateTransformCache(cloud_msg->header.frame_id, cloud_msg->header.stamp))
{
ROS_ERROR_THROTTLE(1, "Transform cache was not updated. Self-filtering may fail.");
return;
}
/* mask out points on the robot */
shape_mask_->maskContainment(*cloud_msg, sensor_origin_eigen, 0.0, obstacle_range_, mask_);
updateMask(*cloud_msg, sensor_origin_eigen, mask_);
octomap::KeySet free_cells, occupied_cells, model_cells, clip_cells;
boost::scoped_ptr<sensor_msgs::PointCloud2> filtered_cloud;
//We only use these iterators if we are creating a filtered_cloud for
//publishing. We cannot default construct these, so we use scoped_ptr's
//to defer construction
boost::scoped_ptr<sensor_msgs::PointCloud2Iterator<float> > iter_filtered_x;
boost::scoped_ptr<sensor_msgs::PointCloud2Iterator<float> > iter_filtered_y;
boost::scoped_ptr<sensor_msgs::PointCloud2Iterator<float> > iter_filtered_z;
if (!filtered_cloud_topic_.empty()) {
filtered_cloud.reset(new sensor_msgs::PointCloud2());
filtered_cloud->header = cloud_msg->header;
sensor_msgs::PointCloud2Modifier pcd_modifier(*filtered_cloud);
pcd_modifier.setPointCloud2FieldsByString(1, "xyz");
pcd_modifier.resize(cloud_msg->width * cloud_msg->height);
//we have created a filtered_out, so we can create the iterators now
iter_filtered_x.reset(new sensor_msgs::PointCloud2Iterator<float>(*filtered_cloud, "x"));
iter_filtered_y.reset(new sensor_msgs::PointCloud2Iterator<float>(*filtered_cloud, "y"));
iter_filtered_z.reset(new sensor_msgs::PointCloud2Iterator<float>(*filtered_cloud, "z"));
}
size_t filtered_cloud_size = 0;
tree_->lockRead();
try
{
/* do ray tracing to find which cells this point cloud indicates should be free, and which it indicates
* should be occupied */
double raytrace_range_2 = raytrace_range_ * raytrace_range_;
for (unsigned int row = 0; row < cloud_msg->height; row += point_subsample_)
{
unsigned int row_c = row * cloud_msg->width;
sensor_msgs::PointCloud2ConstIterator<float> pt_iter(*cloud_msg, "x");
//set iterator to point at start of the current row
pt_iter += row_c;
for (unsigned int col = 0; col < cloud_msg->width; col += point_subsample_,
pt_iter += point_subsample_)
{
//if (mask_[row_c + col] == point_containment_filter::ShapeMask::CLIP)
// continue;
/* check for NaN */
if (!isnan(pt_iter[0]) && !isnan(pt_iter[1]) && !isnan(pt_iter[2]))
{
/* transform to map frame */
tf::Vector3 point_tf = map_H_sensor * tf::Vector3(pt_iter[0], pt_iter[1],
pt_iter[2]);
/* occupied cell at ray endpoint if ray is shorter than max range and this point
isn't on a part of the robot*/
if (mask_[row_c + col] == point_containment_filter::ShapeMask::INSIDE)
model_cells.insert(tree_->coordToKey(point_tf.getX(), point_tf.getY(), point_tf.getZ()));
else if (mask_[row_c + col] == point_containment_filter::ShapeMask::CLIP)
{
octomap::point3d clip_end(point_tf.getX(), point_tf.getY(), point_tf.getZ());
if (point_tf.length2() > raytrace_range_2)
clip_end = sensor_origin + (clip_end - sensor_origin).normalized() * (float)raytrace_range_;
clip_cells.insert(tree_->coordToKey(clip_end));
}
else
{
occupied_cells.insert(tree_->coordToKey(point_tf.getX(), point_tf.getY(), point_tf.getZ()));
//build list of valid points if we want to publish them
if (filtered_cloud)
{
**iter_filtered_x = pt_iter[0];
**iter_filtered_y = pt_iter[1];
**iter_filtered_z = pt_iter[2];
++filtered_cloud_size;
++*iter_filtered_x;
++*iter_filtered_y;
++*iter_filtered_z;
}
}
}
}
}
/* compute the free cells along each ray that ends at an occupied cell */
for (octomap::KeySet::iterator it = occupied_cells.begin(), end = occupied_cells.end(); it != end; ++it)
if (tree_->computeRayKeys(sensor_origin, tree_->keyToCoord(*it), key_ray_))
free_cells.insert(key_ray_.begin(), key_ray_.end());
/* compute the free cells along each ray that ends at a model cell */
for (octomap::KeySet::iterator it = model_cells.begin(), end = model_cells.end(); it != end; ++it)
if (tree_->computeRayKeys(sensor_origin, tree_->keyToCoord(*it), key_ray_))
free_cells.insert(key_ray_.begin(), key_ray_.end());
/* compute the free cells along each ray that ends at a clipped cell */
for (octomap::KeySet::iterator it = clip_cells.begin(), end = clip_cells.end(); it != end; ++it)
if (tree_->computeRayKeys(sensor_origin, tree_->keyToCoord(*it), key_ray_))
free_cells.insert(key_ray_.begin(), key_ray_.end());
}
catch (...)
{
tree_->unlockRead();
return;
}
tree_->unlockRead();
/* cells that overlap with the model are not occupied */
for (octomap::KeySet::iterator it = model_cells.begin(), end = model_cells.end(); it != end; ++it)
occupied_cells.erase(*it);
/* occupied cells are not free */
for (octomap::KeySet::iterator it = occupied_cells.begin(), end = occupied_cells.end(); it != end; ++it)
free_cells.erase(*it);
tree_->lockWrite();
try
{
/* mark free cells only if not seen occupied in this cloud */
for (octomap::KeySet::iterator it = free_cells.begin(), end = free_cells.end(); it != end; ++it)
tree_->updateNode(*it, false);
/* now mark all occupied cells */
for (octomap::KeySet::iterator it = occupied_cells.begin(), end = occupied_cells.end(); it != end; ++it)
tree_->updateNode(*it, true);
// set the logodds to the minimum for the cells that are part of the model
const float lg = tree_->getClampingThresMinLog() - tree_->getClampingThresMaxLog();
for (octomap::KeySet::iterator it = model_cells.begin(), end = model_cells.end(); it != end; ++it)
tree_->updateNode(*it, lg);
}
catch (...)
{
ROS_ERROR("Internal error while updating octree");
}
tree_->unlockWrite();
ROS_DEBUG("Processed point cloud in %lf ms", (ros::WallTime::now() - start).toSec() * 1000.0);
tree_->triggerUpdateCallback();
if (filtered_cloud)
{
sensor_msgs::PointCloud2Modifier pcd_modifier(*filtered_cloud);
pcd_modifier.resize(filtered_cloud_size);
filtered_cloud_publisher_.publish(*filtered_cloud);
}
}
}