16 #ifndef NAV2_SMAC_PLANNER__UTILS_HPP_
17 #define NAV2_SMAC_PLANNER__UTILS_HPP_
23 #include "nlohmann/json.hpp"
25 #include "geometry_msgs/msg/quaternion.hpp"
26 #include "geometry_msgs/msg/pose.hpp"
27 #include "tf2/utils.h"
28 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
29 #include "nav2_costmap_2d/inflation_layer.hpp"
30 #include "visualization_msgs/msg/marker_array.hpp"
31 #include "nav2_smac_planner/types.hpp"
32 #include <rclcpp/rclcpp.hpp>
34 namespace nav2_smac_planner
44 inline geometry_msgs::msg::Pose getWorldCoords(
47 geometry_msgs::msg::Pose msg;
60 inline geometry_msgs::msg::Quaternion getWorldOrientation(
65 q.setEuler(0.0, 0.0, theta);
76 inline double findCircumscribedCost(std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap)
79 std::vector<std::shared_ptr<nav2_costmap_2d::Layer>>::iterator layer;
82 const auto inflation_layer = nav2_costmap_2d::InflationLayer::getInflationLayer(costmap);
83 if (inflation_layer !=
nullptr) {
84 double circum_radius = costmap->getLayeredCostmap()->getCircumscribedRadius();
85 double resolution = costmap->getCostmap()->getResolution();
86 double inflation_radius = inflation_layer->getInflationRadius();
87 if (inflation_radius < circum_radius) {
89 rclcpp::get_logger(
"computeCircumscribedCost"),
90 "The inflation radius (%f) is smaller than the circumscribed radius (%f) "
91 "If this is an SE2-collision checking plugin, it cannot use costmap potential "
92 "field to speed up collision checking by only checking the full footprint "
93 "when robot is within possibly-inscribed radius of an obstacle. This may "
94 "significantly slow down planning times!",
95 inflation_radius, circum_radius);
99 result =
static_cast<double>(inflation_layer->computeCost(circum_radius / resolution));
102 rclcpp::get_logger(
"computeCircumscribedCost"),
103 "No inflation layer found in costmap configuration. "
104 "If this is an SE2-collision checking plugin, it cannot use costmap potential "
105 "field to speed up collision checking by only checking the full footprint "
106 "when robot is within possibly-inscribed radius of an obstacle. This may "
107 "significantly slow down planning times!");
118 inline void fromJsonToMetaData(
const nlohmann::json & json, LatticeMetadata & lattice_metadata)
120 json.at(
"turning_radius").get_to(lattice_metadata.min_turning_radius);
121 json.at(
"grid_resolution").get_to(lattice_metadata.grid_resolution);
122 json.at(
"num_of_headings").get_to(lattice_metadata.number_of_headings);
123 json.at(
"heading_angles").get_to(lattice_metadata.heading_angles);
124 json.at(
"number_of_trajectories").get_to(lattice_metadata.number_of_trajectories);
125 json.at(
"motion_model").get_to(lattice_metadata.motion_model);
133 inline void fromJsonToPose(
const nlohmann::json & json, MotionPose & pose)
137 pose._theta = json[2];
145 inline void fromJsonToMotionPrimitive(
146 const nlohmann::json & json, MotionPrimitive & motion_primitive)
148 json.at(
"trajectory_id").get_to(motion_primitive.trajectory_id);
149 json.at(
"start_angle_index").get_to(motion_primitive.start_angle);
150 json.at(
"end_angle_index").get_to(motion_primitive.end_angle);
151 json.at(
"trajectory_radius").get_to(motion_primitive.turning_radius);
152 json.at(
"trajectory_length").get_to(motion_primitive.trajectory_length);
153 json.at(
"arc_length").get_to(motion_primitive.arc_length);
154 json.at(
"straight_length").get_to(motion_primitive.straight_length);
155 json.at(
"left_turn").get_to(motion_primitive.left_turn);
157 for (
unsigned int i = 0; i < json[
"poses"].size(); i++) {
159 fromJsonToPose(json[
"poses"][i], pose);
160 motion_primitive.poses.push_back(pose);
169 inline std::vector<geometry_msgs::msg::Point> transformFootprintToEdges(
170 const geometry_msgs::msg::Pose & pose,
171 const std::vector<geometry_msgs::msg::Point> & footprint)
173 const double & x = pose.position.x;
174 const double & y = pose.position.y;
175 const double & yaw = tf2::getYaw(pose.orientation);
176 const double sin_yaw = sin(yaw);
177 const double cos_yaw = cos(yaw);
179 std::vector<geometry_msgs::msg::Point> out_footprint;
180 out_footprint.resize(2 * footprint.size());
181 for (
unsigned int i = 0; i < footprint.size(); i++) {
182 out_footprint[2 * i].x = x + cos_yaw * footprint[i].x - sin_yaw * footprint[i].y;
183 out_footprint[2 * i].y = y + sin_yaw * footprint[i].x + cos_yaw * footprint[i].y;
185 out_footprint.back().x = out_footprint[i].x;
186 out_footprint.back().y = out_footprint[i].y;
188 out_footprint[2 * i - 1].x = out_footprint[2 * i].x;
189 out_footprint[2 * i - 1].y = out_footprint[2 * i].y;
192 return out_footprint;
203 inline visualization_msgs::msg::Marker createMarker(
204 const std::vector<geometry_msgs::msg::Point> edge,
205 unsigned int i,
const std::string & frame_id,
const rclcpp::Time & timestamp)
207 visualization_msgs::msg::Marker marker;
208 marker.header.frame_id = frame_id;
209 marker.header.stamp = timestamp;
210 marker.frame_locked =
false;
211 marker.ns =
"planned_footprint";
212 marker.action = visualization_msgs::msg::Marker::ADD;
213 marker.type = visualization_msgs::msg::Marker::LINE_LIST;
214 marker.lifetime = rclcpp::Duration(0, 0);
217 for (
auto & point : edge) {
218 marker.points.push_back(point);
221 marker.pose.orientation.x = 0.0;
222 marker.pose.orientation.y = 0.0;
223 marker.pose.orientation.z = 0.0;
224 marker.pose.orientation.w = 1.0;
225 marker.scale.x = 0.05;
226 marker.scale.y = 0.05;
227 marker.scale.z = 0.05;
228 marker.color.r = 0.0f;
229 marker.color.g = 0.0f;
230 marker.color.b = 1.0f;
231 marker.color.a = 1.3f;
A 2D costmap provides a mapping between points in the world and their associated "costs".
double getResolution() const
Accessor for the resolution of the costmap.
double getOriginY() const
Accessor for the y origin of the costmap.
double getOriginX() const
Accessor for the x origin of the costmap.