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
20#pragma once
21
22#include <chrono>
23#include <memory>
24#include <optional>
25
26#include <rclcpp/rclcpp.hpp>
27
28#include <warehouse_ros/message_collection.h>
29#include <warehouse_ros/database_connection.h>
30
31// TF2
32#include <tf2_ros/buffer.h>
33#include <tf2_ros/transform_listener.h>
34
35// ROS2 Messages
36#include <geometry_msgs/msg/pose.hpp>
37#include <geometry_msgs/msg/pose_stamped.hpp>
38
39// moveit modules
41#include <moveit_msgs/srv/get_cartesian_path.hpp>
42
43namespace moveit_ros
44{
45namespace trajectory_cache
46{
47
118{
119public:
130 explicit TrajectoryCache(const rclcpp::Node::SharedPtr& node);
131
145 struct Options
146 {
147 std::string db_path = ":memory:";
148 uint32_t db_port = 0;
149
152 };
153
165 bool init(const Options& options);
166
173 unsigned countTrajectories(const std::string& cache_namespace);
174
181 unsigned countCartesianTrajectories(const std::string& cache_namespace);
182
189 std::string getDbPath() const;
190
192 uint32_t getDbPort() const;
193
195 double getExactMatchPrecision() const;
196
198 void setExactMatchPrecision(double exact_match_precision);
199
202
205 size_t num_additional_trajectories_to_preserve_when_deleting_worse);
206
228 std::vector<warehouse_ros::MessageWithMetadata<moveit_msgs::msg::RobotTrajectory>::ConstPtr>
230 const std::string& cache_namespace,
231 const moveit_msgs::msg::MotionPlanRequest& plan_request, double start_tolerance,
232 double goal_tolerance, bool metadata_only = false,
233 const std::string& sort_by = "execution_time_s", bool ascending = true) const;
234
249 warehouse_ros::MessageWithMetadata<moveit_msgs::msg::RobotTrajectory>::ConstPtr fetchBestMatchingTrajectory(
250 const moveit::planning_interface::MoveGroupInterface& move_group, const std::string& cache_namespace,
251 const moveit_msgs::msg::MotionPlanRequest& plan_request, double start_tolerance, double goal_tolerance,
252 bool metadata_only = false, const std::string& sort_by = "execution_time_s", bool ascending = true) const;
253
278 const std::string& cache_namespace, const moveit_msgs::msg::MotionPlanRequest& plan_request,
279 const moveit_msgs::msg::RobotTrajectory& trajectory, double execution_time_s,
280 double planning_time_s, bool prune_worse_trajectories = true);
281
301 moveit_msgs::srv::GetCartesianPath::Request
303 const std::vector<geometry_msgs::msg::Pose>& waypoints, double max_step,
304 double jump_threshold, bool avoid_collisions = true);
305
321 std::vector<warehouse_ros::MessageWithMetadata<moveit_msgs::msg::RobotTrajectory>::ConstPtr>
323 const std::string& cache_namespace,
324 const moveit_msgs::srv::GetCartesianPath::Request& plan_request,
325 double min_fraction, double start_tolerance, double goal_tolerance,
326 bool metadata_only = false, const std::string& sort_by = "execution_time_s",
327 bool ascending = true) const;
328
344 warehouse_ros::MessageWithMetadata<moveit_msgs::msg::RobotTrajectory>::ConstPtr fetchBestMatchingCartesianTrajectory(
345 const moveit::planning_interface::MoveGroupInterface& move_group, const std::string& cache_namespace,
346 const moveit_msgs::srv::GetCartesianPath::Request& plan_request, double min_fraction, double start_tolerance,
347 double goal_tolerance, bool metadata_only = false, const std::string& sort_by = "execution_time_s",
348 bool ascending = true) const;
349
375 const std::string& cache_namespace,
376 const moveit_msgs::srv::GetCartesianPath::Request& plan_request,
377 const moveit_msgs::msg::RobotTrajectory& trajectory, double execution_time_s,
378 double planning_time_s, double fraction, bool prune_worse_trajectories = true);
379
382private:
401 bool extractAndAppendTrajectoryStartToQuery(warehouse_ros::Query& query,
403 const moveit_msgs::msg::MotionPlanRequest& plan_request,
404 double match_tolerance) const;
405
419 bool extractAndAppendTrajectoryGoalToQuery(warehouse_ros::Query& query,
421 const moveit_msgs::msg::MotionPlanRequest& plan_request,
422 double match_tolerance) const;
423
436 bool extractAndAppendTrajectoryStartToMetadata(warehouse_ros::Metadata& metadata,
438 const moveit_msgs::msg::MotionPlanRequest& plan_request) const;
439
452 bool extractAndAppendTrajectoryGoalToMetadata(warehouse_ros::Metadata& metadata,
454 const moveit_msgs::msg::MotionPlanRequest& plan_request) const;
455
476 bool extractAndAppendCartesianTrajectoryStartToQuery(warehouse_ros::Query& query,
478 const moveit_msgs::srv::GetCartesianPath::Request& plan_request,
479 double match_tolerance) const;
480
494 bool extractAndAppendCartesianTrajectoryGoalToQuery(warehouse_ros::Query& query,
496 const moveit_msgs::srv::GetCartesianPath::Request& plan_request,
497 double match_tolerance) const;
498
511 bool extractAndAppendCartesianTrajectoryStartToMetadata(
512 warehouse_ros::Metadata& metadata, const moveit::planning_interface::MoveGroupInterface& move_group,
513 const moveit_msgs::srv::GetCartesianPath::Request& plan_request) const;
514
527 bool extractAndAppendCartesianTrajectoryGoalToMetadata(
528 warehouse_ros::Metadata& metadata, const moveit::planning_interface::MoveGroupInterface& move_group,
529 const moveit_msgs::srv::GetCartesianPath::Request& plan_request) const;
530
533 rclcpp::Node::SharedPtr node_;
534 rclcpp::Logger logger_;
535 warehouse_ros::DatabaseConnection::Ptr db_;
536
537 Options options_;
538
539 std::unique_ptr<tf2_ros::Buffer> tf_buffer_;
540 std::shared_ptr<tf2_ros::TransformListener> tf_listener_;
541};
542
543} // namespace trajectory_cache
544} // namespace moveit_ros
Client class to conveniently use the ROS interfaces provided by the move_group node.
Trajectory Cache manager for MoveIt.
bool insertTrajectory(const moveit::planning_interface::MoveGroupInterface &move_group, const std::string &cache_namespace, const moveit_msgs::msg::MotionPlanRequest &plan_request, const moveit_msgs::msg::RobotTrajectory &trajectory, double execution_time_s, double planning_time_s, bool prune_worse_trajectories=true)
Inserts a trajectory into the database if it is the best matching trajectory seen so far.
unsigned countCartesianTrajectories(const std::string &cache_namespace)
Count the number of cartesian trajectories for a particular cache namespace.
unsigned countTrajectories(const std::string &cache_namespace)
Count the number of non-cartesian trajectories for a particular cache namespace.
void setNumAdditionalTrajectoriesToPreserveWhenDeletingWorse(size_t num_additional_trajectories_to_preserve_when_deleting_worse)
Set the number of additional trajectories to preserve when deleting worse trajectories.
double getExactMatchPrecision() const
Gets the exact match precision.
size_t getNumAdditionalTrajectoriesToPreserveWhenDeletingWorse() const
Get the number of trajectories to preserve when deleting worse trajectories.
void setExactMatchPrecision(double exact_match_precision)
Sets the exact match precision.
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, double min_fraction, double start_tolerance, double goal_tolerance, bool metadata_only=false, const std::string &sort_by="execution_time_s", bool ascending=true) const
Fetches the best cartesian trajectory that fits within the requested tolerances for start and goal co...
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, double start_tolerance, double goal_tolerance, bool metadata_only=false, const std::string &sort_by="execution_time_s", bool ascending=true) const
Fetches all plans that fit within the requested tolerances for start and goal conditions,...
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, double start_tolerance, double goal_tolerance, bool metadata_only=false, const std::string &sort_by="execution_time_s", bool ascending=true) const
Fetches the best trajectory that fits within the requested tolerances for start and goal conditions,...
moveit_msgs::srv::GetCartesianPath::Request constructGetCartesianPathRequest(moveit::planning_interface::MoveGroupInterface &move_group, const std::vector< geometry_msgs::msg::Pose > &waypoints, double max_step, double jump_threshold, bool avoid_collisions=true)
Constructs a GetCartesianPath request.
std::string getDbPath() const
Gets the database path.
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, double min_fraction, double start_tolerance, double goal_tolerance, bool metadata_only=false, const std::string &sort_by="execution_time_s", bool ascending=true) const
Fetches all cartesian trajectories that fit within the requested tolerances for start and goal condit...
bool init(const Options &options)
Initializes the TrajectoryCache.
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::msg::RobotTrajectory &trajectory, double execution_time_s, double planning_time_s, double fraction, bool prune_worse_trajectories=true)
Inserts a cartesian trajectory into the database if it is the best matching cartesian trajectory seen...
uint32_t getDbPort() const
Gets the database port.