Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
constraint_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/constraint_critic.hpp"
16 
17 namespace mppi::critics
18 {
19 
21 {
22  auto getParam = parameters_handler_->getParamGetter(name_);
23  auto getParentParam = parameters_handler_->getParamGetter(parent_name_);
24 
25  getParam(power_, "cost_power", 1);
26  getParam(weight_, "cost_weight", 4.0f);
27  RCLCPP_INFO(
28  logger_, "ConstraintCritic instantiated with %d power and %f weight.",
29  power_, weight_);
30 
31  float vx_max, vy_max, vx_min;
32  getParentParam(vx_max, "vx_max", 0.5f);
33  getParentParam(vy_max, "vy_max", 0.0f);
34  getParentParam(vx_min, "vx_min", -0.35f);
35 
36  const float min_sgn = vx_min > 0.0f ? 1.0f : -1.0f;
37  max_vel_ = sqrtf(vx_max * vx_max + vy_max * vy_max);
38  min_vel_ = min_sgn * sqrtf(vx_min * vx_min + vy_max * vy_max);
39 }
40 
42 {
43  if (!enabled_) {
44  return;
45  }
46 
47  // Differential motion model
48  auto diff = dynamic_cast<DiffDriveMotionModel *>(data.motion_model.get());
49  if (diff != nullptr) {
50  if (power_ > 1u) {
51  data.costs += (((((data.state.vx - max_vel_).max(0.0f) + (min_vel_ - data.state.vx).
52  max(0.0f)) * data.model_dt).rowwise().sum().eval()) * weight_).pow(power_).eval();
53  } else {
54  data.costs += (((((data.state.vx - max_vel_).max(0.0f) + (min_vel_ - data.state.vx).
55  max(0.0f)) * data.model_dt).rowwise().sum().eval()) * weight_).eval();
56  }
57  return;
58  }
59 
60  // Omnidirectional motion model
61  auto omni = dynamic_cast<OmniMotionModel *>(data.motion_model.get());
62  if (omni != nullptr) {
63  auto & vx = data.state.vx;
64  unsigned int n_rows = data.state.vx.rows();
65  unsigned int n_cols = data.state.vx.cols();
66  Eigen::ArrayXXf sgn(n_rows, n_cols);
67  sgn = vx.unaryExpr([](const float x){return copysignf(1.0f, x);});
68 
69  auto vel_total = sgn * (data.state.vx.square() + data.state.vy.square()).sqrt();
70  if (power_ > 1u) {
71  data.costs += ((((vel_total - max_vel_).max(0.0f) + (min_vel_ - vel_total).
72  max(0.0f)) * data.model_dt).rowwise().sum().eval() * weight_).pow(power_).eval();
73  } else {
74  data.costs += ((((vel_total - max_vel_).max(0.0f) + (min_vel_ - vel_total).
75  max(0.0f)) * data.model_dt).rowwise().sum().eval() * weight_).eval();
76  }
77  return;
78  }
79 
80  // Ackermann motion model
81  auto acker = dynamic_cast<AckermannMotionModel *>(data.motion_model.get());
82  if (acker != nullptr) {
83  auto & vx = data.state.vx;
84  auto & wz = data.state.wz;
85  float min_turning_rad = acker->getMinTurningRadius();
86  auto out_of_turning_rad_motion = (min_turning_rad - (vx.abs() / wz.abs())).max(0.0f);
87  if (power_ > 1u) {
88  data.costs += ((((vx - max_vel_).max(0.0f) + (min_vel_ - vx).max(0.0f) +
89  out_of_turning_rad_motion) * data.model_dt).rowwise().sum().eval() *
90  weight_).pow(power_).eval();
91  } else {
92  data.costs += ((((vx - max_vel_).max(0.0f) + (min_vel_ - vx).max(0.0f) +
93  out_of_turning_rad_motion) * data.model_dt).rowwise().sum().eval() * weight_).eval();
94  }
95  return;
96  }
97 }
98 
99 } // namespace mppi::critics
100 
101 #include <pluginlib/class_list_macros.hpp>
102 
Ackermann motion model.
Differential drive motion model.
Omnidirectional motion model.
auto getParamGetter(const std::string &ns)
Get an object to retrieve parameters.
Critic objective function for enforcing feasible constraints.
void score(CriticData &data) override
Evaluate cost related to goal following.
void initialize() override
Initialize critic.
Abstract critic objective function to score trajectories.
Data to pass to critics for scoring, including state, trajectories, pruned path, global goal,...
Definition: critic_data.hpp:40