16 #include "nav2_mppi_controller/critics/path_angle_critic.hpp"
20 namespace mppi::critics
25 auto getParentParam = parameters_handler_->
getParamGetter(parent_name_);
26 getParentParam(enforce_path_inversion_,
"enforce_path_inversion",
false);
28 getParentParam(vx_min,
"vx_min", -0.35);
29 if (fabs(vx_min) < 1e-6f) {
30 reversing_allowed_ =
false;
31 }
else if (vx_min < 0.0f) {
32 reversing_allowed_ =
true;
36 getParam(offset_from_furthest_,
"offset_from_furthest", 4);
37 getParam(power_,
"cost_power", 1);
38 getParam(weight_,
"cost_weight", 2.2f);
40 threshold_to_consider_,
41 "threshold_to_consider", 0.5f);
43 max_angle_to_furthest_,
44 "max_angle_to_furthest", 0.785398f);
47 getParam(mode,
"mode", mode);
48 mode_ =
static_cast<PathAngleMode
>(mode);
49 if (!reversing_allowed_ && mode_ == PathAngleMode::NO_DIRECTIONAL_PREFERENCE) {
50 mode_ = PathAngleMode::FORWARD_PREFERENCE;
53 "Path angle mode set to no directional preference, but controller's settings "
54 "don't allow for reversing! Setting mode to forward preference.");
59 "PathAngleCritic instantiated with %d power and %f weight. Mode set to: %s",
60 power_, weight_, modeToStr(mode_).c_str());
69 geometry_msgs::msg::Pose goal = utils::getCriticGoal(data, enforce_path_inversion_);
71 if (utils::withinPositionGoalTolerance(
72 threshold_to_consider_, data.state.pose.pose, goal))
77 utils::setPathFurthestPointIfNotSet(data);
78 auto offsetted_idx = std::min(
79 *data.furthest_reached_path_point + offset_from_furthest_,
80 static_cast<size_t>(data.path.x.size()) - 1);
82 const float goal_x = data.path.x(offsetted_idx);
83 const float goal_y = data.path.y(offsetted_idx);
84 const float goal_yaw = data.path.yaws(offsetted_idx);
85 const geometry_msgs::msg::Pose & pose = data.state.pose.pose;
88 case PathAngleMode::FORWARD_PREFERENCE:
89 if (utils::posePointAngle(pose, goal_x, goal_y,
true) < max_angle_to_furthest_) {
93 case PathAngleMode::NO_DIRECTIONAL_PREFERENCE:
94 if (utils::posePointAngle(pose, goal_x, goal_y,
false) < max_angle_to_furthest_) {
98 case PathAngleMode::CONSIDER_FEASIBLE_PATH_ORIENTATIONS:
99 if (utils::posePointAngle(pose, goal_x, goal_y, goal_yaw) < max_angle_to_furthest_) {
107 int last_idx = data.trajectories.y.cols() - 1;
108 auto diff_y = goal_y - data.trajectories.y.col(last_idx);
109 auto diff_x = goal_x - data.trajectories.x.col(last_idx);
110 auto yaws_between_points = diff_y.binaryExpr(
111 diff_x, [&](
const float & y,
const float & x){
return atan2f(y, x);}).eval();
114 case PathAngleMode::FORWARD_PREFERENCE:
116 auto last_yaws = data.trajectories.yaws.col(last_idx);
117 auto yaws = utils::shortest_angular_distance(
118 last_yaws, yaws_between_points).abs();
120 data.costs += (yaws * weight_).pow(power_);
122 data.costs += yaws * weight_;
126 case PathAngleMode::NO_DIRECTIONAL_PREFERENCE:
128 auto last_yaws = data.trajectories.yaws.col(last_idx);
129 auto yaws_between_points_corrected = utils::normalize_yaws_between_points(last_yaws,
130 yaws_between_points);
131 auto corrected_yaws = utils::shortest_angular_distance(
132 last_yaws, yaws_between_points_corrected).abs();
134 data.costs += (corrected_yaws * weight_).pow(power_);
136 data.costs += corrected_yaws * weight_;
140 case PathAngleMode::CONSIDER_FEASIBLE_PATH_ORIENTATIONS:
142 auto last_yaws = data.trajectories.yaws.col(last_idx);
143 auto yaws_between_points_corrected = utils::normalize_yaws_between_points(goal_yaw,
144 yaws_between_points);
145 auto corrected_yaws = utils::shortest_angular_distance(
146 last_yaws, yaws_between_points_corrected).abs();
148 data.costs += (corrected_yaws * weight_).pow(power_);
150 data.costs += corrected_yaws * weight_;
159 #include <pluginlib/class_list_macros.hpp>
161 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,...