-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
theta_star_planner.cpp
273 lines (237 loc) · 10.3 KB
/
theta_star_planner.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
// Copyright 2020 Anshumaan Singh
//
// 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 <memory>
#include <string>
#include "geometry_msgs/msg/pose_stamped.hpp"
#include "rclcpp/rclcpp.hpp"
#include "nav2_theta_star_planner/theta_star_planner.hpp"
#include "nav2_theta_star_planner/theta_star.hpp"
namespace nav2_theta_star_planner
{
void ThetaStarPlanner::configure(
const rclcpp_lifecycle::LifecycleNode::WeakPtr & parent,
std::string name, std::shared_ptr<tf2_ros::Buffer> tf,
std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros)
{
planner_ = std::make_unique<theta_star::ThetaStar>();
parent_node_ = parent;
auto node = parent_node_.lock();
logger_ = node->get_logger();
clock_ = node->get_clock();
name_ = name;
tf_ = tf;
planner_->costmap_ = costmap_ros->getCostmap();
global_frame_ = costmap_ros->getGlobalFrameID();
nav2_util::declare_parameter_if_not_declared(
node, name_ + ".how_many_corners", rclcpp::ParameterValue(8));
node->get_parameter(name_ + ".how_many_corners", planner_->how_many_corners_);
if (planner_->how_many_corners_ != 8 && planner_->how_many_corners_ != 4) {
planner_->how_many_corners_ = 8;
RCLCPP_WARN(logger_, "Your value for - .how_many_corners was overridden, and is now set to 8");
}
nav2_util::declare_parameter_if_not_declared(
node, name_ + ".allow_unknown", rclcpp::ParameterValue(true));
node->get_parameter(name_ + ".allow_unknown", planner_->allow_unknown_);
nav2_util::declare_parameter_if_not_declared(
node, name_ + ".w_euc_cost", rclcpp::ParameterValue(1.0));
node->get_parameter(name_ + ".w_euc_cost", planner_->w_euc_cost_);
nav2_util::declare_parameter_if_not_declared(
node, name_ + ".w_traversal_cost", rclcpp::ParameterValue(2.0));
node->get_parameter(name_ + ".w_traversal_cost", planner_->w_traversal_cost_);
planner_->w_heuristic_cost_ = planner_->w_euc_cost_ < 1.0 ? planner_->w_euc_cost_ : 1.0;
nav2_util::declare_parameter_if_not_declared(
node, name_ + ".terminal_checking_interval", rclcpp::ParameterValue(5000));
node->get_parameter(name_ + ".terminal_checking_interval", planner_->terminal_checking_interval_);
nav2_util::declare_parameter_if_not_declared(
node, name + ".use_final_approach_orientation", rclcpp::ParameterValue(false));
node->get_parameter(name + ".use_final_approach_orientation", use_final_approach_orientation_);
}
void ThetaStarPlanner::cleanup()
{
RCLCPP_INFO(logger_, "CleaningUp plugin %s of type nav2_theta_star_planner", name_.c_str());
planner_.reset();
}
void ThetaStarPlanner::activate()
{
RCLCPP_INFO(logger_, "Activating plugin %s of type nav2_theta_star_planner", name_.c_str());
// Add callback for dynamic parameters
auto node = parent_node_.lock();
dyn_params_handler_ = node->add_on_set_parameters_callback(
std::bind(&ThetaStarPlanner::dynamicParametersCallback, this, std::placeholders::_1));
}
void ThetaStarPlanner::deactivate()
{
RCLCPP_INFO(logger_, "Deactivating plugin %s of type nav2_theta_star_planner", name_.c_str());
auto node = parent_node_.lock();
if (node && dyn_params_handler_) {
node->remove_on_set_parameters_callback(dyn_params_handler_.get());
}
dyn_params_handler_.reset();
}
nav_msgs::msg::Path ThetaStarPlanner::createPlan(
const geometry_msgs::msg::PoseStamped & start,
const geometry_msgs::msg::PoseStamped & goal,
std::function<bool()> cancel_checker)
{
nav_msgs::msg::Path global_path;
auto start_time = std::chrono::steady_clock::now();
std::unique_lock<nav2_costmap_2d::Costmap2D::mutex_t> lock(*(planner_->costmap_->getMutex()));
// Corner case of start and goal beeing on the same cell
unsigned int mx_start, my_start, mx_goal, my_goal;
if (!planner_->costmap_->worldToMap(
start.pose.position.x, start.pose.position.y, mx_start, my_start))
{
throw nav2_core::StartOutsideMapBounds(
"Start Coordinates of(" + std::to_string(start.pose.position.x) + ", " +
std::to_string(start.pose.position.y) + ") was outside bounds");
}
if (!planner_->costmap_->worldToMap(
goal.pose.position.x, goal.pose.position.y, mx_goal, my_goal))
{
throw nav2_core::GoalOutsideMapBounds(
"Goal Coordinates of(" + std::to_string(goal.pose.position.x) + ", " +
std::to_string(goal.pose.position.y) + ") was outside bounds");
}
if (planner_->costmap_->getCost(mx_goal, my_goal) == nav2_costmap_2d::LETHAL_OBSTACLE) {
throw nav2_core::GoalOccupied(
"Goal Coordinates of(" + std::to_string(goal.pose.position.x) + ", " +
std::to_string(goal.pose.position.y) + ") was in lethal cost");
}
if (mx_start == mx_goal && my_start == my_goal) {
global_path.header.stamp = clock_->now();
global_path.header.frame_id = global_frame_;
geometry_msgs::msg::PoseStamped pose;
pose.header = global_path.header;
pose.pose.position.z = 0.0;
pose.pose = start.pose;
// if we have a different start and goal orientation, set the unique path pose to the goal
// orientation, unless use_final_approach_orientation=true where we need it to be the start
// orientation to avoid movement from the local planner
if (start.pose.orientation != goal.pose.orientation && !use_final_approach_orientation_) {
pose.pose.orientation = goal.pose.orientation;
}
global_path.poses.push_back(pose);
return global_path;
}
planner_->clearStart();
planner_->setStartAndGoal(start, goal);
RCLCPP_DEBUG(
logger_, "Got the src and dst... (%i, %i) && (%i, %i)",
planner_->src_.x, planner_->src_.y, planner_->dst_.x, planner_->dst_.y);
getPlan(global_path, cancel_checker);
// check if a plan is generated
size_t plan_size = global_path.poses.size();
if (plan_size > 0) {
global_path.poses.back().pose.orientation = goal.pose.orientation;
}
// If use_final_approach_orientation=true, interpolate the last pose orientation from the
// previous pose to set the orientation to the 'final approach' orientation of the robot so
// it does not rotate.
// And deal with corner case of plan of length 1
if (use_final_approach_orientation_) {
if (plan_size == 1) {
global_path.poses.back().pose.orientation = start.pose.orientation;
} else if (plan_size > 1) {
double dx, dy, theta;
auto last_pose = global_path.poses.back().pose.position;
auto approach_pose = global_path.poses[plan_size - 2].pose.position;
dx = last_pose.x - approach_pose.x;
dy = last_pose.y - approach_pose.y;
theta = atan2(dy, dx);
global_path.poses.back().pose.orientation =
nav2_util::geometry_utils::orientationAroundZAxis(theta);
}
}
auto stop_time = std::chrono::steady_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::microseconds>(stop_time - start_time);
RCLCPP_DEBUG(logger_, "the time taken is : %i", static_cast<int>(dur.count()));
RCLCPP_DEBUG(logger_, "the nodes_opened are: %i", planner_->nodes_opened);
return global_path;
}
void ThetaStarPlanner::getPlan(
nav_msgs::msg::Path & global_path,
std::function<bool()> cancel_checker)
{
std::vector<coordsW> path;
if (planner_->isUnsafeToPlan()) {
global_path.poses.clear();
throw nav2_core::PlannerException("Either of the start or goal pose are an obstacle! ");
} else if (planner_->generatePath(path, cancel_checker)) {
global_path = linearInterpolation(path, planner_->costmap_->getResolution());
} else {
global_path.poses.clear();
throw nav2_core::NoValidPathCouldBeFound("Could not generate path between the given poses");
}
global_path.header.stamp = clock_->now();
global_path.header.frame_id = global_frame_;
}
nav_msgs::msg::Path ThetaStarPlanner::linearInterpolation(
const std::vector<coordsW> & raw_path,
const double & dist_bw_points)
{
nav_msgs::msg::Path pa;
geometry_msgs::msg::PoseStamped p1;
for (unsigned int j = 0; j < raw_path.size() - 1; j++) {
coordsW pt1 = raw_path[j];
p1.pose.position.x = pt1.x;
p1.pose.position.y = pt1.y;
pa.poses.push_back(p1);
coordsW pt2 = raw_path[j + 1];
double distance = std::hypot(pt2.x - pt1.x, pt2.y - pt1.y);
int loops = static_cast<int>(distance / dist_bw_points);
double sin_alpha = (pt2.y - pt1.y) / distance;
double cos_alpha = (pt2.x - pt1.x) / distance;
for (int k = 1; k < loops; k++) {
p1.pose.position.x = pt1.x + k * dist_bw_points * cos_alpha;
p1.pose.position.y = pt1.y + k * dist_bw_points * sin_alpha;
pa.poses.push_back(p1);
}
}
return pa;
}
rcl_interfaces::msg::SetParametersResult
ThetaStarPlanner::dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters)
{
rcl_interfaces::msg::SetParametersResult result;
for (auto parameter : parameters) {
const auto & type = parameter.get_type();
const auto & name = parameter.get_name();
if (type == ParameterType::PARAMETER_INTEGER) {
if (name == name_ + ".how_many_corners") {
planner_->how_many_corners_ = parameter.as_int();
}
if (name == name_ + ".terminal_checking_interval") {
planner_->terminal_checking_interval_ = parameter.as_int();
}
} else if (type == ParameterType::PARAMETER_DOUBLE) {
if (name == name_ + ".w_euc_cost") {
planner_->w_euc_cost_ = parameter.as_double();
} else if (name == name_ + ".w_traversal_cost") {
planner_->w_traversal_cost_ = parameter.as_double();
}
} else if (type == ParameterType::PARAMETER_BOOL) {
if (name == name_ + ".use_final_approach_orientation") {
use_final_approach_orientation_ = parameter.as_bool();
} else if (name == name_ + ".allow_unknown") {
planner_->allow_unknown_ = parameter.as_bool();
}
}
}
result.successful = true;
return result;
}
} // namespace nav2_theta_star_planner
#include "pluginlib/class_list_macros.hpp"
PLUGINLIB_EXPORT_CLASS(nav2_theta_star_planner::ThetaStarPlanner, nav2_core::GlobalPlanner)