Nav2 Navigation Stack - lyrical  lyrical
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  auto current_footprint = footprint_.load();
35  if (!current_footprint) {
36  return false;
37  }
38 
39  footprint = toPointVector(current_footprint->polygon);
40  footprint_header = current_footprint->header;
41 
42  return true;
43 }
44 
45 bool
47  std::vector<geometry_msgs::msg::Point> & footprint,
48  std_msgs::msg::Header & footprint_header)
49 {
50  if (!getFootprintRaw(footprint, footprint_header)) {
51  return false;
52  }
53 
54  geometry_msgs::msg::PoseStamped current_pose;
55  if (!nav2_util::getCurrentPose(
56  current_pose, tf_, footprint_header.frame_id, robot_base_frame_,
57  transform_tolerance_, footprint_header.stamp))
58  {
59  return false;
60  }
61 
62  double x = current_pose.pose.position.x;
63  double y = current_pose.pose.position.y;
64  double theta = tf2::getYaw(current_pose.pose.orientation);
65 
66  std::vector<geometry_msgs::msg::Point> temp;
67  transformFootprint(-x, -y, 0, footprint, temp);
68  transformFootprint(0, 0, -theta, temp, footprint);
69 
70  footprint_header.frame_id = robot_base_frame_;
71  footprint_header.stamp = current_pose.header.stamp;
72 
73  return true;
74 }
75 
76 void
78  const geometry_msgs::msg::PolygonStamped::ConstSharedPtr & msg)
79 {
80  footprint_.store(msg);
81  if (!footprint_received_.load()) {
82  footprint_received_.store(true);
83  }
84 }
85 
86 } // 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