Skip to content

Commit feb3418

Browse files
authored
[mppi] fix division by zero, clean raw pointers code (#5636)
* [mppi] fix division by zero leading to cost values being NaNs, which then propagate through all the critics and results in NaN control_sequence. These NaNs were removed by the hard applyControlSequenceConstraints(), but replaced with ax_max & wz_max. These lead to high steering at the start of a run Signed-off-by: Adi Vardi <adi.vardi@enway.ai> * [mppi] clean ackermann constraints Signed-off-by: Adi Vardi <adi.vardi@enway.ai> * fix long line Signed-off-by: Adi Vardi <adi.vardi@enway.ai> --------- Signed-off-by: Adi Vardi <adi.vardi@enway.ai>
1 parent 2a73560 commit feb3418

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

nav2_mppi_controller/include/nav2_mppi_controller/motion_models.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,10 @@ class AckermannMotionModel : public MotionModel
163163
*/
164164
void applyConstraints(models::ControlSequence & control_sequence) override
165165
{
166-
const auto vx_ptr = control_sequence.vx.data();
167-
auto wz_ptr = control_sequence.wz.data();
168-
int steps = control_sequence.vx.size();
169-
for(int i = 0; i < steps; i++) {
170-
float wz_constrained = fabs(*(vx_ptr + i) / min_turning_r_);
171-
float & wz_curr = *(wz_ptr + i);
172-
wz_curr = utils::clamp(-1 * wz_constrained, wz_constrained, wz_curr);
173-
}
166+
const auto wz_constrained = control_sequence.vx.abs() / min_turning_r_;
167+
control_sequence.wz = control_sequence.wz
168+
.max((-wz_constrained))
169+
.min(wz_constrained);
174170
}
175171

176172
/**

nav2_mppi_controller/src/critics/constraint_critic.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ void ConstraintCritic::score(CriticData & data)
8282
if (acker != nullptr) {
8383
auto & vx = data.state.vx;
8484
auto & wz = data.state.wz;
85-
float min_turning_rad = acker->getMinTurningRadius();
86-
auto out_of_turning_rad_motion = (min_turning_rad - (vx.abs() / wz.abs())).max(0.0f);
85+
const float min_turning_rad = acker->getMinTurningRadius();
86+
87+
const float epsilon = 1e-6f;
88+
auto wz_safe = wz.abs().max(epsilon); // Replace small wz values to avoid division by 0
89+
auto out_of_turning_rad_motion = (min_turning_rad - (vx.abs() / wz_safe)).max(0.0f);
90+
8791
if (power_ > 1u) {
8892
data.costs += ((((vx - max_vel_).max(0.0f) + (min_vel_ - vx).max(0.0f) +
8993
out_of_turning_rad_motion) * data.model_dt).rowwise().sum().eval() *

0 commit comments

Comments
 (0)