Nav2 Navigation Stack - jazzy  jazzy
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  using xt::evaluation_strategy::immediate;
44 
45  if (!enabled_) {
46  return;
47  }
48 
49  // Differential motion model
50  auto diff = dynamic_cast<DiffDriveMotionModel *>(data.motion_model.get());
51  if (diff != nullptr) {
52  if (power_ > 1u) {
53  data.costs += xt::pow(
54  xt::sum(
55  (std::move(
56  xt::maximum(data.state.vx - max_vel_, 0.0f) +
57  xt::maximum(min_vel_ - data.state.vx, 0.0f))) *
58  data.model_dt, {1}, immediate) * weight_, power_);
59  } else {
60  data.costs += xt::sum(
61  (std::move(
62  xt::maximum(data.state.vx - max_vel_, 0.0f) +
63  xt::maximum(min_vel_ - data.state.vx, 0.0f))) *
64  data.model_dt, {1}, immediate) * weight_;
65  }
66  return;
67  }
68 
69  // Omnidirectional motion model
70  auto omni = dynamic_cast<OmniMotionModel *>(data.motion_model.get());
71  if (omni != nullptr) {
72  auto sgn = xt::eval(xt::where(data.state.vx > 0.0f, 1.0f, -1.0f));
73  auto vel_total = sgn * xt::hypot(data.state.vx, data.state.vy);
74  if (power_ > 1u) {
75  data.costs += xt::pow(
76  xt::sum(
77  (std::move(
78  xt::maximum(vel_total - max_vel_, 0.0f) +
79  xt::maximum(min_vel_ - vel_total, 0.0f))) *
80  data.model_dt, {1}, immediate) * weight_, power_);
81  } else {
82  data.costs += xt::sum(
83  (std::move(
84  xt::maximum(vel_total - max_vel_, 0.0f) +
85  xt::maximum(min_vel_ - vel_total, 0.0f))) *
86  data.model_dt, {1}, immediate) * weight_;
87  }
88  return;
89  }
90 
91  // Ackermann motion model
92  auto acker = dynamic_cast<AckermannMotionModel *>(data.motion_model.get());
93  if (acker != nullptr) {
94  auto & vx = data.state.vx;
95  auto & wz = data.state.wz;
96  auto out_of_turning_rad_motion = xt::maximum(
97  acker->getMinTurningRadius() - (xt::fabs(vx) / xt::fabs(wz)), 0.0f);
98  if (power_ > 1u) {
99  data.costs += xt::pow(
100  xt::sum(
101  (std::move(
102  xt::maximum(data.state.vx - max_vel_, 0.0f) +
103  xt::maximum(min_vel_ - data.state.vx, 0.0f) + out_of_turning_rad_motion)) *
104  data.model_dt, {1}, immediate) * weight_, power_);
105  } else {
106  data.costs += xt::sum(
107  (std::move(
108  xt::maximum(data.state.vx - max_vel_, 0.0f) +
109  xt::maximum(min_vel_ - data.state.vx, 0.0f) + out_of_turning_rad_motion)) *
110  data.model_dt, {1}, immediate) * weight_;
111  }
112  return;
113  }
114 }
115 
116 } // namespace mppi::critics
117 
118 #include <pluginlib/class_list_macros.hpp>
119 
Ackermann motion model.
Differential drive motion model.
Omnidirectional motion model.
auto getParamGetter(const std::string &ns)
Get an object to retreive 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:45