moveit2
The MoveIt Motion Planning Framework for ROS 2.
stomp_moveit_task.hpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2023, PickNik Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of PickNik Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
55 #pragma once
56 
57 #include <stomp/task.h>
58 
59 namespace stomp_moveit
60 {
61 // @brief A function that computes randomized noisy values for input trajectory values.
63  std::function<bool(const Eigen::MatrixXd& values, Eigen::MatrixXd& noisy_values, Eigen::MatrixXd& noise)>;
64 // @brief A function that computes cost values and a validity result for input trajectory values
65 using CostFn = std::function<bool(const Eigen::MatrixXd& values, Eigen::VectorXd& costs, bool& validity)>;
66 // @brief A function that applies a filter (smoothing, clamping, ...) of an input trajectory
67 using FilterFn = std::function<bool(const Eigen::MatrixXd& values, Eigen::MatrixXd& filtered_values)>;
68 // @brief A function that is called with every iteration to report on the current trajectory candidate and cost
69 using PostIterationFn = std::function<void(int iteration_number, double cost, const Eigen::MatrixXd& values)>;
70 // @brief A function that is called when STOMP terminates, providing success flag, cost and solution trajectory
71 using DoneFn =
72  std::function<void(bool success, int total_iterations, double final_cost, const Eigen::MatrixXd& values)>;
73 
74 // @brief A STOMP task that allows injecting generic callbacks for a STOMP planning instance
75 //
76 // The ComposableTask stores custom functions for the most important callback types in STOMP and applies them during
77 // a motion planning run. This class is used for injecting MoveIt concepts and other custom features into STOMP.
78 class ComposableTask final : public stomp::Task
79 {
80 public:
81  ComposableTask(NoiseGeneratorFn noise_generator_fn, CostFn cost_fn, FilterFn filter_fn,
82  PostIterationFn post_iteration_fn, DoneFn done_fn)
83  : noise_generator_fn_(std::move(noise_generator_fn))
84  , cost_fn_(std::move(cost_fn))
85  , filter_fn_(std::move(filter_fn))
86  , post_iteration_fn_(std::move(post_iteration_fn))
87  , done_fn_(std::move(done_fn))
88  {
89  }
90 
91  ~ComposableTask() = default;
92 
104  bool generateNoisyParameters(const Eigen::MatrixXd& parameters, std::size_t /*start_timestep*/,
105  std::size_t /*num_timesteps*/, int /*iteration_number*/, int /*rollout_number*/,
106  Eigen::MatrixXd& parameters_noise, Eigen::MatrixXd& noise) override
107  {
108  return noise_generator_fn_(parameters, parameters_noise, noise);
109  }
110 
121  bool computeCosts(const Eigen::MatrixXd& parameters, std::size_t /*start_timestep*/, std::size_t /*num_timesteps*/,
122  int /*iteration_number*/, Eigen::VectorXd& costs, bool& validity) override
123  {
124  return cost_fn_(parameters, costs, validity);
125  }
126 
138  bool computeNoisyCosts(const Eigen::MatrixXd& parameters, std::size_t /*start_timestep*/,
139  std::size_t /*num_timesteps*/, int /*iteration_number*/, int /*rollout_number*/,
140  Eigen::VectorXd& costs, bool& validity) override
141  {
142  return cost_fn_(parameters, costs, validity);
143  }
144 
156  bool filterParameterUpdates(std::size_t /*start_timestep*/, std::size_t /*num_timesteps*/, int /*iteration_number*/,
157  const Eigen::MatrixXd& parameters, Eigen::MatrixXd& updates) override
158  {
159  return filter_fn_(parameters, updates);
160  }
161 
171  void postIteration(std::size_t /*start_timestep*/, std::size_t /*num_timesteps*/, int iteration_number, double cost,
172  const Eigen::MatrixXd& parameters) override
173  {
174  post_iteration_fn_(iteration_number, cost, parameters);
175  }
176 
185  void done(bool success, int total_iterations, double final_cost, const Eigen::MatrixXd& parameters) override
186  {
187  done_fn_(success, total_iterations, final_cost, parameters);
188  }
189 
190 private:
191  NoiseGeneratorFn noise_generator_fn_;
192  CostFn cost_fn_;
193  FilterFn filter_fn_;
194  PostIterationFn post_iteration_fn_;
195  DoneFn done_fn_;
196 };
197 } // namespace stomp_moveit
bool filterParameterUpdates(std::size_t, std::size_t, int, const Eigen::MatrixXd &parameters, Eigen::MatrixXd &updates) override
Filters the given parameters which is applied after the update. It could be used for clipping of join...
void postIteration(std::size_t, std::size_t, int iteration_number, double cost, const Eigen::MatrixXd &parameters) override
Called by STOMP at the end of each iteration.
void done(bool success, int total_iterations, double final_cost, const Eigen::MatrixXd &parameters) override
Called by Stomp at the end of the optimization process.
bool generateNoisyParameters(const Eigen::MatrixXd &parameters, std::size_t, std::size_t, int, int, Eigen::MatrixXd &parameters_noise, Eigen::MatrixXd &noise) override
Generates a noisy trajectory from the parameters.
ComposableTask(NoiseGeneratorFn noise_generator_fn, CostFn cost_fn, FilterFn filter_fn, PostIterationFn post_iteration_fn, DoneFn done_fn)
bool computeNoisyCosts(const Eigen::MatrixXd &parameters, std::size_t, std::size_t, int, int, Eigen::VectorXd &costs, bool &validity) override
computes the state costs as a function of the distance from the bias parameters
bool computeCosts(const Eigen::MatrixXd &parameters, std::size_t, std::size_t, int, Eigen::VectorXd &costs, bool &validity) override
computes the state costs as a function of the distance from the bias parameters
std::function< bool(const Eigen::MatrixXd &values, Eigen::MatrixXd &noisy_values, Eigen::MatrixXd &noise)> NoiseGeneratorFn
std::function< void(bool success, int total_iterations, double final_cost, const Eigen::MatrixXd &values)> DoneFn
std::function< bool(const Eigen::MatrixXd &values, Eigen::MatrixXd &filtered_values)> FilterFn
std::function< bool(const Eigen::MatrixXd &values, Eigen::VectorXd &costs, bool &validity)> CostFn
std::function< void(int iteration_number, double cost, const Eigen::MatrixXd &values)> PostIterationFn