Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
utils.hpp
1 // Copyright (c) 2021, Samsung Research America
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License. Reserved.
14 
15 #ifndef NAV2_SMAC_PLANNER__UTILS_HPP_
16 #define NAV2_SMAC_PLANNER__UTILS_HPP_
17 
18 #include <vector>
19 #include <memory>
20 
21 #include "Eigen/Core"
22 #include "geometry_msgs/msg/quaternion.hpp"
23 #include "geometry_msgs/msg/pose.hpp"
24 #include "tf2/utils.h"
25 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
26 #include "nav2_costmap_2d/inflation_layer.hpp"
27 
28 namespace nav2_smac_planner
29 {
30 
38 inline geometry_msgs::msg::Pose getWorldCoords(
39  const float & mx, const float & my, const nav2_costmap_2d::Costmap2D * costmap)
40 {
41  geometry_msgs::msg::Pose msg;
42  msg.position.x =
43  static_cast<float>(costmap->getOriginX()) + (mx + 0.5) * costmap->getResolution();
44  msg.position.y =
45  static_cast<float>(costmap->getOriginY()) + (my + 0.5) * costmap->getResolution();
46  return msg;
47 }
48 
54 inline geometry_msgs::msg::Quaternion getWorldOrientation(
55  const float & theta)
56 {
57  // theta is in radians already
58  tf2::Quaternion q;
59  q.setEuler(0.0, 0.0, theta);
60  return tf2::toMsg(q);
61 }
62 
70 inline double findCircumscribedCost(std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap)
71 {
72  double result = -1.0;
73  bool inflation_layer_found = false;
74  std::vector<std::shared_ptr<nav2_costmap_2d::Layer>>::iterator layer;
75 
76  // check if the costmap has an inflation layer
77  for (layer = costmap->getLayeredCostmap()->getPlugins()->begin();
78  layer != costmap->getLayeredCostmap()->getPlugins()->end();
79  ++layer)
80  {
81  std::shared_ptr<nav2_costmap_2d::InflationLayer> inflation_layer =
82  std::dynamic_pointer_cast<nav2_costmap_2d::InflationLayer>(*layer);
83  if (!inflation_layer) {
84  continue;
85  }
86 
87  inflation_layer_found = true;
88  double circum_radius = costmap->getLayeredCostmap()->getCircumscribedRadius();
89  double resolution = costmap->getCostmap()->getResolution();
90  result = static_cast<double>(inflation_layer->computeCost(circum_radius / resolution));
91  }
92 
93  if (!inflation_layer_found) {
94  RCLCPP_WARN(
95  rclcpp::get_logger("computeCircumscribedCost"),
96  "No inflation layer found in costmap configuration. "
97  "If this is an SE2-collision checking plugin, it cannot use costmap potential "
98  "field to speed up collision checking by only checking the full footprint "
99  "when robot is within possibly-inscribed radius of an obstacle. This may "
100  "significantly slow down planning times!");
101  }
102 
103  return result;
104 }
105 
111 inline void fromJsonToMetaData(const nlohmann::json & json, LatticeMetadata & lattice_metadata)
112 {
113  json.at("turning_radius").get_to(lattice_metadata.min_turning_radius);
114  json.at("grid_resolution").get_to(lattice_metadata.grid_resolution);
115  json.at("num_of_headings").get_to(lattice_metadata.number_of_headings);
116  json.at("heading_angles").get_to(lattice_metadata.heading_angles);
117  json.at("number_of_trajectories").get_to(lattice_metadata.number_of_trajectories);
118  json.at("motion_model").get_to(lattice_metadata.motion_model);
119 }
120 
126 inline void fromJsonToPose(const nlohmann::json & json, MotionPose & pose)
127 {
128  pose._x = json[0];
129  pose._y = json[1];
130  pose._theta = json[2];
131 }
132 
138 inline void fromJsonToMotionPrimitive(
139  const nlohmann::json & json, MotionPrimitive & motion_primitive)
140 {
141  json.at("trajectory_id").get_to(motion_primitive.trajectory_id);
142  json.at("start_angle_index").get_to(motion_primitive.start_angle);
143  json.at("end_angle_index").get_to(motion_primitive.end_angle);
144  json.at("trajectory_radius").get_to(motion_primitive.turning_radius);
145  json.at("trajectory_length").get_to(motion_primitive.trajectory_length);
146  json.at("arc_length").get_to(motion_primitive.arc_length);
147  json.at("straight_length").get_to(motion_primitive.straight_length);
148  json.at("left_turn").get_to(motion_primitive.left_turn);
149 
150  for (unsigned int i = 0; i < json["poses"].size(); i++) {
151  MotionPose pose;
152  fromJsonToPose(json["poses"][i], pose);
153  motion_primitive.poses.push_back(pose);
154  }
155 }
156 
157 } // namespace nav2_smac_planner
158 
159 #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:531
double getOriginY() const
Accessor for the y origin of the costmap.
Definition: costmap_2d.cpp:526
double getOriginX() const
Accessor for the x origin of the costmap.
Definition: costmap_2d.cpp:521