forked from ros-navigation/navigation2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcostmap.cpp
266 lines (227 loc) · 8.73 KB
/
costmap.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
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <vector>
#include <algorithm>
#include "nav2_util/costmap.hpp"
#include "tf2/LinearMath/Quaternion.h"
#include "nav2_util/geometry_utils.hpp"
using std::vector;
namespace nav2_util
{
using nav2_util::geometry_utils::orientationAroundZAxis;
const Costmap::CostValue Costmap::no_information = 255;
const Costmap::CostValue Costmap::lethal_obstacle = 254;
const Costmap::CostValue Costmap::inscribed_inflated_obstacle = 253;
const Costmap::CostValue Costmap::medium_cost = 128;
const Costmap::CostValue Costmap::free_space = 0;
// TODO(orduno): Port ROS1 Costmap package
Costmap::Costmap(
rclcpp::Node * node, bool trinary_costmap, bool track_unknown_space,
int lethal_threshold, int unknown_cost_value)
: node_(node), trinary_costmap_(trinary_costmap), track_unknown_space_(track_unknown_space),
lethal_threshold_(lethal_threshold), unknown_cost_value_(unknown_cost_value)
{
if (lethal_threshold_ < 0. || lethal_threshold_ > 100.) {
RCLCPP_WARN(
node_->get_logger(), "Costmap: Lethal threshold set to %d, it should be within"
" bounds 0-100. This could result in potential collisions!", lethal_threshold_);
// lethal_threshold_ = std::max(std::min(lethal_threshold_, 100), 0);
}
}
Costmap::~Costmap()
{
}
void Costmap::set_static_map(const nav_msgs::msg::OccupancyGrid & occupancy_grid)
{
RCLCPP_INFO(node_->get_logger(), "Costmap: Setting static costmap");
costmap_properties_.map_load_time = node_->now();
costmap_properties_.update_time = node_->now();
costmap_properties_.layer = "Master";
// Store the properties of the occupancy grid
costmap_properties_.resolution = occupancy_grid.info.resolution;
costmap_properties_.size_x = occupancy_grid.info.width;
costmap_properties_.size_y = occupancy_grid.info.height;
costmap_properties_.origin = occupancy_grid.info.origin;
uint32_t size_x = costmap_properties_.size_x;
uint32_t size_y = costmap_properties_.size_y;
costs_.resize(size_x * size_y);
// TODO(orduno): for now just doing a direct mapping of values from the original static map
// i.e. no cell inflation, etc.
std::vector<int8_t> static_map_cell_values = occupancy_grid.data;
unsigned int index = 0;
for (unsigned int i = 0; i < size_y; ++i) {
for (unsigned int j = 0; j < size_x; ++j) {
unsigned char value = static_map_cell_values[index];
costs_[index] = interpret_value(value);
++index;
}
}
map_provided_ = true;
}
void Costmap::set_test_costmap(const TestCostmap & testCostmapType)
{
costmap_properties_.map_load_time = node_->now();
costmap_properties_.update_time = node_->now();
costmap_properties_.layer = "master";
costmap_properties_.resolution = 1;
costmap_properties_.size_x = 10;
costmap_properties_.size_y = 10;
costmap_properties_.origin.position.x = 0.0;
costmap_properties_.origin.position.y = 0.0;
costmap_properties_.origin.position.z = 0.0;
// Define map rotation
// Provided as yaw with counterclockwise rotation, with yaw = 0 meaning no rotation
costmap_properties_.origin.orientation = orientationAroundZAxis(0.0);
costs_ = get_test_data(testCostmapType);
using_test_map_ = true;
}
nav2_msgs::msg::Costmap Costmap::get_costmap(
const nav2_msgs::msg::CostmapMetaData & /*specifications*/)
{
if (!map_provided_ && !using_test_map_) {
throw std::runtime_error("Costmap has not been set.");
}
// TODO(orduno): build a costmap given the specifications
// for now using the specs of the static map
nav2_msgs::msg::Costmap costmap;
costmap.header.stamp = node_->now();
costmap.header.frame_id = "map";
costmap.metadata = costmap_properties_;
costmap.data = costs_;
return costmap;
}
vector<uint8_t> Costmap::get_test_data(const TestCostmap testCostmapType)
{
// TODO(orduno): alternatively use a mathematical function
const uint8_t n = no_information;
const uint8_t x = lethal_obstacle;
const uint8_t i = inscribed_inflated_obstacle;
const uint8_t u = medium_cost;
const uint8_t o = free_space;
vector<uint8_t> costmapFree =
// 0 1 2 3 4 5 6 7 8 9
{o, o, o, o, o, o, o, o, o, o, // 0
o, o, o, o, o, o, o, o, o, o, // 1
o, o, o, o, o, o, o, o, o, o, // 2
o, o, o, o, o, o, o, o, o, o, // 3
o, o, o, o, o, o, o, o, o, o, // 4
o, o, o, o, o, o, o, o, o, o, // 5
o, o, o, o, o, o, o, o, o, o, // 6
o, o, o, o, o, o, o, o, o, o, // 7
o, o, o, o, o, o, o, o, o, o, // 8
o, o, o, o, o, o, o, o, o, o}; // 9
vector<uint8_t> costmapBounded =
// 0 1 2 3 4 5 6 7 8 9
{n, n, n, n, n, n, n, n, n, n, // 0
n, o, o, o, o, o, o, o, o, n, // 1
n, o, o, o, o, o, o, o, o, n, // 2
n, o, o, o, o, o, o, o, o, n, // 3
n, o, o, o, o, o, o, o, o, n, // 4
n, o, o, o, o, o, o, o, o, n, // 5
n, o, o, o, o, o, o, o, o, n, // 6
n, o, o, o, o, o, o, o, o, n, // 7
n, o, o, o, o, o, o, o, o, n, // 8
n, n, n, n, n, n, n, n, n, n}; // 9
vector<uint8_t> costmapObstacleBL =
// 0 1 2 3 4 5 6 7 8 9
{n, n, n, n, n, n, n, n, n, n, // 0
n, o, o, o, o, o, o, o, o, n, // 1
n, o, o, o, o, o, o, o, o, n, // 2
n, o, o, o, o, o, o, o, o, n, // 3
n, o, o, o, o, o, o, o, o, n, // 4
n, o, x, x, x, o, o, o, o, n, // 5
n, o, x, x, x, o, o, o, o, n, // 6
n, o, x, x, x, o, o, o, o, n, // 7
n, o, o, o, o, o, o, o, o, n, // 8
n, n, n, n, n, n, n, n, n, n}; // 9
vector<uint8_t> costmapObstacleTL =
// 0 1 2 3 4 5 6 7 8 9
{n, n, n, n, n, n, n, n, n, n, // 0
n, o, o, o, o, o, o, o, o, n, // 1
n, o, x, x, x, o, o, o, o, n, // 2
n, o, x, x, x, o, o, o, o, n, // 3
n, o, x, x, x, o, o, o, o, n, // 4
n, o, o, o, o, o, o, o, o, n, // 5
n, o, o, o, o, o, o, o, o, n, // 6
n, o, o, o, o, o, o, o, o, n, // 7
n, o, o, o, o, o, o, o, o, n, // 8
n, n, n, n, n, n, n, n, n, n}; // 9
vector<uint8_t> costmapMaze =
// 0 1 2 3 4 5 6 7 8 9
{n, n, n, n, n, n, n, n, n, n, // 0
n, o, o, o, o, o, o, o, o, n, // 1
n, x, x, o, x, x, x, o, x, n, // 2
n, o, o, o, o, x, o, o, o, n, // 3
n, o, x, x, o, x, o, x, o, n, // 4
n, o, x, x, o, x, o, x, o, n, // 5
n, o, o, x, o, x, o, x, o, n, // 6
n, x, o, x, o, x, o, x, o, n, // 7
n, o, o, o, o, o, o, x, o, n, // 8
n, n, n, n, n, n, n, n, n, n}; // 9
vector<uint8_t> costmapMaze2 =
// 0 1 2 3 4 5 6 7 8 9
{n, n, n, n, n, n, n, n, n, n, // 0
n, o, o, o, o, o, o, o, o, n, // 1
n, x, x, u, x, x, x, o, x, n, // 2
n, o, o, o, o, o, o, o, u, n, // 3
n, o, x, x, o, x, x, x, u, n, // 4
n, o, x, x, o, o, o, x, u, n, // 5
n, o, o, x, u, x, o, x, u, n, // 6
n, x, o, x, u, x, i, x, u, n, // 7
n, o, o, o, o, o, o, o, o, n, // 8
n, n, n, n, n, n, n, n, n, n}; // 9
switch (testCostmapType) {
case TestCostmap::open_space:
return costmapFree;
case TestCostmap::bounded:
return costmapBounded;
case TestCostmap::bottom_left_obstacle:
return costmapObstacleBL;
case TestCostmap::top_left_obstacle:
return costmapObstacleTL;
case TestCostmap::maze1:
return costmapMaze;
case TestCostmap::maze2:
return costmapMaze2;
default:
return costmapFree;
}
}
uint8_t Costmap::interpret_value(const int8_t value) const
{
if (track_unknown_space_ && value == unknown_cost_value_) {
return no_information;
} else if (!track_unknown_space_ && value == unknown_cost_value_) {
return free_space;
} else if (value >= lethal_threshold_) {
return lethal_obstacle;
} else if (trinary_costmap_) {
return free_space;
}
double scale = static_cast<double>(value / lethal_threshold_);
return static_cast<uint8_t>(scale * lethal_obstacle);
}
bool Costmap::is_free(const unsigned int x_coordinate, const unsigned int y_coordinate) const
{
unsigned int index = y_coordinate * costmap_properties_.size_x + x_coordinate;
return is_free(index);
}
bool Costmap::is_free(const unsigned int index) const
{
if (costs_[index] < Costmap::inscribed_inflated_obstacle) {
return true;
}
return false;
}
} // namespace nav2_util