15 #ifndef NAV2_MPPI_CONTROLLER__OPTIMIZER_HPP_
16 #define NAV2_MPPI_CONTROLLER__OPTIMIZER_HPP_
22 #pragma GCC diagnostic push
23 #pragma GCC diagnostic ignored "-Warray-bounds"
24 #pragma GCC diagnostic ignored "-Wstringop-overflow"
25 #include <xtensor/xtensor.hpp>
26 #include <xtensor/xview.hpp>
27 #pragma GCC diagnostic pop
29 #include "rclcpp_lifecycle/lifecycle_node.hpp"
31 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
32 #include "nav2_core/goal_checker.hpp"
33 #include "nav2_core/controller_exceptions.hpp"
35 #include "geometry_msgs/msg/twist.hpp"
36 #include "geometry_msgs/msg/pose_stamped.hpp"
37 #include "geometry_msgs/msg/twist_stamped.hpp"
38 #include "nav_msgs/msg/path.hpp"
40 #include "nav2_mppi_controller/models/optimizer_settings.hpp"
41 #include "nav2_mppi_controller/motion_models.hpp"
42 #include "nav2_mppi_controller/critic_manager.hpp"
43 #include "nav2_mppi_controller/models/state.hpp"
44 #include "nav2_mppi_controller/models/trajectories.hpp"
45 #include "nav2_mppi_controller/models/path.hpp"
46 #include "nav2_mppi_controller/tools/noise_generator.hpp"
47 #include "nav2_mppi_controller/tools/parameters_handler.hpp"
48 #include "nav2_mppi_controller/tools/utils.hpp"
79 rclcpp_lifecycle::LifecycleNode::WeakPtr parent,
const std::string & name,
80 std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros,
97 const geometry_msgs::msg::PoseStamped & robot_pose,
98 const geometry_msgs::msg::Twist & robot_speed,
const nav_msgs::msg::Path & plan,
124 void reset(
bool reset_dynamic_speed_limits =
true);
140 const geometry_msgs::msg::PoseStamped & robot_pose,
141 const geometry_msgs::msg::Twist & robot_speed,
142 const nav_msgs::msg::Path & plan,
207 xt::xtensor<float, 2> & trajectories,
208 const xt::xtensor<float, 2> & state)
const;
221 geometry_msgs::msg::TwistStamped
234 void setOffset(
double controller_frequency);
243 rclcpp_lifecycle::LifecycleNode::WeakPtr parent_;
244 std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros_;
248 std::shared_ptr<MotionModel> motion_model_;
258 std::array<mppi::models::Control, 4> control_history_;
261 geometry_msgs::msg::Pose goal_;
262 xt::xtensor<float, 1> costs_;
265 state_, generated_trajectories_, path_, goal_,
266 costs_, settings_.model_dt,
false,
nullptr,
nullptr,
267 std::nullopt, std::nullopt};
269 rclcpp::Logger
logger_{rclcpp::get_logger(
"MPPIController")};
Manager of objective function plugins for scoring trajectories.
Generates noise trajectories from optimal trajectory.
Main algorithm optimizer of the MPPI Controller.
geometry_msgs::msg::TwistStamped evalControl(const geometry_msgs::msg::PoseStamped &robot_pose, const geometry_msgs::msg::Twist &robot_speed, const nav_msgs::msg::Path &plan, const geometry_msgs::msg::Pose &goal, nav2_core::GoalChecker *goal_checker)
Compute control using MPPI algorithm.
void updateStateVelocities(models::State &state) const
Update velocities in state.
void setMotionModel(const std::string &model)
Set the motion model of the vehicle platform.
void setOffset(double controller_frequency)
Using control frequence and time step size, determine if trajectory offset should be used to populate...
void reset(bool reset_dynamic_speed_limits=true)
Reset the optimization problem to initial conditions.
rclcpp::Logger logger_
Caution, keep references.
void prepare(const geometry_msgs::msg::PoseStamped &robot_pose, const geometry_msgs::msg::Twist &robot_speed, const nav_msgs::msg::Path &plan, const geometry_msgs::msg::Pose &goal, nav2_core::GoalChecker *goal_checker)
Prepare state information on new request for trajectory rollouts.
void integrateStateVelocities(models::Trajectories &trajectories, const models::State &state) const
Rollout velocities in state to poses.
~Optimizer()
Destructor for mppi::Optimizer.
void updateControlSequence()
Update control sequence with state controls weighted by costs using softmax function.
void generateNoisedTrajectories()
updates generated trajectories with noised trajectories from the last cycle's optimal control
bool fallback(bool fail)
Perform fallback behavior to try to recover from a set of trajectories in collision.
bool isHolonomic() const
Whether the motion model is holonomic.
models::Trajectories & getGeneratedTrajectories()
Get the trajectories generated in a cycle for visualization.
void updateInitialStateVelocities(models::State &state) const
Update initial velocity in state.
xt::xtensor< float, 2 > getOptimizedTrajectory()
Get the optimal trajectory for a cycle for visualization.
void shutdown()
Shutdown for optimizer at process end.
void optimize()
Main function to generate, score, and return trajectories.
Optimizer()=default
Constructor for mppi::Optimizer.
void setSpeedLimit(double speed_limit, bool percentage)
Set the maximum speed based on the speed limits callback.
void shiftControlSequence()
Shift the optimal control sequence after processing for next iterations initial conditions after exec...
void applyControlSequenceConstraints()
Apply hard vehicle constraints on control sequence.
void getParams()
Obtain the main controller's parameters.
void propagateStateVelocitiesFromInitials(models::State &state) const
predict velocities in state using model for time horizon equal to timesteps
void initialize(rclcpp_lifecycle::LifecycleNode::WeakPtr parent, const std::string &name, std::shared_ptr< nav2_costmap_2d::Costmap2DROS > costmap_ros, ParametersHandler *dynamic_parameters_handler)
Initializes optimizer on startup.
geometry_msgs::msg::TwistStamped getControlFromSequenceAsTwist(const builtin_interfaces::msg::Time &stamp)
Convert control sequence to a twist commant.
Handles getting parameters and dynamic parmaeter changes.
Function-object for checking whether a goal has been reached.
A 2D costmap provides a mapping between points in the world and their associated "costs".
Data to pass to critics for scoring, including state, trajectories, pruned path, global goal,...
A control sequence over time (e.g. trajectory)
Settings for the optimizer to use.
Path represented as a tensor.
State information: velocities, controls, poses, speed.