moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
get_cartesian_path_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/srv/get_cartesian_path.hpp>
29
32
33namespace moveit_ros
34{
35namespace trajectory_cache
36{
37
38using ::warehouse_ros::Metadata;
39using ::warehouse_ros::Query;
40
41using ::moveit::core::MoveItErrorCode;
42using ::moveit::planning_interface::MoveGroupInterface;
43
44using ::moveit_msgs::srv::GetCartesianPath;
45
46// "Start" features. ===============================================================================
47
48// CartesianWorkspaceFeatures.
49
50CartesianWorkspaceFeatures::CartesianWorkspaceFeatures() : name_("CartesianWorkspaceFeatures")
51{
52}
53
55{
56 return name_;
57}
58
60 const GetCartesianPath::Request& source,
61 const MoveGroupInterface& move_group,
62 double exact_match_precision) const
63{
64 return appendFeaturesAsExactFetchQuery(query, source, move_group, exact_match_precision);
65};
66
68 const GetCartesianPath::Request& source,
69 const MoveGroupInterface& move_group,
70 double /*exact_match_precision*/) const
71{
72 query.append(name_ + ".group_name", source.group_name);
73 query.append(name_ + ".header.frame_id", getCartesianPathRequestFrameId(move_group, source));
74 return moveit::core::MoveItErrorCode::SUCCESS;
75};
76
78 const GetCartesianPath::Request& source,
79 const MoveGroupInterface& move_group) const
80{
81 metadata.append(name_ + ".group_name", source.group_name);
82 metadata.append(name_ + ".header.frame_id", getCartesianPathRequestFrameId(move_group, source));
83 return moveit::core::MoveItErrorCode::SUCCESS;
84};
85
86// CartesianStartStateJointStateFeatures.
87
89 : name_("CartesianStartStateJointStateFeatures"), match_tolerance_(match_tolerance)
90{
91}
92
94{
95 return name_;
96}
97
99 Query& query, const GetCartesianPath::Request& source, const MoveGroupInterface& move_group,
100 double exact_match_precision) const
101{
102 return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, match_tolerance_ + exact_match_precision);
103};
104
106 Query& query, const GetCartesianPath::Request& source, const MoveGroupInterface& move_group,
107 double exact_match_precision) const
108{
109 return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, exact_match_precision);
110};
111
113 Metadata& metadata, const GetCartesianPath::Request& source, const MoveGroupInterface& move_group) const
114{
115 return appendRobotStateJointStateAsInsertMetadata(metadata, source.start_state, move_group, name_ + ".start_state");
116};
117
118MoveItErrorCode CartesianStartStateJointStateFeatures::appendFeaturesAsFetchQueryWithTolerance(
119 Query& query, const GetCartesianPath::Request& source, const MoveGroupInterface& move_group,
120 double match_tolerance) const
121{
122 return appendRobotStateJointStateAsFetchQueryWithTolerance(query, source.start_state, move_group, match_tolerance,
123 name_ + ".start_state");
124};
125
126// "Goal" features. ================================================================================
127
128// CartesianMaxSpeedAndAccelerationFeatures.
129
131 : name_("CartesianMaxSpeedAndAccelerationFeatures")
132{
133}
134
136{
137 return name_;
138}
139
141 Query& query, const GetCartesianPath::Request& source, const MoveGroupInterface& move_group,
142 double exact_match_precision) const
143{
144 return appendFeaturesAsExactFetchQuery(query, source, move_group, exact_match_precision);
145};
146
148 Query& query, const GetCartesianPath::Request& source, const MoveGroupInterface& /*move_group*/,
149 double /*exact_match_precision*/) const
150{
151 if (source.max_velocity_scaling_factor <= 0 || source.max_velocity_scaling_factor > 1.0)
152 {
153 query.appendLTE(name_ + ".max_velocity_scaling_factor", 1.0);
154 }
155 else
156 {
157 query.appendLTE(name_ + ".max_velocity_scaling_factor", source.max_velocity_scaling_factor);
158 }
159
160 if (source.max_acceleration_scaling_factor <= 0 || source.max_acceleration_scaling_factor > 1.0)
161 {
162 query.appendLTE(name_ + ".max_acceleration_scaling_factor", 1.0);
163 }
164 else
165 {
166 query.appendLTE(name_ + ".max_acceleration_scaling_factor", source.max_acceleration_scaling_factor);
167 }
168
169 if (source.max_cartesian_speed > 0)
170 {
171 query.append(name_ + ".cartesian_speed_limited_link", source.cartesian_speed_limited_link);
172 query.appendLTE(name_ + ".max_cartesian_speed", source.max_cartesian_speed);
173 }
174
175 return moveit::core::MoveItErrorCode::SUCCESS;
176};
177
179 Metadata& metadata, const GetCartesianPath::Request& source, const MoveGroupInterface& /*move_group*/) const
180{
181 if (source.max_velocity_scaling_factor <= 0 || source.max_velocity_scaling_factor > 1.0)
182 {
183 metadata.append(name_ + ".max_velocity_scaling_factor", 1.0);
184 }
185 else
186 {
187 metadata.append(name_ + ".max_velocity_scaling_factor", source.max_velocity_scaling_factor);
188 }
189
190 if (source.max_acceleration_scaling_factor <= 0 || source.max_acceleration_scaling_factor > 1.0)
191 {
192 metadata.append(name_ + ".max_acceleration_scaling_factor", 1.0);
193 }
194 else
195 {
196 metadata.append(name_ + ".max_acceleration_scaling_factor", source.max_acceleration_scaling_factor);
197 }
198
199 if (source.max_cartesian_speed > 0)
200 {
201 metadata.append(name_ + ".cartesian_speed_limited_link", source.cartesian_speed_limited_link);
202 metadata.append(name_ + ".max_cartesian_speed", source.max_cartesian_speed);
203 }
204
205 return moveit::core::MoveItErrorCode::SUCCESS;
206};
207
208// CartesianMaxStepAndJumpThresholdFeatures.
209
211 : name_("CartesianMaxStepAndJumpThresholdFeatures")
212{
213}
214
216{
217 return name_;
218}
219
221 Query& query, const GetCartesianPath::Request& source, const MoveGroupInterface& move_group,
222 double exact_match_precision) const
223{
224 return appendFeaturesAsExactFetchQuery(query, source, move_group, exact_match_precision);
225};
226
228 Query& query, const GetCartesianPath::Request& source, const MoveGroupInterface& /*move_group*/,
229 double /*exact_match_precision*/) const
230{
231 query.appendLTE(name_ + ".max_step", source.max_step);
232
233 if (source.jump_threshold > 0)
234 {
235 query.appendLTE(name_ + ".jump_threshold", source.jump_threshold);
236 }
237 if (source.prismatic_jump_threshold > 0)
238 {
239 query.appendLTE(name_ + ".prismatic_jump_threshold", source.prismatic_jump_threshold);
240 }
241 if (source.revolute_jump_threshold > 0)
242 {
243 query.appendLTE(name_ + ".revolute_jump_threshold", source.revolute_jump_threshold);
244 }
245
246 return moveit::core::MoveItErrorCode::SUCCESS;
247};
248
250 Metadata& metadata, const GetCartesianPath::Request& source, const MoveGroupInterface& /*move_group*/) const
251{
252 metadata.append(name_ + ".max_step", source.max_step);
253
254 if (source.jump_threshold > 0)
255 {
256 metadata.append(name_ + ".jump_threshold", source.jump_threshold);
257 }
258 if (source.prismatic_jump_threshold > 0)
259 {
260 metadata.append(name_ + ".prismatic_jump_threshold", source.prismatic_jump_threshold);
261 }
262 if (source.revolute_jump_threshold > 0)
263 {
264 metadata.append(name_ + ".revolute_jump_threshold", source.revolute_jump_threshold);
265 }
266
267 return moveit::core::MoveItErrorCode::SUCCESS;
268};
269
270// CartesianWaypointsFeatures.
271
273 : name_("CartesianWaypointsFeatures"), match_tolerance_(match_tolerance)
274{
275}
276
278{
279 return name_;
280}
281
283 const GetCartesianPath::Request& source,
284 const MoveGroupInterface& move_group,
285 double exact_match_precision) const
286{
287 return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, match_tolerance_ + exact_match_precision);
288};
289
291 const GetCartesianPath::Request& source,
292 const MoveGroupInterface& move_group,
293 double exact_match_precision) const
294{
295 return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, exact_match_precision);
296};
297
299 const GetCartesianPath::Request& source,
300 const MoveGroupInterface& move_group) const
301{
302 std::string path_request_frame_id = getCartesianPathRequestFrameId(move_group, source);
303 std::string base_frame = move_group.getRobotModel()->getModelFrame();
304
305 metadata.append(name_ + ".link_name", source.link_name);
306 metadata.append(name_ + ".robot_model.frame_id", base_frame);
307
308 // Waypoints.
309
310 size_t waypoint_idx = 0;
311 for (const auto& waypoint : source.waypoints)
312 {
313 std::string meta_name = name_ + ".waypoints_" + std::to_string(waypoint_idx++);
314
315 geometry_msgs::msg::Point canonical_position = waypoint.position;
316 geometry_msgs::msg::Quaternion canonical_orientation = waypoint.orientation;
317
318 // Canonicalize to robot base frame if necessary.
319 if (path_request_frame_id != base_frame)
320 {
321 if (MoveItErrorCode status = restateInNewFrame(move_group.getTF(), path_request_frame_id, base_frame,
322 &canonical_position, &canonical_orientation, tf2::TimePointZero);
323 status != MoveItErrorCode::SUCCESS)
324 {
325 // NOTE: methyldragon -
326 // Ideally we would restore the original state here and undo our changes, however copy of the query is not
327 // supported.
328 std::stringstream ss;
329 ss << "Skipping " << name_ << " metadata append: " << status.message;
330 return MoveItErrorCode(status.val, status.message);
331 }
332 }
333
334 // Position
335 metadata.append(meta_name + ".position.x", canonical_position.x);
336 metadata.append(meta_name + ".position.y", canonical_position.y);
337 metadata.append(meta_name + ".position.z", canonical_position.z);
338
339 // Orientation
340 metadata.append(meta_name + ".orientation.x", canonical_orientation.x);
341 metadata.append(meta_name + ".orientation.y", canonical_orientation.y);
342 metadata.append(meta_name + ".orientation.z", canonical_orientation.z);
343 metadata.append(meta_name + ".orientation.w", canonical_orientation.w);
344 }
345
346 return moveit::core::MoveItErrorCode::SUCCESS;
347};
348
349MoveItErrorCode CartesianWaypointsFeatures::appendFeaturesAsFetchQueryWithTolerance(
350 Query& query, const GetCartesianPath::Request& source, const MoveGroupInterface& move_group,
351 double match_tolerance) const
352{
353 std::string path_request_frame_id = getCartesianPathRequestFrameId(move_group, source);
354 std::string base_frame = move_group.getRobotModel()->getModelFrame();
355
356 query.append(name_ + ".link_name", source.link_name);
357 query.append(name_ + ".robot_model.frame_id", base_frame);
358
359 // Waypoints.
360
361 size_t waypoint_idx = 0;
362 for (const auto& waypoint : source.waypoints)
363 {
364 std::string meta_name = name_ + ".waypoints_" + std::to_string(waypoint_idx++);
365
366 geometry_msgs::msg::Point canonical_position = waypoint.position;
367 geometry_msgs::msg::Quaternion canonical_orientation = waypoint.orientation;
368
369 // Canonicalize to robot base frame if necessary.
370 if (path_request_frame_id != base_frame)
371 {
372 if (MoveItErrorCode status = restateInNewFrame(move_group.getTF(), path_request_frame_id, base_frame,
373 &canonical_position, &canonical_orientation, tf2::TimePointZero);
374 status != MoveItErrorCode::SUCCESS)
375 {
376 // NOTE: methyldragon -
377 // Ideally we would restore the original state here and undo our changes, however copy of the query is not
378 // supported.
379 std::stringstream ss;
380 ss << "Skipping " << name_ << " query append: " << status.message;
381 return MoveItErrorCode(status.val, status.message);
382 }
383 }
384
385 // Position
386 queryAppendCenterWithTolerance(query, meta_name + ".position.x", canonical_position.x, match_tolerance);
387 queryAppendCenterWithTolerance(query, meta_name + ".position.y", canonical_position.y, match_tolerance);
388 queryAppendCenterWithTolerance(query, meta_name + ".position.z", canonical_position.z, match_tolerance);
389
390 // Orientation
391 queryAppendCenterWithTolerance(query, meta_name + ".orientation.x", canonical_orientation.x, match_tolerance);
392 queryAppendCenterWithTolerance(query, meta_name + ".orientation.y", canonical_orientation.y, match_tolerance);
393 queryAppendCenterWithTolerance(query, meta_name + ".orientation.z", canonical_orientation.z, match_tolerance);
394 queryAppendCenterWithTolerance(query, meta_name + ".orientation.w", canonical_orientation.w, match_tolerance);
395 }
396
397 return moveit::core::MoveItErrorCode::SUCCESS;
398};
399
400// CartesianPathConstraintsFeatures.
401
403 : name_("CartesianPathConstraintsFeatures"), match_tolerance_(match_tolerance)
404{
405}
406
408{
409 return name_;
410}
411
412MoveItErrorCode
413CartesianPathConstraintsFeatures::appendFeaturesAsFuzzyFetchQuery(Query& query, const GetCartesianPath::Request& source,
414 const MoveGroupInterface& move_group,
415 double exact_match_precision) const
416{
417 return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, match_tolerance_ + exact_match_precision);
418};
419
420MoveItErrorCode
421CartesianPathConstraintsFeatures::appendFeaturesAsExactFetchQuery(Query& query, const GetCartesianPath::Request& source,
422 const MoveGroupInterface& move_group,
423 double exact_match_precision) const
424{
425 return appendFeaturesAsFetchQueryWithTolerance(query, source, move_group, exact_match_precision);
426};
427
429 Metadata& metadata, const GetCartesianPath::Request& source, const MoveGroupInterface& move_group) const
430{
431 return appendConstraintsAsInsertMetadata(metadata, { source.path_constraints }, move_group,
433 name_ + ".path_constraints");
434};
435
436MoveItErrorCode CartesianPathConstraintsFeatures::appendFeaturesAsFetchQueryWithTolerance(
437 Query& query, const GetCartesianPath::Request& source, const MoveGroupInterface& move_group,
438 double match_tolerance) const
439{
440 return appendConstraintsAsFetchQueryWithTolerance(query, { source.path_constraints }, move_group, match_tolerance,
442 name_ + ".path_constraints");
443};
444
445} // namespace trajectory_cache
446} // namespace moveit_ros
moveit::core::MoveItErrorCode appendFeaturesAsExactFetchQuery(warehouse_ros::Query &query, const moveit_msgs::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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 appendFeaturesAsInsertMetadata(warehouse_ros::Metadata &metadata, const moveit_msgs::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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 appendFeaturesAsExactFetchQuery(warehouse_ros::Query &query, const moveit_msgs::srv::GetCartesianPath::Request &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 appendFeaturesAsExactFetchQuery(warehouse_ros::Query &query, const moveit_msgs::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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 appendFeaturesAsExactFetchQuery(warehouse_ros::Query &query, const moveit_msgs::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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::srv::GetCartesianPath::Request &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_msgs::srv::GetCartesianPath::Request 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,...
moveit::core::MoveItErrorCode restateInNewFrame(const std::shared_ptr< tf2_ros::Buffer > &tf, const std::string &target_frame, const std::string &source_frame, geometry_msgs::msg::Point *translation, geometry_msgs::msg::Quaternion *rotation, const tf2::TimePoint &lookup_time=tf2::TimePointZero)
Restates a translation and rotation in a new frame.
Definition utils.cpp:88
std::string getCartesianPathRequestFrameId(const moveit::planning_interface::MoveGroupInterface &move_group, const moveit_msgs::srv::GetCartesianPath::Request &path_request)
Gets cartesian path request frame ID. If path_request 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,...
void queryAppendCenterWithTolerance(warehouse_ros::Query &query, const std::string &name, double center, double tolerance)
Appends a range inclusive query with some tolerance about some center value.