moveit2
The MoveIt Motion Planning Framework for ROS 2.
motion_plan_request_features.cpp
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 
22 #include <rclcpp/logging.hpp>
23 #include <warehouse_ros/message_collection.h>
24 
28 #include <moveit_msgs/msg/motion_plan_request.hpp>
29 
32 
33 namespace moveit_ros
34 {
35 namespace trajectory_cache
36 {
37 
38 using ::warehouse_ros::Metadata;
39 using ::warehouse_ros::Query;
40 
41 using ::moveit::core::MoveItErrorCode;
42 using ::moveit::planning_interface::MoveGroupInterface;
43 
45 
46 // "Start" features. ===============================================================================
47 
48 // WorkspaceFeatures.
49 
50 WorkspaceFeatures::WorkspaceFeatures() : name_("WorkspaceFeatures")
51 {
52 }
53 
54 std::string WorkspaceFeatures::getName() const
55 {
56  return name_;
57 }
58 
59 MoveItErrorCode WorkspaceFeatures::appendFeaturesAsFuzzyFetchQuery(Query& query, const MotionPlanRequest& source,
60  const MoveGroupInterface& move_group,
61  double exact_match_precision) const
62 {
63  return appendFeaturesAsExactFetchQuery(query, source, move_group, exact_match_precision);
64 };
65 
66 MoveItErrorCode WorkspaceFeatures::appendFeaturesAsExactFetchQuery(Query& query, const MotionPlanRequest& source,
67  const MoveGroupInterface& move_group,
68  double /*exact_match_precision*/) const
69 {
70  query.append(name_ + ".group_name", source.group_name);
71  query.append(name_ + ".workspace_parameters.header.frame_id",
72  getWorkspaceFrameId(move_group, source.workspace_parameters));
73  query.appendGTE(name_ + ".workspace_parameters.min_corner.x", source.workspace_parameters.min_corner.x);
74  query.appendGTE(name_ + ".workspace_parameters.min_corner.y", source.workspace_parameters.min_corner.y);
75  query.appendGTE(name_ + ".workspace_parameters.min_corner.z", source.workspace_parameters.min_corner.z);
76  query.appendLTE(name_ + ".workspace_parameters.max_corner.x", source.workspace_parameters.max_corner.x);
77  query.appendLTE(name_ + ".workspace_parameters.max_corner.y", source.workspace_parameters.max_corner.y);
78  query.appendLTE(name_ + ".workspace_parameters.max_corner.z", source.workspace_parameters.max_corner.z);
79  return MoveItErrorCode::SUCCESS;
80 };
81 
82 MoveItErrorCode WorkspaceFeatures::appendFeaturesAsInsertMetadata(Metadata& metadata, const MotionPlanRequest& source,
83  const MoveGroupInterface& move_group) const
84 {
85  metadata.append(name_ + ".group_name", source.group_name);
86  metadata.append(name_ + ".workspace_parameters.header.frame_id",
87  getWorkspaceFrameId(move_group, source.workspace_parameters));
88  metadata.append(name_ + ".workspace_parameters.min_corner.x", source.workspace_parameters.min_corner.x);
89  metadata.append(name_ + ".workspace_parameters.min_corner.y", source.workspace_parameters.min_corner.y);
90  metadata.append(name_ + ".workspace_parameters.min_corner.z", source.workspace_parameters.min_corner.z);
91  metadata.append(name_ + ".workspace_parameters.max_corner.x", source.workspace_parameters.max_corner.x);
92  metadata.append(name_ + ".workspace_parameters.max_corner.y", source.workspace_parameters.max_corner.y);
93  metadata.append(name_ + ".workspace_parameters.max_corner.z", source.workspace_parameters.max_corner.z);
94  return MoveItErrorCode::SUCCESS;
95 };
96 
97 // StartStateJointStateFeatures.
98 
100  : name_("StartStateJointStateFeatures"), match_tolerance_(match_tolerance)
101 {
102 }
103 
105 {
106  return name_;
107 }
108 
110  const MotionPlanRequest& source,
111  const MoveGroupInterface& move_group,
112  double exact_match_precision) const
113 {
114  return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, match_tolerance_ + exact_match_precision);
115 };
116 
118  const MotionPlanRequest& source,
119  const MoveGroupInterface& move_group,
120  double exact_match_precision) const
121 {
122  return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, exact_match_precision);
123 };
124 
126  const MotionPlanRequest& source,
127  const MoveGroupInterface& move_group) const
128 {
129  return appendRobotStateJointStateAsInsertMetadata(metadata, source.start_state, move_group, name_ + ".start_state");
130 };
131 
132 MoveItErrorCode StartStateJointStateFeatures::appendFeaturesAsFetchQueryWithTolerance(
133  Query& query, const MotionPlanRequest& source, const MoveGroupInterface& move_group, double match_tolerance) const
134 {
135  return appendRobotStateJointStateAsFetchQueryWithTolerance(query, source.start_state, move_group, match_tolerance,
136  name_ + ".start_state");
137 };
138 
139 // "Goal" features. ================================================================================
140 
141 // MaxSpeedAndAccelerationFeatures.
142 
143 MaxSpeedAndAccelerationFeatures::MaxSpeedAndAccelerationFeatures() : name_("MaxSpeedAndAccelerationFeatures")
144 {
145 }
146 
148 {
149  return name_;
150 }
151 
153  const MotionPlanRequest& source,
154  const MoveGroupInterface& move_group,
155  double exact_match_precision) const
156 {
157  return appendFeaturesAsExactFetchQuery(query, source, move_group, exact_match_precision);
158 };
159 
160 MoveItErrorCode
162  const MoveGroupInterface& /*move_group*/,
163  double /*exact_match_precision*/) const
164 {
165  if (source.max_velocity_scaling_factor <= 0 || source.max_velocity_scaling_factor > 1.0)
166  {
167  query.appendLTE(name_ + ".max_velocity_scaling_factor", 1.0);
168  }
169  else
170  {
171  query.appendLTE(name_ + ".max_velocity_scaling_factor", source.max_velocity_scaling_factor);
172  }
173 
174  if (source.max_acceleration_scaling_factor <= 0 || source.max_acceleration_scaling_factor > 1.0)
175  {
176  query.appendLTE(name_ + ".max_acceleration_scaling_factor", 1.0);
177  }
178  else
179  {
180  query.appendLTE(name_ + ".max_acceleration_scaling_factor", source.max_acceleration_scaling_factor);
181  }
182 
183  if (source.max_cartesian_speed > 0)
184  {
185  query.append(name_ + ".cartesian_speed_limited_link", source.cartesian_speed_limited_link);
186  query.appendLTE(name_ + ".max_cartesian_speed", source.max_cartesian_speed);
187  }
188 
189  return MoveItErrorCode::SUCCESS;
190 };
191 
192 MoveItErrorCode
194  const MoveGroupInterface& /*move_group*/) const
195 {
196  if (source.max_velocity_scaling_factor <= 0 || source.max_velocity_scaling_factor > 1.0)
197  {
198  metadata.append(name_ + ".max_velocity_scaling_factor", 1.0);
199  }
200  else
201  {
202  metadata.append(name_ + ".max_velocity_scaling_factor", source.max_velocity_scaling_factor);
203  }
204 
205  if (source.max_acceleration_scaling_factor <= 0 || source.max_acceleration_scaling_factor > 1.0)
206  {
207  metadata.append(name_ + ".max_acceleration_scaling_factor", 1.0);
208  }
209  else
210  {
211  metadata.append(name_ + ".max_acceleration_scaling_factor", source.max_acceleration_scaling_factor);
212  }
213 
214  if (source.max_cartesian_speed > 0)
215  {
216  metadata.append(name_ + ".cartesian_speed_limited_link", source.cartesian_speed_limited_link);
217  metadata.append(name_ + ".max_cartesian_speed", source.max_cartesian_speed);
218  }
219 
220  return MoveItErrorCode::SUCCESS;
221 };
222 
223 // GoalConstraintsFeatures.
224 
226  : name_("GoalConstraintsFeatures"), match_tolerance_(match_tolerance)
227 {
228 }
229 
231 {
232  return name_;
233 }
234 
236  const MoveGroupInterface& move_group,
237  double exact_match_precision) const
238 {
239  return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, match_tolerance_ + exact_match_precision);
240 };
241 
243  const MoveGroupInterface& move_group,
244  double exact_match_precision) const
245 {
246  return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, exact_match_precision);
247 };
248 
250  const MotionPlanRequest& source,
251  const MoveGroupInterface& move_group) const
252 {
253  std::string workspace_id = getWorkspaceFrameId(move_group, source.workspace_parameters);
254  return appendConstraintsAsInsertMetadata(metadata, source.goal_constraints, move_group, workspace_id,
255  name_ + ".goal_constraints");
256 };
257 
258 MoveItErrorCode GoalConstraintsFeatures::appendFeaturesAsFetchQueryWithTolerance(Query& query,
259  const MotionPlanRequest& source,
260  const MoveGroupInterface& move_group,
261  double match_tolerance) const
262 {
263  std::string workspace_id = getWorkspaceFrameId(move_group, source.workspace_parameters);
264  return appendConstraintsAsFetchQueryWithTolerance(query, source.goal_constraints, move_group, match_tolerance,
265  workspace_id, name_ + ".goal_constraints");
266 };
267 
268 // PathConstraintsFeatures.
269 
271  : name_("PathConstraintsFeatures"), match_tolerance_(match_tolerance)
272 {
273 }
274 
276 {
277  return name_;
278 }
279 
281  const MoveGroupInterface& move_group,
282  double exact_match_precision) const
283 {
284  return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, match_tolerance_ + exact_match_precision);
285 };
286 
288  const MoveGroupInterface& move_group,
289  double exact_match_precision) const
290 {
291  return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, exact_match_precision);
292 };
293 
295  const MotionPlanRequest& source,
296  const MoveGroupInterface& move_group) const
297 {
298  std::string workspace_id = getWorkspaceFrameId(move_group, source.workspace_parameters);
299  return appendConstraintsAsInsertMetadata(metadata, { source.path_constraints }, move_group, workspace_id,
300  name_ + ".path_constraints");
301 };
302 
303 MoveItErrorCode PathConstraintsFeatures::appendFeaturesAsFetchQueryWithTolerance(Query& query,
304  const MotionPlanRequest& source,
305  const MoveGroupInterface& move_group,
306  double match_tolerance) const
307 {
308  std::string workspace_id = getWorkspaceFrameId(move_group, source.workspace_parameters);
309  return appendConstraintsAsFetchQueryWithTolerance(query, { source.path_constraints }, move_group, match_tolerance,
310  workspace_id, name_ + ".path_constraints");
311 };
312 
313 // TrajectoryConstraintsFeatures.
314 
316  : name_("TrajectoryConstraintsFeatures"), match_tolerance_(match_tolerance)
317 {
318 }
319 
321 {
322  return name_;
323 }
324 
326  const MotionPlanRequest& source,
327  const MoveGroupInterface& move_group,
328  double exact_match_precision) const
329 {
330  return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, match_tolerance_ + exact_match_precision);
331 };
332 
334  const MotionPlanRequest& source,
335  const MoveGroupInterface& move_group,
336  double exact_match_precision) const
337 {
338  return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, exact_match_precision);
339 };
340 
341 MoveItErrorCode
343  const MoveGroupInterface& move_group) const
344 {
345  std::string workspace_id = getWorkspaceFrameId(move_group, source.workspace_parameters);
346  return appendConstraintsAsInsertMetadata(metadata, source.trajectory_constraints.constraints, move_group,
347  workspace_id, name_ + ".trajectory_constraints.constraints");
348 };
349 
350 MoveItErrorCode TrajectoryConstraintsFeatures::appendFeaturesAsFetchQueryWithTolerance(
351  Query& query, const MotionPlanRequest& source, const MoveGroupInterface& move_group, double match_tolerance) const
352 {
353  std::string workspace_id = getWorkspaceFrameId(move_group, source.workspace_parameters);
354  return appendConstraintsAsFetchQueryWithTolerance(query, source.trajectory_constraints.constraints, move_group,
355  match_tolerance, workspace_id,
356  name_ + ".trajectory_constraints.constraints");
357 };
358 
359 } // namespace trajectory_cache
360 } // namespace moveit_ros
moveit::core::MoveItErrorCode appendFeaturesAsExactFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with exact matching.
moveit::core::MoveItErrorCode appendFeaturesAsFuzzyFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with fuzzy matching.
moveit::core::MoveItErrorCode appendFeaturesAsInsertMetadata(warehouse_ros::Metadata &metadata, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group) const override
Extracts relevant features from FeatureSourceT, to be appended to a cache entry's metadata.
std::string getName() const override
Gets the name of the features implementation.
std::string getName() const override
Gets the name of the features implementation.
moveit::core::MoveItErrorCode appendFeaturesAsExactFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with exact matching.
moveit::core::MoveItErrorCode appendFeaturesAsFuzzyFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with fuzzy matching.
moveit::core::MoveItErrorCode appendFeaturesAsInsertMetadata(warehouse_ros::Metadata &metadata, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group) const override
Extracts relevant features from FeatureSourceT, to be appended to a cache entry's metadata.
moveit::core::MoveItErrorCode appendFeaturesAsExactFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with exact matching.
moveit::core::MoveItErrorCode appendFeaturesAsFuzzyFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with fuzzy matching.
std::string getName() const override
Gets the name of the features implementation.
moveit::core::MoveItErrorCode appendFeaturesAsInsertMetadata(warehouse_ros::Metadata &metadata, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group) const override
Extracts relevant features from FeatureSourceT, to be appended to a cache entry's metadata.
moveit::core::MoveItErrorCode appendFeaturesAsExactFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with exact matching.
moveit::core::MoveItErrorCode appendFeaturesAsFuzzyFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with fuzzy matching.
moveit::core::MoveItErrorCode appendFeaturesAsInsertMetadata(warehouse_ros::Metadata &metadata, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group) const override
Extracts relevant features from FeatureSourceT, to be appended to a cache entry's metadata.
std::string getName() const override
Gets the name of the features implementation.
moveit::core::MoveItErrorCode appendFeaturesAsFuzzyFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with fuzzy matching.
moveit::core::MoveItErrorCode appendFeaturesAsInsertMetadata(warehouse_ros::Metadata &metadata, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group) const override
Extracts relevant features from FeatureSourceT, to be appended to a cache entry's metadata.
moveit::core::MoveItErrorCode appendFeaturesAsExactFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with exact matching.
std::string getName() const override
Gets the name of the features implementation.
moveit::core::MoveItErrorCode appendFeaturesAsInsertMetadata(warehouse_ros::Metadata &metadata, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group) const override
Extracts relevant features from FeatureSourceT, to be appended to a cache entry's metadata.
moveit::core::MoveItErrorCode appendFeaturesAsFuzzyFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with fuzzy matching.
std::string getName() const override
Gets the name of the features implementation.
moveit::core::MoveItErrorCode appendFeaturesAsExactFetchQuery(warehouse_ros::Query &query, const moveit_msgs::msg::MotionPlanRequest &source, const moveit::planning_interface::MoveGroupInterface &move_group, double exact_match_precision) const override
Extracts relevant features from FeatureSourceT, to be appended to a fetch query, with exact matching.
moveit_msgs::msg::MotionPlanRequest features to key the trajectory cache on.
Utilities used by the trajectory_cache package.
moveit::core::MoveItErrorCode appendConstraintsAsFetchQueryWithTolerance(warehouse_ros::Query &query, std::vector< moveit_msgs::msg::Constraints > constraints, const moveit::planning_interface::MoveGroupInterface &move_group, double match_tolerance, const std::string &reference_frame_id, const std::string &prefix)
Extracts relevant features from a vector of moveit_msgs::msg::Constraints messages to a fetch query,...
std::string getWorkspaceFrameId(const moveit::planning_interface::MoveGroupInterface &move_group, const moveit_msgs::msg::WorkspaceParameters &workspace_parameters)
Gets workspace frame ID. If workspace_parameters has no frame ID, fetch it from move_group.
moveit::core::MoveItErrorCode appendConstraintsAsInsertMetadata(warehouse_ros::Metadata &metadata, std::vector< moveit_msgs::msg::Constraints > constraints, const moveit::planning_interface::MoveGroupInterface &move_group, const std::string &reference_frame_id, const std::string &prefix)
Extracts relevant features from a vector of moveit_msgs::msg::Constraints messages to a cache entry's...
moveit::core::MoveItErrorCode appendRobotStateJointStateAsInsertMetadata(warehouse_ros::Metadata &metadata, const moveit_msgs::msg::RobotState &robot_state, const moveit::planning_interface::MoveGroupInterface &move_group, const std::string &prefix)
Extracts relevant features from a vector of moveit_msgs::msg::Constraints messages to a cache entry's...
moveit::core::MoveItErrorCode appendRobotStateJointStateAsFetchQueryWithTolerance(warehouse_ros::Query &query, const moveit_msgs::msg::RobotState &robot_state, const moveit::planning_interface::MoveGroupInterface &move_group, double match_tolerance, const std::string &prefix)
Extracts relevant features from a vector of moveit_msgs::msg::Constraints messages to a fetch query,...
moveit_msgs::msg::MotionPlanRequest MotionPlanRequest