Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
spin.cpp
1 // Copyright (c) 2018 Intel Corporation, 2019 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.
14 
15 #include <cmath>
16 #include <thread>
17 #include <algorithm>
18 #include <memory>
19 #include <utility>
20 
21 #include "nav2_behaviors/plugins/spin.hpp"
22 #include "nav2_ros_common/node_utils.hpp"
23 #include "nav2_util/geometry_utils.hpp"
24 
25 using namespace std::chrono_literals;
26 
27 namespace nav2_behaviors
28 {
29 
30 Spin::Spin()
31 : TimedBehavior<SpinAction>(),
32  feedback_(std::make_shared<SpinAction::Feedback>()),
33  min_rotational_vel_(0.0),
34  max_rotational_vel_(0.0),
35  rotational_acc_lim_(0.0),
36  cmd_yaw_(0.0),
37  prev_yaw_(0.0),
38  relative_yaw_(0.0),
39  simulate_ahead_time_(0.0)
40 {
41 }
42 
43 Spin::~Spin() = default;
44 
46 {
47  auto node = node_.lock();
48  if (!node) {
49  throw std::runtime_error{"Failed to lock node"};
50  }
51 
52  simulate_ahead_time_ = node->declare_or_get_parameter("simulate_ahead_time", 2.0);
53  max_rotational_vel_ = node->declare_or_get_parameter("max_rotational_vel", 1.0);
54  min_rotational_vel_ = node->declare_or_get_parameter("min_rotational_vel", 0.4);
55  rotational_acc_lim_ = node->declare_or_get_parameter("rotational_acc_lim", 3.2);
56 }
57 
58 ResultStatus Spin::onRun(const std::shared_ptr<const SpinActionGoal> command)
59 {
60  geometry_msgs::msg::PoseStamped current_pose;
61  if (!nav2_util::getCurrentPose(
62  current_pose, *tf_, local_frame_, robot_base_frame_,
63  transform_tolerance_))
64  {
65  std::string error_msg = "Current robot pose is not available.";
66  RCLCPP_ERROR(logger_, error_msg.c_str());
67  return ResultStatus{Status::FAILED, SpinActionResult::TF_ERROR, error_msg};
68  }
69 
70  prev_yaw_ = tf2::getYaw(current_pose.pose.orientation);
71  relative_yaw_ = 0.0;
72 
73  cmd_yaw_ = command->target_yaw;
74  RCLCPP_INFO(
75  logger_, "Turning %0.2f for spin behavior.",
76  cmd_yaw_);
77 
78  command_time_allowance_ = command->time_allowance;
79  cmd_disable_collision_checks_ = command->disable_collision_checks;
80  end_time_ = this->clock_->now() + command_time_allowance_;
81 
82  return ResultStatus{Status::SUCCEEDED, SpinActionResult::NONE, ""};
83 }
84 
86 {
87  rclcpp::Duration time_remaining = end_time_ - this->clock_->now();
88  if (time_remaining.seconds() < 0.0 && command_time_allowance_.seconds() > 0.0) {
89  stopRobot();
90  std::string error_msg = "Exceeded time allowance before reaching the Spin goal - Exiting Spin";
91  RCLCPP_WARN(logger_, error_msg.c_str());
92  return ResultStatus{Status::FAILED, SpinActionResult::TIMEOUT, error_msg};
93  }
94 
95  geometry_msgs::msg::PoseStamped current_pose;
96  if (!nav2_util::getCurrentPose(
97  current_pose, *tf_, local_frame_, robot_base_frame_,
98  transform_tolerance_))
99  {
100  std::string error_msg = "Current robot pose is not available.";
101  RCLCPP_ERROR(logger_, error_msg.c_str());
102  return ResultStatus{Status::FAILED, SpinActionResult::TF_ERROR, error_msg};
103  }
104 
105  const double current_yaw = tf2::getYaw(current_pose.pose.orientation);
106 
107  double delta_yaw = current_yaw - prev_yaw_;
108  if (abs(delta_yaw) > M_PI) {
109  delta_yaw = copysign(2 * M_PI - abs(delta_yaw), prev_yaw_);
110  }
111 
112  relative_yaw_ += delta_yaw;
113  prev_yaw_ = current_yaw;
114 
115  feedback_->angular_distance_traveled = static_cast<float>(relative_yaw_);
116  action_server_->publish_feedback(feedback_);
117 
118  double remaining_yaw = abs(cmd_yaw_) - abs(relative_yaw_);
119  if (remaining_yaw < 1e-6) {
120  stopRobot();
121  return ResultStatus{Status::SUCCEEDED, SpinActionResult::NONE, ""};
122  }
123 
124  double vel = sqrt(2 * rotational_acc_lim_ * remaining_yaw);
125  vel = std::min(std::max(vel, min_rotational_vel_), max_rotational_vel_);
126 
127  auto cmd_vel = std::make_unique<geometry_msgs::msg::TwistStamped>();
128  cmd_vel->header.frame_id = robot_base_frame_;
129  cmd_vel->header.stamp = clock_->now();
130  cmd_vel->twist.angular.z = copysign(vel, cmd_yaw_);
131 
132  geometry_msgs::msg::Pose pose = current_pose.pose;
133 
134  if (!isCollisionFree(relative_yaw_, cmd_vel->twist, pose)) {
135  stopRobot();
136  std::string error_msg = "Collision Ahead - Exiting Spin";
137  RCLCPP_WARN(logger_, error_msg.c_str());
138  return ResultStatus{Status::FAILED, SpinActionResult::COLLISION_AHEAD, error_msg};
139  }
140 
141  vel_pub_->publish(std::move(cmd_vel));
142 
143  return ResultStatus{Status::RUNNING, SpinActionResult::NONE, ""};
144 }
145 
147  const double & relative_yaw,
148  const geometry_msgs::msg::Twist & cmd_vel,
149  geometry_msgs::msg::Pose & pose)
150 {
151  if (cmd_disable_collision_checks_) {
152  return true;
153  }
154 
155  // Simulate ahead by simulate_ahead_time_ in cycle_frequency_ increments
156  int cycle_count = 0;
157  double sim_position_change;
158  const int max_cycle_count = static_cast<int>(cycle_frequency_ * simulate_ahead_time_);
159  geometry_msgs::msg::Pose init_pose = pose;
160  double init_theta = tf2::getYaw(init_pose.orientation);
161  bool fetch_data = true;
162 
163  while (cycle_count < max_cycle_count) {
164  sim_position_change = cmd_vel.angular.z * (cycle_count / cycle_frequency_);
165  double new_theta = init_theta + sim_position_change;
166  pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(new_theta);
167  cycle_count++;
168 
169  if (abs(relative_yaw) - abs(sim_position_change) <= 0.0) {
170  break;
171  }
172 
173  if (!local_collision_checker_->isCollisionFree(pose, fetch_data)) {
174  return false;
175  }
176  fetch_data = false;
177  }
178  return true;
179 }
180 
181 } // namespace nav2_behaviors
182 
183 #include "pluginlib/class_list_macros.hpp"
184 PLUGINLIB_EXPORT_CLASS(nav2_behaviors::Spin, nav2_core::Behavior)
An action server behavior for spinning in.
Definition: spin.hpp:36
ResultStatus onRun(const std::shared_ptr< const SpinActionGoal > command) override
Initialization to run behavior.
Definition: spin.cpp:58
ResultStatus onCycleUpdate() override
Loop function to run behavior.
Definition: spin.cpp:85
void onConfigure() override
Configuration of behavior action.
Definition: spin.cpp:45
bool isCollisionFree(const double &distance, const geometry_msgs::msg::Twist &cmd_vel, geometry_msgs::msg::Pose &pose)
Check if pose is collision free.
Definition: spin.cpp:146
Abstract interface for behaviors to adhere to with pluginlib.
Definition: behavior.hpp:42