Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
utils.hpp
1 // Copyright (c) 2021, Samsung Research America
2 // Copyright (c) 2023, Open Navigation LLC
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License. Reserved.
15 
16 #ifndef NAV2_SMAC_PLANNER__UTILS_HPP_
17 #define NAV2_SMAC_PLANNER__UTILS_HPP_
18 
19 #include <vector>
20 #include <memory>
21 #include <string>
22 
23 #include "nlohmann/json.hpp"
24 #include "Eigen/Core"
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>
33 
34 namespace nav2_smac_planner
35 {
36 
44 inline geometry_msgs::msg::Pose getWorldCoords(
45  const float & mx, const float & my, const nav2_costmap_2d::Costmap2D * costmap)
46 {
47  geometry_msgs::msg::Pose msg;
48  msg.position.x =
49  static_cast<float>(costmap->getOriginX()) + mx * costmap->getResolution();
50  msg.position.y =
51  static_cast<float>(costmap->getOriginY()) + my * costmap->getResolution();
52  return msg;
53 }
54 
60 inline geometry_msgs::msg::Quaternion getWorldOrientation(
61  const float & theta)
62 {
63  // theta is in radians already
64  tf2::Quaternion q;
65  q.setEuler(0.0, 0.0, theta);
66  return tf2::toMsg(q);
67 }
68 
76 inline double findCircumscribedCost(std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap)
77 {
78  double result = -1.0;
79  std::vector<std::shared_ptr<nav2_costmap_2d::Layer>>::iterator layer;
80 
81  // check if the costmap has an inflation 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) {
88  RCLCPP_ERROR(
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);
96  result = 0.0;
97  return result;
98  }
99  result = static_cast<double>(inflation_layer->computeCost(circum_radius / resolution));
100  } else {
101  RCLCPP_WARN(
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!");
108  }
109 
110  return result;
111 }
112 
118 inline void fromJsonToMetaData(const nlohmann::json & json, LatticeMetadata & lattice_metadata)
119 {
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);
126 }
127 
133 inline void fromJsonToPose(const nlohmann::json & json, MotionPose & pose)
134 {
135  pose._x = json[0];
136  pose._y = json[1];
137  pose._theta = json[2];
138 }
139 
145 inline void fromJsonToMotionPrimitive(
146  const nlohmann::json & json, MotionPrimitive & motion_primitive)
147 {
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);
156 
157  for (unsigned int i = 0; i < json["poses"].size(); i++) {
158  MotionPose pose;
159  fromJsonToPose(json["poses"][i], pose);
160  motion_primitive.poses.push_back(pose);
161  }
162 }
163 
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)
172 {
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);
178 
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;
184  if (i == 0) {
185  out_footprint.back().x = out_footprint[i].x;
186  out_footprint.back().y = out_footprint[i].y;
187  } else {
188  out_footprint[2 * i - 1].x = out_footprint[2 * i].x;
189  out_footprint[2 * i - 1].y = out_footprint[2 * i].y;
190  }
191  }
192  return out_footprint;
193 }
194 
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)
206 {
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);
215 
216  marker.id = i;
217  for (auto & point : edge) {
218  marker.points.push_back(point);
219  }
220 
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;
232  return marker;
233 }
234 
235 
236 } // namespace nav2_smac_planner
237 
238 #endif // NAV2_SMAC_PLANNER__UTILS_HPP_
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:68
double getResolution() const
Accessor for the resolution of the costmap.
Definition: costmap_2d.cpp:544
double getOriginY() const
Accessor for the y origin of the costmap.
Definition: costmap_2d.cpp:539
double getOriginX() const
Accessor for the x origin of the costmap.
Definition: costmap_2d.cpp:534