-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathclear_costmap_service.cpp
159 lines (131 loc) · 5.05 KB
/
clear_costmap_service.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
// 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 <string>
#include <algorithm>
#include <memory>
#include "nav2_costmap_2d/clear_costmap_service.hpp"
#include "nav2_costmap_2d/costmap_2d_ros.hpp"
namespace nav2_costmap_2d
{
using std::vector;
using std::string;
using std::shared_ptr;
using std::any_of;
using ClearExceptRegion = nav2_msgs::srv::ClearCostmapExceptRegion;
using ClearAroundRobot = nav2_msgs::srv::ClearCostmapAroundRobot;
using ClearEntirely = nav2_msgs::srv::ClearEntireCostmap;
ClearCostmapService::ClearCostmapService(
const nav2_util::LifecycleNode::WeakPtr & parent,
Costmap2DROS & costmap)
: costmap_(costmap)
{
auto node = parent.lock();
logger_ = node->get_logger();
reset_value_ = costmap_.getCostmap()->getDefaultValue();
clear_except_service_ = node->create_service<ClearExceptRegion>(
"clear_except_" + costmap_.getName(),
std::bind(
&ClearCostmapService::clearExceptRegionCallback, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
clear_around_service_ = node->create_service<ClearAroundRobot>(
"clear_around_" + costmap.getName(),
std::bind(
&ClearCostmapService::clearAroundRobotCallback, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
clear_entire_service_ = node->create_service<ClearEntirely>(
"clear_entirely_" + costmap_.getName(),
std::bind(
&ClearCostmapService::clearEntireCallback, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
}
void ClearCostmapService::clearExceptRegionCallback(
const shared_ptr<rmw_request_id_t>/*request_header*/,
const shared_ptr<ClearExceptRegion::Request> request,
const shared_ptr<ClearExceptRegion::Response>/*response*/)
{
RCLCPP_INFO(
logger_, "%s",
("Received request to clear except a region the " + costmap_.getName()).c_str());
clearRegion(request->reset_distance, true);
}
void ClearCostmapService::clearAroundRobotCallback(
const shared_ptr<rmw_request_id_t>/*request_header*/,
const shared_ptr<ClearAroundRobot::Request> request,
const shared_ptr<ClearAroundRobot::Response>/*response*/)
{
clearRegion(request->reset_distance, false);
}
void ClearCostmapService::clearEntireCallback(
const std::shared_ptr<rmw_request_id_t>/*request_header*/,
const std::shared_ptr<ClearEntirely::Request>/*request*/,
const std::shared_ptr<ClearEntirely::Response>/*response*/)
{
RCLCPP_INFO(
logger_, "%s",
("Received request to clear entirely the " + costmap_.getName()).c_str());
clearEntirely();
}
void ClearCostmapService::clearRegion(const double reset_distance, bool invert)
{
double x, y;
if (!getPosition(x, y)) {
RCLCPP_ERROR(
logger_, "%s",
"Cannot clear map because robot pose cannot be retrieved.");
return;
}
auto layers = costmap_.getLayeredCostmap()->getPlugins();
for (auto & layer : *layers) {
if (layer->isClearable()) {
auto costmap_layer = std::static_pointer_cast<CostmapLayer>(layer);
clearLayerRegion(costmap_layer, x, y, reset_distance, invert);
}
}
// AlexeyMerzlyakov: No need to clear layer region for costmap filters
// as they are always supposed to be not clearable.
}
void ClearCostmapService::clearLayerRegion(
shared_ptr<CostmapLayer> & costmap, double pose_x, double pose_y, double reset_distance,
bool invert)
{
std::unique_lock<Costmap2D::mutex_t> lock(*(costmap->getMutex()));
double start_point_x = pose_x - reset_distance / 2;
double start_point_y = pose_y - reset_distance / 2;
double end_point_x = start_point_x + reset_distance;
double end_point_y = start_point_y + reset_distance;
int start_x, start_y, end_x, end_y;
costmap->worldToMapEnforceBounds(start_point_x, start_point_y, start_x, start_y);
costmap->worldToMapEnforceBounds(end_point_x, end_point_y, end_x, end_y);
costmap->clearArea(start_x, start_y, end_x, end_y, invert);
double ox = costmap->getOriginX(), oy = costmap->getOriginY();
double width = costmap->getSizeInMetersX(), height = costmap->getSizeInMetersY();
costmap->addExtraBounds(ox, oy, ox + width, oy + height);
}
void ClearCostmapService::clearEntirely()
{
std::unique_lock<Costmap2D::mutex_t> lock(*(costmap_.getCostmap()->getMutex()));
costmap_.resetLayers();
}
bool ClearCostmapService::getPosition(double & x, double & y) const
{
geometry_msgs::msg::PoseStamped pose;
if (!costmap_.getRobotPose(pose)) {
return false;
}
x = pose.pose.position.x;
y = pose.pose.position.y;
return true;
}
} // namespace nav2_costmap_2d