Robowflex  v0.1
Making MoveIt Easy
trajectory.h
Go to the documentation of this file.
1 /* Author: Constantinos Chamzas, Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_TRAJECTORY_
4 #define ROBOWFLEX_TRAJECTORY_
5 
6 #include <tuple>
7 #include <functional>
8 
9 #include <moveit/robot_state/robot_state.h>
10 #include <moveit/robot_trajectory/robot_trajectory.h>
11 #include <moveit_msgs/RobotTrajectory.h>
12 
14 
15 namespace robowflex
16 {
17  /** \cond IGNORE */
20  /** \endcond */
21 
22  /** \brief A metric over robot states.
23  */
24  using PathMetric =
25  std::function<double(const robot_state::RobotState &, const robot_state::RobotState &)>;
26 
27  /** \cond IGNORE */
29  /** \endcond */
30 
31  /** \class robowflex::TrajectoryPtr
32  \brief A shared pointer wrapper for robowflex::Trajectory. */
33 
34  /** \class robowflex::TrajectoryConstPtr
35  \brief A const shared pointer wrapper for robowflex::Trajectory. */
36 
37  /** \brief The Trajectory class is a wrapper around _MoveIt!_'s robot_trajectory::RobotTrajectory,
38  * with extra convenience functions such interpolation and collision checking.
39  * There are also utilities to load and save trajectories from YAML files (toYAMLFile() and
40  * fromYAMLFile()).
41  */
42  class Trajectory
43  {
44  public:
45  /** \brief Constructor for an empty trajectory.
46  * \param[in] robot Robot to construct trajectory for.
47  * \param[in] group Planning group of the trajectory.
48  */
49  Trajectory(const RobotConstPtr &robot, const std::string &group);
50 
51  /** \brief Constructor from moveit trajectory.
52  * \param[in] trajectory Trajectory to initialize with.
53  */
55 
56  /** \brief Constructor from moveit trajectory.
57  * \param[in] trajectory Trajectory to initialize with.
58  */
59  Trajectory(const robot_trajectory::RobotTrajectoryPtr trajectory);
60 
61  /** \name IO
62  \{ */
63 
64  /** \brief Set the trajectory to be the same as a message.
65  * \param[in] reference_state A full state that contains the values for all the joints
66  * \param[in] msg Message used to set the trajectory
67  */
68  void useMessage(const robot_state::RobotState &reference_state,
69  const moveit_msgs::RobotTrajectory &msg);
70 
71  /** \brief Set the trajectory to be the same as a message.
72  * \param[in] reference_state A full state that contains the values for all the joints.
73  * \param[in] msg Message used to set the trajectory
74  */
75  void useMessage(const robot_state::RobotState &reference_state,
76  const trajectory_msgs::JointTrajectory &msg);
77 
78  /** \brief Dump a trajectory to a file.
79  * \param[in] filename Trajectory filename.
80  * \return True on success.
81  */
82  bool toYAMLFile(const std::string &filename) const;
83 
84  /** \brief Load a trajectory from a YAML file.
85  * \param[in] reference_state A full state that contains the values for all the joints.
86  * \param[in] filename Trajectory filename.
87  * \return True on success.
88  */
89  bool fromYAMLFile(const robot_state::RobotState &reference_state, const std::string &filename);
90 
91  /** \} */
92 
93  /** \name Getters and Setters
94  \{ */
95 
96  /** \brief Get a const reference to the trajectory.
97  * \return The trajectory.
98  */
99  const robot_trajectory::RobotTrajectoryPtr &getTrajectoryConst() const;
100 
101  /** \brief Get a reference to the trajectory.
102  * \return The trajectory.
103  */
104  robot_trajectory::RobotTrajectoryPtr &getTrajectory();
105 
106  /** \brief Get the message that describes the trajectory.
107  * \return The trajectory message.
108  */
109  moveit_msgs::RobotTrajectory getMessage() const;
110 
111  /** \brief Returns the number of waypoints of the trajectory.
112  * \return The numbers of waypoints of the trajectory.
113  */
115 
116  /** \} */
117 
118  /** \name Adding and Modifying States
119  \{ */
120 
121  /** \brief Add a waypoint at the end of the trajectory.
122  * \param[in] state State to add at end of trajectory.
123  * \param[in] dt Time to this waypoint from previous.
124  */
125  void addSuffixWaypoint(const robot_state::RobotState &state, double dt = 1.);
126 
127  /** \} */
128 
129  /** \name Processing Functions
130  \{ */
131 
132  /** \brief Computes the time parameterization of a path according to a desired max velocity or
133  * acceleration.
134  * \param[in] max_velocity Maximum path velocity.
135  * \param[in] max_acceleration Maximum path acceleration.
136  * \return True on success, false on failure.
137  */
138  bool computeTimeParameterization(double max_velocity = 1., double max_acceleration = 1.);
139 
140  /** \brief Computes the time parameterization of a path according to a desired max velocity or
141  * acceleration.
142  * \param[in] trajectory to compute time parameterization.
143  * \param[in] max_velocity Maximum path velocity.
144  * \param[in] max_acceleration Maximum path acceleration.
145  * \return True on success, false on failure.
146  */
148  double max_velocity = 1., double max_acceleration = 1.);
149 
150  /** \brief Insert a number of states in a path so that the path is made up of exactly count states.
151  * States are inserted uniformly (more states on longer segments). Changes are performed only if a
152  * path has less than count states.
153  * \param[in] count number of states to insert.
154  */
155  void interpolate(unsigned int count);
156 
157  /** \brief Converts a trajectory into a vector of position vectors. The values are in the same order
158  * as reported by getJointNames(), which is consistent within MoveIt.
159  * \return The trajectory in vector form.
160  */
162 
163  /** \brief Get the names of the variables that make up this trajectory, in the same order as in
164  * MoveIt JointModelGroup.
165  * \return A vector of joint names in order.
166  */
168 
169  /** \brief Adds a specified part of a trajectory to the end of the current trajectory. The default,
170  * when \a start_index or \a end_index are ommitted, is to add the whole trajectory.
171  * \param[in] source The trajectory containing the part to append to the end of current trajectory.
172  * \param[in] dt Time step between last point in current traj and first point of append traj.
173  * \param[in] start_index Index of first traj point of the part to append from the source traj.
174  * \param[in] end_index Index of last traj point of the part to append from the source traj.
175  */
176  Trajectory &append(const Trajectory &source, double dt, size_t start_index = 0,
177  size_t end_index = std::numeric_limits<std::size_t>::max());
178 
179  /** \} */
180 
181  /** \name Metrics
182  \{ */
183 
184  /** \brief Get the length of a path.
185  * Optionally, a metric can be specified other than the default (the L2 norm).
186  * \param[in] metric An optional metric to use to compute the length of the path.
187  * \return Length of the path according to the metric.
188  */
189  double getLength(const PathMetric &metric = {}) const;
190 
191  /** \brief Checks if a path is collsion free.
192  * \param[in] scene Scene to collision check the path with.
193  * \return True if the path is collision free in the scene.
194  */
195  bool isCollisionFree(const SceneConstPtr &scene) const;
196 
197  /** \brief Get the average, minimum, and maximum clearance of a path.
198  * \param[in] scene Scene to compute clearance to.
199  * \return In order, the average, minimum, and maximum clearance of a path to a scene.
200  */
202 
203  /** \brief Get the smoothness of a path relative to some metric.
204  * See internal function documentation for details.
205  * \param[in] metric An optional metric to use to compute the length of the path segments.
206  * \return Smoothness of the path.
207  */
208  double getSmoothness(const PathMetric &metric = {}) const;
209 
210  /** \brief Returns the joint positions from the last state in a planned trajectory in \a response.
211  * \return A map of joint name to joint position of the last state in \a response.
212  */
214 
215  /** \} */
216 
217  protected:
218  robot_trajectory::RobotTrajectoryPtr trajectory_;
219  };
220 } // namespace robowflex
221 
222 #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::Robot.
A const shared pointer wrapper for robowflex::Scene.
The Trajectory class is a wrapper around MoveIt!'s robot_trajectory::RobotTrajectory,...
Definition: trajectory.h:43
Trajectory(const RobotConstPtr &robot, const std::string &group)
Constructor for an empty trajectory.
Definition: trajectory.cpp:18
void interpolate(unsigned int count)
Insert a number of states in a path so that the path is made up of exactly count states....
Definition: trajectory.cpp:104
bool computeTimeParameterization(double max_velocity=1., double max_acceleration=1.)
Computes the time parameterization of a path according to a desired max velocity or acceleration.
Definition: trajectory.cpp:91
Trajectory & append(const Trajectory &source, double dt, size_t start_index=0, size_t end_index=std::numeric_limits< std::size_t >::max())
Adds a specified part of a trajectory to the end of the current trajectory. The default,...
Definition: trajectory.cpp:172
std::map< std::string, double > getFinalPositions() const
Returns the joint positions from the last state in a planned trajectory in response.
Definition: trajectory.cpp:284
robot_trajectory::RobotTrajectoryPtr trajectory_
Definition: trajectory.h:218
std::vector< std::vector< double > > vectorize() const
Converts a trajectory into a vector of position vectors. The values are in the same order as reported...
Definition: trajectory.cpp:157
bool fromYAMLFile(const robot_state::RobotState &reference_state, const std::string &filename)
Load a trajectory from a YAML file.
Definition: trajectory.cpp:54
const robot_trajectory::RobotTrajectoryPtr & getTrajectoryConst() const
Get a const reference to the trajectory.
Definition: trajectory.cpp:69
std::vector< std::string > getJointNames() const
Get the names of the variables that make up this trajectory, in the same order as in MoveIt JointMode...
Definition: trajectory.cpp:167
void addSuffixWaypoint(const robot_state::RobotState &state, double dt=1.)
Add a waypoint at the end of the trajectory.
Definition: trajectory.cpp:64
bool toYAMLFile(const std::string &filename) const
Dump a trajectory to a file.
Definition: trajectory.cpp:45
double getLength(const PathMetric &metric={}) const
Get the length of a path. Optionally, a metric can be specified other than the default (the L2 norm).
Definition: trajectory.cpp:178
std::tuple< double, double, double > getClearance(const SceneConstPtr &scene) const
Get the average, minimum, and maximum clearance of a path.
Definition: trajectory.cpp:214
bool isCollisionFree(const SceneConstPtr &scene) const
Checks if a path is collsion free.
Definition: trajectory.cpp:196
robot_trajectory::RobotTrajectoryPtr & getTrajectory()
Get a reference to the trajectory.
Definition: trajectory.cpp:74
double getSmoothness(const PathMetric &metric={}) const
Get the smoothness of a path relative to some metric. See internal function documentation for details...
Definition: trajectory.cpp:237
void useMessage(const robot_state::RobotState &reference_state, const moveit_msgs::RobotTrajectory &msg)
Set the trajectory to be the same as a message.
Definition: trajectory.cpp:33
moveit_msgs::RobotTrajectory getMessage() const
Get the message that describes the trajectory.
Definition: trajectory.cpp:79
std::size_t getNumWaypoints() const
Returns the number of waypoints of the trajectory.
Definition: trajectory.cpp:86
Functions for loading and animating robots in Blender.
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
Functions for loading and animating scenes in Blender.
robot_trajectory::RobotTrajectory trajectory(rmodel,"right_arm")