Robowflex  v0.1
Making MoveIt Easy
robowflex_library/include/robowflex_library/robot.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_ROBOT_
4 #define ROBOWFLEX_ROBOT_
5 
6 #include <string> // for std::string
7 #include <vector> // for std::vector
8 #include <map> // for std::map
9 
10 #include <Eigen/Core>
11 #include <Eigen/Geometry>
12 
13 #include <urdf/model.h>
14 #include <srdfdom/model.h>
15 #include <tinyxml2.h>
16 
17 #include <moveit_msgs/PositionConstraint.h>
18 #include <moveit_msgs/OrientationConstraint.h>
19 #include <moveit_msgs/Constraints.h>
20 
21 #include <moveit/robot_model/robot_model.h>
22 #include <moveit/robot_state/robot_state.h>
23 #include <moveit/robot_trajectory/robot_trajectory.h>
24 #include <moveit/robot_model_loader/robot_model_loader.h>
25 #include <moveit/kinematic_constraints/kinematic_constraint.h>
26 
31 
32 namespace robowflex
33 {
34  /** \cond IGNORE */
36  ROBOWFLEX_CLASS_FORWARD(Geometry);
37  /** \endcond */
38 
39  /** \cond IGNORE */
41  /** \endcond */
42 
43  /** \class robowflex::RobotPtr
44  \brief A shared pointer wrapper for robowflex::Robot. */
45 
46  /** \class robowflex::RobotConstPtr
47  \brief A const shared pointer wrapper for robowflex::Robot. */
48 
49  /** \brief Loads information about a robot and maintains information about a robot's state.
50  *
51  * The Robot class is a wrapper around _MoveIt!_'s robot_model::RobotModel and a "scratch"
52  * robot_state::RobotState. Mostly, this class handles loading the relevant information to the parameter
53  * server without the use of a launch file. The necessary files are the URDF and SRDF (both either XML or
54  * xacro files), and the joint limits and kinematics plugin (both YAML files). These pieces of information
55  * are loaded to the parameter server under the name provided to the constructor so that multiple robots
56  * can be used. Note that all information is also loaded under the unique namespace generated by the
57  * IO::Handler.
58  *
59  * By default, no IK kinematics plugins are loaded due to startup costs (particularly for R2). You must
60  * call loadKinematics() with the desired planning groups (as defined by the SRDF) if you want IK.
61  *
62  * Additionally, post-processing function hooks are provided if modifications need to be done to any of
63  * the necessary files. This enables the addition of extra joints, semantic information, joint limit
64  * overloading, etc. as needed. These functions are called after the robot is initially loaded, so it is
65  * possible to access robot model information in these functions. For example, see
66  * setSRDFPostProcessAddPlanarJoint().
67  */
68  class Robot
69  {
70  public:
71  /** \brief A function that runs after loading a YAML file and can modify its contents. Returns true on
72  * success, false on failure.
73  */
74  typedef std::function<bool(YAML::Node &)> PostProcessYAMLFunction;
75 
76  /** \brief A function that runs after loading a XML file and can modify its contents. Returns true on
77  * success, false on failure.
78  */
79  typedef std::function<bool(tinyxml2::XMLDocument &)> PostProcessXMLFunction;
80 
81  static const std::string ROBOT_DESCRIPTION; ///< Default robot description name.
82  static const std::string ROBOT_SEMANTIC; ///< Default robot semantic description suffix.
83  static const std::string ROBOT_PLANNING; ///< Default robot planning description suffix.
84  static const std::string ROBOT_KINEMATICS; ///< Default robot kinematics description suffix.
85 
86  /** \brief Constructor.
87  * \param[in] name The name of the robot. Used to namespace information under.
88  */
89  Robot(const std::string &name);
90 
91  // non-copyable
92  Robot(Robot const &) = delete;
93  void operator=(Robot const &) = delete;
94 
95  /** \name Initialization and Loading
96  \{ */
97 
98  /** \brief Initializes a robot from a kinematic description.
99  * A default semantic description is used.
100  * \param[in] urdf_file Location of the robot's URDF (XML or .xacro file).
101  * \return True on success, false on failure.
102  */
103  bool initialize(const std::string &urdf_file);
104 
105  /** \brief Initialize a robot with a kinematics description.
106  * \param[in] kinematics_file Location of the kinematics plugin information (a YAML file).
107  * \return True on success, false on failure.
108  */
109  bool initializeKinematics(const std::string &kinematics_file);
110 
111  /** \brief Initializes a robot from a kinematic and semantic description.
112  * All files are loaded under the robot's namespace.
113  * \param[in] urdf_file Location of the robot's URDF (XML or .xacro file).
114  * \param[in] srdf_file Location of the robot's SRDF (XML or .xacro file).
115  * \param[in] limits_file Location of the joint limit information (a YAML file). Optional.
116  * \param[in] kinematics_file Location of the kinematics plugin information (a YAML file). Optional.
117  * \return True on success, false on failure.
118  */
119  bool initialize(const std::string &urdf_file, const std::string &srdf_file,
120  const std::string &limits_file = "", const std::string &kinematics_file = "");
121 
122  /** \brief Initializes a robot from a YAML config which includes URDF (urdf) and optional the SRDF
123  * (srdf), joint limits (joint_limits), IK plugins (kinematics) and a default state (robot_state).
124  * All files are loaded under the robot's namespace. The names of the YAML keys are in parenthesis.
125  * \param[in] config_file Location of the yaml config file.
126  * \return True on success, false on failure.
127  */
128  bool initializeFromYAML(const std::string &config_file);
129 
130  /** \brief Loads a YAML file into the robot's namespace under \a name.
131  * \param[in] name Name to load file under.
132  * \param[in] file File to load.
133  * \return True on success, false on failure.
134  */
135  bool loadYAMLFile(const std::string &name, const std::string &file);
136 
137  /** \brief Loads a YAML file into the robot's namespace under \a name, with a post-process function.
138  * \param[in] name Name to load file under.
139  * \param[in] file File to load.
140  * \param[in] function Optional post processing function.
141  * \return True on success, false on failure.
142  */
143  bool loadYAMLFile(const std::string &name, const std::string &file,
144  const PostProcessYAMLFunction &function);
145 
146  /** \brief Loads an XML or .xacro file into a string.
147  * \param[in] file File to load.
148  * \return XML string upon success, empty string on failure.
149  */
150  std::string loadXMLFile(const std::string &file);
151 
152  /** \brief Sets a post processing function for loading the URDF.
153  * \param[in] function The function to use.
154  */
156 
157  /** \brief Checks if a node link exist with named name_link
158  * \param[in] doc The URDF description.
159  * \param[in] name The name of the link to find.
160  * \return True if link exists, false otherwise.
161  */
162  bool isLinkURDF(tinyxml2::XMLDocument &doc, const std::string &name);
163 
164  /** \brief Sets a post processing function for loading the SRDF.
165  * \param[in] function The function to use.
166  */
168 
169  /** \brief Sets a post processing function for loading the joint limits file.
170  * \param[in] function The function to use.
171  */
173 
174  /** \brief Sets a post processing function for loading the kinematics plugin file.
175  * \param[in] function The function to use.
176  */
178 
179  /** \brief Adds a planar virtual joint through the SRDF to the loaded robot with name \a name. This
180  * joint will have three degrees of freedom: <name>/x, <name>/y, and <name>/theta. Will apply this
181  * joint between the world and the root frame.
182  * \param[in] name Name for new joint.
183  */
185 
186  /** \brief Adds a planar virtual joint through the SRDF to the loaded robot with name \a name. This
187  * joint will have three degrees of freedom: <name>/x, <name>/y, and <name>/theta. Will apply this
188  * joint between the world and the root frame.
189  * \param[in] name Name for new joint.
190  */
192 
193  /** \brief Loads the kinematics plugin for a joint group and its subgroups. No kinematics are loaded
194  * by default.
195  * \param[in] group Joint group name to load.
196  * \param[in] load_subgroups Load kinematic solvers for subgroups of the requested group.
197  * \return True on success, false on failure.
198  */
199  bool loadKinematics(const std::string &group, bool load_subgroups = true);
200 
201  /** \} */
202 
203  /** \name Getters and Setters
204  \{ */
205 
206  /** \brief Get the robot's model name.
207  * \return The robot's model name.
208  */
209  const std::string &getModelName() const;
210 
211  /** \brief Get the robot's name.
212  * \return The robot's name.
213  */
214  const std::string &getName() const;
215 
216  /** \brief Get a const reference to the loaded robot model.
217  * \return The robot model.
218  */
219  const robot_model::RobotModelPtr &getModelConst() const;
220 
221  /** \brief Get a reference to the loaded robot model.
222  * \return The robot model.
223  */
224  robot_model::RobotModelPtr &getModel();
225 
226  /** \brief Get the raw URDF Model.
227  * \return The URDF Model.
228  */
229  urdf::ModelInterfaceConstSharedPtr getURDF() const;
230 
231  /** \brief Get the raw URDF Model as a string.
232  * \return The URDF Model as a string.
233  */
234  const std::string &getURDFString() const;
235 
236  /** \brief Get the raw SRDF Model.
237  * \return The SRDF model.
238  */
239  srdf::ModelConstSharedPtr getSRDF() const;
240 
241  /** \brief Get the raw SRDF Model as a string.
242  * \return The SRDF model as a string.
243  */
244  const std::string &getSRDFString() const;
245 
246  /** \brief Get the underlying IO handler used for this robot.
247  * \return A reference to the IO handler.
248  */
249  const IO::Handler &getHandlerConst() const;
250 
251  /** \brief Get the underlying IO handler used for this robot.
252  * \return A reference to the IO handler.
253  */
255 
256  /** \} */
257 
258  /** \name Robot State Operations
259  \{ */
260 
261  /** \brief Get a const reference to the scratch robot state.
262  * \return The scratch robot state.
263  */
264  const robot_model::RobotStatePtr &getScratchStateConst() const;
265 
266  /** \brief Get a reference to the scratch robot state.
267  * \return The scratch robot state.
268  */
269  robot_model::RobotStatePtr &getScratchState();
270 
271  /** \brief Allocate a new robot state that is a clone of the current scratch state.
272  * \return The new robot state.
273  */
274  robot_model::RobotStatePtr cloneScratchState() const;
275 
276  /** \brief Allocate a new robot state.
277  * \return The new robot state.
278  */
279  robot_model::RobotStatePtr allocState() const;
280 
281  /** \brief Sets the scratch state from a vector of joint positions (all must be specified)
282  * \param[in] positions Joint positions to set.
283  */
284  void setState(const std::vector<double> &positions);
285 
286  /** \brief Sets the scratch state from a map of joint name to position.
287  * \param[in] variable_map Joint positions to set.
288  */
289  void setState(const std::map<std::string, double> &variable_map);
290 
291  /** \brief Sets the scratch state from a vector of joint names and their positions.
292  * \param[in] variable_names Joint names.
293  * \param[in] variable_position Position of joint variable (index matches entry in \a variable_names)
294  */
295  void setState(const std::vector<std::string> &variable_names,
296  const std::vector<double> &variable_position);
297 
298  /** \brief Sets the scratch state from a joint state message.
299  * \param[in] state The state to set.
300  */
301  void setState(const sensor_msgs::JointState &state);
302 
303  /** \brief Sets the scratch state from a robot state message.
304  * \param[in] state The state to set.
305  */
306  void setState(const moveit_msgs::RobotState &state);
307 
308  /** \brief Sets the scratch state from a robot state message saved to a YAML file.
309  * \param[in] file The YAML file to load.
310  */
311  void setStateFromYAMLFile(const std::string &file);
312 
313  /** \brief Sets the group of the scratch state to a vector of joint positions.
314  * \param[in] name Name of group to set.
315  * \param[in] positions Positions to set.
316  */
317  void setGroupState(const std::string &name, const std::vector<double> &positions);
318 
319  /** \brief Gets the current joint positions of the scratch state.
320  * \return A vector of joint positions.
321  */
323 
324  /** \brief Get the current scratch state as a message.
325  * \return The scratch state as a MoveIt message.
326  */
327  moveit_msgs::RobotState getStateMsg() const;
328 
329  /** \brief Set the group state of a MoveIt RobotState message.
330  * \param[out] state The state message to set.
331  * \param[in] group Group in state to set.
332  * \param[in] positions Positions to set group state to.
333  */
334  void setStateMsgGroupState(moveit_msgs::RobotState &state, const std::string &group,
335  const std::vector<double> &positions) const;
336 
337  /** \brief Gets the names of joints of the robot.
338  * \return A vector of joint names.
339  */
341 
342  /** \brief Checks if a joint exists in the robot.
343  * \return True if the joint exists, false otherwise.
344  */
345  bool hasJoint(const std::string &joint) const;
346 
347  /** \brief Get the current pose of a link on the scratch state.
348  * \param[in] name The name of the link to find the transform of.
349  * \return The transform of link \a name.
350  */
351  const RobotPose &getLinkTF(const std::string &name) const;
352 
353  /** \brief Get the current pose of a link \a target in the frame of \a base.
354  * \param[in] base The link to use as the base frame.
355  * \param[in] target The link to find the transform of.
356  * \return The transform of link \a target in the frame of \a base.
357  */
358  const RobotPose getRelativeLinkTF(const std::string &base, const std::string &target) const;
359 
360  /** \} */
361 
362  /** \name Inverse Kinematics
363  \{ */
364 
365  /** \brief Robot IK Query options.
366  * IK queries in Robowflex consist of:
367  * a) A position specified by some geometric region (a robowflex::Geometry) at a pose.
368  * b) An orientation specified by some base orientation with allowable deviations specified by
369  * tolerances on the XYZ Euler axes.
370  * It is recommended to use the provided constructors to specify a query, or to use the addRequest()
371  * function. Multiple target tips can be specified, but note that not all kinematics solvers support
372  * multi-tip IK. Additionally, a robowflex::Scene can be specified to do collision-aware IK.
373  */
374  struct IKQuery
375  {
377 
378  /** \brief Metric for evaluating states within an IK query.
379  * \param[in] state Resulting state from query.
380  * \param[in] scene Input scene for query, if available. Nullptr otherwise.
381  * \param[in] result Result of evaluating validity of constraint.
382  * \return Value of the state.
383  */
384  using Metric =
385  std::function<double(const robot_state::RobotState &state, const SceneConstPtr &scene,
387 
388  /** \name Query Targets
389  \{ */
390 
391  std::string group; ///< Target joint group to do IK for.
392  std::vector<std::string> tips; ///< List of end-effectors.
393  std::vector<GeometryConstPtr> regions; ///< Regions to sample target positions from.
394  RobotPoseVector region_poses; ///< Poses of regions.
396  EigenSTL::vector_Vector3d tolerances; ///< XYZ Euler orientation tolerances.
397 
398  /** \} */
399 
400  /** \name Additional Solver Options
401  \{ */
402 
403  SceneConstPtr scene; ///< If provided, use this scene for collision checking.
404  bool verbose{false}; ///< Verbose output of collision checking.
405  bool random_restart{true}; ///< Randomly reset joint states.
406  kinematics::KinematicsQueryOptions options; ///< Other query options.
407  std::size_t attempts{constants::ik_attempts}; ///< IK attempts (samples within regions).
408  double timeout{0.}; ///< Timeout for each query.
409 
410  bool validate{false}; ///< If true, double check if result is valid and use this rather than
411  ///< validity reported by the solver.
412  double valid_distance{0.}; ///< If positive and validate = true, will return success if result is
413  ///< within distance of kinematic constraint set.
414 
415  std::vector<Metric> metrics; ///< Metrics used to evaluate configurations. If metrics are added,
416  ///< solver will use all allotted resources to search for the best
417  ///< configuration. If there are multiple metrics, metric values
418  ///< will be added together.
419 
420  /** \} */
421 
422  /** \brief Constructor. Empty for fine control.
423  * \param[in] group Group to set.
424  */
425  IKQuery(const std::string &group);
426 
427  /** \name Directional Offset Constructors
428  \{ */
429 
430  /** \brief Constructor. Initialize an IK query based on offsets from an initial robot state.
431  * \param[in] group Group to set.
432  * \param[in] tip Tip frame to apply offset to.
433  * \param[in] start Initial robot state to compute offset for.
434  * \param[in] direction Vector direction of end-effector motion. Will be used as unit vector.
435  * \param[in] distance Distance to travel in that direction.
436  */
437  IKQuery(const std::string &group, //
438  const std::string &tip, //
439  const robot_state::RobotState &start, //
440  const Eigen::Vector3d &direction, //
441  double distance);
442 
443  /** \brief Constructor. Initialize an IK query based on offsets from an initial robot state.
444  * \param[in] group Group to set.
445  * \param[in] tip Tip frame to apply offset to.
446  * \param[in] start Initial robot state to compute offset for.
447  * \param[in] position_offset Position offset to apply from current tip position.
448  * \param[in] rotation_offset Rotational offset to apply from current tip position.
449  */
450  IKQuery(const std::string &group, //
451  const std::string &tip, //
452  const robot_state::RobotState &start, //
453  const Eigen::Vector3d &position_offset, //
454  const Eigen::Quaterniond &rotation_offset = Eigen::Quaterniond::Identity());
455 
456  /** \brief Constructor. Initialize an IK query based on an offset from an initial robot state.
457  * Only for single-tip systems.
458  * \param[in] group Group to set.
459  * \param[in] tip Tip frame to apply offset to.
460  * \param[in] start Initial robot state to compute offset for.
461  * \param[in] offset Offset to apply from current tip position.
462  */
463  IKQuery(const std::string &group, //
464  const std::string &tip, //
465  const robot_state::RobotState &start, //
466  const RobotPose &offset);
467 
468  /** \} */
469 
470  /** \name Single Target Constructors
471  \{ */
472 
473  /** \brief Constructor. Initialize a basic IK query to reach the desired \a pose.
474  * \param[in] group Group to set.
475  * \param[in] pose Desired pose of end-effector.
476  * \param[in] radius Radius tolerance around position.
477  * \param[in] tolerances Tolerance about \a orientation.
478  */
479  IKQuery(const std::string &group, //
480  const RobotPose &pose, //
481  double radius = constants::ik_tolerance, //
482  const Eigen::Vector3d &tolerance = constants::ik_vec_tolerance);
483 
484  /** \brief Constructor. Initialize a basic IK query to reach the desired \a position and \a
485  * orientation.
486  * \param[in] group Group to set.
487  * \param[in] position Position to achieve.
488  * \param[in] orientation Mean orientation.
489  * \param[in] radius Radius tolerance around position.
490  * \param[in] tolerances Tolerance about \a orientation.
491  */
492  IKQuery(const std::string &group, //
493  const Eigen::Vector3d &position, //
494  const Eigen::Quaterniond &orientation, //
495  double radius = constants::ik_tolerance, //
496  const Eigen::Vector3d &tolerance = constants::ik_vec_tolerance);
497 
498  /** \brief Constructor. Initialize an IK query to reach somewhere in the provided \a region (at a
499  * \a pose) and \a orientation.
500  * \param[in] group Group to set.
501  * \param[in] region Region of points for position.
502  * \param[in] pose Pose of the \a region.
503  * \param[in] orientation Mean orientation.
504  * \param[in] tolerances Tolerance about \a orientation.
505  * \param[in] scene Optional scene to do collision checking against.
506  * \param[in] verbose If true, will give verbose collision checking output.
507  */
508  IKQuery(const std::string &group, //
509  const GeometryConstPtr &region, //
510  const RobotPose &pose, //
511  const Eigen::Quaterniond &orientation, //
512  const Eigen::Vector3d &tolerance = constants::ik_vec_tolerance, //
513  const ScenePtr &scene = nullptr, //
514  bool verbose = false);
515 
516  /** Constructor. Initialize a basic IK query to grasp an object.
517  * \param[in] group Group to set.
518  * \param[in] offset Offset of end-effector from object frame.
519  * \param[in] scene Scene that object is in.
520  * \param[in] object Name of object to grasp.
521  * \param[in] tolerances Position tolerances on the XYZ axes for the grasp.
522  * \param[in] verbose If true, will give verbose collision checking output.
523  */
524  IKQuery(const std::string &group, //
525  const RobotPose &offset, //
526  const ScenePtr &scene, //
527  const std::string &object, //
528  const Eigen::Vector3d &tolerances = constants::ik_vec_tolerance,
529  bool verbose = false);
530 
531  /** \brief Constructor. Initialize an IK query from MoveIt message constraints.
532  * \param[in] group Group to set.
533  * \param[in] pc Position constraint.
534  * \param[in] oc Orientation constraint.
535  */
536  IKQuery(const std::string &group, //
537  const moveit_msgs::PositionConstraint &pc, //
538  const moveit_msgs::OrientationConstraint &oc);
539 
540  /** \} */
541 
542  /** \name Multiple Target Constructors
543  \{ */
544 
545  /** \brief Constructor. Initialize a basic multi-target IK query so that each of the \a tips reach
546  * their desired \a poses.
547  * \param[in] group Group to set.
548  * \param[in] poses Desired poses of end-effector tips.
549  * \param[in] input_tips End-effector tips to target.
550  * \param[in] radius Radius tolerance around position.
551  * \param[in] tolerances Tolerance about \a orientation.
552  * \param[in] scene Optional scene to do collision checking against.
553  * \param[in] verbose If true, will give verbose collision checking output.
554  */
555  IKQuery(const std::string &group, //
556  const RobotPoseVector &poses, //
557  const std::vector<std::string> &input_tips, //
558  double radius = constants::ik_tolerance, //
559  const Eigen::Vector3d &tolerance = constants::ik_vec_tolerance, //
560  const ScenePtr &scene = nullptr, //
561  bool verbose = false);
562 
563  /** Constructor. Initialize a multi-target IK query so that each of the \a tips reach somewhere in
564  * the provided \a region (at a \a pose) and \a orientation.
565  * \param[in] group Group to set.
566  * \param[in] input_tips End-effector tips to target.
567  * \param[in] regions Region of points for position for each tip.
568  * \param[in] poses Pose of the \a region for each tip.
569  * \param[in] orientations Mean orientation for each tip.
570  * \param[in] tolerances Tolerance about \a orientation for each tip.
571  * \param[in] scene Optional scene to do collision checking against.
572  * \param[in] verbose If true, will give verbose collision checking output.
573  */
574  IKQuery(const std::string &group, //
575  const std::vector<std::string> &input_tips, //
577  const RobotPoseVector &poses, //
579  const EigenSTL::vector_Vector3d &tolerances, //
580  const ScenePtr &scene = nullptr, //
581  bool verbose = false);
582 
583  /** \} */
584 
585  /** \name Other Setters
586  \{ */
587 
588  /** \brief Add a request for a \a tip.
589  * \param[in] tip Tip for the request.
590  * \param[in] region Region of points for position.
591  * \param[in] pose Pose of the \a region.
592  * \param[in] orientation Mean orientation.
593  * \param[in] tolerances Tolerance about \a orientation.
594  */
595  void addRequest(const std::string &tip, const GeometryConstPtr &region, const RobotPose &pose,
596  const Eigen::Quaterniond &orientation,
597  const Eigen::Vector3d &tolerance = constants::ik_vec_tolerance);
598 
599  /** \brief Set a scene to use for collision checking for this IK request.
600  * \param[in] scene Scene to check collision against.
601  * \param[in] verbose If true, will output verbose collision checking output.
602  */
603  void setScene(const ScenePtr &scene, bool verbose = false);
604 
605  /** \brief Add a metric to this IK query.
606  * \param[in] metric_function Metric to add to query.
607  */
608  void addMetric(const Metric &metric_function);
609 
610  /** \brief Add a metric to the query to evaluate distance to constraint.
611  * \param[in] weight Multiplicative weight to metric value.
612  */
613  void addDistanceMetric(double weight = 1.);
614 
615  /** \brief Add a metric to the query to evaluate how "centered" the joints of the robot are (from
616  * their 0 position).
617  * \param[in] weight Multiplicative weight to metric value.
618  */
619  void addCenteringMetric(double weight = 1.);
620 
621  /** \brief Add a metric to the query to evaluate clearance from provided scene.
622  * \param[in] weight Multiplicative weight to metric value.
623  */
624  void addClearanceMetric(double weight = 1.);
625 
626  /** \} */
627 
628  /** \name Sampling Region
629  \{ */
630 
631  /** \brief Sample desired end-effector pose for region at \a index.
632  * \param[out] pose The sampled pose.
633  * \param[in] index The index of the region to sample.
634  * \return True on success, false on failure.
635  */
636  bool sampleRegion(RobotPose &pose, std::size_t index) const;
637 
638  /** \brief Sample desired end-effector pose for each region.
639  * \param[out] poses The sampled poses.
640  * \return True on success, false on failure.
641  */
642  bool sampleRegions(RobotPoseVector &poses) const;
643 
644  /** \} */
645 
646  /** \name Other Functions
647  \{ */
648 
649  /** \brief Get this IK query as a constraint message.
650  * \param[in] base_frame Base frame of the IK solver used. Can be obtained with
651  * Robot::getSolverBaseFrame().
652  * \param[out] msg Message to fill.
653  */
654  void getMessage(const std::string &base_frame, moveit_msgs::Constraints &msg) const;
655 
656  /** \brief Get this IK query as a kinematic constraint set.
657  * \param[in] robot Robot this IK query is for.
658  * \return The IK query as a set of kinematic constraints.
659  */
660  kinematic_constraints::KinematicConstraintSetPtr getAsConstraints(const Robot &robot) const;
661 
662  /** \brief Get the value of the metrics assigned to this query for a given state and result.
663  * \param[in] state The state to evaluate.
664  * \param[in] result The result of evaluating this state against the kinematic constraint set.
665  * \return The value of the metric functions, summed.
666  */
667  double getMetricValue(const robot_state::RobotState &state,
669 
670  /** \} */
671  };
672 
673  /** \brief Sets a group of the scratch state from an IK query. If the IK query fails the scratch state
674  * retains its initial value.
675  * \param[in] query Query for inverse kinematics. See Robot::IKQuery documentation for more.
676  * \return True on success, false on failure.
677  */
678  bool setFromIK(const IKQuery &query);
679 
680  /** \brief Sets a robot state from an IK query. If the IK query fails the scratch state
681  * retains its initial value.
682  * \param[in] query Query for inverse kinematics. See Robot::IKQuery documentation for more.
683  * \param[out] state Robot state to set from IK.
684  * \return True on success, false on failure.
685  */
686  bool setFromIK(const IKQuery &query, robot_state::RobotState &state) const;
687 
688  /** \brief Validates that a state satisfies an IK query's request poses.
689  * \param[in] query The query to validate.
690  * \param[in] state The state to validate against the query.
691  * \return True if the query is satisfied, false otherwise.
692  */
693  bool validateIKQuery(const IKQuery &query, const robot_state::RobotState &state) const;
694 
695  /** \brief Returns the distance of the state to satisfying the IK query.
696  * \param[in] query The query to check.
697  * \param[in] state The state to check against the query.
698  * \return The distance of the state to satisfaction of the query.
699  */
700  double distanceToIKQuery(const IKQuery &query, const robot_state::RobotState &state) const;
701 
702  /** \brief Get the tip frames for the IK solver for a given joint model group \a group.
703  * \param[in] group The group to get the tip frames for.
704  * \return The list of tip frames. Will return an empty list on error.
705  */
707 
708  /** \brief Get the base frame for the IK solver given a joint model group \a group.
709  * \param[in] group The group to get the base frame for.
710  * \return The base frame. Will return an empty string on error.
711  */
712  std::string getSolverBaseFrame(const std::string &group) const;
713 
714  /** \} */
715 
716  /** \name IO
717  \{ */
718 
719  /** \brief Dumps the current configuration of the robot as a YAML file.
720  * \param[in] file File to write to.
721  * \return True on success, false on failure.
722  */
723  bool toYAMLFile(const std::string &file) const;
724 
725  /** \brief Dumps the names of links and absolute paths to their visual mesh files to a YAML file.
726  * \param[in] file File to save to.The name of the link to find the transform of.
727  * \return True on success, false on failure.
728  */
729  bool dumpGeometry(const std::string &file) const;
730 
731  /** \brief Dumps the tranforms of all links of a robot at its current state to a file.
732  * \param[in] filename Filename to output to.
733  * \return True on success, false on failure.
734  */
735  bool dumpTransforms(const std::string &filename) const;
736 
737  /** \brief Dumps the tranforms of all links of a robot through a robot trajectory to a file.
738  * \param[in] path Path to output.
739  * \param[in] filename Filename to output to.
740  * \param[in] fps The transforms (frames) per second used to interpolate the given path.
741  * \param[in] threshold The minimum distance between states before transforms are output.
742  * \return True on success, false on failure.
743  */
744  bool dumpPathTransforms(const robot_trajectory::RobotTrajectory &path, const std::string &filename,
745  double fps = 30, double threshold = 0.0) const;
746 
747  /** \brief Dumps the current scratch configuration of the robot to a YAML file compatible with a
748  * scene.
749  * \param[in] filename Filename to output to.
750  * \return True on success, false on failure.
751  */
752  bool dumpToScene(const std::string &filename) const;
753 
754  /** \} */
755 
756  protected:
757  /** \name Protected Initialization
758  \{ */
759 
760  /** \brief Loads the URDF file.
761  * \param[in] urdf_file The URDF file name.
762  * \return True on success, false on failure.
763  */
764  bool loadURDFFile(const std::string &urdf_file);
765 
766  /** \brief Loads the SRDF file.
767  * \param[in] srdf_file The SRDF file name.
768  * \return True on success, false on failure.
769  */
770  bool loadSRDFFile(const std::string &srdf_file);
771 
772  /** \brief Initializes and loads the robot. Calls post-processing functions and creates scratch state.
773  * \param[in] namespaced Whether or not the parameter server description is under the handler
774  * namespace.
775  */
776  void initializeInternal(bool namespaced = true);
777 
778  /** \brief Loads a robot model from the loaded information on the parameter server.
779  * \param[in] description Robot description on parameter server.
780  */
781  void loadRobotModel(const std::string &description);
782 
783  /** \brief Updates a loaded XML string based on an XML post-process function. Called after initial,
784  * unmodified robot is loaded.
785  * \param[in,out] string Input XML string.
786  * \param[in] function XML processing function.
787  */
788  void updateXMLString(std::string &string, const PostProcessXMLFunction &function);
789 
790  /** \} */
791 
792  const std::string name_; ///< Robot name.
793  IO::Handler handler_; ///< IO handler (namespaced with \a name_)
794 
795  std::string urdf_; ///< The URDF as a string.
796  std::string srdf_; ///< The SRDF as a string.
797 
798  PostProcessXMLFunction urdf_function_; ///< URDF post-processing function.
799  PostProcessXMLFunction srdf_function_; ///< SRDF post-processing function.
800  PostProcessYAMLFunction limits_function_; ///< Limits YAML post-processing function.
801  PostProcessYAMLFunction kinematics_function_; ///< Kinematics plugin YAML post-processing function.
802 
804  robot_model::RobotModelPtr model_; ///< Loaded robot model.
805  std::map<std::string, robot_model::SolverAllocatorFn> imap_; ///< Kinematic solver allocator map.
806  kinematics_plugin_loader::KinematicsPluginLoaderPtr kinematics_; ///< Kinematic plugin loader.
807 
808  robot_state::RobotStatePtr scratch_; ///< Scratch robot state.
809  };
810 
811  /** \cond IGNORE */
813  /** \endcond */
814 
815  /** \class robowflex::ParamRobotPtr
816  \brief A shared pointer wrapper for robowflex::ParamRobot. */
817 
818  /** \class robowflex::ParamRobotConstPtr
819  \brief A const shared pointer wrapper for robowflex::ParamRobot. */
820 
821  /** \brief Loads information about a robot from the parameter server.
822  */
823  class ParamRobot : public Robot
824  {
825  public:
826  /** Constructor. Loads robot from parameter server.
827  * \param[in] name Name for this robot.
828  */
829  ParamRobot(const std::string &name = "DEFAULT");
830  };
831 } // namespace robowflex
832 
833 #endif
#define ROBOWFLEX_CLASS_FORWARD(C)
Macro that forward declares a class and defines two shared ptrs types:
Definition: class_forward.h:16
A const shared pointer wrapper for robowflex::Geometry.
ROS parameter server handler to handle namespacing and automatic parameter deletion.
Definition: handler.h:19
Loads information about a robot from the parameter server.
ParamRobot(const std::string &name="DEFAULT")
Loads information about a robot and maintains information about a robot's state.
bool dumpGeometry(const std::string &file) const
Dumps the names of links and absolute paths to their visual mesh files to a YAML file.
std::vector< std::string > getJointNames() const
Gets the names of joints of the robot.
const RobotPose & getLinkTF(const std::string &name) const
Get the current pose of a link on the scratch state.
void setSRDFPostProcessAddFloatingJoint(const std::string &name)
Adds a planar virtual joint through the SRDF to the loaded robot with name name. This joint will have...
void setLimitsPostProcessFunction(const PostProcessYAMLFunction &function)
Sets a post processing function for loading the joint limits file.
static const std::string ROBOT_KINEMATICS
Default robot kinematics description suffix.
bool initializeFromYAML(const std::string &config_file)
Initializes a robot from a YAML config which includes URDF (urdf) and optional the SRDF (srdf),...
std::vector< double > getState() const
Gets the current joint positions of the scratch state.
kinematics_plugin_loader::KinematicsPluginLoaderPtr kinematics_
Kinematic plugin loader.
void loadRobotModel(const std::string &description)
Loads a robot model from the loaded information on the parameter server.
const RobotPose getRelativeLinkTF(const std::string &base, const std::string &target) const
Get the current pose of a link target in the frame of base.
PostProcessXMLFunction urdf_function_
URDF post-processing function.
bool validateIKQuery(const IKQuery &query, const robot_state::RobotState &state) const
Validates that a state satisfies an IK query's request poses.
Robot(const std::string &name)
Constructor.
bool dumpTransforms(const std::string &filename) const
Dumps the tranforms of all links of a robot at its current state to a file.
IO::Handler & getHandler()
Get the underlying IO handler used for this robot.
std::function< bool(YAML::Node &)> PostProcessYAMLFunction
A function that runs after loading a YAML file and can modify its contents. Returns true on success,...
robot_model::RobotStatePtr cloneScratchState() const
Allocate a new robot state that is a clone of the current scratch state.
void initializeInternal(bool namespaced=true)
Initializes and loads the robot. Calls post-processing functions and creates scratch state.
srdf::ModelConstSharedPtr getSRDF() const
Get the raw SRDF Model.
PostProcessXMLFunction srdf_function_
SRDF post-processing function.
void setSRDFPostProcessFunction(const PostProcessXMLFunction &function)
Sets a post processing function for loading the SRDF.
const IO::Handler & getHandlerConst() const
Get the underlying IO handler used for this robot.
std::string loadXMLFile(const std::string &file)
Loads an XML or .xacro file into a string.
PostProcessYAMLFunction limits_function_
Limits YAML post-processing function.
void setKinematicsPostProcessFunction(const PostProcessYAMLFunction &function)
Sets a post processing function for loading the kinematics plugin file.
void setURDFPostProcessFunction(const PostProcessXMLFunction &function)
Sets a post processing function for loading the URDF.
std::vector< std::string > getSolverTipFrames(const std::string &group) const
Get the tip frames for the IK solver for a given joint model group group.
void updateXMLString(std::string &string, const PostProcessXMLFunction &function)
Updates a loaded XML string based on an XML post-process function. Called after initial,...
double distanceToIKQuery(const IKQuery &query, const robot_state::RobotState &state) const
Returns the distance of the state to satisfying the IK query.
bool isLinkURDF(tinyxml2::XMLDocument &doc, const std::string &name)
Checks if a node link exist with named name_link.
PostProcessYAMLFunction kinematics_function_
Kinematics plugin YAML post-processing function.
bool loadURDFFile(const std::string &urdf_file)
Loads the URDF file.
bool toYAMLFile(const std::string &file) const
Dumps the current configuration of the robot as a YAML file.
void operator=(Robot const &)=delete
robot_model::RobotStatePtr & getScratchState()
Get a reference to the scratch robot state.
urdf::ModelInterfaceConstSharedPtr getURDF() const
Get the raw URDF Model.
std::string getSolverBaseFrame(const std::string &group) const
Get the base frame for the IK solver given a joint model group group.
IO::Handler handler_
IO handler (namespaced with name_)
void setGroupState(const std::string &name, const std::vector< double > &positions)
Sets the group of the scratch state to a vector of joint positions.
bool initialize(const std::string &urdf_file)
Initializes a robot from a kinematic description. A default semantic description is used.
void setStateMsgGroupState(moveit_msgs::RobotState &state, const std::string &group, const std::vector< double > &positions) const
Set the group state of a MoveIt RobotState message.
bool dumpToScene(const std::string &filename) const
Dumps the current scratch configuration of the robot to a YAML file compatible with a scene.
bool setFromIK(const IKQuery &query)
Sets a group of the scratch state from an IK query. If the IK query fails the scratch state retains i...
bool loadYAMLFile(const std::string &name, const std::string &file)
Loads a YAML file into the robot's namespace under name.
void setSRDFPostProcessAddPlanarJoint(const std::string &name)
Adds a planar virtual joint through the SRDF to the loaded robot with name name. This joint will have...
bool loadKinematics(const std::string &group, bool load_subgroups=true)
Loads the kinematics plugin for a joint group and its subgroups. No kinematics are loaded by default.
const std::string & getName() const
Get the robot's name.
static const std::string ROBOT_DESCRIPTION
Default robot description name.
Robot(Robot const &)=delete
const robot_model::RobotStatePtr & getScratchStateConst() const
Get a const reference to the scratch robot state.
static const std::string ROBOT_SEMANTIC
Default robot semantic description suffix.
const std::string & getSRDFString() const
Get the raw SRDF Model as a string.
moveit_msgs::RobotState getStateMsg() const
Get the current scratch state as a message.
robot_model::RobotModelPtr & getModel()
Get a reference to the loaded robot model.
std::map< std::string, robot_model::SolverAllocatorFn > imap_
Kinematic solver allocator map.
const std::string & getModelName() const
Get the robot's model name.
robot_state::RobotStatePtr scratch_
Scratch robot state.
void setState(const std::vector< double > &positions)
Sets the scratch state from a vector of joint positions (all must be specified)
std::shared_ptr< robot_model_loader::RobotModelLoader > loader_
Robot model loader.
robot_model::RobotModelPtr model_
Loaded robot model.
bool hasJoint(const std::string &joint) const
Checks if a joint exists in the robot.
std::function< bool(tinyxml2::XMLDocument &)> PostProcessXMLFunction
A function that runs after loading a XML file and can modify its contents. Returns true on success,...
const robot_model::RobotModelPtr & getModelConst() const
Get a const reference to the loaded robot model.
robot_model::RobotStatePtr allocState() const
Allocate a new robot state.
static const std::string ROBOT_PLANNING
Default robot planning description suffix.
const std::string & getURDFString() const
Get the raw URDF Model as a string.
void setStateFromYAMLFile(const std::string &file)
Sets the scratch state from a robot state message saved to a YAML file.
bool initializeKinematics(const std::string &kinematics_file)
Initialize a robot with a kinematics description.
bool dumpPathTransforms(const robot_trajectory::RobotTrajectory &path, const std::string &filename, double fps=30, double threshold=0.0) const
Dumps the tranforms of all links of a robot through a robot trajectory to a file.
bool loadSRDFFile(const std::string &srdf_file)
Loads the SRDF file.
A const shared pointer wrapper for robowflex::Scene.
A shared pointer wrapper for robowflex::Scene.
Functions for loading and animating robots in Blender.
static const double ik_tolerance
Definition: constants.h:27
static const unsigned int ik_attempts
Definition: constants.h:28
static const Eigen::Vector3d ik_vec_tolerance
Definition: constants.h:29
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
std::decay< decltype(std::declval< moveit::core::Transforms >().getTransform("")) >::type RobotPose
A pose (point in SE(3)) used in various functions. Defined from what MoveIt! uses.
Definition: adapter.h:24
Functions for loading and animating scenes in Blender.
Robot IK Query options. IK queries in Robowflex consist of: a) A position specified by some geometric...
void addRequest(const std::string &tip, const GeometryConstPtr &region, const RobotPose &pose, const Eigen::Quaterniond &orientation, const Eigen::Vector3d &tolerance=constants::ik_vec_tolerance)
Add a request for a tip.
kinematics::KinematicsQueryOptions options
Other query options.
void addCenteringMetric(double weight=1.)
Add a metric to the query to evaluate how "centered" the joints of the robot are (from their 0 positi...
std::string group
Target joint group to do IK for.
bool verbose
Verbose output of collision checking.
void addClearanceMetric(double weight=1.)
Add a metric to the query to evaluate clearance from provided scene.
EigenSTL::vector_Vector3d tolerances
XYZ Euler orientation tolerances.
bool sampleRegion(RobotPose &pose, std::size_t index) const
Sample desired end-effector pose for region at index.
kinematic_constraints::KinematicConstraintSetPtr getAsConstraints(const Robot &robot) const
Get this IK query as a kinematic constraint set.
bool sampleRegions(RobotPoseVector &poses) const
Sample desired end-effector pose for each region.
double getMetricValue(const robot_state::RobotState &state, const kinematic_constraints::ConstraintEvaluationResult &result) const
Get the value of the metrics assigned to this query for a given state and result.
void setScene(const ScenePtr &scene, bool verbose=false)
Set a scene to use for collision checking for this IK request.
std::vector< Eigen::Quaterniond > orientations
Target orientations.
SceneConstPtr scene
If provided, use this scene for collision checking.
void addMetric(const Metric &metric_function)
Add a metric to this IK query.
std::size_t attempts
IK attempts (samples within regions).
void addDistanceMetric(double weight=1.)
Add a metric to the query to evaluate distance to constraint.
std::vector< std::string > tips
List of end-effectors.
IKQuery(const std::string &group)
Constructor. Empty for fine control.
std::vector< GeometryConstPtr > regions
Regions to sample target positions from.
void getMessage(const std::string &base_frame, moveit_msgs::Constraints &msg) const
Get this IK query as a constraint message.