16 #include "nav2_mppi_controller/critics/path_angle_critic.hpp"
20 namespace mppi::critics
25 auto getParentParam = parameters_handler_->
getParamGetter(parent_name_);
27 getParentParam(vx_min,
"vx_min", -0.35);
28 if (fabs(vx_min) < 1e-6f) {
29 reversing_allowed_ =
false;
30 }
else if (vx_min < 0.0f) {
31 reversing_allowed_ =
true;
35 getParam(offset_from_furthest_,
"offset_from_furthest", 4);
36 getParam(power_,
"cost_power", 1);
37 getParam(weight_,
"cost_weight", 2.2f);
39 threshold_to_consider_,
40 "threshold_to_consider", 0.5f);
42 max_angle_to_furthest_,
43 "max_angle_to_furthest", 0.785398f);
46 getParam(mode,
"mode", mode);
47 mode_ =
static_cast<PathAngleMode
>(mode);
48 if (!reversing_allowed_ && mode_ == PathAngleMode::NO_DIRECTIONAL_PREFERENCE) {
49 mode_ = PathAngleMode::FORWARD_PREFERENCE;
52 "Path angle mode set to no directional preference, but controller's settings "
53 "don't allow for reversing! Setting mode to forward preference.");
58 "PathAngleCritic instantiated with %d power and %f weight. Mode set to: %s",
59 power_, weight_, modeToStr(mode_).c_str());
64 if (!enabled_ || data.state.local_path_length < threshold_to_consider_) {
68 utils::setPathFurthestPointIfNotSet(data);
69 auto offsetted_idx = std::min(
70 *data.furthest_reached_path_point + offset_from_furthest_,
71 static_cast<size_t>(data.path.x.size()) - 1);
73 const float goal_x = data.path.x(offsetted_idx);
74 const float goal_y = data.path.y(offsetted_idx);
75 const float goal_yaw = data.path.yaws(offsetted_idx);
76 const geometry_msgs::msg::Pose & pose = data.state.pose.pose;
79 case PathAngleMode::FORWARD_PREFERENCE:
80 if (utils::posePointAngle(pose, goal_x, goal_y,
true) < max_angle_to_furthest_) {
84 case PathAngleMode::NO_DIRECTIONAL_PREFERENCE:
85 if (utils::posePointAngle(pose, goal_x, goal_y,
false) < max_angle_to_furthest_) {
89 case PathAngleMode::CONSIDER_FEASIBLE_PATH_ORIENTATIONS:
90 if (utils::posePointAngle(pose, goal_x, goal_y, goal_yaw) < max_angle_to_furthest_) {
98 int last_idx = data.trajectories.y.cols() - 1;
99 auto diff_y = goal_y - data.trajectories.y.col(last_idx);
100 auto diff_x = goal_x - data.trajectories.x.col(last_idx);
101 auto yaws_between_points = diff_y.binaryExpr(
102 diff_x, [&](
const float & y,
const float & x){
return atan2f(y, x);}).eval();
105 case PathAngleMode::FORWARD_PREFERENCE:
107 auto last_yaws = data.trajectories.yaws.col(last_idx);
108 auto yaws = utils::shortest_angular_distance(
109 last_yaws, yaws_between_points).abs();
111 data.costs += (yaws * weight_).pow(power_);
113 data.costs += yaws * weight_;
117 case PathAngleMode::NO_DIRECTIONAL_PREFERENCE:
119 auto last_yaws = data.trajectories.yaws.col(last_idx);
120 auto yaws_between_points_corrected = utils::normalize_yaws_between_points(last_yaws,
121 yaws_between_points);
122 auto corrected_yaws = utils::shortest_angular_distance(
123 last_yaws, yaws_between_points_corrected).abs();
125 data.costs += (corrected_yaws * weight_).pow(power_);
127 data.costs += corrected_yaws * weight_;
131 case PathAngleMode::CONSIDER_FEASIBLE_PATH_ORIENTATIONS:
133 auto last_yaws = data.trajectories.yaws.col(last_idx);
134 auto yaws_between_points_corrected = utils::normalize_yaws_between_points(goal_yaw,
135 yaws_between_points);
136 auto corrected_yaws = utils::shortest_angular_distance(
137 last_yaws, yaws_between_points_corrected).abs();
139 data.costs += (corrected_yaws * weight_).pow(power_);
141 data.costs += corrected_yaws * weight_;
150 #include <pluginlib/class_list_macros.hpp>
152 PLUGINLIB_EXPORT_CLASS(
auto getParamGetter(const std::string &ns)
Get an object to retrieve parameters.
Abstract critic objective function to score trajectories.
void initialize() override
Initialize critic.
void score(CriticData &data) override
Evaluate cost related to robot orientation at goal pose (considered only if robot near last goal in c...
Data to pass to critics for scoring, including state, trajectories, pruned path, global goal,...