43 #include <tf2/LinearMath/Quaternion.h>
44 #include <tf2/LinearMath/Vector3.h>
46 #include <unordered_map>
52 static const rclcpp::Logger LOGGER =
53 rclcpp::get_logger(
"moveit_cached_ik_kinematics_plugin.cached_ik_kinematics_plugin");
80 Pose(
const geometry_msgs::msg::Pose& pose);
92 using IKEntry = std::pair<std::vector<Pose>, std::vector<double>>;
103 void initializeCache(
const std::string& robot_id,
const std::string& group_name,
const std::string& cache_name,
114 void updateCache(
const IKEntry& nearest,
const std::vector<Pose>& poses,
const std::vector<double>& config)
const;
120 double configDistance2(
const std::vector<double>& config1,
const std::vector<double>& config2)
const;
152 class IKCacheMap :
public std::unordered_map<std::string, IKCache*>
158 IKCacheMap(
const std::string& robot_description,
const std::string& group_name,
unsigned int num_joints);
165 const std::vector<std::string>& active,
166 const std::vector<Pose>& poses)
const;
172 const std::vector<std::string>& active,
const std::vector<Pose>& poses,
173 const std::vector<double>& config);
176 std::string
getKey(
const std::vector<std::string>& fixed,
const std::vector<std::string>& active)
const;
185 template <
typename KinematicsPlugin,
typename =
bool>
190 template <
typename KinematicsPlugin>
191 struct HasRobotModelApi<KinematicsPlugin, decltype(std::declval<KinematicsPlugin&>().initialize(
192 std::declval<const rclcpp::Node::SharedPtr&>(),
193 std::declval<const moveit::core::RobotModel&>(), std::string(),
194 std::string(), std::vector<std::string>(), 0.0))> : std::true_type
198 template <
typename KinematicsPlugin,
typename =
bool>
203 template <
typename KinematicsPlugin>
205 decltype(std::declval<KinematicsPlugin&>().KinematicsPlugin::initialize(
206 std::string(), std::string(), std::string(), std::vector<std::string>(), 0.0))>
212 template <
class KinematicsPlugin>
228 const std::string& group_name,
const std::string& base_frame,
229 const std::vector<std::string>& tip_frames,
double search_discretization)
override
232 return initializeImpl(node, robot_model, group_name, base_frame, tip_frames, search_discretization);
235 bool getPositionIK(
const geometry_msgs::msg::Pose& ik_pose,
const std::vector<double>& ik_seed_state,
236 std::vector<double>& solution, moveit_msgs::msg::MoveItErrorCodes& error_code,
239 bool searchPositionIK(
const geometry_msgs::msg::Pose& ik_pose,
const std::vector<double>& ik_seed_state,
240 double timeout, std::vector<double>& solution, moveit_msgs::msg::MoveItErrorCodes& error_code,
243 bool searchPositionIK(
const geometry_msgs::msg::Pose& ik_pose,
const std::vector<double>& ik_seed_state,
244 double timeout,
const std::vector<double>& consistency_limits, std::vector<double>& solution,
245 moveit_msgs::msg::MoveItErrorCodes& error_code,
248 bool searchPositionIK(
const geometry_msgs::msg::Pose& ik_pose,
const std::vector<double>& ik_seed_state,
249 double timeout, std::vector<double>& solution,
const IKCallbackFn& solution_callback,
250 moveit_msgs::msg::MoveItErrorCodes& error_code,
253 bool searchPositionIK(
const geometry_msgs::msg::Pose& ik_pose,
const std::vector<double>& ik_seed_state,
254 double timeout,
const std::vector<double>& consistency_limits, std::vector<double>& solution,
255 const IKCallbackFn& solution_callback, moveit_msgs::msg::MoveItErrorCodes& error_code,
259 rclcpp::Node::SharedPtr node_;
263 void initCache(
const std::string& robot_id,
const std::string& group_name,
const std::string& cache_name);
268 template <
class T = KinematicsPlugin>
269 typename std::enable_if<HasRobotModelApi<T>::value,
bool>
::type
271 const std::string& group_name,
const std::string& base_frame,
272 const std::vector<std::string>& tip_frames,
double search_discretization)
274 if (tip_frames.size() != 1)
276 RCLCPP_ERROR(LOGGER,
"This solver does not support multiple tip frames");
281 if (!KinematicsPlugin::initialize(node, robot_model, group_name, base_frame, tip_frames, search_discretization))
283 initCache(robot_model.
getName(), group_name, base_frame + tip_frames[0]);
287 template <
class T = KinematicsPlugin>
288 typename std::enable_if<!HasRobotModelApi<T>::value,
bool>
::type
290 const std::string& ,
const std::string& ,
291 const std::vector<std::string>& ,
double )
296 template <
class T = KinematicsPlugin>
297 typename std::enable_if<HasRobotDescApi<T>::value,
bool>
::type
298 initializeImpl(
const std::string& robot_description,
const std::string& group_name,
const std::string& base_frame,
299 const std::string& tip_frame,
double search_discretization)
302 if (!KinematicsPlugin::initialize(robot_description, group_name, base_frame, tip_frame, search_discretization))
304 initCache(robot_description, group_name, base_frame + tip_frame);
308 template <
class T = KinematicsPlugin>
309 typename std::enable_if<!HasRobotDescApi<T>::value,
bool>
::type
310 initializeImpl(
const std::string& ,
const std::string& ,
const std::string& ,
311 const std::string& ,
double )
328 template <
class KinematicsPlugin>
338 const std::string& group_name,
const std::string& base_frame,
339 const std::vector<std::string>& tip_frames,
double search_discretization)
override;
341 bool searchPositionIK(
const std::vector<geometry_msgs::msg::Pose>& ik_poses,
const std::vector<double>& ik_seed_state,
342 double timeout,
const std::vector<double>& consistency_limits, std::vector<double>& solution,
343 const IKCallbackFn& solution_callback, moveit_msgs::msg::MoveItErrorCodes& error_code,
~CachedIKKinematicsPlugin() override
CachedIKKinematicsPlugin()
bool getPositionIK(const geometry_msgs::msg::Pose &ik_pose, const std::vector< double > &ik_seed_state, std::vector< double > &solution, moveit_msgs::msg::MoveItErrorCodes &error_code, const KinematicsQueryOptions &options=KinematicsQueryOptions()) const override
bool initialize(const rclcpp::Node::SharedPtr &node, const moveit::core::RobotModel &robot_model, const std::string &group_name, const std::string &base_frame, const std::vector< std::string > &tip_frames, double search_discretization) override
kinematics::KinematicsQueryOptions KinematicsQueryOptions
kinematics::KinematicsBase::IKCallbackFn IKCallbackFn
bool searchPositionIK(const geometry_msgs::msg::Pose &ik_pose, const std::vector< double > &ik_seed_state, double timeout, std::vector< double > &solution, moveit_msgs::msg::MoveItErrorCodes &error_code, const KinematicsQueryOptions &options=KinematicsQueryOptions()) const override
bool searchPositionIK(const std::vector< geometry_msgs::msg::Pose > &ik_poses, const std::vector< double > &ik_seed_state, double timeout, const std::vector< double > &consistency_limits, std::vector< double > &solution, const IKCallbackFn &solution_callback, moveit_msgs::msg::MoveItErrorCodes &error_code, const KinematicsQueryOptions &options=KinematicsQueryOptions(), const moveit::core::RobotState *context_state=nullptr) const override
bool initialize(const rclcpp::Node::SharedPtr &node, const moveit::core::RobotModel &robot_model, const std::string &group_name, const std::string &base_frame, const std::vector< std::string > &tip_frames, double search_discretization) override
kinematics::KinematicsQueryOptions KinematicsQueryOptions
const IKEntry & getBestApproximateIKSolution(const std::vector< std::string > &fixed, const std::vector< std::string > &active, const std::vector< Pose > &poses) const
std::string getKey(const std::vector< std::string > &fixed, const std::vector< std::string > &active) const
std::string robot_description_
void updateCache(const IKEntry &nearest, const std::vector< std::string > &fixed, const std::vector< std::string > &active, const std::vector< Pose > &poses, const std::vector< double > &config)
IKCacheMap(const std::string &robot_description, const std::string &group_name, unsigned int num_joints)
A cache of inverse kinematic solutions.
double min_pose_distance_
unsigned int max_cache_size_
const IKEntry & getBestApproximateIKSolution(const Pose &pose) const
std::pair< std::vector< Pose >, std::vector< double > > IKEntry
unsigned int last_saved_cache_size_
void verifyCache(kdl_kinematics_plugin::KDLKinematicsPlugin &fk) const
NearestNeighborsGNAT< IKEntry * > ik_nn_
double configDistance2(const std::vector< double > &config1, const std::vector< double > &config2) const
std::filesystem::path cache_file_name_
void initializeCache(const std::string &robot_id, const std::string &group_name, const std::string &cache_name, const unsigned int num_joints, const Options &opts=Options())
double min_config_distance2_
void updateCache(const IKEntry &nearest, const Pose &pose, const std::vector< double > &config) const
IKCache(const IKCache &)=delete
std::vector< IKEntry > ik_cache_
Specific implementation of kinematics using KDL. This version supports any kinematic chain,...
std::function< void(const geometry_msgs::msg::Pose &, const std::vector< double > &, moveit_msgs::msg::MoveItErrorCodes &)> IKCallbackFn
Signature for a callback to validate an IK solution. Typically used for collision checking.
Definition of a kinematic model. This class is not thread safe, however multiple instances can be cre...
const std::string & getName() const
Get the model name.
Representation of a robot's state. This includes position, velocity, acceleration and effort.
double min_joint_config_distance
std::string cached_ik_path
unsigned int max_cache_size
class to represent end effector pose
double distance(const Pose &pose) const
tf2::Quaternion orientation
A set of options for the kinematics solver.