Fix std::normal_distribution when stdev is zero#73
Merged
Conversation
Signed-off-by: Francisco Martín Rico <fmrico@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR prevents runtime aborts from std::normal_distribution when a computed standard deviation becomes zero by clamping distribution standard deviations to a small positive minimum across both AMCL localizers.
Changes:
- Clamp initialization noise (
std_dev_xy,std_dev_yaw) to a minimum value when constructing normal distributions. - Clamp motion-model noise in
predict()(translation and yaw) to avoid zero-stdev distributions when there is no motion. - Clamp additional distributions used during initialization/reseeding (e.g., yaw noise and
index_dist) to avoid zero-stdev assertions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| localizers/easynav_simple_localizer/src/easynav_simple_localizer/AMCLLocalizer.cpp | Adds min-stdev clamping for several normal distributions used in init, pose init callback, prediction, and reseeding. |
| localizers/easynav_costmap_localizer/src/easynav_costmap_localizer/AMCLLocalizer.cpp | Mirrors the same min-stdev clamping changes in the costmap-based AMCL implementation. |
Comments suppressed due to low confidence (2)
localizers/easynav_simple_localizer/src/easynav_simple_localizer/AMCLLocalizer.cpp:643
reseed()can be called withparticles_.size() == 1, which makesN_top == 0. That leads to invalid covariance math (division by zero) and an infinite loop when selectingselected_idxbecause there is no valid index in[0, N_top). Add a guard forN_top == 0and make the covariance-derived Cholesky terms robust to zero/degenerate covariance.
double yaw_variance = computeYawVariance(particles_, 0, N_top);
double yaw_stddev = std::sqrt(yaw_variance);
std::normal_distribution<double> yaw_noise(0.0, std::max(yaw_stddev, min_noise_yaw_));
std::normal_distribution<double> index_dist(0.0, std::max(0.05 * static_cast<double>(N), 1e-12));
std::normal_distribution<double> standard_normal(0.0, 1.0);
localizers/easynav_costmap_localizer/src/easynav_costmap_localizer/AMCLLocalizer.cpp:687
reseed()can be called withparticles_.size() == 1, which makesN_top == 0. That leads to invalid covariance math (division by zero) and an infinite loop when selectingselected_idxbecause there is no valid index in[0, N_top). Add a guard forN_top == 0and make the covariance-derived Cholesky terms robust to zero/degenerate covariance.
double yaw_variance = computeYawVariance(particles_, 0, N_top);
double yaw_stddev = std::sqrt(yaw_variance);
std::normal_distribution<double> yaw_noise(0.0, std::max(yaw_stddev, min_noise_yaw_));
std::normal_distribution<double> index_dist(0.0, std::max(0.05 * static_cast<double>(N), 1e-12));
std::normal_distribution<double> standard_normal(0.0, 1.0);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+505
to
+509
| for (auto & p : particles_) { | ||
| std::normal_distribution<double> noise_dx(0.0, std::abs(dx) * noise_translation_); | ||
| std::normal_distribution<double> noise_dy(0.0, std::abs(dy) * noise_translation_); | ||
| std::normal_distribution<double> noise_dz(0.0, std::abs(dz) * noise_translation_); | ||
| std::normal_distribution<double> noise_dx( | ||
| 0.0, std::max(std::abs(dx) * noise_translation_, 1e-12)); | ||
| std::normal_distribution<double> noise_dy( | ||
| 0.0, std::max(std::abs(dy) * noise_translation_, 1e-12)); |
Comment on lines
+548
to
+552
| for (auto & p : particles_) { | ||
| std::normal_distribution<double> noise_dx(0.0, std::abs(dx) * noise_translation_); | ||
| std::normal_distribution<double> noise_dy(0.0, std::abs(dy) * noise_translation_); | ||
| std::normal_distribution<double> noise_dz(0.0, std::abs(dz) * noise_translation_); | ||
| std::normal_distribution<double> noise_dx( | ||
| 0.0, std::max(std::abs(dx) * noise_translation_, 1e-12)); | ||
| std::normal_distribution<double> noise_dy( | ||
| 0.0, std::max(std::abs(dy) * noise_translation_, 1e-12)); |
Signed-off-by: Francisco Martín Rico <fmrico@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dear EasyNavers,
I found this execution error in Ubuntu 26.04:
This PR fixes this bug by using a minumun of 1e-12 for stdev.
I hope it helps!!!