Skip to content

Commit 7801267

Browse files
tonynajjarAngsa Deployment Team
andauthored
Stvl distance decay (#324)
* Fix sign Signed-off-by: Angsa Deployment Team <[email protected]> * Merge branch 'stvl-distance-clearer-take-2' into jazzy * Fix order Signed-off-by: Tony Najjar <[email protected]> --------- Signed-off-by: Angsa Deployment Team <[email protected]> Signed-off-by: Tony Najjar <[email protected]> Co-authored-by: Angsa Deployment Team <[email protected]>
1 parent 924ae24 commit 7801267

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

spatio_temporal_voxel_layer/include/spatio_temporal_voxel_layer/spatio_temporal_voxel_grid.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,16 @@ class SpatioTemporalVoxelGrid
130130
rclcpp::Clock::SharedPtr clock,
131131
const float & voxel_size, const double & background_value,
132132
const int & decay_model, const double & voxel_decay,
133-
const bool & pub_voxels);
133+
const double & voxel_distance_decay, const bool & pub_voxels);
134134
~SpatioTemporalVoxelGrid(void);
135135

136136
// Core making and clearing functions
137137
void Mark(const std::vector<observation::MeasurementReading> & marking_observations);
138138
void operator()(const observation::MeasurementReading & obs) const;
139139
void ClearFrustums(
140140
const std::vector<observation::MeasurementReading> & clearing_observations,
141-
std::unordered_set<occupany_cell> & cleared_cells);
141+
std::unordered_set<occupany_cell> & cleared_cells,
142+
openvdb::Vec3d & robot_pose_world);
142143

143144
// Get the pointcloud of the underlying occupancy grid
144145
void GetOccupancyPointCloud(std::unique_ptr<sensor_msgs::msg::PointCloud2> & pc2);
@@ -168,7 +169,8 @@ class SpatioTemporalVoxelGrid
168169
const double & time_delta, const double & acceleration_factor);
169170
void TemporalClearAndGenerateCostmap(
170171
std::vector<frustum_model> & frustums,
171-
std::unordered_set<occupany_cell> & cleared_cells);
172+
std::unordered_set<occupany_cell> & cleared_cells,
173+
openvdb::Vec3d & robot_pose_world);
172174

173175
// Populate the costmap ROS api and pointcloud with a marked point
174176
void PopulateCostmapAndPointcloud(const openvdb::Coord & pt);
@@ -181,7 +183,7 @@ class SpatioTemporalVoxelGrid
181183

182184
mutable openvdb::DoubleGrid::Ptr _grid;
183185
int _decay_model;
184-
double _background_value, _voxel_size, _voxel_decay;
186+
double _background_value, _voxel_size, _voxel_decay, _voxel_distance_decay;
185187
bool _pub_voxels;
186188
std::unique_ptr<std::vector<geometry_msgs::msg::Point32>> _grid_points;
187189
std::unordered_map<occupany_cell, uint> * _cost_map;

spatio_temporal_voxel_layer/include/spatio_temporal_voxel_layer/spatio_temporal_voxel_layer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class SpatioTemporalVoxelLayer : public nav2_costmap_2d::CostmapLayer
181181
std::unique_ptr<rclcpp::Duration> _map_save_duration;
182182
rclcpp::Time _last_map_save_time;
183183
std::string _global_frame;
184-
double _voxel_size, _voxel_decay;
184+
double _voxel_size, _voxel_decay, _voxel_distance_decay;
185185
int _combination_method, _mark_threshold;
186186
volume_grid::GlobalDecayModel _decay_model;
187187
bool _update_footprint_enabled, _enabled;

spatio_temporal_voxel_layer/src/spatio_temporal_voxel_grid.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ namespace volume_grid
4949
SpatioTemporalVoxelGrid::SpatioTemporalVoxelGrid(
5050
rclcpp::Clock::SharedPtr clock,
5151
const float & voxel_size, const double & background_value,
52-
const int & decay_model, const double & voxel_decay, const bool & pub_voxels)
52+
const int & decay_model, const double & voxel_decay, const double & voxel_distance_decay,
53+
const bool & pub_voxels)
5354
: _clock(clock), _decay_model(decay_model), _background_value(background_value),
54-
_voxel_size(voxel_size), _voxel_decay(voxel_decay), _pub_voxels(pub_voxels),
55+
_voxel_size(voxel_size), _voxel_decay(voxel_decay), _voxel_distance_decay(voxel_distance_decay),
56+
_pub_voxels(pub_voxels),
5557
_grid_points(std::make_unique<std::vector<geometry_msgs::msg::Point32>>()),
5658
_cost_map(new std::unordered_map<occupany_cell, uint>)
5759
/*****************************************************************************/
@@ -95,7 +97,7 @@ void SpatioTemporalVoxelGrid::InitializeGrid(void)
9597
/*****************************************************************************/
9698
void SpatioTemporalVoxelGrid::ClearFrustums(
9799
const std::vector<observation::MeasurementReading> & clearing_readings,
98-
std::unordered_set<occupany_cell> & cleared_cells)
100+
std::unordered_set<occupany_cell> & cleared_cells, openvdb::Vec3d & robot_pose_world)
99101
/*****************************************************************************/
100102
{
101103
boost::unique_lock<boost::mutex> lock(_grid_lock);
@@ -113,7 +115,7 @@ void SpatioTemporalVoxelGrid::ClearFrustums(
113115
std::vector<frustum_model> obs_frustums;
114116

115117
if (clearing_readings.size() == 0) {
116-
TemporalClearAndGenerateCostmap(obs_frustums, cleared_cells);
118+
TemporalClearAndGenerateCostmap(obs_frustums, cleared_cells, robot_pose_world);
117119
return;
118120
}
119121

@@ -142,20 +144,22 @@ void SpatioTemporalVoxelGrid::ClearFrustums(
142144
frustum->TransformModel();
143145
obs_frustums.emplace_back(frustum, it->_decay_acceleration);
144146
}
145-
TemporalClearAndGenerateCostmap(obs_frustums, cleared_cells);
147+
TemporalClearAndGenerateCostmap(obs_frustums, cleared_cells, robot_pose_world);
146148
}
147149

148150
/*****************************************************************************/
149151
void SpatioTemporalVoxelGrid::TemporalClearAndGenerateCostmap(
150152
std::vector<frustum_model> & frustums,
151-
std::unordered_set<occupany_cell> & cleared_cells)
153+
std::unordered_set<occupany_cell> & cleared_cells,
154+
openvdb::Vec3d & robot_pose_world)
152155
/*****************************************************************************/
153156
{
154157
// sample time once for all clearing readings
155158
const double cur_time = _clock->now().seconds();
156159

157160
// check each point in the grid for inclusion in a frustum
158161
openvdb::DoubleGrid::ValueOnCIter cit_grid = _grid->cbeginValueOn();
162+
double voxel_distance_decay_squared = _voxel_distance_decay * _voxel_distance_decay;
159163
for (; cit_grid.test(); ++cit_grid) {
160164
const openvdb::Coord pt_index(cit_grid.getCoord());
161165
const openvdb::Vec3d pose_world = this->IndexToWorld(pt_index);
@@ -164,6 +168,23 @@ void SpatioTemporalVoxelGrid::TemporalClearAndGenerateCostmap(
164168
bool frustum_cycle = false;
165169
bool cleared_point = false;
166170

171+
// spatial filtering
172+
if (_voxel_distance_decay > 0.0) {
173+
// check squared distance from robot to voxel
174+
double distance_2d_squared =
175+
(pose_world[0] - robot_pose_world[0]) * (pose_world[0] - robot_pose_world[0]) +
176+
(pose_world[1] - robot_pose_world[1]) * (pose_world[1] - robot_pose_world[1]);
177+
178+
if (distance_2d_squared > voxel_distance_decay_squared) {
179+
cleared_point = true;
180+
if (!this->ClearGridPoint(pt_index)) {
181+
std::cout << "Failed to clear point." << std::endl;
182+
}
183+
cleared_cells.insert(occupany_cell(pose_world[0], pose_world[1]));
184+
continue;
185+
}
186+
}
187+
167188
const double time_since_marking = cur_time - cit_grid.getValue();
168189
const double base_duration_to_decay = GetTemporalClearingDuration(
169190
time_since_marking);

spatio_temporal_voxel_layer/src/spatio_temporal_voxel_layer.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ void SpatioTemporalVoxelLayer::onInitialize(void)
119119
// decay param
120120
declareParameter("voxel_decay", rclcpp::ParameterValue(-1.0));
121121
node->get_parameter(name_ + ".voxel_decay", _voxel_decay);
122+
// distance decay param
123+
declareParameter("voxel_distance_decay", rclcpp::ParameterValue(-1.0));
124+
node->get_parameter(name_ + ".voxel_distance_decay", _voxel_distance_decay);
122125
// whether to map or navigate
123126
declareParameter("mapping_mode", rclcpp::ParameterValue(false));
124127
node->get_parameter(name_ + ".mapping_mode", _mapping_mode);
@@ -159,7 +162,7 @@ void SpatioTemporalVoxelLayer::onInitialize(void)
159162

160163
_voxel_grid = std::make_unique<volume_grid::SpatioTemporalVoxelGrid>(
161164
node->get_clock(), _voxel_size, static_cast<double>(default_value_), _decay_model,
162-
_voxel_decay, _publish_voxels);
165+
_voxel_decay, _voxel_distance_decay, _publish_voxels);
163166

164167
matchSize();
165168

@@ -777,7 +780,10 @@ void SpatioTemporalVoxelLayer::updateBounds(
777780
should_save = node->now() - _last_map_save_time > *_map_save_duration;
778781
}
779782
if (!_mapping_mode) {
780-
_voxel_grid->ClearFrustums(clearing_observations, cleared_cells);
783+
openvdb::Vec3d robot_pose_world;
784+
robot_pose_world[0] = robot_x;
785+
robot_pose_world[1] = robot_y;
786+
_voxel_grid->ClearFrustums(clearing_observations, cleared_cells, robot_pose_world);
781787
} else if (should_save) {
782788
_last_map_save_time = node->now();
783789
time_t rawtime;

0 commit comments

Comments
 (0)