Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
ego_polar_coords.hpp
1 // Copyright (c) 2023 Alberto J. Tudela Roldán
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 #ifndef NAV2_GRACEFUL_CONTROLLER__EGO_POLAR_COORDS_HPP_
16 #define NAV2_GRACEFUL_CONTROLLER__EGO_POLAR_COORDS_HPP_
17 
18 #include <cmath>
19 
20 #include "angles/angles.h"
21 #include "geometry_msgs/msg/pose.hpp"
22 #include "tf2/utils.hpp"
23 
24 namespace nav2_graceful_controller
25 {
26 
32 {
33  float r; // Radial distance between the robot pose and the target pose.
34  float phi; // Orientation of target with respect to the line of sight
35  // from the robot to the target.
36  float delta; // Steering angle of the robot with respect to the line of sight.
37 
39  const float & r_in = 0.0,
40  const float & phi_in = 0.0,
41  const float & delta_in = 0.0)
42  : r(r_in), phi(phi_in), delta(delta_in) {}
43 
55  const geometry_msgs::msg::Pose & target,
56  const geometry_msgs::msg::Pose & current = geometry_msgs::msg::Pose(), bool backward = false)
57  {
58  // Compute the difference between the target and the current pose
59  float dX = target.position.x - current.position.x;
60  float dY = target.position.y - current.position.y;
61  // Compute the line of sight from the robot to the target
62  // Flip it if the robot is moving backwards
63  float line_of_sight = backward ? (std::atan2(-dY, dX) + M_PI) : std::atan2(-dY, dX);
64  // Compute the ego polar coordinates
65  r = sqrt(dX * dX + dY * dY);
66  phi = angles::normalize_angle(tf2::getYaw(target.orientation) + line_of_sight);
67  delta = angles::normalize_angle(tf2::getYaw(current.orientation) + line_of_sight);
68  }
69 };
70 
71 } // namespace nav2_graceful_controller
72 
73 #endif // NAV2_GRACEFUL_CONTROLLER__EGO_POLAR_COORDS_HPP_
Egocentric polar coordinates defined as the difference between the robot pose and the target pose rel...
EgocentricPolarCoordinates(const geometry_msgs::msg::Pose &target, const geometry_msgs::msg::Pose &current=geometry_msgs::msg::Pose(), bool backward=false)
Construct a new egocentric polar coordinates as the difference between the robot pose and the target ...