Skip to content

Reduce MPPI computation time, add OpenMP support#11

Open
adivardi wants to merge 32 commits intoenway-develfrom
av/mppi_freq
Open

Reduce MPPI computation time, add OpenMP support#11
adivardi wants to merge 32 commits intoenway-develfrom
av/mppi_freq

Conversation

@adivardi
Copy link
Copy Markdown
Collaborator

@adivardi adivardi commented Mar 20, 2026

Additions & Changes

This PR improves the runtime frequency of the MPPI controller. The goal is to reduce the occurrences of the controller's frequency dropping below the requested 20Hz, which causes degraded performance.

The main culprit in MPPI are the collision checks, which take a big percentage of the runtime, especially when driving near a long obstacles (such as a wall)

Changes:

Cost critic:

  • Check for collisions backwards, starting from the end of the trajectory, since collisions are more probable away from robot.
    • reduces by avg of 2ms & 5000 footprint checks (avg over 50m, ~ 2000 iterations)
  • Check center cost before doing a collision check
  • Call costmap_ros_->getRobotFootprint()) once
  • Add OpenMP support for the collision checks
    • With 8 threads, reduces time by x4

Optimizer:

  • Clamp exponential arguments: caps all exp() arguments at -80, where the value is already ~1e-35, slightly above float32's smallest fraction of ~1.2e-38. This eliminates potential underflows & slow paths in the exp implementation.
    • reduces computation by ~6x, from 2000-3000us to ~400us
  • Add noalias call
    • very slight reduction in computation, but no harm to add

Checklist (tick off when done or remove when not relevant):

  • Set good PR title for changelog
  • Perform a self-review if the PR has more than 50 lines of changes

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a comprehensive overhaul of the MPPI controller's visualization framework, providing users with much finer control over debugging and analysis outputs. Alongside these visualization enhancements, a new critic has been integrated to promote smoother robot motion by penalizing sudden direction changes. Significant performance gains are also achieved through the introduction of OpenMP parallelization in the core cost evaluation, making the controller more efficient and robust.

Highlights

  • Enhanced Visualization System: The MPPI controller's visualization capabilities have been significantly refactored, introducing granular control over what is published. This includes separate toggles for optimal trajectory messages, optimal path, optimal footprints, and candidate trajectories with total or individual critic costs. The visualization parameters are now grouped under a dedicated 'Visualization' namespace in the YAML configuration.
  • New Direction Change Critic: A new DirectionChangeCritic has been added to penalize trajectories that involve abrupt changes in the robot's driving direction. This critic uses the robot's actual speed from odometry feedback to assess and penalize velocity differences when crossing zero, promoting smoother motion.
  • Performance Improvements with OpenMP: The CostCritic now supports OpenMP parallelization, allowing it to utilize multiple CPU cores for faster collision checking. A new parameter num_threads has been introduced to configure the number of threads, with an automatic heuristic for optimal thread count.
  • Improved Collision Checking Logic: The CostCritic's inCollision method has been optimized to perform an early exit if the center cost already indicates a lethal or inscribed obstacle, avoiding more expensive footprint checks. The collision checking now iterates trajectories backward, prioritizing collision detection at the end of trajectories.
  • Refined Optimizer Stability: The softmax calculation within the optimizer has been improved to prevent exponent overflow, enhancing numerical stability, especially when dealing with very large cost differences between trajectories.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly enhances the nav2_mppi_controller by refactoring and expanding its visualization capabilities, introducing a new DirectionChangeCritic, and implementing performance optimizations. The TrajectoryVisualizer now supports publishing various data including candidate trajectories with total and individual critic costs, optimal trajectories, paths, and footprints, controlled by new parameters. OpenMP parallelization has been integrated into the CostCritic for improved performance, along with optimizations to collision checking and Eigen matrix operations. A new DirectionChangeCritic is added to penalize changes in driving direction. A review comment highlights an unimplemented feature related to publish_transformed_path that is being tested, suggesting either implementation or test removal. Another comment points out an unused variable in the DirectionChangeCritic implementation that should be removed for code cleanliness.

@adivardi adivardi force-pushed the av/mppi_freq branch 2 times, most recently from 17a55cd to 5a96778 Compare March 23, 2026 07:52
@adivardi adivardi changed the title Av/mppi freq Reduce MPPI computation time, add OpenMP support Mar 23, 2026
@adivardi adivardi added the enhancement New feature or request label Mar 23, 2026
Copy link
Copy Markdown
Collaborator Author

@adivardi adivardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self review

@adivardi adivardi marked this pull request as ready for review March 23, 2026 09:59
@adivardi adivardi requested a review from georgflick March 23, 2026 09:59
adivardi added 11 commits March 27, 2026 13:56
The clamp (min(80 * temperature)) caps all exp() arguments at -80, where the value is already ~1e-35, slightly above float32's smallest fraction of ~1.2e-38.
This eliminates potential underflows &= slow paths in the exp implementation.
This reduces computation by ~6x, from 2000-300us to ~400us
Let's Eigen know there is no aliasing, very small optimization
Reduces computation time from 18-20ms to 5ms
This reverts commit 9d721b9.
Copy link
Copy Markdown

@georgflick georgflick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need to add the openMP dependency also to the package.xml ? or are you not adding it, because it's optional?

@@ -0,0 +1,51 @@
// Copyright (c) 2024 Enway GmbH, Adi Vardi
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be 2026?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -0,0 +1,69 @@
// Copyright (c) 2024 Enway GmbH, Adi Vardi
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@adivardi
Copy link
Copy Markdown
Collaborator Author

adivardi commented Apr 8, 2026

don't you need to add the openMP dependency also to the package.xml ? or are you not adding it, because it's optional?

yeah. I copied from ros-navigation#5933

What do you think? I guess I will also PR this upstream at some point

@adivardi adivardi requested a review from georgflick April 8, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants