Skip to content

Commit d63fd8d

Browse files
author
Tiago Silva
authored
cherry-picked melodic fix #200 (#204)
1 parent 6ff34ba commit d63fd8d

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

include/spatio_temporal_voxel_layer/spatio_temporal_voxel_grid.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
// STL
4444
#include <math.h>
4545
#include <unordered_map>
46+
#include <unordered_set>
4647
#include <ctime>
4748
#include <iostream>
4849
#include <utility>
@@ -135,7 +136,9 @@ class SpatioTemporalVoxelGrid
135136
// Core making and clearing functions
136137
void Mark(const std::vector<observation::MeasurementReading> & marking_observations);
137138
void operator()(const observation::MeasurementReading & obs) const;
138-
void ClearFrustums(const std::vector<observation::MeasurementReading> & clearing_observations);
139+
void ClearFrustums(
140+
const std::vector<observation::MeasurementReading> & clearing_observations,
141+
std::unordered_set<occupany_cell> & cleared_cells);
139142

140143
// Get the pointcloud of the underlying occupancy grid
141144
void GetOccupancyPointCloud(std::unique_ptr<sensor_msgs::msg::PointCloud2> & pc2);
@@ -162,7 +165,9 @@ class SpatioTemporalVoxelGrid
162165
double GetTemporalClearingDuration(const double & time_delta);
163166
double GetFrustumAcceleration(
164167
const double & time_delta, const double & acceleration_factor);
165-
void TemporalClearAndGenerateCostmap(std::vector<frustum_model> & frustums);
168+
void TemporalClearAndGenerateCostmap(
169+
std::vector<frustum_model> & frustums,
170+
std::unordered_set<occupany_cell> & cleared_cells);
166171

167172
// Populate the costmap ROS api and pointcloud with a marked point
168173
void PopulateCostmapAndPointcloud(const openvdb::Coord & pt);

include/spatio_temporal_voxel_layer/spatio_temporal_voxel_layer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class SpatioTemporalVoxelLayer : public nav2_costmap_2d::CostmapLayer
117117

118118
// Functions to interact with maps
119119
void UpdateROSCostmap(
120-
double * min_x, double * min_y, double * max_x, double * max_y);
120+
double * min_x, double * min_y, double * max_x, double * max_y,
121+
std::unordered_set<volume_grid::occupany_cell> & cleared_cells);
121122
bool updateFootprint(
122123
double robot_x, double robot_y, double robot_yaw,
123124
double * min_x, double * min_y, double * max_x, double * max_y);

src/spatio_temporal_voxel_grid.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ void SpatioTemporalVoxelGrid::InitializeGrid(void)
9494

9595
/*****************************************************************************/
9696
void SpatioTemporalVoxelGrid::ClearFrustums(
97-
const std::vector<observation::MeasurementReading> & clearing_readings)
97+
const std::vector<observation::MeasurementReading> & clearing_readings,
98+
std::unordered_set<occupany_cell> & cleared_cells)
9899
/*****************************************************************************/
99100
{
100101
boost::unique_lock<boost::mutex> lock(_grid_lock);
@@ -110,7 +111,7 @@ void SpatioTemporalVoxelGrid::ClearFrustums(
110111
std::vector<frustum_model> obs_frustums;
111112

112113
if (clearing_readings.size() == 0) {
113-
TemporalClearAndGenerateCostmap(obs_frustums);
114+
TemporalClearAndGenerateCostmap(obs_frustums, cleared_cells);
114115
return;
115116
}
116117

@@ -139,12 +140,13 @@ void SpatioTemporalVoxelGrid::ClearFrustums(
139140
frustum->TransformModel();
140141
obs_frustums.emplace_back(frustum, it->_decay_acceleration);
141142
}
142-
TemporalClearAndGenerateCostmap(obs_frustums);
143+
TemporalClearAndGenerateCostmap(obs_frustums, cleared_cells);
143144
}
144145

145146
/*****************************************************************************/
146147
void SpatioTemporalVoxelGrid::TemporalClearAndGenerateCostmap(
147-
std::vector<frustum_model> & frustums)
148+
std::vector<frustum_model> & frustums,
149+
std::unordered_set<occupany_cell> & cleared_cells)
148150
/*****************************************************************************/
149151
{
150152
// sample time once for all clearing readings
@@ -154,9 +156,11 @@ void SpatioTemporalVoxelGrid::TemporalClearAndGenerateCostmap(
154156
openvdb::DoubleGrid::ValueOnCIter cit_grid = _grid->cbeginValueOn();
155157
for (; cit_grid.test(); ++cit_grid) {
156158
const openvdb::Coord pt_index(cit_grid.getCoord());
159+
const openvdb::Vec3d pose_world = this->IndexToWorld(pt_index);
157160

158161
std::vector<frustum_model>::iterator frustum_it = frustums.begin();
159162
bool frustum_cycle = false;
163+
bool cleared_point = false;
160164

161165
const double time_since_marking = cur_time - cit_grid.getValue();
162166
const double base_duration_to_decay = GetTemporalClearingDuration(
@@ -167,7 +171,7 @@ void SpatioTemporalVoxelGrid::TemporalClearAndGenerateCostmap(
167171
continue;
168172
}
169173

170-
if (frustum_it->frustum->IsInside(this->IndexToWorld(pt_index)) ) {
174+
if (frustum_it->frustum->IsInside(pose_world) ) {
171175
frustum_cycle = true;
172176

173177
const double frustum_acceleration = GetFrustumAcceleration(
@@ -177,9 +181,11 @@ void SpatioTemporalVoxelGrid::TemporalClearAndGenerateCostmap(
177181
frustum_acceleration;
178182
if (time_until_decay < 0.) {
179183
// expired by acceleration
184+
cleared_point = true;
180185
if (!this->ClearGridPoint(pt_index)) {
181186
std::cout << "Failed to clear point." << std::endl;
182187
}
188+
break;
183189
} else {
184190
const double updated_mark = cit_grid.getValue() -
185191
frustum_acceleration;
@@ -195,14 +201,22 @@ void SpatioTemporalVoxelGrid::TemporalClearAndGenerateCostmap(
195201
if (!frustum_cycle) {
196202
if (base_duration_to_decay < 0.) {
197203
// expired by temporal clearing
204+
cleared_point = true;
198205
if (!this->ClearGridPoint(pt_index)) {
199206
std::cout << "Failed to clear point." << std::endl;
200207
}
201-
continue;
202208
}
203209
}
204-
// if here, we can add to costmap and PC2
205-
PopulateCostmapAndPointcloud(pt_index);
210+
211+
if (cleared_point)
212+
{
213+
cleared_cells.insert(occupany_cell(pose_world[0], pose_world[1]));
214+
}
215+
else
216+
{
217+
// if here, we can add to costmap and PC2
218+
PopulateCostmapAndPointcloud(pt_index);
219+
}
206220
}
207221
}
208222

src/spatio_temporal_voxel_layer.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,8 @@ void SpatioTemporalVoxelLayer::updateCosts(
670670

671671
/*****************************************************************************/
672672
void SpatioTemporalVoxelLayer::UpdateROSCostmap(
673-
double * min_x, double * min_y, double * max_x, double * max_y)
673+
double * min_x, double * min_y, double * max_x, double * max_y,
674+
std::unordered_set<volume_grid::occupany_cell> & cleared_cells)
674675
/*****************************************************************************/
675676
{
676677
// grabs map of occupied cells from grid and adds to costmap_
@@ -688,6 +689,12 @@ void SpatioTemporalVoxelLayer::UpdateROSCostmap(
688689
touch(it->first.x, it->first.y, min_x, min_y, max_x, max_y);
689690
}
690691
}
692+
693+
std::unordered_set<volume_grid::occupany_cell>::iterator cell;
694+
for (cell = cleared_cells.begin(); cell != cleared_cells.end(); ++cell)
695+
{
696+
touch(cell->x, cell->y, min_x, min_y, max_x, max_y);
697+
}
691698
}
692699

693700
/*****************************************************************************/
@@ -723,14 +730,16 @@ void SpatioTemporalVoxelLayer::updateBounds(
723730
ObservationsResetAfterReading();
724731
current_ = current;
725732

733+
std::unordered_set<volume_grid::occupany_cell> cleared_cells;
734+
726735
// navigation mode: clear observations, mapping mode: save maps and publish
727736
bool should_save = false;
728737
auto node = node_.lock();
729738
if (_map_save_duration) {
730739
should_save = node->now() - _last_map_save_time > *_map_save_duration;
731740
}
732741
if (!_mapping_mode) {
733-
_voxel_grid->ClearFrustums(clearing_observations);
742+
_voxel_grid->ClearFrustums(clearing_observations, cleared_cells);
734743
} else if (should_save) {
735744
_last_map_save_time = node->now();
736745
time_t rawtime;
@@ -752,7 +761,7 @@ void SpatioTemporalVoxelLayer::updateBounds(
752761
_voxel_grid->Mark(marking_observations);
753762

754763
// update the ROS Layered Costmap
755-
UpdateROSCostmap(min_x, min_y, max_x, max_y);
764+
UpdateROSCostmap(min_x, min_y, max_x, max_y, cleared_cells);
756765

757766
// publish point cloud in navigation mode
758767
if (_publish_voxels && !_mapping_mode) {

0 commit comments

Comments
 (0)