15 #include "nav2_mppi_controller/critics/path_align_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 const size_t path_segments_count = *data.furthest_reached_path_point;
58 if (path_segments_count < offset_from_furthest_) {
63 utils::setPathCostsIfNotSet(data, costmap_ros_);
64 const size_t closest_initial_path_point = utils::findPathTrajectoryInitialPoint(data);
65 unsigned int invalid_ctr = 0;
66 const float range = *data.furthest_reached_path_point - closest_initial_path_point;
67 for (
size_t i = closest_initial_path_point; i < *data.furthest_reached_path_point; i++) {
68 if (!(*data.path_pts_valid)[i]) {invalid_ctr++;}
69 if (
static_cast<float>(invalid_ctr) / range > max_path_occupancy_ratio_ && invalid_ctr > 2) {
74 const auto P_x = xt::view(data.path.x, xt::range(_, -1));
75 const auto P_y = xt::view(data.path.y, xt::range(_, -1));
76 const auto P_yaw = xt::view(data.path.yaws, xt::range(_, -1));
78 const size_t batch_size = data.trajectories.x.shape(0);
79 const size_t time_steps = data.trajectories.x.shape(1);
80 auto && cost = xt::xtensor<float, 1>::from_shape({data.costs.shape(0)});
83 std::vector<float> path_integrated_distances(path_segments_count, 0.0f);
84 float dx = 0.0f, dy = 0.0f;
85 for (
unsigned int i = 1; i != path_segments_count; i++) {
86 dx = P_x(i) - P_x(i - 1);
87 dy = P_y(i) - P_y(i - 1);
88 float curr_dist = sqrtf(dx * dx + dy * dy);
89 path_integrated_distances[i] = path_integrated_distances[i - 1] + curr_dist;
92 float traj_integrated_distance = 0.0f;
93 float summed_path_dist = 0.0f, dyaw = 0.0f;
94 float num_samples = 0.0f;
95 float Tx = 0.0f, Ty = 0.0f;
97 for (
size_t t = 0; t < batch_size; ++t) {
98 traj_integrated_distance = 0.0f;
99 summed_path_dist = 0.0f;
102 const auto T_x = xt::view(data.trajectories.x, t, xt::all());
103 const auto T_y = xt::view(data.trajectories.y, t, xt::all());
104 for (
size_t p = trajectory_point_step_; p < time_steps; p += trajectory_point_step_) {
107 dx = Tx - T_x(p - trajectory_point_step_);
108 dy = Ty - T_y(p - trajectory_point_step_);
109 traj_integrated_distance += sqrtf(dx * dx + dy * dy);
110 path_pt = utils::findClosestPathPt(
111 path_integrated_distances, traj_integrated_distance, path_pt);
115 if ((*data.path_pts_valid)[path_pt]) {
116 dx = P_x(path_pt) - Tx;
117 dy = P_y(path_pt) - Ty;
119 if (use_path_orientations_) {
120 const auto T_yaw = xt::view(data.trajectories.yaws, t, xt::all());
121 dyaw = angles::shortest_angular_distance(P_yaw(path_pt), T_yaw(p));
122 summed_path_dist += sqrtf(dx * dx + dy * dy + dyaw * dyaw);
124 summed_path_dist += sqrtf(dx * dx + dy * dy);
128 if (num_samples > 0) {
129 cost[t] = summed_path_dist / num_samples;
135 data.costs += xt::pow(std::move(cost) * weight_, power_);
140 #include <pluginlib/class_list_macros.hpp>
142 PLUGINLIB_EXPORT_CLASS(
Abstract critic objective function to score trajectories.
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,...