15 #ifndef NAV2_MPPI_CONTROLLER__OPTIMIZER_HPP_
16 #define NAV2_MPPI_CONTROLLER__OPTIMIZER_HPP_
18 #include <Eigen/Dense>
24 #include "rclcpp_lifecycle/lifecycle_node.hpp"
26 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
27 #include "nav2_core/goal_checker.hpp"
28 #include "nav2_core/controller_exceptions.hpp"
29 #include "tf2_ros/buffer.hpp"
30 #include "pluginlib/class_loader.hpp"
32 #include "geometry_msgs/msg/twist.hpp"
33 #include "geometry_msgs/msg/pose_stamped.hpp"
34 #include "geometry_msgs/msg/twist_stamped.hpp"
35 #include "nav_msgs/msg/path.hpp"
37 #include "nav2_mppi_controller/models/optimizer_settings.hpp"
38 #include "nav2_mppi_controller/motion_models.hpp"
39 #include "nav2_mppi_controller/critic_manager.hpp"
40 #include "nav2_mppi_controller/models/state.hpp"
41 #include "nav2_mppi_controller/models/trajectories.hpp"
42 #include "nav2_mppi_controller/models/path.hpp"
43 #include "nav2_mppi_controller/tools/noise_generator.hpp"
44 #include "nav2_mppi_controller/tools/parameters_handler.hpp"
45 #include "nav2_mppi_controller/tools/utils.hpp"
46 #include "nav2_mppi_controller/optimal_trajectory_validator.hpp"
78 nav2::LifecycleNode::WeakPtr parent,
const std::string & name,
79 std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros,
80 std::shared_ptr<tf2_ros::Buffer> tf_buffer,
97 std::tuple<geometry_msgs::msg::TwistStamped, Eigen::ArrayXXf>
evalControl(
98 const geometry_msgs::msg::PoseStamped & robot_pose,
99 const geometry_msgs::msg::Twist & robot_speed,
const nav_msgs::msg::Path & plan,
131 void reset(
bool reset_dynamic_speed_limits =
true);
156 const geometry_msgs::msg::PoseStamped & robot_pose,
157 const geometry_msgs::msg::Twist & robot_speed,
158 const nav_msgs::msg::Path & plan,
223 Eigen::Array<float, Eigen::Dynamic, 3> & trajectories,
224 const Eigen::ArrayXXf & state)
const;
237 geometry_msgs::msg::TwistStamped
250 void setOffset(
double controller_frequency);
259 nav2::LifecycleNode::WeakPtr parent_;
260 std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros_;
263 std::shared_ptr<tf2_ros::Buffer> tf_buffer_;
265 std::shared_ptr<MotionModel> motion_model_;
271 std::unique_ptr<pluginlib::ClassLoader<OptimalTrajectoryValidator>> validator_loader_;
272 OptimalTrajectoryValidator::Ptr trajectory_validator_;
278 std::array<mppi::models::Control, 4> control_history_;
281 geometry_msgs::msg::Pose goal_;
282 Eigen::ArrayXf costs_;
285 state_, generated_trajectories_, path_, goal_,
286 costs_, settings_.model_dt,
false,
nullptr,
nullptr,
287 std::nullopt, std::nullopt};
289 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.
void updateStateVelocities(models::State &state) const
Update velocities in state.
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.
void setOffset(double controller_frequency)
Using control frequencies and time step size, determine if trajectory offset should be used to popula...
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 initialize(nav2::LifecycleNode::WeakPtr parent, const std::string &name, std::shared_ptr< nav2_costmap_2d::Costmap2DROS > costmap_ros, std::shared_ptr< tf2_ros::Buffer > tf_buffer, ParametersHandler *dynamic_parameters_handler)
Initializes optimizer on startup.
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.
~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.
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
geometry_msgs::msg::TwistStamped getControlFromSequenceAsTwist(const builtin_interfaces::msg::Time &stamp)
Convert control sequence to a twist command.
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.