Nav2 Navigation Stack - humble  humble
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.0);
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.5);
33  getParentParam(vy_max, "vy_max", 0.0);
34  getParentParam(vx_min, "vx_min", -0.35);
35 
36  const float min_sgn = vx_min > 0.0 ? 1.0 : -1.0;
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  auto sgn = xt::where(data.state.vx > 0.0, 1.0, -1.0);
50  auto vel_total = sgn * xt::sqrt(data.state.vx * data.state.vx + data.state.vy * data.state.vy);
51  auto out_of_max_bounds_motion = xt::maximum(vel_total - max_vel_, 0);
52  auto out_of_min_bounds_motion = xt::maximum(min_vel_ - vel_total, 0);
53 
54  auto acker = dynamic_cast<AckermannMotionModel *>(data.motion_model.get());
55  if (acker != nullptr) {
56  auto & vx = data.state.vx;
57  auto & wz = data.state.wz;
58  auto out_of_turning_rad_motion = xt::maximum(
59  acker->getMinTurningRadius() - (xt::fabs(vx) / xt::fabs(wz)), 0.0);
60 
61  data.costs += xt::pow(
62  xt::sum(
63  (std::move(out_of_max_bounds_motion) +
64  std::move(out_of_min_bounds_motion) +
65  std::move(out_of_turning_rad_motion)) *
66  data.model_dt, {1}, immediate) * weight_, power_);
67  return;
68  }
69 
70  data.costs += xt::pow(
71  xt::sum(
72  (std::move(out_of_max_bounds_motion) +
73  std::move(out_of_min_bounds_motion)) *
74  data.model_dt, {1}, immediate) * weight_, power_);
75 }
76 
77 } // namespace mppi::critics
78 
79 #include <pluginlib/class_list_macros.hpp>
80 
Ackermann 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:39