moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
acceleration_filter.hpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2024, 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
35/* Author: Paul Gesel
36Description: applies smoothing by limiting the acceleration between consecutive commands.
37The purpose of the plugin is to prevent the robot's acceleration limits from being violated by instantaneous changes
38to the servo command topics.
39
40 In the diagrams below, the c-v lines show the displacement that will occur given the current velocity. The t-c lines
41 shows the displacement between the current position and the desired position. The dashed lines shows the maximum
42 possible displacements that are within the acceleration limits. The v-t lines shows the acceleration commands that
43 will be used by this acceleration-limiting plugin. The x point shows the position that will be used for each scenario.
44
45Scenario A: The desired position is within the acceleration limits. The next commanded point will be exactly the
46desired point.
47 ________
48 | |
49c --|-----xt |
50 \__|__ v |
51 |________|
52
53Scenario B: The line between the current position and the desired position intersects the acceleration limits, but the
54reference position is not within the bounds. The next commanded point will be the point on the displacement line that
55is closest to the reference.
56 ________
57 | |
58c --|--------x------t
59 \__|__ v |
60 |________|
61
62Scenario C: Neither the displacement line intersects the acceleration limits nor does the reference point lie within
63the limits. In this case, the next commanded point will be the one that minimizes the robot's velocity while
64maintaining its direction.
65 ________
66 | |
67c --------x--- v |
68 \ | |
69 \ |________|
70 t
71 */
72
73#pragma once
74
75#include <cstddef>
76
81#include <moveit_core/moveit_acceleration_filter_parameters.hpp>
82
83#include <osqp.h>
84#include <cstddef> // for size_t
85#include <Eigen/Sparse>
86
87// OSQP shipped v1.0 in 2024 with a redesigned C API. Neither v0.6.x nor v1.0
88// exposes a numeric version macro that's usable from a preprocessor #if
89// (v0.6's OSQP_VERSION is a string literal in constants.h; v1's numeric
90// macros live in a non-installed private/version.h), so we don't have a
91// clean OSQP-provided switch.
92//
93// Preferred signal: MOVEIT_OSQP_V1 is defined at compile time from
94// moveit_core/online_signal_smoothing/CMakeLists.txt based on the version
95// find_package(osqp) actually resolved. That keeps compile-time API selection
96// consistent with link-time library selection when a build environment has
97// BOTH v0.6 (apt) and v1.0 (source-overlay) installed.
98//
99// Fallback for tooling that doesn't run through our CMakeLists (LSP,
100// clang-tidy stand-alone, etc.): probe for a v1-only header. The osqp CMake
101// target sets INTERFACE_INCLUDE_DIRECTORIES to ${prefix}/include/osqp, so
102// headers are searched directly without an `osqp/` prefix.
103//
104// TODO(#3786): remove the v0.6.x branch once every supported distro consumes
105// an osqp_vendor >= 1.0.0. Concrete trigger: Humble EOL (2027-05) or when
106// Jazzy/Kilted release an osqp_vendor 1.0 of their own. See #3751 for the
107// distro-compat-guard policy.
108#ifndef MOVEIT_OSQP_V1
109#if __has_include(<osqp_api_types.h>)
110#define MOVEIT_OSQP_V1 1
111#else
112#define MOVEIT_OSQP_V1 0
113#endif
114#endif
115
117{
119
120// Plugin
122{
123public:
131 bool initialize(rclcpp::Node::SharedPtr node, moveit::core::RobotModelConstPtr robot_model,
132 size_t num_joints) override;
133
142 bool doSmoothing(Eigen::VectorXd& positions, Eigen::VectorXd& velocities, Eigen::VectorXd& accelerations) override;
143
151 bool reset(const Eigen::VectorXd& positions, const Eigen::VectorXd& velocities,
152 const Eigen::VectorXd& accelerations) override;
153
158 {
159 if (osqp_solver_ != nullptr)
160 {
161 osqp_cleanup(osqp_solver_);
162 }
163 }
164
165private:
167 rclcpp::Node::SharedPtr node_;
169 online_signal_smoothing::Params params_;
171 size_t num_joints_;
173 Eigen::VectorXd last_velocities_;
174 Eigen::VectorXd last_positions_;
176 Eigen::VectorXd cur_acceleration_;
177 Eigen::VectorXd positions_offset_;
178 Eigen::VectorXd velocities_offset_;
180 Eigen::VectorXd max_acceleration_limits_;
181 Eigen::VectorXd min_acceleration_limits_;
183 moveit::core::RobotModelConstPtr robot_model_;
185 Eigen::SparseMatrix<double> constraints_sparse_;
187 OSQPDataWrapperPtr osqp_data_;
188#if MOVEIT_OSQP_V1
189 OSQPSolver* osqp_solver_ = nullptr;
190#else
191 OSQPWorkspace* osqp_solver_ = nullptr;
192#endif
193 OSQPSettings osqp_settings_;
194};
195} // namespace online_signal_smoothing
#define MOVEIT_STRUCT_FORWARD(C)
bool initialize(rclcpp::Node::SharedPtr node, moveit::core::RobotModelConstPtr robot_model, size_t num_joints) override
bool reset(const Eigen::VectorXd &positions, const Eigen::VectorXd &velocities, const Eigen::VectorXd &accelerations) override
bool doSmoothing(Eigen::VectorXd &positions, Eigen::VectorXd &velocities, Eigen::VectorXd &accelerations) override
Wrapper struct to make memory management easier for using osqp's C API. On OSQP v0....