Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
velocity_deadband_critic.cpp
1 // Copyright (c) 2022 Samsung Research America, @artofnothingness Alexey Budyakov
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 "nav2_mppi_controller/critics/velocity_deadband_critic.hpp"
16 
17 namespace mppi::critics
18 {
19 
21 {
22  auto getParam = parameters_handler_->getParamGetter(name_);
23 
24  getParam(power_, "cost_power", 1);
25  getParam(weight_, "cost_weight", 35.0);
26 
27  // Recast double to float
28  std::vector<double> deadband_velocities{0.0, 0.0, 0.0};
29  getParam(deadband_velocities, "deadband_velocities", std::vector<double>{0.0, 0.0, 0.0});
30  std::transform(
31  deadband_velocities.begin(), deadband_velocities.end(), deadband_velocities_.begin(),
32  [](double d) {return static_cast<float>(d);});
33 
34  RCLCPP_INFO_STREAM(
35  logger_, "VelocityDeadbandCritic instantiated with "
36  << power_ << " power, " << weight_ << " weight, deadband_velocity ["
37  << deadband_velocities_.at(0) << "," << deadband_velocities_.at(1) << ","
38  << deadband_velocities_.at(2) << "]");
39 }
40 
42 {
43  using xt::evaluation_strategy::immediate;
44 
45  if (!enabled_) {
46  return;
47  }
48 
49  auto & vx = data.state.vx;
50  auto & wz = data.state.wz;
51 
52  if (data.motion_model->isHolonomic()) {
53  auto & vy = data.state.vy;
54  if (power_ > 1u) {
55  data.costs += xt::pow(
56  xt::sum(
57  std::move(
58  xt::maximum(fabs(deadband_velocities_.at(0)) - xt::fabs(vx), 0) +
59  xt::maximum(fabs(deadband_velocities_.at(1)) - xt::fabs(vy), 0) +
60  xt::maximum(fabs(deadband_velocities_.at(2)) - xt::fabs(wz), 0)) *
61  data.model_dt,
62  {1}, immediate) *
63  weight_,
64  power_);
65  } else {
66  data.costs += xt::sum(
67  (std::move(
68  xt::maximum(fabs(deadband_velocities_.at(0)) - xt::fabs(vx), 0) +
69  xt::maximum(fabs(deadband_velocities_.at(1)) - xt::fabs(vy), 0) +
70  xt::maximum(fabs(deadband_velocities_.at(2)) - xt::fabs(wz), 0))) *
71  data.model_dt,
72  {1}, immediate) *
73  weight_;
74  }
75  return;
76  }
77 
78  if (power_ > 1u) {
79  data.costs += xt::pow(
80  xt::sum(
81  std::move(
82  xt::maximum(fabs(deadband_velocities_.at(0)) - xt::fabs(vx), 0) +
83  xt::maximum(fabs(deadband_velocities_.at(2)) - xt::fabs(wz), 0)) *
84  data.model_dt,
85  {1}, immediate) *
86  weight_,
87  power_);
88  } else {
89  data.costs += xt::sum(
90  (std::move(
91  xt::maximum(fabs(deadband_velocities_.at(0)) - xt::fabs(vx), 0) +
92  xt::maximum(fabs(deadband_velocities_.at(2)) - xt::fabs(wz), 0))) *
93  data.model_dt,
94  {1}, immediate) *
95  weight_;
96  }
97  return;
98 }
99 
100 } // namespace mppi::critics
101 
102 #include <pluginlib/class_list_macros.hpp>
103 
auto getParamGetter(const std::string &ns)
Get an object to retreive parameters.
Abstract critic objective function to score trajectories.
Critic objective function for enforcing feasible constraints.
void initialize() override
Initialize critic.
void score(CriticData &data) override
Evaluate cost related to goal following.
Data to pass to critics for scoring, including state, trajectories, pruned path, global goal,...
Definition: critic_data.hpp:39