moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
trajectory_cache.hpp
Go to the documentation of this file.
1// Copyright 2024 Intrinsic Innovation LLC.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
19
20#pragma once
21
22#include <chrono>
23#include <cstdint>
24#include <memory>
25#include <vector>
26
27#include <rclcpp/rclcpp.hpp>
28
29#include <warehouse_ros/message_collection.h>
30#include <warehouse_ros/database_connection.h>
31
32#include <geometry_msgs/msg/pose.hpp>
33#include <geometry_msgs/msg/pose_stamped.hpp>
34
35// moveit modules
37#include <moveit_msgs/msg/motion_plan_request.hpp>
38#include <moveit_msgs/msg/robot_trajectory.hpp>
39#include <moveit_msgs/srv/get_cartesian_path.hpp>
40
43
44namespace moveit_ros
45{
46namespace trajectory_cache
47{
48
153{
154public:
159
163 static std::vector<std::unique_ptr<FeaturesInterface<moveit_msgs::msg::MotionPlanRequest>>>
164 getDefaultFeatures(double start_tolerance, double goal_tolerance);
165
169 static std::unique_ptr<CacheInsertPolicyInterface<moveit_msgs::msg::MotionPlanRequest,
171 moveit_msgs::msg::RobotTrajectory>>
173
177 static std::vector<std::unique_ptr<FeaturesInterface<moveit_msgs::srv::GetCartesianPath::Request>>>
178 getDefaultCartesianFeatures(double start_tolerance, double goal_tolerance, double min_fraction);
179
183 static std::unique_ptr<
184 CacheInsertPolicyInterface<moveit_msgs::srv::GetCartesianPath::Request,
185 moveit_msgs::srv::GetCartesianPath::Response, moveit_msgs::msg::RobotTrajectory>>
187
189 static std::string getDefaultSortFeature();
190
192
197
207 explicit TrajectoryCache(const rclcpp::Node::SharedPtr& node);
208
222 struct Options
223 {
224 std::string db_path = ":memory:";
225 uint32_t db_port = 0;
226
229 };
230
241 bool init(const Options& options);
242
244
249
256 unsigned countTrajectories(const std::string& cache_namespace);
257
264 unsigned countCartesianTrajectories(const std::string& cache_namespace);
265
267 std::string getDbPath() const;
268
270 uint32_t getDbPort() const;
271
273 double getExactMatchPrecision() const;
274
276 void setExactMatchPrecision(double exact_match_precision);
277
280
283 size_t num_additional_trajectories_to_preserve_when_pruning_worse);
284
286
291
307 std::vector<warehouse_ros::MessageWithMetadata<moveit_msgs::msg::RobotTrajectory>::ConstPtr>
309 const moveit::planning_interface::MoveGroupInterface& move_group, const std::string& cache_namespace,
310 const moveit_msgs::msg::MotionPlanRequest& plan_request,
311 const std::vector<std::unique_ptr<FeaturesInterface<moveit_msgs::msg::MotionPlanRequest>>>& features,
312 const std::string& sort_by, bool ascending = true, bool metadata_only = false) const;
313
328 warehouse_ros::MessageWithMetadata<moveit_msgs::msg::RobotTrajectory>::ConstPtr fetchBestMatchingTrajectory(
329 const moveit::planning_interface::MoveGroupInterface& move_group, const std::string& cache_namespace,
330 const moveit_msgs::msg::MotionPlanRequest& plan_request,
331 const std::vector<std::unique_ptr<FeaturesInterface<moveit_msgs::msg::MotionPlanRequest>>>& features,
332 const std::string& sort_by, bool ascending = true, bool metadata_only = false) const;
333
355 const std::string& cache_namespace, const moveit_msgs::msg::MotionPlanRequest& plan_request,
357 CacheInsertPolicyInterface<moveit_msgs::msg::MotionPlanRequest,
359 moveit_msgs::msg::RobotTrajectory>& cache_insert_policy,
360 bool prune_worse_trajectories = true,
361 const std::vector<std::unique_ptr<FeaturesInterface<moveit_msgs::msg::MotionPlanRequest>>>&
362 additional_features = {});
363
365
370
386 std::vector<warehouse_ros::MessageWithMetadata<moveit_msgs::msg::RobotTrajectory>::ConstPtr>
388 const moveit::planning_interface::MoveGroupInterface& move_group, const std::string& cache_namespace,
389 const moveit_msgs::srv::GetCartesianPath::Request& plan_request,
390 const std::vector<std::unique_ptr<FeaturesInterface<moveit_msgs::srv::GetCartesianPath::Request>>>& features,
391 const std::string& sort_by, bool ascending = true, bool metadata_only = false) const;
392
407 warehouse_ros::MessageWithMetadata<moveit_msgs::msg::RobotTrajectory>::ConstPtr fetchBestMatchingCartesianTrajectory(
408 const moveit::planning_interface::MoveGroupInterface& move_group, const std::string& cache_namespace,
409 const moveit_msgs::srv::GetCartesianPath::Request& plan_request,
410 const std::vector<std::unique_ptr<FeaturesInterface<moveit_msgs::srv::GetCartesianPath::Request>>>& features,
411 const std::string& sort_by, bool ascending = true, bool metadata_only = false) const;
412
434 const moveit::planning_interface::MoveGroupInterface& move_group, const std::string& cache_namespace,
435 const moveit_msgs::srv::GetCartesianPath::Request& plan_request,
436 const moveit_msgs::srv::GetCartesianPath::Response& plan,
437 CacheInsertPolicyInterface<moveit_msgs::srv::GetCartesianPath::Request,
438 moveit_msgs::srv::GetCartesianPath::Response, moveit_msgs::msg::RobotTrajectory>&
439 cache_insert_policy,
440 bool prune_worse_trajectories = true,
441 const std::vector<std::unique_ptr<FeaturesInterface<moveit_msgs::srv::GetCartesianPath::Request>>>&
442 additional_features = {});
443
445
446private:
447 rclcpp::Node::SharedPtr node_;
448 rclcpp::Logger logger_;
449 warehouse_ros::DatabaseConnection::Ptr db_;
450
451 Options options_;
452};
453
454} // namespace trajectory_cache
455} // namespace moveit_ros
Abstract template class for injecting logic for determining when to prune and insert a cache entry,...
Client class to conveniently use the ROS interfaces provided by the move_group node.
unsigned countCartesianTrajectories(const std::string &cache_namespace)
Count the number of cartesian trajectories for a particular cache namespace.
static std::unique_ptr< CacheInsertPolicyInterface< moveit_msgs::msg::MotionPlanRequest, moveit::planning_interface::MoveGroupInterface::Plan, moveit_msgs::msg::RobotTrajectory > > getDefaultCacheInsertPolicy()
Gets the default cache insert policy for MotionPlanRequest messages.
warehouse_ros::MessageWithMetadata< moveit_msgs::msg::RobotTrajectory >::ConstPtr fetchBestMatchingTrajectory(const moveit::planning_interface::MoveGroupInterface &move_group, const std::string &cache_namespace, const moveit_msgs::msg::MotionPlanRequest &plan_request, const std::vector< std::unique_ptr< FeaturesInterface< moveit_msgs::msg::MotionPlanRequest > > > &features, const std::string &sort_by, bool ascending=true, bool metadata_only=false) const
Fetches the best trajectory keyed on user-specified features, with respect to some cache feature.
size_t getNumAdditionalTrajectoriesToPreserveWhenPruningWorse() const
Get the number of trajectories to preserve when pruning worse trajectories.
unsigned countTrajectories(const std::string &cache_namespace)
Count the number of non-cartesian trajectories for a particular cache namespace.
static std::vector< std::unique_ptr< FeaturesInterface< moveit_msgs::srv::GetCartesianPath::Request > > > getDefaultCartesianFeatures(double start_tolerance, double goal_tolerance, double min_fraction)
Gets the default features for GetCartesianPath requests.
TrajectoryCache(const rclcpp::Node::SharedPtr &node)
Constructs a TrajectoryCache.
static std::string getDefaultSortFeature()
Gets the default sort feature.
double getExactMatchPrecision() const
Gets the exact match precision.
std::vector< warehouse_ros::MessageWithMetadata< moveit_msgs::msg::RobotTrajectory >::ConstPtr > fetchAllMatchingTrajectories(const moveit::planning_interface::MoveGroupInterface &move_group, const std::string &cache_namespace, const moveit_msgs::msg::MotionPlanRequest &plan_request, const std::vector< std::unique_ptr< FeaturesInterface< moveit_msgs::msg::MotionPlanRequest > > > &features, const std::string &sort_by, bool ascending=true, bool metadata_only=false) const
Fetches all trajectories keyed on user-specified features, returning them as a vector,...
bool insertCartesianTrajectory(const moveit::planning_interface::MoveGroupInterface &move_group, const std::string &cache_namespace, const moveit_msgs::srv::GetCartesianPath::Request &plan_request, const moveit_msgs::srv::GetCartesianPath::Response &plan, CacheInsertPolicyInterface< moveit_msgs::srv::GetCartesianPath::Request, moveit_msgs::srv::GetCartesianPath::Response, moveit_msgs::msg::RobotTrajectory > &cache_insert_policy, bool prune_worse_trajectories=true, const std::vector< std::unique_ptr< FeaturesInterface< moveit_msgs::srv::GetCartesianPath::Request > > > &additional_features={})
Inserts a cartesian trajectory into the database, with user-specified insert policy.
void setNumAdditionalTrajectoriesToPreserveWhenPruningWorse(size_t num_additional_trajectories_to_preserve_when_pruning_worse)
Set the number of additional trajectories to preserve when pruning worse trajectories.
static std::unique_ptr< CacheInsertPolicyInterface< moveit_msgs::srv::GetCartesianPath::Request, moveit_msgs::srv::GetCartesianPath::Response, moveit_msgs::msg::RobotTrajectory > > getDefaultCartesianCacheInsertPolicy()
Gets the default cache insert policy for GetCartesianPath requests.
void setExactMatchPrecision(double exact_match_precision)
Sets the exact match precision.
bool insertTrajectory(const moveit::planning_interface::MoveGroupInterface &move_group, const std::string &cache_namespace, const moveit_msgs::msg::MotionPlanRequest &plan_request, const moveit::planning_interface::MoveGroupInterface::Plan &plan, CacheInsertPolicyInterface< moveit_msgs::msg::MotionPlanRequest, moveit::planning_interface::MoveGroupInterface::Plan, moveit_msgs::msg::RobotTrajectory > &cache_insert_policy, bool prune_worse_trajectories=true, const std::vector< std::unique_ptr< FeaturesInterface< moveit_msgs::msg::MotionPlanRequest > > > &additional_features={})
Inserts a trajectory into the database, with user-specified insert policy.
std::string getDbPath() const
Gets the database path.
static std::vector< std::unique_ptr< FeaturesInterface< moveit_msgs::msg::MotionPlanRequest > > > getDefaultFeatures(double start_tolerance, double goal_tolerance)
Gets the default features for MotionPlanRequest messages.
bool init(const Options &options)
Initializes the TrajectoryCache.
warehouse_ros::MessageWithMetadata< moveit_msgs::msg::RobotTrajectory >::ConstPtr fetchBestMatchingCartesianTrajectory(const moveit::planning_interface::MoveGroupInterface &move_group, const std::string &cache_namespace, const moveit_msgs::srv::GetCartesianPath::Request &plan_request, const std::vector< std::unique_ptr< FeaturesInterface< moveit_msgs::srv::GetCartesianPath::Request > > > &features, const std::string &sort_by, bool ascending=true, bool metadata_only=false) const
Fetches the best cartesian trajectory keyed on user-specified features, with respect to some cache fe...
uint32_t getDbPort() const
Gets the database port.
std::vector< warehouse_ros::MessageWithMetadata< moveit_msgs::msg::RobotTrajectory >::ConstPtr > fetchAllMatchingCartesianTrajectories(const moveit::planning_interface::MoveGroupInterface &move_group, const std::string &cache_namespace, const moveit_msgs::srv::GetCartesianPath::Request &plan_request, const std::vector< std::unique_ptr< FeaturesInterface< moveit_msgs::srv::GetCartesianPath::Request > > > &features, const std::string &sort_by, bool ascending=true, bool metadata_only=false) const
Fetches all cartesian trajectories keyed on user-specified features, returning them as a vector,...
Abstract template class for extracting features from some FeatureSourceT.
The representation of a motion plan (as ROS messages).