Main MRPT website > C++ reference for MRPT 1.9.9
PlannerRRT_common.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, 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 
10 #include "nav-precomp.h" // Precomp header
11 
14 #include <mrpt/math/CPolygon.h>
15 
16 using namespace mrpt::nav;
17 using namespace mrpt::utils;
18 using namespace mrpt::math;
19 using namespace mrpt::poses;
20 using namespace std;
21 
23  : robot_shape_circular_radius(0.30),
24  ptg_cache_files_directory("."),
25  goalBias(0.05),
26  maxLength(1.0),
27  minDistanceBetweenNewNodes(0.10),
28  minAngBetweenNewNodes(mrpt::utils::DEG2RAD(15)),
29  ptg_verbose(true),
30  save_3d_log_freq(0)
31 {
32  robot_shape.push_back(mrpt::math::TPoint2D(-0.5, -0.5));
33  robot_shape.push_back(mrpt::math::TPoint2D(0.8, -0.4));
34  robot_shape.push_back(mrpt::math::TPoint2D(0.8, 0.4));
35  robot_shape.push_back(mrpt::math::TPoint2D(-0.5, 0.5));
36 }
37 
38 PlannerTPS_VirtualBase::PlannerTPS_VirtualBase() : m_initialized_PTG(false) {}
40 {
41  ASSERTMSG_(
42  !m_PTGs.empty(),
43  "No PTG was defined! At least one must be especified.");
44 
45  // Convert to CPolygon for API requisites:
46  mrpt::math::CPolygon poly_robot_shape;
47  poly_robot_shape.clear();
48  if (!params.robot_shape.empty())
49  {
50  vector<double> xm, ym;
51  params.robot_shape.getPlotData(xm, ym);
52  poly_robot_shape.setAllVertices(xm, ym);
53  }
54 
55  for (size_t i = 0; i < m_PTGs.size(); i++)
56  {
57  mrpt::utils::CTimeLoggerEntry tle(m_timelogger, "PTG_initialization");
58 
59  // Polygonal robot shape?
60  {
63  m_PTGs[i].get());
64  if (diff_ptg)
65  {
66  ASSERTMSG_(
67  !poly_robot_shape.empty(),
68  "No polygonal robot shape specified, and PTG requires "
69  "one!");
70  diff_ptg->setRobotShape(poly_robot_shape);
71  }
72  }
73  // Circular robot shape?
74  {
77  m_PTGs[i].get());
78  if (ptg)
79  {
80  ASSERTMSG_(
81  params.robot_shape_circular_radius > 0,
82  "No circular robot shape specified, and PTG requires one!");
83  ptg->setRobotShapeRadius(params.robot_shape_circular_radius);
84  }
85  }
86 
87  m_PTGs[i]->initialize(
89  "%s/TPRRT_PTG_%03u.dat.gz",
90  params.ptg_cache_files_directory.c_str(),
91  static_cast<unsigned int>(i)),
92  params.ptg_verbose);
93  }
94 
95  m_initialized_PTG = true;
96 }
97 
99  const mrpt::utils::CConfigFileBase& ini, const std::string& sSect)
100 {
101  // Robot shape:
102  // ==========================
103  // polygonal shape
104  {
105  // Robot shape is a bit special to load:
106  params.robot_shape.clear();
107  const std::string sShape = ini.read_string(sSect, "robot_shape", "");
108  if (!sShape.empty())
109  {
110  CMatrixDouble mShape;
111  if (!mShape.fromMatlabStringFormat(sShape))
113  "Error parsing robot_shape matrix: '%s'", sShape.c_str());
114  ASSERT_(size(mShape, 1) == 2);
115  ASSERT_(size(mShape, 2) >= 3);
116 
117  for (size_t i = 0; i < size(mShape, 2); i++)
118  params.robot_shape.push_back(
119  TPoint2D(mShape(0, i), mShape(1, i)));
120  }
121  }
122  // circular shape
123  params.robot_shape_circular_radius =
124  ini.read_double(sSect, "robot_shape_circular_radius", 0.0);
125 
126  // Load PTG tables:
127  // ==========================
128  m_PTGs.clear();
129 
130  const size_t PTG_COUNT =
131  ini.read_int(sSect, "PTG_COUNT", 0, true); // load the number of PTGs
132  for (unsigned int n = 0; n < PTG_COUNT; n++)
133  {
134  // Generate it:
135  const std::string sPTGName =
136  ini.read_string(sSect, format("PTG%u_Type", n), "", true);
137  m_PTGs.push_back(
140  sPTGName, ini, sSect, format("PTG%u_", n))));
141  }
142 }
143 
144 // Auxiliary function:
146  const mrpt::maps::CPointsMap& in_map, mrpt::maps::CPointsMap& out_map,
147  const mrpt::poses::CPose2D& asSeenFrom, const double MAX_DIST_XY)
148 {
149  size_t nObs;
150  const float *obs_xs, *obs_ys, *obs_zs;
151  in_map.getPointsBuffer(nObs, obs_xs, obs_ys, obs_zs);
152 
153  out_map.clear();
154  out_map.reserve(nObs); // Prealloc mem for speed-up
155 
156  const CPose2D invPose = -asSeenFrom;
157  // We can safely discard the rest of obstacles, since they cannot be
158  // converted into TP-Obstacles anyway!
159 
160  for (size_t obs = 0; obs < nObs; obs++)
161  {
162  const double gx = obs_xs[obs], gy = obs_ys[obs];
163 
164  if (std::abs(gx - asSeenFrom.x()) > MAX_DIST_XY ||
165  std::abs(gy - asSeenFrom.y()) > MAX_DIST_XY)
166  continue; // ignore this obstacle: anyway, I don't know how to map
167  // it to TP-Obs!
168 
169  double ox, oy;
170  invPose.composePoint(gx, gy, ox, oy);
171 
172  out_map.insertPointFast(ox, oy, 0);
173  }
174 }
175 
176 /*---------------------------------------------------------------
177 SpaceTransformer
178 ---------------------------------------------------------------*/
180  const mrpt::maps::CSimplePointsMap& in_obstacles,
182  const double MAX_DIST, std::vector<double>& out_TPObstacles)
183 {
184  using namespace mrpt::nav;
185  try
186  {
187  // Take "k_rand"s and "distances" such that the collision hits the
188  // obstacles
189  // in the "grid" of the given PT
190  // --------------------------------------------------------------------
191  size_t nObs;
192  const float *obs_xs, *obs_ys, *obs_zs;
193  // = in_obstacles.getPointsCount();
194  in_obstacles.getPointsBuffer(nObs, obs_xs, obs_ys, obs_zs);
195 
196  // Init obs ranges:
197  in_PTG->initTPObstacles(out_TPObstacles);
198 
199  for (size_t obs = 0; obs < nObs; obs++)
200  {
201  const float ox = obs_xs[obs];
202  const float oy = obs_ys[obs];
203 
204  if (std::abs(ox) > MAX_DIST || std::abs(oy) > MAX_DIST)
205  continue; // ignore this obstacle: anyway, I don't know how to
206  // map it to TP-Obs!
207 
208  in_PTG->updateTPObstacle(ox, oy, out_TPObstacles);
209  }
210 
211  // Leave distances in out_TPObstacles un-normalized ([0,1]), so they
212  // just represent real distances in meters.
213  }
214  catch (std::exception& e)
215  {
216  cerr << "[PT_RRT::SpaceTransformer] Exception:" << endl;
217  cerr << e.what() << endl;
218  }
219  catch (...)
220  {
221  cerr << "\n[PT_RRT::SpaceTransformer] Unexpected exception!:\n";
222  cerr << format("*in_PTG = %p\n", (void*)in_PTG);
223  if (in_PTG)
224  cerr << format("PTG = %s\n", in_PTG->getDescription().c_str());
225  cerr << endl;
226  }
227 }
228 
230  const int tp_space_k_direction,
231  const mrpt::maps::CSimplePointsMap& in_obstacles,
233  const double MAX_DIST, double& out_TPObstacle_k)
234 {
235  using namespace mrpt::nav;
236  try
237  {
238  // Take "k_rand"s and "distances" such that the collision hits the
239  // obstacles
240  // in the "grid" of the given PT
241  // --------------------------------------------------------------------
242  size_t nObs;
243  const float *obs_xs, *obs_ys, *obs_zs;
244  // = in_obstacles.getPointsCount();
245  in_obstacles.getPointsBuffer(nObs, obs_xs, obs_ys, obs_zs);
246 
247  // Init obs ranges:
248  in_PTG->initTPObstacleSingle(tp_space_k_direction, out_TPObstacle_k);
249 
250  for (size_t obs = 0; obs < nObs; obs++)
251  {
252  const float ox = obs_xs[obs];
253  const float oy = obs_ys[obs];
254 
255  if (std::abs(ox) > MAX_DIST || std::abs(oy) > MAX_DIST)
256  continue; // ignore this obstacle: anyway, I don't know how to
257  // map it to TP-Obs!
258 
259  in_PTG->updateTPObstacleSingle(
260  ox, oy, tp_space_k_direction, out_TPObstacle_k);
261  }
262 
263  // Leave distances in out_TPObstacles un-normalized ([0,1]), so they
264  // just represent real distances in meters.
265  }
266  catch (std::exception& e)
267  {
268  cerr << "[PT_RRT::SpaceTransformer] Exception:" << endl;
269  cerr << e.what() << endl;
270  }
271  catch (...)
272  {
273  cerr << "\n[PT_RRT::SpaceTransformer] Unexpected exception!:\n";
274  cerr << format("*in_PTG = %p\n", (void*)in_PTG);
275  if (in_PTG)
276  cerr << format("PTG = %s\n", in_PTG->getDescription().c_str());
277  cerr << endl;
278  }
279 }
void clear()
Erase all the contents of the map.
Definition: CMetricMap.cpp:31
std::shared_ptr< CParameterizedTrajectoryGenerator > Ptr
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
virtual void updateTPObstacleSingle(double ox, double oy, uint16_t k, double &tp_obstacle_k) const =0
Like updateTPObstacle() but for one direction only (k) in TP-Space.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void getPlotData(std::vector< double > &x, std::vector< double > &y) const
Gets plot data, ready to use on a 2D plot.
Base class for all PTGs using a 2D circular robot shape model.
void internal_loadConfig_PTG(const mrpt::utils::CConfigFileBase &cfgSource, const std::string &sSectionName=std::string("PTG_CONFIG"))
Load all PTG params from a config file source.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLenum GLsizei n
Definition: glext.h:5074
void spaceTransformerOneDirectionOnly(const int tp_space_k_direction, const mrpt::maps::CSimplePointsMap &in_obstacles, const mrpt::nav::CParameterizedTrajectoryGenerator *in_PTG, const double MAX_DIST, double &out_TPObstacle_k)
void initTPObstacles(std::vector< double > &TP_Obstacles) const
Resizes and populates the initial appropriate contents in a vector of tp-obstacles (collision-free ra...
void spaceTransformer(const mrpt::maps::CSimplePointsMap &in_obstacles, const mrpt::nav::CParameterizedTrajectoryGenerator *in_PTG, const double MAX_DIST, std::vector< double > &out_TPObstacles)
A wrapper of a TPolygon2D class, implementing CSerializable.
Definition: CPolygon.h:22
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans. ...
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
STL namespace.
virtual void reserve(size_t newLength)=0
Reserves memory for a given number of points: the size of the map does not change, it only reserves the memory.
void composePoint(double lx, double ly, double &gx, double &gy) const
An alternative, slightly more efficient way of doing with G and L being 2D points and P this 2D pose...
Definition: CPose2D.cpp:188
This class allows loading and storing values and vectors of different types from a configuration text...
void initTPObstacleSingle(uint16_t k, double &TP_Obstacle_k) const
#define MAX_DIST(s)
Definition: deflate.h:285
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans or other sensors...
Definition: CPointsMap.h:63
This is the base class for any user-defined PTG.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
void setRobotShapeRadius(const double robot_radius)
Robot shape must be set before initialization, either from ctor params or via this method...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
#define DEG2RAD
GLsizei const GLchar ** string
Definition: glext.h:4101
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
void internal_initialize_PTG()
Must be called after setting all params (see internal_loadConfig_PTG()) and before calling solve() ...
void setRobotShape(const mrpt::math::CPolygon &robotShape)
Robot shape must be set before initialization, either from ctor params or via this method...
virtual void updateTPObstacle(double ox, double oy, std::vector< double > &tp_obstacles) const =0
Updates the radial map of closest TP-Obstacles given a single obstacle point at (ox,oy)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon construction and destructio...
Definition: CTimeLogger.h:152
static void transformPointcloudWithSquareClipping(const mrpt::maps::CPointsMap &in_map, mrpt::maps::CPointsMap &out_map, const mrpt::poses::CPose2D &asSeenFrom, const double MAX_DIST_XY)
void getPointsBuffer(size_t &outPointsCount, const float *&xs, const float *&ys, const float *&zs) const
Provides a direct access to points buffer, or nullptr if there is no points in the map...
Definition: CPointsMap.cpp:259
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
#define ASSERT_(f)
Base class for all PTGs suitable to non-holonomic, differentially-driven (or Ackermann) vehicles base...
mrpt::math::TPolygon2D robot_shape
The robot shape used when computing collisions; it&#39;s loaded from the config file/text as a single 2xN...
GLsizei maxLength
Definition: glext.h:4932
GLsizeiptr size
Definition: glext.h:3923
virtual void insertPointFast(float x, float y, float z=0)=0
The virtual method for insertPoint() without calling mark_as_modified()
Lightweight 2D point.
#define ASSERTMSG_(f, __ERROR_MSG)
GLenum const GLfloat * params
Definition: glext.h:3534
static CParameterizedTrajectoryGenerator * CreatePTG(const std::string &ptgClassName, const mrpt::utils::CConfigFileBase &cfg, const std::string &sSection, const std::string &sKeyPrefix)
The class factory for creating a PTG from a list of parameters in a section of a given config file (p...
mrpt::utils::CTimeLogger m_timelogger



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019