Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
footprint_subscriber.cpp
1 // Copyright (c) 2019 Intel Corporation
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.
14 
15 #include <string>
16 #include <vector>
17 #include <memory>
18 
19 #include "nav2_costmap_2d/footprint_subscriber.hpp"
20 #include "tf2/utils.hpp"
21 
22 namespace nav2_costmap_2d
23 {
24 
25 bool
27  std::vector<geometry_msgs::msg::Point> & footprint,
28  std_msgs::msg::Header & footprint_header)
29 {
30  if (!footprint_received_.load()) {
31  return false;
32  }
33 
34 #ifdef __cpp_lib_atomic_shared_ptr
35  auto current_footprint = footprint_.load();
36 #else
37  auto current_footprint = std::atomic_load(&footprint_);
38 #endif
39  if (!current_footprint) {
40  return false;
41  }
42 
43  footprint = toPointVector(current_footprint->polygon);
44  footprint_header = current_footprint->header;
45 
46  return true;
47 }
48 
49 bool
51  std::vector<geometry_msgs::msg::Point> & footprint,
52  std_msgs::msg::Header & footprint_header)
53 {
54  if (!getFootprintRaw(footprint, footprint_header)) {
55  return false;
56  }
57 
58  geometry_msgs::msg::PoseStamped current_pose;
59  if (!nav2_util::getCurrentPose(
60  current_pose, tf_, footprint_header.frame_id, robot_base_frame_,
61  transform_tolerance_, footprint_header.stamp))
62  {
63  return false;
64  }
65 
66  double x = current_pose.pose.position.x;
67  double y = current_pose.pose.position.y;
68  double theta = tf2::getYaw(current_pose.pose.orientation);
69 
70  std::vector<geometry_msgs::msg::Point> temp;
71  transformFootprint(-x, -y, 0, footprint, temp);
72  transformFootprint(0, 0, -theta, temp, footprint);
73 
74  footprint_header.frame_id = robot_base_frame_;
75  footprint_header.stamp = current_pose.header.stamp;
76 
77  return true;
78 }
79 
80 void
82  const geometry_msgs::msg::PolygonStamped::ConstSharedPtr & msg)
83 {
84 #ifdef __cpp_lib_atomic_shared_ptr
85  footprint_.store(msg);
86 #else
87  std::atomic_store(&footprint_, msg);
88 #endif
89  if (!footprint_received_.load()) {
90  footprint_received_.store(true);
91  }
92 }
93 
94 } // namespace nav2_costmap_2d
void footprint_callback(const geometry_msgs::msg::PolygonStamped::ConstSharedPtr &msg)
Callback to process new footprint updates.
bool getFootprintInRobotFrame(std::vector< geometry_msgs::msg::Point > &footprint, std_msgs::msg::Header &footprint_header)
Returns the latest robot footprint, transformed into robot base frame (unoriented).
bool getFootprintRaw(std::vector< geometry_msgs::msg::Point > &footprint, std_msgs::msg::Header &footprint_header)
Returns the latest robot footprint, in the form as received from topic (oriented).
void transformFootprint(double x, double y, double theta, const std::vector< geometry_msgs::msg::Point > &footprint_spec, std::vector< geometry_msgs::msg::Point > &oriented_footprint)
Given a pose and base footprint, build the oriented footprint of the robot (list of Points)
Definition: footprint.cpp:112
std::vector< geometry_msgs::msg::Point > toPointVector(const geometry_msgs::msg::Polygon &polygon)
Convert Polygon msg to vector of Points.
Definition: footprint.cpp:102