Main MRPT website > C++ reference
MRPT logo
CParameterizedTrajectoryGenerator.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2015, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef CParameterizedTrajectoryGenerator_H
10 #define CParameterizedTrajectoryGenerator_H
11 
13 #include <mrpt/math/CPolygon.h>
14 #include <mrpt/utils/CStream.h>
15 #include <mrpt/utils/TParameters.h>
16 #include <mrpt/nav/link_pragmas.h>
17 #include <mrpt/utils/mrpt_stdint.h> // compiler-independent version of "stdint.h"
18 #include <limits> // numeric_limits
19 
20 namespace mrpt
21 {
22  namespace opengl { class CSetOfLines; }
23 
24  namespace nav
25  {
26  /** This is the base class for any user-defined PTG.
27  * The class factory interface in CParameterizedTrajectoryGenerator::CreatePTG.
28  *
29  * Papers:
30  * - J.L. Blanco, J. Gonzalez-Jimenez, J.A. Fernandez-Madrigal, "Extending Obstacle Avoidance Methods through Multiple Parameter-Space Transformations", Autonomous Robots, vol. 24, no. 1, 2008. http://ingmec.ual.es/~jlblanco/papers/blanco2008eoa_DRAFT.pdf
31  *
32  * Changes history:
33  * - 30/JUN/2004: Creation (JLBC)
34  * - 16/SEP/2004: Totally redesigned.
35  * - 15/SEP/2005: Totally rewritten again, for integration into MRPT Applications Repository.
36  * - 19/JUL/2009: Simplified to use only STL data types, and created the class factory interface.
37  * \ingroup mrpt_nav_grp
38  */
40  {
41  public:
43  protected:
44  /** Constructor: possible values in "params":
45  * - ref_distance: The maximum distance in PTGs
46  * - resolution: The cell size
47  * - v_max, w_max: Maximum robot speeds.
48  *
49  * See docs of derived classes for additional parameters:
50  */
52 
53  /** Initialized the collision grid with the given size and resolution. */
54  void initializeCollisionsGrid(float refDistance,float resolution);
55 
56  public:
57  /** The class factory for creating a PTG from a list of parameters "params".
58  * Possible values in "params" are:
59  * - "PTG_type": It's an integer number such as "1" -> CPTG1, "2"-> CPTG2, etc...
60  * - Those explained in CParameterizedTrajectoryGenerator::CParameterizedTrajectoryGenerator
61  * - Those explained in the specific PTG being created (e.g. CPTG1, CPTG2, etc...)
62  *
63  * \exception std::logic_error On invalid or missing parameters.
64  */
66 
67  /** Gets a short textual description of the PTG and its parameters.
68  */
69  virtual std::string getDescription() const = 0 ;
70 
71  /** Destructor */
73 
74  /** The main method: solves the diferential equation to generate a family of parametrical trajectories.
75  */
76  void simulateTrajectories(
77  uint16_t alphaValuesCount,
78  float max_time,
79  float max_dist,
80  unsigned int max_n,
81  float diferencial_t,
82  float min_dist,
83  float *out_max_acc_v = NULL,
84  float *out_max_acc_w = NULL);
85 
86 
87  /** Computes the closest (alpha,d) TP coordinates of the trajectory point closest to the Workspace (WS) Cartesian coordinates (x,y).
88  * \param[in] x X coordinate of the query point.
89  * \param[in] y Y coordinate of the query point.
90  * \param[out] out_k Trajectory parameter index (discretized alpha value, 0-based index).
91  * \param[out] out_d Trajectory distance, normalized such that D_max becomes 1.
92  *
93  * \return true if the distance between (x,y) and the actual trajectory point is below the given tolerance (in meters).
94  * \note The default implementation in CParameterizedTrajectoryGenerator relies on a look-up-table. Derived classes may redefine this to closed-form expressions, when they exist.
95  */
96  virtual bool inverseMap_WS2TP(float x, float y, int &out_k, float &out_d, float tolerance_dist = 0.10f) const;
97 
98  /** The "lambda" function, see paper for info. It takes the (a,d) pair that is closest to a given location. */
100  "Use inverseMap_WS2TP() instead", \
101  void lambdaFunction( float x, float y, int &out_k, float &out_d ); \
102  )
103 
104  /** Converts an "alpha" value (into the discrete set) into a feasible motion command.
105  */
106  void directionToMotionCommand( uint16_t k, float &out_v, float &out_w );
107 
108  uint16_t getAlfaValuesCount() const { return m_alphaValuesCount; };
109  size_t getPointsCountInCPath_k(uint16_t k) const { return CPoints[k].size(); };
110 
111  void getCPointWhen_d_Is ( float d, uint16_t k, float &x, float &y, float &phi, float &t, float *v = NULL, float *w = NULL );
112 
113  float GetCPathPoint_x( uint16_t k, int n ) const { return CPoints[k][n].x; }
114  float GetCPathPoint_y( uint16_t k, int n ) const { return CPoints[k][n].y; }
115  float GetCPathPoint_phi(uint16_t k, int n ) const { return CPoints[k][n].phi; }
116  float GetCPathPoint_t( uint16_t k, int n ) const { return CPoints[k][n].t; }
117  float GetCPathPoint_d( uint16_t k, int n ) const { return CPoints[k][n].dist; }
118  float GetCPathPoint_v( uint16_t k, int n ) const { return CPoints[k][n].v; }
119  float GetCPathPoint_w( uint16_t k, int n ) const { return CPoints[k][n].w; }
120 
121  float getMax_V() const { return V_MAX; }
122  float getMax_W() const { return W_MAX; }
123  float getMax_V_inTPSpace() const { return maxV_inTPSpace; }
124 
125  /** Alfa value for the discrete corresponding value.
126  * \sa alpha2index
127  */
128  float index2alpha( uint16_t k ) const
129  {
130  return (float)(M_PI * (-1 + 2 * (k+0.5f) / ((float)m_alphaValuesCount) ));
131  }
132 
133  /** Discrete index value for the corresponding alpha value.
134  * \sa index2alpha
135  */
136  uint16_t alpha2index( float alpha ) const
137  {
138  if (alpha>M_PI) alpha-=(float)M_2PI;
139  if (alpha<-M_PI) alpha+=(float)M_2PI;
140  return (uint16_t)(0.5f*(m_alphaValuesCount*(1+alpha/M_PI) - 1));
141  }
142 
143  /** Dump PTG trajectories in a binary file "./reactivenav.logs/PTGs/PTG%i.dat", with "%i" being the user-supplied parameter "nPT",
144  * and in FIVE text files: "./reactivenav.logs/PTGs/PTG%i_{x,y,phi,t,d}.txt".
145  *
146  * Text files are loadable from MATLAB/Octave, and can be visualized with the script [MRPT_DIR]/scripts/viewPTG.m ,
147  * also online: http://mrpt.googlecode.com/svn/trunk/scripts/viewPTG.m
148  *
149  * \note The directory "./reactivenav.logs/PTGs" will be created if doesn't exist.
150  * \return false on any error writing to disk.
151  */
152  bool debugDumpInFiles(const int nPT);
153 
154  /** Returns the representation of one trajectory of this PTG as a 3D OpenGL object (a simple curved line).
155  * \param[in] k The 0-based index of the selected trajectory (discrete "alpha" parameter).
156  * \param[out] gl_obj Output object.
157  * \param[in] decimate_distance Minimum distance between path points (in meters).
158  * \param[in] max_path_distance If >0, cut the path at this distance (in meters).
159  */
160  void renderPathAsSimpleLine(
161  const uint16_t k,
163  const float decimate_distance = 0.1f,
164  const float max_path_distance = 0.0f) const;
165 
166 
167  /** A list of all the pairs (alpha,distance) such as the robot collides at that cell.
168  * - map key (uint16_t) -> alpha value (k)
169  * - map value (float) -> the MINIMUM distance (d), in meters, associated with that "k".
170  */
171  //typedef std::map<uint16_t,float> TCollisionCell;
172  typedef std::vector<std::pair<uint16_t,float> > TCollisionCell;
173 
174  /** An internal class for storing the collision grid */
175  class NAV_IMPEXP CColisionGrid : public mrpt::utils::CDynamicGrid<TCollisionCell>
176  {
177  private:
179 
180  public:
181  CColisionGrid(float x_min, float x_max,float y_min, float y_max, float resolution, CParameterizedTrajectoryGenerator* parent )
182  : mrpt::utils::CDynamicGrid<TCollisionCell>(x_min,x_max,y_min,y_max,resolution),
183  m_parent(parent)
184  {
185  }
186  virtual ~CColisionGrid() { }
187 
188  bool saveToFile( mrpt::utils::CStream* fil, const mrpt::math::CPolygon & computed_robotShape ); //!< Save to file, true = OK
189  bool loadFromFile( mrpt::utils::CStream* fil, const mrpt::math::CPolygon & current_robotShape ); //!< Load from file, true = OK
190 
191  /** For an obstacle (x,y), returns a vector with all the pairs (a,d) such as the robot collides.
192  */
193  const TCollisionCell & getTPObstacle( const float obsX, const float obsY) const;
194 
195  /** Updates the info into a cell: It updates the cell only if the distance d for the path k is lower than the previous value:
196  * \param cellInfo The index of the cell
197  * \param k The path index (alpha discreet value)
198  * \param d The distance (in TP-Space, range 0..1) to collision.
199  */
200  void updateCellInfo( const unsigned int icx, const unsigned int icy, const uint16_t k, const float dist );
201 
202  }; // end of class CColisionGrid
203 
204  /** The collision grid */
206 
207  // Save/Load from files.
208  bool SaveColGridsToFile( const std::string &filename, const mrpt::math::CPolygon & computed_robotShape ); // true = OK
209  bool LoadColGridsFromFile( const std::string &filename, const mrpt::math::CPolygon & current_robotShape ); // true = OK
210 
211 
212  float refDistance;
213 
214  /** The main method to be implemented in derived classes.
215  */
216  virtual void PTG_Generator( float alpha, float t, float x, float y, float phi, float &v, float &w) = 0;
217 
218  /** To be implemented in derived classes:
219  */
220  virtual bool PTG_IsIntoDomain( float x, float y ) = 0;
221 
222 protected:
223  float V_MAX, W_MAX;
225 
226  /** Specifies the min/max values for "k" and "n", respectively.
227  * \sa m_lambdaFunctionOptimizer
228  */
230  {
232  k_min( std::numeric_limits<uint16_t>::max() ),
233  k_max( std::numeric_limits<uint16_t>::min() ),
234  n_min( std::numeric_limits<uint32_t>::max() ),
235  n_max( std::numeric_limits<uint32_t>::min() )
236  {}
237 
238  uint16_t k_min,k_max;
239  uint32_t n_min,n_max;
240 
241  bool isEmpty() const { return k_min==std::numeric_limits<uint16_t>::max(); }
242  };
243 
244  /** This grid will contain indexes data for speeding-up the default, brute-force lambda function.
245  */
247 
248  // Computed from simulations while generating trajectories:
250 
251  /** The number of discrete values for "alpha" between -PI and +PI.
252  */
254 
255  /** The trajectories in the C-Space:
256  */
257  struct TCPoint
258  {
259  TCPoint(const float x_,const float y_,const float phi_,
260  const float t_,const float dist_,
261  const float v_,const float w_) :
262  x(x_), y(y_), phi(phi_), t(t_), dist(dist_), v(v_), w(w_)
263  {}
264 
265  float x, y, phi,t, dist,v,w;
266  };
267  typedef std::vector<TCPoint> TCPointVector;
268  std::vector<TCPointVector> CPoints;
269 
270  /** Free all the memory buffers */
271  void FreeMemory();
272 
273  }; // end of class
274 
275  /** A type for lists of PTGs */
276  typedef std::vector<mrpt::nav::CParameterizedTrajectoryGenerator*> TListPTGs;
277 
278  }
279 }
280 
281 
282 #endif
283 
std::vector< std::pair< uint16_t, float > > TCollisionCell
A list of all the pairs (alpha,distance) such as the robot collides at that cell. ...
mrpt::utils::CDynamicGrid< TCellForLambdaFunction > m_lambdaFunctionOptimizer
This grid will contain indexes data for speeding-up the default, brute-force lambda function...
For usage when passing a dynamic number of (numeric) arguments to a function, by name.
Definition: TParameters.h:46
#define MRPT_MAKE_ALIGNED_OPERATOR_NEW
Definition: memory.h:112
EIGEN_STRONG_INLINE const AdjointReturnType t() const
Transpose.
float index2alpha(uint16_t k) const
Alfa value for the discrete corresponding value.
A wrapper of a TPolygon2D class, implementing CSerializable.
Definition: CPolygon.h:25
STL namespace.
#define M_PI
Definition: bits.h:65
#define M_2PI
Definition: mrpt_macros.h:362
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
This is the base class for any user-defined PTG.
A 2D grid of dynamic size which stores any kind of data at each cell.
Definition: CDynamicGrid.h:39
#define MRPT_DECLARE_DEPRECATED_FUNCTION(__MSG, __FUNC)
Usage: MRPT_DECLARE_DEPRECATED_FUNCTION("Use XX instead", void myFunc(double));.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
uint16_t alpha2index(float alpha) const
Discrete index value for the corresponding alpha value.
CColisionGrid(float x_min, float x_max, float y_min, float y_max, float resolution, CParameterizedTrajectoryGenerator *parent)
Specifies the min/max values for "k" and "n", respectively.
A set of independent lines (or segments), one line with its own start and end positions (X...
Definition: CSetOfLines.h:34
std::vector< mrpt::nav::CParameterizedTrajectoryGenerator * > TListPTGs
A type for lists of PTGs.
uint16_t m_alphaValuesCount
The number of discrete values for "alpha" between -PI and +PI.
TCPoint(const float x_, const float y_, const float phi_, const float t_, const float dist_, const float v_, const float w_)



Page generated by Doxygen 1.8.9.1 for MRPT 1.3.0 SVN: at Sun Sep 13 03:55:12 UTC 2015