15 #ifndef NAV2_MPPI_CONTROLLER__OPTIMIZER_HPP_
16 #define NAV2_MPPI_CONTROLLER__OPTIMIZER_HPP_
18 #include <Eigen/Dense>
26 #include "rclcpp_lifecycle/lifecycle_node.hpp"
28 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
29 #include "nav2_core/goal_checker.hpp"
30 #include "nav2_core/controller_exceptions.hpp"
31 #include "nav2_ros_common/tf2_factories.hpp"
32 #include "pluginlib/class_loader.hpp"
34 #include "geometry_msgs/msg/accel_stamped.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"
39 #include "nav2_util/geometry_utils.hpp"
41 #include "nav2_mppi_controller/models/optimizer_settings.hpp"
42 #include "nav2_mppi_controller/motion_models.hpp"
43 #include "nav2_mppi_controller/critic_manager.hpp"
44 #include "nav2_mppi_controller/models/state.hpp"
45 #include "nav2_mppi_controller/models/trajectories.hpp"
46 #include "nav2_mppi_controller/models/path.hpp"
47 #include "nav2_mppi_controller/tools/noise_generator.hpp"
48 #include "nav2_mppi_controller/tools/parameters_handler.hpp"
49 #include "nav2_mppi_controller/tools/utils.hpp"
50 #include "nav2_mppi_controller/optimal_trajectory_validator.hpp"
82 nav2::LifecycleNode::WeakPtr parent,
const std::string & name,
83 std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros,
84 nav2::TransformBuffer::SharedPtr tf_buffer,
101 std::tuple<geometry_msgs::msg::TwistStamped, Eigen::ArrayXXf>
evalControl(
102 const geometry_msgs::msg::PoseStamped & robot_pose,
103 const geometry_msgs::msg::Twist & robot_speed,
const nav_msgs::msg::Path & plan,
128 const Eigen::ArrayXf &
getCosts()
const {
return costs_;}
134 const std::vector<std::pair<std::string, Eigen::ArrayXf>> &
getCriticCosts()
const
145 return critics_data_.trajectories_in_collision;
159 void reset(
bool reset_dynamic_speed_limits =
true);
190 const geometry_msgs::msg::PoseStamped & robot_pose,
191 const geometry_msgs::msg::Twist & robot_speed,
192 const nav_msgs::msg::Path & plan,
263 Eigen::Array<float, Eigen::Dynamic, 3> & trajectories,
264 const Eigen::ArrayXXf & state)
const;
277 geometry_msgs::msg::TwistStamped
290 void setOffset(
double controller_period);
299 nav2::LifecycleNode::WeakPtr parent_;
300 std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros_;
303 nav2::TransformBuffer::SharedPtr tf_buffer_;
305 std::shared_ptr<MotionModel> motion_model_;
311 std::unique_ptr<pluginlib::ClassLoader<MotionModel>> motion_model_loader_;
312 std::unique_ptr<pluginlib::ClassLoader<OptimalTrajectoryValidator>> validator_loader_;
313 OptimalTrajectoryValidator::Ptr trajectory_validator_;
319 std::array<mppi::models::Control, 4> control_history_;
322 geometry_msgs::msg::Pose goal_;
323 Eigen::ArrayXf costs_;
326 state_, generated_trajectories_, path_, goal_,
327 costs_, settings_.model_dt,
false,
nullptr,
nullptr,
328 std::nullopt, std::nullopt, {}};
330 rclcpp::Logger
logger_{rclcpp::get_logger(
"MPPIController")};
332 geometry_msgs::msg::Twist last_command_vel_;
Manager of objective function plugins for scoring trajectories.
const std::vector< std::pair< std::string, Eigen::ArrayXf > > & getCriticCosts() const
Get stored per-critic costs from last evaluation.
Generates noise trajectories from optimal trajectory.
Main algorithm optimizer of the MPPI Controller.
void updateStateVelocities(models::State &state) const
Update velocities in state.
const std::vector< std::pair< std::string, Eigen::ArrayXf > > & getCriticCosts() const
Get per-critic cost breakdown from last evaluation.
const std::vector< bool > & getCollisionFlags() const
Get per-trajectory collision flags from last evaluation.
const models::ControlSequence & getOptimalControlSequence()
Get the optimal control sequence for a cycle for visualization.
void setMotionModel(const std::string &model)
Set the motion model of the vehicle platform.
Eigen::ArrayXXf getOptimizedTrajectory()
Get the optimal trajectory for a cycle for visualization.
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.
std::tuple< geometry_msgs::msg::TwistStamped, Eigen::ArrayXXf > 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 integrateStateVelocities(models::Trajectories &trajectories, const models::State &state) const
Rollout velocities in state to poses.
const models::OptimizerSettings & getSettings() const
Get the motion model time step.
void applyControlSequenceInterIterationConstraints()
Apply inter-iteration dynamic feasibility constraints on the first control sequence element before no...
~Optimizer()
Destructor for mppi::Optimizer.
void updateControlSequence()
Update control sequence with state controls weighted by costs using softmax function.
bool isSpeedLimitActive() const
Check if a dynamic speed limit is currently active.
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.
void shutdown()
Shutdown for optimizer at process end.
void optimize()
Main function to generate, score, and return trajectories.
void setOffset(double controller_period)
Using control period and time step size, determine if trajectory offset should be used to populate in...
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
const Eigen::ArrayXf & getCosts() const
Get the aggregated trajectory costs from last evaluation.
geometry_msgs::msg::TwistStamped getControlFromSequenceAsTwist(const builtin_interfaces::msg::Time &stamp)
Convert control sequence to a twist command.
void initialize(nav2::LifecycleNode::WeakPtr parent, const std::string &name, std::shared_ptr< nav2_costmap_2d::Costmap2DROS > costmap_ros, nav2::TransformBuffer::SharedPtr tf_buffer, ParametersHandler *dynamic_parameters_handler)
Initializes optimizer on startup.
Handles getting parameters and dynamic parameter 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.