42#include <stomp/stomp.h>
50#include <stomp_moveit_parameters.hpp>
69 const robot_trajectory::RobotTrajectoryPtr& input_trajectory,
70 robot_trajectory::RobotTrajectoryPtr& output_trajectory)
72 Eigen::MatrixXd waypoints;
75 if (!input_trajectory || input_trajectory->empty())
86 output_trajectory = std::make_shared<robot_trajectory::RobotTrajectory>(start_state.
getRobotModel(), group);
95 const moveit::core::RobotModelConstPtr& robot_model,
96 robot_trajectory::RobotTrajectoryPtr& seed)
98 if (req.trajectory_constraints.constraints.empty())
103 const auto* joint_group = robot_model->getJointModelGroup(req.group_name);
104 const auto& names = joint_group->getActiveJointModelNames();
105 const auto dof = names.size();
107 trajectory_msgs::msg::JointTrajectory seed_traj;
108 const auto& constraints = req.trajectory_constraints.constraints;
110 for (
size_t i = 0; i < constraints.size(); ++i)
112 auto n = constraints[i].joint_constraints.size();
115 RCLCPP_WARN(getLogger(),
"Seed trajectory index %lu does not have %lu constraints (has %lu instead).", i, dof, n);
119 trajectory_msgs::msg::JointTrajectoryPoint joint_pt;
121 for (
size_t j = 0; j < constraints[i].joint_constraints.size(); ++j)
123 const auto& c = constraints[i].joint_constraints[j];
124 if (c.joint_name != names[j])
126 RCLCPP_WARN(getLogger(),
127 "Seed trajectory (index %lu, joint %lu) joint name '%s' does not match expected name '%s'", i, j,
128 c.joint_name.c_str(), names[j].c_str());
131 joint_pt.positions.push_back(c.position);
134 seed_traj.points.push_back(joint_pt);
136 seed_traj.joint_names = names;
140 seed = std::make_shared<robot_trajectory::RobotTrajectory>(robot_model, joint_group);
141 seed->setRobotTrajectoryMsg(robot_state, seed_traj);
143 return !seed->empty();
149 const size_t num_timesteps = config.num_timesteps;
163 if (!constraints.
empty())
175 const std::vector<double> stddev(group->getActiveJointModels().size(), 0.1);
179 auto iteration_callback_fn =
181 auto done_callback_fn =
185 stomp::TaskPtr task =
186 std::make_shared<ComposableTask>(noise_generator_fn, cost_fn, filter_fn, iteration_callback_fn, done_callback_fn);
191stomp::StompConfiguration
getStompConfig(
const stomp_moveit::Params& params,
size_t num_dimensions)
193 stomp::StompConfiguration config;
194 config.num_dimensions = num_dimensions;
196 config.initialization_method = stomp::TrajectoryInitializations::LINEAR_INTERPOLATION;
197 config.num_iterations = params.num_iterations;
198 config.num_iterations_after_valid = params.num_iterations_after_valid;
199 config.num_timesteps = params.num_timesteps;
200 config.delta_t = params.delta_t;
201 config.exponentiated_cost_sensitivity = params.exponentiated_cost_sensitivity;
202 config.num_rollouts = params.num_rollouts;
203 config.max_rollouts = params.max_rollouts;
204 config.control_cost_weight = params.control_cost_weight;
210 const stomp_moveit::Params& params)
218 auto time_start = std::chrono::steady_clock::now();
222 res.
error_code.val = moveit_msgs::msg::MoveItErrorCodes::SUCCESS;
230 if (!goal_sampler || !goal_sampler->sample(goal_state))
232 res.
error_code.val = moveit_msgs::msg::MoveItErrorCodes::INVALID_GOAL_CONSTRAINTS;
238 auto config =
getStompConfig(params_, group->getActiveJointModels().size() );
239 robot_trajectory::RobotTrajectoryPtr input_trajectory;
242 config.num_timesteps = input_trajectory->size();
245 stomp_ = std::make_shared<stomp::Stomp>(config, task);
247 std::condition_variable cv;
249 bool finished =
false;
250 auto timeout_future = std::async(std::launch::async, [&,
stomp = stomp_]() {
251 std::unique_lock<std::mutex> lock(cv_mutex);
252 cv.wait_for(lock, std::chrono::duration<double>(req.allowed_planning_time), [&finished] { return finished; });
264 timeout_future.valid() && timeout_future.wait_for(std::chrono::nanoseconds(1)) == std::future_status::ready;
266 timed_out ? moveit_msgs::msg::MoveItErrorCodes::TIMED_OUT : moveit_msgs::msg::MoveItErrorCodes::PLANNING_FAILED;
270 std::unique_lock<std::mutex> lock(cv_mutex);
276 std::chrono::duration<double> elapsed_seconds = std::chrono::steady_clock::now() - time_start;
283 RCLCPP_ERROR(getLogger(),
284 "StompPlanningContext::solve(planning_interface::MotionPlanDetailedResponse&) is not implemented!");
294 return stomp->cancel();
305 const std::shared_ptr<rclcpp::Publisher<visualization_msgs::msg::MarkerArray>>& path_publisher)
307 path_publisher_ = path_publisher;
312 return path_publisher_;
This class assists in the generation of a ConstraintSampler for a particular group from a moveit_msgs...
ConstraintSamplerPtr selectSampler(const planning_scene::PlanningSceneConstPtr &scene, const std::string &group_name, const moveit_msgs::msg::Constraints &constr) const
Selects among the potential sampler allocators.
A class that contains many different constraints, and can check RobotState *versus the full set....
bool empty() const
Returns whether or not there are any constraints in the set.
bool add(const moveit_msgs::msg::Constraints &c, const moveit::core::Transforms &tf)
Add all known constraints.
const moveit_msgs::msg::Constraints & getAllConstraints() const
Get all constraints in the set.
const std::vector< const JointModel * > & getActiveJointModels() const
Get the active joints in this group (that have controllable DOF). This does not include mimic joints.
Representation of a robot's state. This includes position, velocity, acceleration and effort.
const RobotModelConstPtr & getRobotModel() const
Get the robot model this state is constructed for.
Representation of a particular planning context – the planning scene and the request are known,...
MotionPlanRequest request_
The planning request for this context.
const MotionPlanRequest & getMotionPlanRequest() const
Get the motion plan request associated to this planning context.
const planning_scene::PlanningSceneConstPtr & getPlanningScene() const
Get the planning scene associated to this planning context.
const std::string & getGroupName() const
Get the name of the group this planning context is for.
void clear() override
Clear the data structures used by the planner.
void solve(planning_interface::MotionPlanResponse &res) override
Solve the motion planning problem and store the result in res. This function should not clear data st...
std::shared_ptr< rclcpp::Publisher< visualization_msgs::msg::MarkerArray > > getPathPublisher()
void setPathPublisher(const std::shared_ptr< rclcpp::Publisher< visualization_msgs::msg::MarkerArray > > &path_publisher)
bool terminate() override
If solve() is running, terminate the computation. Return false if termination not possible....
StompPlanningContext(const std::string &name, const std::string &group_name, const stomp_moveit::Params ¶ms)
MoveIt-based cost functions that can be passed to STOMP via a ComposableTask.
Filter functions that can be passed to STOMP via a ComposableTask.
rclcpp::Logger getLogger()
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::msg::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
Convert a robot state msg (with accompanying extra transforms) to a MoveIt robot state.
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
This namespace includes the base class for MoveIt planners.
moveit_msgs::msg::MotionPlanRequest MotionPlanRequest
This namespace includes the central class for representing planning contexts.
CostFn getConstraintsCostFunction(const std::shared_ptr< const planning_scene::PlanningScene > &planning_scene, const moveit::core::JointModelGroup *group, const moveit_msgs::msg::Constraints &constraints_msg, double cost_scale)
CostFn sum(const std::vector< CostFn > &cost_functions)
CostFn getCollisionCostFunction(const std::shared_ptr< const planning_scene::PlanningScene > &planning_scene, const moveit::core::JointModelGroup *group, double collision_penalty)
FilterFn chain(const std::vector< FilterFn > &filter_functions)
FilterFn enforcePositionBounds(const moveit::core::JointModelGroup *group)
FilterFn simpleSmoothingMatrix(size_t num_timesteps)
NoiseGeneratorFn getNormalDistributionGenerator(size_t num_timesteps, const std::vector< double > &stddev)
PostIterationFn getIterationPathPublisher(const rclcpp::Publisher< visualization_msgs::msg::MarkerArray >::SharedPtr &marker_publisher, const std::shared_ptr< const planning_scene::PlanningScene > &planning_scene, const moveit::core::JointModelGroup *group)
Get post iteration function that publishes the EE path of the generated trajectory.
DoneFn getSuccessTrajectoryPublisher(const rclcpp::Publisher< visualization_msgs::msg::MarkerArray >::SharedPtr &marker_publisher, const std::shared_ptr< const planning_scene::PlanningScene > &planning_scene, const moveit::core::JointModelGroup *group)
Get Done function that publishes the EE path of the generated trajectory.
Eigen::MatrixXd robotTrajectoryToMatrix(const robot_trajectory::RobotTrajectory &trajectory)
void fillRobotTrajectory(const Eigen::MatrixXd &trajectory_values, const moveit::core::RobotState &reference_state, robot_trajectory::RobotTrajectory &trajectory)
bool extractSeedTrajectory(const planning_interface::MotionPlanRequest &req, const moveit::core::RobotModelConstPtr &robot_model, robot_trajectory::RobotTrajectoryPtr &seed)
bool solveWithStomp(const std::shared_ptr< stomp::Stomp > &stomp, const moveit::core::RobotState &start_state, const moveit::core::RobotState &goal_state, const moveit::core::JointModelGroup *group, const robot_trajectory::RobotTrajectoryPtr &input_trajectory, robot_trajectory::RobotTrajectoryPtr &output_trajectory)
std::function< bool(const Eigen::MatrixXd &values, Eigen::VectorXd &costs, bool &validity)> CostFn
stomp::StompConfiguration getStompConfig(const stomp_moveit::Params ¶ms, size_t num_dimensions)
std::vector< double > getPositions(const moveit::core::RobotState &state, const Joints &joints)
stomp::TaskPtr createStompTask(const stomp::StompConfiguration &config, StompPlanningContext &context)
Noise generator functions for randomizing trajectories in STOMP via a ComposableTask.
Planning Context implementation for STOMP.
A STOMP task definition that allows injecting custom functions for planning.
Response to a planning query.
moveit::core::MoveItErrorCode error_code
robot_trajectory::RobotTrajectoryPtr trajectory
: Helper functions for visualizing trajectory markers for STOMP planning iterations.