Main MRPT website > C++ reference
MRPT logo
CParameterizedTrajectoryGenerator.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef CParameterizedTrajectoryGenerator_H
00029 #define CParameterizedTrajectoryGenerator_H
00030 
00031 #include <mrpt/utils/CDynamicGrid.h>
00032 #include <mrpt/utils/CStream.h>
00033 #include <mrpt/utils/TParameters.h>
00034 #include <mrpt/reactivenav/link_pragmas.h>
00035 
00036 namespace mrpt
00037 {
00038   namespace reactivenav
00039   {
00040           using namespace mrpt::utils;
00041 
00042         /** This is the base class for any user defined PTG.
00043          *   The class factory interface in CParameterizedTrajectoryGenerator::CreatePTG.
00044          *
00045          * Changes history:
00046          *              - 30/JUN/2004: Creation (JLBC)
00047          *              - 16/SEP/2004: Totally redesigned.
00048          *              - 15/SEP/2005: Totally rewritten again, for integration into MRPT Applications Repository.
00049          *              - 19/JUL/2009: Simplified to use only STL data types, and created the class factory interface.
00050          *  \ingroup mrpt_reactivenav_grp
00051          */
00052         class REACTIVENAV_IMPEXP  CParameterizedTrajectoryGenerator
00053         {
00054         public:
00055                 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00056         protected:
00057         /** Constructor: possible values in "params":
00058                  *   - ref_distance: The maximum distance in PTGs
00059                  *   - resolution: The cell size
00060                  *   - v_max, w_max: Maximum robot speeds.
00061                  *   - system_TAU, system_DELAY (Optional): Robot dynamics
00062                  */
00063         CParameterizedTrajectoryGenerator(const TParameters<double> &params);
00064 
00065                 /** Initialized the collision grid with the given size and resolution. */
00066         void initializeCollisionsGrid(float refDistance,float resolution);
00067 
00068         public:
00069                 /** The class factory for creating a PTG from a list of parameters "params".
00070                   *  Possible values in "params" are:
00071                   *       - "PTG_type": It's an integer number such as "1" -> CPTG1, "2"-> CPTG2, etc...
00072                   *       - Those explained in CParameterizedTrajectoryGenerator::CParameterizedTrajectoryGenerator
00073                   *       - Those explained in the specific PTG being created (e.g. CPTG1, CPTG2, etc...)
00074                   *
00075                   * \exception std::logic_error On invalid or missing parameters.
00076                   */
00077                 static CParameterizedTrajectoryGenerator * CreatePTG(const TParameters<double> &params);
00078 
00079                 /** Gets a short textual description of the PTG and its parameters.
00080                   */
00081                 virtual std::string getDescription() const = 0 ;
00082 
00083         /** Destructor */
00084         virtual ~CParameterizedTrajectoryGenerator() {  }
00085 
00086         /** The main method: solves the diferential equation to generate a family of parametrical trajectories.
00087                  */
00088         void simulateTrajectories(
00089                                 uint16_t        alfaValuesCount,
00090                                 float                   max_time,
00091                                 float                   max_dist,
00092                                 unsigned int    max_n,
00093                                 float                   diferencial_t,
00094                                 float                   min_dist,
00095                                 float                   *out_max_acc_v = NULL,
00096                                 float                   *out_max_acc_w = NULL);
00097 
00098         /** The "lambda" function, see paper for info. It takes the (a,d) pair that is closest to a given location.
00099                  */
00100         virtual void lambdaFunction( float x, float y, int &out_k, float &out_d );
00101 
00102         /** Converts an "alfa" value (into the discrete set) into a feasible motion command.
00103                  */
00104         void directionToMotionCommand( uint16_t k, float &out_v, float &out_w );
00105 
00106         size_t getAlfaValuesCount() { return alfaValuesCount; };
00107         size_t getPointsCountInCPath_k(uint16_t k) { return CPoints[k].size(); };
00108 
00109         void   getCPointWhen_d_Is ( float d, uint16_t k, float &x, float &y, float &phi, float &t, float *v = NULL, float *w = NULL );
00110 
00111         float  GetCPathPoint_x( uint16_t k, int n ){ return CPoints[k][n].x; }
00112         float  GetCPathPoint_y( uint16_t k, int n ){ return CPoints[k][n].y; }
00113         float  GetCPathPoint_phi(uint16_t k, int n ){ return CPoints[k][n].phi; }
00114         float  GetCPathPoint_t( uint16_t k, int n ){ return CPoints[k][n].t; }
00115         float  GetCPathPoint_d( uint16_t k, int n ){ return CPoints[k][n].dist; }
00116         float  GetCPathPoint_v( uint16_t k, int n ){ return CPoints[k][n].v; }
00117         float  GetCPathPoint_w( uint16_t k, int n ){ return CPoints[k][n].w; }
00118 
00119         void    allocMemForVerticesData( int nVertices );
00120 
00121         void    setVertex_xy( uint16_t k, int n, int m, float x, float y )
00122         {
00123                         vertexPoints_x[k][ n*nVertices + m ] = x;
00124             vertexPoints_y[k][ n*nVertices + m ] = y;
00125         }
00126 
00127         float  getVertex_x( uint16_t k, int n, int m )
00128         {
00129                 int idx = n*nVertices + m;
00130 //                assert( idx>=0);assert(idx<nVertices * nPointsInEachPath[k] );
00131                                 return vertexPoints_x[k][idx];
00132         }
00133 
00134         float  getVertex_y( uint16_t k, int n, int m )
00135         {
00136                 int idx = n*nVertices + m;
00137 //                assert( idx>=0);assert(idx<nVertices * nPointsInEachPath[k] );
00138                                 return vertexPoints_y[k][idx];
00139         }
00140 
00141                 float*  getVertixesArray_x( uint16_t k, int n )
00142                 {
00143                 int idx = n*nVertices;
00144                                 return &vertexPoints_x[k][idx];
00145                 }
00146 
00147                 float*  getVertixesArray_y( uint16_t k, int n )
00148                 {
00149                 int idx = n*nVertices;
00150                                 return &vertexPoints_y[k][idx];
00151                 }
00152 
00153                 unsigned int getVertixesCount() { return nVertices; }
00154 
00155         float   getMax_V() { return V_MAX; }
00156         float   getMax_W() { return W_MAX; }
00157         float   getMax_V_inTPSpace() { return maxV_inTPSpace; }
00158 
00159                 /** Alfa value for the discrete corresponding value.
00160                  * \sa alfa2index
00161                  */
00162                 float  index2alfa( uint16_t k )
00163                 {
00164                         return (float)(M_PI * (-1 + 2 * (k+0.5f) / ((float)alfaValuesCount) ));
00165                 }
00166 
00167                 /** Discrete index value for the corresponding alfa value.
00168                  * \sa index2alfa
00169                  */
00170                 uint16_t  alfa2index( float alfa )
00171                 {
00172                         if (alfa>M_PI)  alfa-=(float)M_2PI;
00173                         if (alfa<-M_PI) alfa+=(float)M_2PI;
00174                         return (uint16_t)(0.5f*(alfaValuesCount*(1+alfa/M_PI) - 1));
00175                 }
00176 
00177                 /** Dump PTG trajectories in files in directory "./PTGs/".
00178                  */
00179         void    debugDumpInFiles(int nPT);
00180 
00181                 /**  A list of all the pairs (alpha,distance) such as the robot collides at that cell.
00182                   *  - map key   (uint16_t) -> alpha value (k)
00183                   *      - map value (float)    -> the MINIMUM distance (d), in meters, associated with that "k".
00184                   */
00185                 typedef std::map<uint16_t,float> TCollisionCell;
00186 
00187                 /** An internal class for storing the collision grid  */
00188                 class REACTIVENAV_IMPEXP CColisionGrid : public mrpt::utils::CDynamicGrid<TCollisionCell>
00189         {
00190                 private:
00191                                 CParameterizedTrajectoryGenerator const * m_parent;
00192 
00193                 public:
00194                                 CColisionGrid(float x_min, float x_max,float y_min, float y_max, float resolution, CParameterizedTrajectoryGenerator* parent )
00195                                   : mrpt::utils::CDynamicGrid<TCollisionCell>(x_min,x_max,y_min,y_max,resolution),
00196                                     m_parent(parent)
00197                                 {
00198                                 }
00199                                 virtual ~CColisionGrid() { }
00200 
00201                                 bool    saveToFile( mrpt::utils::CStream* fil );        //!< Save to file, true = OK
00202                 bool    loadFromFile( mrpt::utils::CStream* fil );      //!< Load from file,  true = OK
00203 
00204                 /** For an obstacle (x,y), returns a vector with all the pairs (a,d) such as the robot collides.
00205                                   */
00206                                 const TCollisionCell & getTPObstacle( const float obsX, const float obsY) const;
00207 
00208                 /** 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:
00209                                   *     \param cellInfo The index of the cell
00210                                   * \param k The path index (alfa discreet value)
00211                                   * \param d The distance (in TP-Space, range 0..1) to collision.
00212                                   */
00213                 void updateCellInfo( const unsigned int icx, const unsigned int icy, const uint16_t k, const float dist );
00214         };
00215 
00216                 /** The collision grid */
00217                 CColisionGrid   m_collisionGrid;
00218 
00219         // Save/Load from files.
00220                 bool    SaveColGridsToFile( const std::string &filename );      // true = OK
00221         bool    LoadColGridsFromFile( const std::string &filename ); // true = OK
00222 
00223 
00224                 float   refDistance;
00225 
00226                 /** The main method to be implemented in derived classes.
00227                  */
00228         virtual void PTG_Generator( float alfa, float t, float x, float y, float phi, float &v, float &w) = 0;
00229 
00230                 /** To be implemented in derived classes:
00231                   */
00232                 virtual bool PTG_IsIntoDomain( float x, float y ) = 0;
00233 
00234 protected:
00235                 // Given a-priori:
00236         float                   V_MAX, W_MAX;
00237                 float                   TAU, DELAY;
00238 
00239                 float                   turningRadiusReference;
00240 
00241                 /** Specifies the min/max values for "k" and "n", respectively.
00242                   * \sa m_lambdaFunctionOptimizer
00243                   */
00244                 struct TCellForLambdaFunction
00245                 {
00246                         TCellForLambdaFunction()
00247                         {
00248                                 k_min=k_max=n_min=n_max=-1;
00249                         }
00250 
00251                         int             k_min,k_max,n_min,n_max;
00252                 };
00253 
00254                 /** This grid will contain indexes data for speeding-up the default, brute-force lambda function.
00255                   */
00256                 CDynamicGrid<TCellForLambdaFunction>    m_lambdaFunctionOptimizer;
00257 
00258                 // Computed from simulations while generating trajectories:
00259                 float   maxV_inTPSpace;
00260 
00261         bool    flag1, flag2;
00262 
00263                 /** The number of discrete values for ALFA between -PI and +PI.
00264                   */
00265         unsigned int     alfaValuesCount;
00266 
00267                 /** The trajectories in the C-Space:
00268                   */
00269         struct TCPoint
00270         {
00271                         TCPoint(        float   x,
00272                                                 float   y,
00273                                                 float   phi,
00274                                                 float   t,
00275                                                 float   dist,
00276                                                 float   v,
00277                                                 float   w)
00278                         {
00279                                 this->x = x;
00280                                 this->y = y;
00281                                 this->phi = phi;
00282                                 this->t = t;
00283                                 this->dist = dist;
00284                                 this->v = v;
00285                                 this->w = w;
00286                         };
00287 
00288                         float  x, y, phi,t, dist,v,w;
00289         };
00290                 typedef std::vector<TCPoint> TCPointVector;
00291                 std::vector<TCPointVector>      CPoints;
00292 
00293                 /** The shape of the robot along the trajectories:
00294                   */
00295                 std::vector<vector_float> vertexPoints_x,vertexPoints_y;
00296         int     nVertices;
00297 
00298                 /** Free all the memory buffers:
00299                   */
00300         void    FreeMemory();
00301 
00302         }; // end of class
00303 
00304         /** A type for lists of PTGs */
00305         typedef std::vector<mrpt::reactivenav::CParameterizedTrajectoryGenerator*>  TListPTGs;
00306 
00307   }
00308 }
00309 
00310 
00311 #endif
00312 



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011