selfdriving
DiffDriveCollisionGridBased.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2023, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/containers/CDynamicGrid.h>
12 #include <mrpt/math/CPolygon.h>
13 #include <mrpt/nav/tpspace/CParameterizedTrajectoryGenerator.h>
14 #include <mrpt/typemeta/TEnumType.h>
15 
16 namespace mpp::ptg
17 {
18 struct TCPoint
19 {
20  TCPoint() = default;
22  const float x_, const float y_, const float phi_, const float t_,
23  const float dist_, const float v_, const float w_)
24  : x(x_), y(y_), phi(phi_), t(t_), dist(dist_), v(v_), w(w_)
25  {
26  }
27  float x = 0, y = 0, phi = 0, t = 0, dist = 0, v = 0, w = 0;
28 };
29 using TCPointVector = std::vector<TCPoint>;
30 
31 mrpt::serialization::CArchive& operator<<(
32  mrpt::serialization::CArchive& o, const TCPoint& p);
33 mrpt::serialization::CArchive& operator>>(
34  mrpt::serialization::CArchive& i, TCPoint& p);
35 
36 /** Base class for all PTGs suitable to non-holonomic, differentially-driven (or
37  * Ackermann) vehicles
38  * based on numerical integration of the trajectories and collision
39  * look-up-table.
40  * Regarding `initialize()`: in this this family of PTGs, the method builds the
41  * collision grid or load it from a cache file.
42  * Collision grids must be calculated before calling getTPObstacle(). Robot
43  * shape must be set before initializing with setRobotShape().
44  * The rest of PTG parameters should have been set at the constructor.
45  *
46  * \note This class is forked from mrpt::nav to expose more parameters related
47  * to the path simulation time steps.
48  *
49  */
50 class DiffDriveCollisionGridBased : public mrpt::nav::CPTG_RobotShape_Polygonal
51 {
52  public:
53  /** The main method to be implemented in derived classes: it defines the
54  * differential-driven differential equation */
55  virtual void ptgDiffDriveSteeringFunction(
56  float alpha, float t, float x, float y, float phi, float& v,
57  float& w) const = 0;
58 
59  /** @name Virtual interface of each PTG implementation
60  * @{ */
61  // getDescription(): remains to be defined in derived classes.
62  // setParams() to be defined in derived classses.
63 
64  /** The default implementation in this class relies on a look-up-table.
65  * Derived classes may redefine this to closed-form expressions, when they
66  * exist.
67  * See full docs in base class
68  * CParameterizedTrajectoryGenerator::inverseMap_WS2TP() */
69  bool inverseMap_WS2TP(
70  double x, double y, int& out_k, double& out_d,
71  double tolerance_dist = 0.10) const override;
72 
73  /** In this class, `out_action_cmd` contains: [0]: linear velocity (m/s),
74  * [1]: angular velocity (rad/s).
75  * See more docs in
76  * CParameterizedTrajectoryGenerator::directionToMotionCommand() */
77  mrpt::kinematics::CVehicleVelCmd::Ptr directionToMotionCommand(
78  uint16_t k) const override;
79  mrpt::kinematics::CVehicleVelCmd::Ptr getSupportedKinematicVelocityCommand()
80  const override;
81 
82  /** Launches an exception in this class: it is not allowed in numerical
83  * integration-based PTGs to change the reference distance
84  * after initialization. */
85  void setRefDistance(const double refDist) override;
86 
87  // Access to PTG paths (see docs in base class)
88  size_t getPathStepCount(uint16_t k) const override;
89  mrpt::math::TPose2D getPathPose(uint16_t k, uint32_t step) const override;
90  double getPathDist(uint16_t k, uint32_t step) const override;
91  bool getPathStepForDist(
92  uint16_t k, double dist, uint32_t& out_step) const override;
93  double getPathStepDuration() const override;
94  double getMaxLinVel() const override { return V_MAX; }
95  double getMaxAngVel() const override { return W_MAX; }
96  void updateTPObstacle(
97  double ox, double oy, std::vector<double>& tp_obstacles) const override;
98  void updateTPObstacleSingle(
99  double ox, double oy, uint16_t k, double& tp_obstacle_k) const override;
100 
101  /** This family of PTGs ignores the dynamic states */
102  void onNewNavDynamicState() override
103  {
104  // Do nothing.
105  }
106 
107  /** @} */ // --- end of virtual methods
108 
109  double getMax_V() const { return V_MAX; }
110  double getMax_W() const { return W_MAX; }
111 
112  protected:
114 
115  void internal_processNewRobotShape() override;
116  void internal_initialize(
117  const std::string& cacheFilename = std::string(),
118  const bool verbose = true) override;
119  void internal_deinitialize() override;
120 
121  /** Possible values in "params" (those in CParameterizedTrajectoryGenerator,
122  * which is called internally, plus):
123  * - `${sKeyPrefix}resolution`: The cell size
124  * - `${sKeyPrefix}v_max`, ``${sKeyPrefix}w_max`: Maximum robot speeds.
125  * - `${sKeyPrefix}shape_x{0,1,2..}`, ``${sKeyPrefix}shape_y{0,1,2..}`:
126  * Polygonal robot shape [Optional, can be also set via
127  * `setRobotPolygonShape()`]
128  *
129  * See docs of derived classes for additional parameters in setParams()
130  */
131  void loadFromConfigFile(
132  const mrpt::config::CConfigFileBase& cfg,
133  const std::string& sSection) override;
134  void saveToConfigFile(
135  mrpt::config::CConfigFileBase& cfg,
136  const std::string& sSection) const override;
137 
138  void loadDefaultParams() override;
139 
140  double V_MAX{.0}, W_MAX{.0};
141  double turningRadiusReference{.10};
142  std::vector<TCPointVector> m_trajectory;
143  double m_resolution{0.05};
144  double m_stepTimeDuration{0.01};
145 
146  void internal_readFromStream(mrpt::serialization::CArchive& in) override;
147  void internal_writeToStream(
148  mrpt::serialization::CArchive& out) const override;
149 
150  /** Numerically solve the diferential equations to generate a family of
151  * trajectories */
152  void simulateTrajectories(float max_time, float max_dist, float dt);
153 
154  mrpt::math::TTwist2D getPathTwist(uint16_t k, uint32_t step) const override;
155 
156  /** A list of all the pairs (alpha,distance) such as the robot collides at
157  *that cell.
158  * - map key (uint16_t) -> alpha value (k)
159  * - map value (float) -> the MINIMUM distance (d), in meters,
160  *associated with that "k".
161  */
162  using TCollisionCell = std::vector<std::pair<uint16_t, float>>;
163 
164  /** An internal class for storing the collision grid */
165  class CCollisionGrid : public mrpt::containers::CDynamicGrid<TCollisionCell>
166  {
167  private:
169 
170  public:
172  float x_min, float x_max, float y_min, float y_max,
173  float resolution, DiffDriveCollisionGridBased* parent)
174  : mrpt::containers::CDynamicGrid<TCollisionCell>(
175  x_min, x_max, y_min, y_max, resolution),
176  m_parent(parent)
177  {
178  }
179  ~CCollisionGrid() override = default;
180  /** Save to file, true = OK */
181  bool saveToFile(
182  mrpt::serialization::CArchive* fil,
183  const mrpt::math::CPolygon& computed_robotShape) const;
184  /** Load from file, true = OK */
185  bool loadFromFile(
186  mrpt::serialization::CArchive* fil,
187  const mrpt::math::CPolygon& current_robotShape);
188 
189  /** For an obstacle (x,y), returns a vector with all the pairs (a,d)
190  * such as the robot collides */
191  const TCollisionCell& getTPObstacle(
192  const float obsX, const float obsY) const;
193 
194  /** Updates the info into a cell: It updates the cell only if the
195  *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(
201  const unsigned int icx, const unsigned int icy, const uint16_t k,
202  const float dist);
203 
204  }; // end of class CCollisionGrid
205 
206  // Save/Load from files.
207  bool saveColGridsToFile(
208  const std::string& filename,
209  const mrpt::math::CPolygon& computed_robotShape) const; // true = OK
210  bool loadColGridsFromFile(
211  const std::string& filename,
212  const mrpt::math::CPolygon& current_robotShape); // true = OK
213 
214  /** The collision grid */
216 
217  /** Specifies the min/max values for "k" and "n", respectively.
218  * \sa m_lambdaFunctionOptimizer
219  */
221  {
223  : k_min(std::numeric_limits<uint16_t>::max()),
224  k_max(std::numeric_limits<uint16_t>::min()),
225  n_min(std::numeric_limits<uint32_t>::max()),
226  n_max(std::numeric_limits<uint32_t>::min())
227  {
228  }
229 
230  uint16_t k_min, k_max;
231  uint32_t n_min, n_max;
232 
233  bool isEmpty() const
234  {
235  return k_min == std::numeric_limits<uint16_t>::max();
236  }
237  };
238 
239  /** This grid will contain indexes data for speeding-up the default,
240  * brute-force lambda function */
241  mrpt::containers::CDynamicGrid<TCellForLambdaFunction>
243 };
244 
245 } // namespace mpp::ptg
246 
247 namespace mrpt::typemeta
248 {
249 // Specialization must occur in the same namespace
250 MRPT_DECLARE_TTYPENAME_NAMESPACE(TCPoint, mpp::ptg)
251 } // namespace mrpt::typemeta
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &i, TCPoint &p)
std::vector< std::pair< uint16_t, float > > TCollisionCell
Definition: DiffDriveCollisionGridBased.h:162
Definition: DiffDriveCollisionGridBased.h:18
float y
Definition: DiffDriveCollisionGridBased.h:27
Definition: DiffDriveCollisionGridBased.h:50
uint32_t n_min
Definition: DiffDriveCollisionGridBased.h:231
bool isEmpty() const
Definition: DiffDriveCollisionGridBased.h:233
float w
Definition: DiffDriveCollisionGridBased.h:27
Definition: DiffDrive_C.h:14
CCollisionGrid(float x_min, float x_max, float y_min, float y_max, float resolution, DiffDriveCollisionGridBased *parent)
Definition: DiffDriveCollisionGridBased.h:171
Definition: DiffDriveCollisionGridBased.h:247
void onNewNavDynamicState() override
Definition: DiffDriveCollisionGridBased.h:102
float x
Definition: DiffDriveCollisionGridBased.h:27
float phi
Definition: DiffDriveCollisionGridBased.h:27
double getMaxLinVel() const override
Definition: DiffDriveCollisionGridBased.h:94
std::vector< TCPointVector > m_trajectory
Definition: DiffDriveCollisionGridBased.h:142
DiffDriveCollisionGridBased const * m_parent
Definition: DiffDriveCollisionGridBased.h:168
double getMax_W() const
Definition: DiffDriveCollisionGridBased.h:110
std::vector< TCPoint > TCPointVector
Definition: DiffDriveCollisionGridBased.h:29
double getMaxAngVel() const override
Definition: DiffDriveCollisionGridBased.h:95
float v
Definition: DiffDriveCollisionGridBased.h:27
TCellForLambdaFunction()
Definition: DiffDriveCollisionGridBased.h:222
double getMax_V() const
Definition: DiffDriveCollisionGridBased.h:109
uint16_t k_min
Definition: DiffDriveCollisionGridBased.h:230
mrpt::containers::CDynamicGrid< TCellForLambdaFunction > m_lambdaFunctionOptimizer
Definition: DiffDriveCollisionGridBased.h:242
TCPoint(const float x_, const float y_, const float phi_, const float t_, const float dist_, const float v_, const float w_)
Definition: DiffDriveCollisionGridBased.h:21
CCollisionGrid m_collisionGrid
Definition: DiffDriveCollisionGridBased.h:215
float t
Definition: DiffDriveCollisionGridBased.h:27
Definition: DiffDriveCollisionGridBased.h:220
float dist
Definition: DiffDriveCollisionGridBased.h:27
Definition: DiffDriveCollisionGridBased.h:165
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &o, const TCPoint &p)