15 #include "nav2_mppi_controller/critics/path_align_legacy_critic.hpp"
17 #include <xtensor/xfixed.hpp>
18 #include <xtensor/xmath.hpp>
20 namespace mppi::critics
23 using namespace xt::placeholders;
24 using xt::evaluation_strategy::immediate;
28 auto getParam = parameters_handler_->getParamGetter(name_);
29 getParam(power_,
"cost_power", 1);
30 getParam(weight_,
"cost_weight", 10.0);
32 getParam(max_path_occupancy_ratio_,
"max_path_occupancy_ratio", 0.07);
33 getParam(offset_from_furthest_,
"offset_from_furthest", 20);
34 getParam(trajectory_point_step_,
"trajectory_point_step", 4);
36 threshold_to_consider_,
37 "threshold_to_consider", 0.5);
38 getParam(use_path_orientations_,
"use_path_orientations",
false);
42 "ReferenceTrajectoryCritic instantiated with %d power and %f weight",
49 if (!enabled_ || utils::withinPositionGoalTolerance(
50 threshold_to_consider_, data.state.pose.pose, data.goal))
56 utils::setPathFurthestPointIfNotSet(data);
57 if (*data.furthest_reached_path_point < offset_from_furthest_) {
62 utils::setPathCostsIfNotSet(data, costmap_ros_);
63 const size_t closest_initial_path_point = utils::findPathTrajectoryInitialPoint(data);
64 unsigned int invalid_ctr = 0;
65 const float range = *data.furthest_reached_path_point - closest_initial_path_point;
66 for (
size_t i = closest_initial_path_point; i < *data.furthest_reached_path_point; i++) {
67 if (!(*data.path_pts_valid)[i]) {invalid_ctr++;}
68 if (
static_cast<float>(invalid_ctr) / range > max_path_occupancy_ratio_ && invalid_ctr > 2) {
73 const auto & T_x = data.trajectories.x;
74 const auto & T_y = data.trajectories.y;
75 const auto & T_yaw = data.trajectories.yaws;
77 const auto P_x = xt::view(data.path.x, xt::range(_, -1));
78 const auto P_y = xt::view(data.path.y, xt::range(_, -1));
79 const auto P_yaw = xt::view(data.path.yaws, xt::range(_, -1));
81 const size_t batch_size = T_x.shape(0);
82 const size_t time_steps = T_x.shape(1);
83 const size_t traj_pts_eval = floor(time_steps / trajectory_point_step_);
84 const size_t path_segments_count = data.path.x.shape(0) - 1;
85 auto && cost = xt::xtensor<float, 1>::from_shape({data.costs.shape(0)});
87 if (path_segments_count < 1) {
91 float dist_sq = 0.0f, dx = 0.0f, dy = 0.0f, dyaw = 0.0f, summed_dist = 0.0f;
92 float min_dist_sq = std::numeric_limits<float>::max();
95 for (
size_t t = 0; t < batch_size; ++t) {
97 for (
size_t p = trajectory_point_step_; p < time_steps; p += trajectory_point_step_) {
98 min_dist_sq = std::numeric_limits<float>::max();
102 for (
size_t s = 0; s < path_segments_count - 1; s++) {
103 xt::xtensor_fixed<float, xt::xshape<2>> P;
104 dx = P_x(s) - T_x(t, p);
105 dy = P_y(s) - T_y(t, p);
106 if (use_path_orientations_) {
107 dyaw = angles::shortest_angular_distance(P_yaw(s), T_yaw(t, p));
108 dist_sq = dx * dx + dy * dy + dyaw * dyaw;
110 dist_sq = dx * dx + dy * dy;
112 if (dist_sq < min_dist_sq) {
113 min_dist_sq = dist_sq;
120 if (min_s != 0 && (*data.path_pts_valid)[min_s]) {
121 summed_dist += sqrtf(min_dist_sq);
125 cost[t] = summed_dist / traj_pts_eval;
128 data.costs += xt::pow(std::move(cost) * weight_, power_);
133 #include <pluginlib/class_list_macros.hpp>
135 PLUGINLIB_EXPORT_CLASS(
Abstract critic objective function to score trajectories.
Critic objective function for aligning to the path. Note: High settings of this will follow the path ...
void score(CriticData &data) override
Evaluate cost related to trajectories path alignment.
void initialize() override
Initialize critic.
Data to pass to critics for scoring, including state, trajectories, pruned path, global goal,...