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



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014