moveit2
The MoveIt Motion Planning Framework for ROS 2.
filter_functions.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 
40 #pragma once
41 
42 #include <Eigen/Geometry>
45 
46 #include <stomp/utils.h>
47 
48 namespace stomp_moveit
49 {
50 namespace filters
51 {
52 // \brief An empty placeholder filter that doesn't apply any updates to the trajectory.
53 static const FilterFn NO_FILTER = [](const Eigen::MatrixXd& /*values*/, Eigen::MatrixXd& /*filtered_values*/) {
54  return true;
55 };
56 
63 FilterFn simpleSmoothingMatrix(size_t num_timesteps)
64 {
65  // Generates a smoothing matrix and applies it for each joint dimension in filtered_values
66  // The 'dt' value is a placeholder timestep duration that is used for approximating the second order derivative
67  // (acceleration) using a finite difference matrix. The actual timestep duration will be computed by a planner
68  // adapter after solving the STOMP trajectory.
69  Eigen::MatrixXd smoothing_matrix;
70  stomp::generateSmoothingMatrix(num_timesteps, 1.0 /* dt */, smoothing_matrix);
71  return [=](const Eigen::MatrixXd& /*values*/, Eigen::MatrixXd& filtered_values) {
72  for (auto row : filtered_values.rowwise())
73  {
74  row.transpose() = smoothing_matrix * row.transpose();
75  }
76  return true;
77  };
78 }
79 
87 {
88  return [=](const Eigen::MatrixXd& values, Eigen::MatrixXd& filtered_values) {
89  filtered_values = values;
90  const auto& joints = group->getActiveJointModels();
91  for (size_t i = 0; i < joints.size(); ++i)
92  {
93  for (int j = 0; j < filtered_values.cols(); ++j)
94  {
95  joints.at(i)->enforcePositionBounds(&filtered_values.coeffRef(i, j));
96  }
97  }
98  return true;
99  };
100 }
101 
108 FilterFn chain(const std::vector<FilterFn>& filter_functions)
109 {
110  return [=](const Eigen::MatrixXd& values, Eigen::MatrixXd& filtered_values) {
111  Eigen::MatrixXd values_in = values;
112  for (const auto& filter_fn : filter_functions)
113  {
114  filter_fn(values_in, filtered_values);
115  values_in = filtered_values;
116  }
117  return true;
118  };
119 }
120 } // namespace filters
121 } // namespace stomp_moveit
const std::vector< const JointModel * > & getActiveJointModels() const
Get the active joints in this group (that have controllable DOF). This does not include mimic joints.
FilterFn chain(const std::vector< FilterFn > &filter_functions)
FilterFn enforcePositionBounds(const moveit::core::JointModelGroup *group)
FilterFn simpleSmoothingMatrix(size_t num_timesteps)
std::function< bool(const Eigen::MatrixXd &values, Eigen::MatrixXd &filtered_values)> FilterFn
A STOMP task definition that allows injecting custom functions for planning.