31 : m_collisionGrid(-1, 1, -1, 1, 0.5, this)
54 v_max_mps,
double,
V_MAX, cfg, sSection);
56 w_max_dps,
double,
W_MAX, cfg, sSection);
63 const int WN = 25, WV = 30;
69 "Resolution of the collision-check look-up-table [m].");
71 sSection,
"v_max_mps",
V_MAX, WN, WV,
72 "Maximum linear velocity for trajectories [m/s].");
75 "Maximum angular velocity for trajectories [deg/s].");
78 "An approximate dimension of the robot (not a critical parameter) " 89 o <<
p.x <<
p.y <<
p.phi <<
p.t <<
p.dist <<
p.v <<
p.w;
95 i >>
p.x >>
p.y >>
p.phi >>
p.t >>
p.dist >>
p.v >>
p.w;
104 float max_time,
float max_dist,
unsigned int max_n,
float diferencial_t,
105 float min_dist,
float* out_max_acc_v,
float* out_max_acc_w)
116 const float radio_max_robot = 1.0f;
123 float ult_dist, ult_dist1, ult_dist2;
126 float x_min = 1e3f, x_max = -1e3;
127 float y_min = 1e3f, y_max = -1e3;
130 float max_acc_lin, max_acc_ang;
132 max_acc_lin = max_acc_ang = 0;
143 float t = .0f, dist = .0f, girado = .0f;
144 float x = .0f,
y = .0f, phi = .0f,
v = .0f,
w = .0f, _x = .0f,
145 _y = .0f, _phi = .0f;
149 float last_vs[2] = {.0f, .0f}, last_ws[2] = {.0f, .0f};
155 while (
t < max_time && dist < max_dist &&
points.size() < max_n &&
156 fabs(girado) < 1.95 *
M_PI)
162 fabs((last_vs[0] - last_vs[1]) / diferencial_t);
164 fabs((last_ws[0] - last_ws[1]) / diferencial_t);
173 last_vs[1] = last_vs[0];
174 last_ws[1] = last_ws[0];
180 x += cos(phi) *
v * diferencial_t;
181 y += sin(phi) *
v * diferencial_t;
182 phi +=
w * diferencial_t;
185 girado +=
w * diferencial_t;
190 dist += v_inTPSpace * diferencial_t;
196 ult_dist2 = fabs(radio_max_robot * (_phi - phi));
197 ult_dist = std::max(ult_dist1, ult_dist2);
199 if (ult_dist > min_dist)
216 x_max = std::max(x_max,
x);
218 y_max = std::max(y_max,
y);
232 if (out_max_acc_v) *out_max_acc_v = max_acc_lin;
233 if (out_max_acc_w) *out_max_acc_w = max_acc_ang;
240 x_min - 0.5f, x_max + 0.5f, y_min - 0.5f, y_max + 0.5f, 0.25f,
263 "[CPTG_DiffDrive_CollisionGridBased::simulateTrajectories] " 264 "Simulation aborted: unexpected exception!\n");
288 const float obsX,
const float obsY)
const 292 return cell !=
nullptr ? *cell : emptyCell;
300 const unsigned int icx,
const unsigned int icy,
const uint16_t k,
308 auto itK = cell->end();
309 for (
auto it = cell->begin(); it != cell->end(); ++it)
316 if (itK == cell->end())
318 cell->push_back(std::make_pair(k, dist));
322 if (dist < itK->second) itK->second = dist;
386 if (!f)
return false;
388 const uint8_t serialize_version =
395 *f << computed_robotShape;
398 *f << m_parent->getDescription() << m_parent->getAlphaValuesCount()
399 <<
static_cast<float>(m_parent->getMax_V())
400 << static_cast<float>(m_parent->getMax_W());
402 *f << m_x_min << m_x_max << m_y_min << m_y_max;
413 *f << m_map[i][k].
first << m_map[i][k].second;
433 if (!f)
return false;
444 *f >> serialized_version;
446 switch (serialized_version)
453 const bool shapes_match =
454 (stored_shape.size() == current_robotShape.size() &&
456 stored_shape.begin(), stored_shape.end(),
457 current_robotShape.begin()));
473 const std::string expected_desc = m_parent->getDescription();
476 if (desc != expected_desc)
return false;
479 #define READ_UINT16_CHECK_IT_MATCHES_STORED(_VAR) \ 483 if (ff != _VAR) return false; \ 485 #define READ_FLOAT_CHECK_IT_MATCHES_STORED(_VAR) \ 489 if (std::abs(ff - _VAR) > 1e-4f) return false; \ 491 #define READ_DOUBLE_CHECK_IT_MATCHES_STORED(_VAR) \ 495 if (std::abs(ff - _VAR) > 1e-6) return false; \ 521 *f >> m_map[i][k].
first >> m_map[i][k].second;
526 catch (
const std::exception& e)
528 std::cerr <<
"[CCollisionGrid::loadFromFile] " << e.what();
538 double x,
double y,
int& out_k,
double& out_d,
double tolerance_dist)
const 544 "Have you called simulateTrajectories() first?");
554 bool at_least_one =
false;
561 for (
int cx = cx0 - 1; cx <= cx0 + 1; cx++)
563 for (
int cy = cy0 - 1; cy <= cy0 + 1; cy++)
592 float selected_d = 0;
593 float selected_dist = std::numeric_limits<float>::max();
598 for (
int k = k_min; k <= k_max; k++)
602 std::min(static_cast<uint32_t>(n_real ? n_real - 1 : 0), n_max);
608 if (dist_a_punto < selected_dist)
610 selected_dist = dist_a_punto;
618 if (selected_k != -1)
622 return (selected_dist <=
square(tolerance_dist));
632 selected_dist = std::numeric_limits<float>::max();
640 if (dist_a_punto < selected_dist)
642 selected_dist = dist_a_punto;
644 selected_d = dist_a_punto;
648 selected_d = std::sqrt(selected_d);
657 const float target_dist = std::sqrt(
x *
x +
y *
y);
658 return (target_dist > target_dist);
665 "Changing reference distance not allowed in this class after " 674 "Changing robot shape not allowed in this class after initialization!");
683 const std::string& cacheFilename,
const bool verbose)
691 <<
"[CPTG_DiffDrive_CollisionGridBased::initialize] Starting... " 692 "*** THIS MAY TAKE A WHILE, BUT MUST BE COMPUTED ONLY ONCE!! **" 698 m_robotShape.size() >= 3,
"Robot shape must have 3 or more vertices");
707 if (verbose) cout <<
"Initializing PTG '" << cacheFilename <<
"'...";
710 const float min_dist = 0.015f;
728 ASSERTMSG_(Ki > 0,
"The PTG seems to be not initialized!");
733 if (verbose) cout <<
"loaded from file OK" << endl;
749 std::vector<mrpt::math::TPoint2D> transf_shape(
754 for (
size_t k = 0; k < Ki; k++)
758 for (
size_t n = 0;
n < (nPoints - 1);
n++)
765 std::numeric_limits<double>::max(),
766 std::numeric_limits<double>::max());
768 -std::numeric_limits<double>::max(),
769 -std::numeric_limits<double>::max());
771 for (
size_t m = 0; m < nVerts; m++)
798 for (
int ix = ix_min; ix < ix_max; ix++)
802 for (
int iy = iy_min; iy < iy_max; iy++)
814 ix - 1, iy - 1, k, d);
821 if (verbose) cout << k <<
"/" << Ki <<
",";
824 if (verbose) cout <<
format(
"Done! [%.03f sec]\n", tictac.
Tac());
869 for (
size_t n = 0;
n < numPoints - 1;
n++)
878 out_step = numPoints - 1;
883 double ox,
double oy, std::vector<double>& tp_obstacles)
const 888 for (
const auto& i : cell)
890 const double dist = i.second;
896 double ox,
double oy,
uint16_t k,
double& tp_obstacle_k)
const 901 for (
const auto& i : cell)
904 const double dist = i.second;
double index2alpha(uint16_t k) const
Alpha value for the discrete corresponding value.
double GetVertex_x(size_t i) const
Methods for accessing the vertices.
double Tac() noexcept
Stops the stopwatch.
GLclampf GLclampf GLclampf alpha
void setRefDistance(const double refDist) override
Launches an exception in this class: it is not allowed in numerical integration-based PTGs to change ...
virtual void internal_writeToStream(mrpt::serialization::CArchive &out) const
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &i, mrpt::nav::TCPoint &p)
int y2idx(double y) const
#define READ_DOUBLE_CHECK_IT_MATCHES_STORED(_VAR)
unsigned __int16 uint16_t
bool saveColGridsToFile(const std::string &filename, const mrpt::math::CPolygon &computed_robotShape) const
size_t verticesCount() const
Returns the vertices count in the polygon:
double RAD2DEG(const double x)
Radians to degrees.
bool getPathStepForDist(uint16_t k, double dist, uint32_t &out_step) const override
Access path k ([0,N-1]=>[-pi,pi] in alpha): largest step count for which the traversed distance is < ...
void saveToConfigFile(mrpt::config::CConfigFileBase &cfg, const std::string &sSection) const override
This method saves the options to a ".ini"-like file or memory-stored string list. ...
void internal_initialize(const std::string &cacheFilename=std::string(), const bool verbose=true) override
Must be called after setting all PTG parameters and before requesting converting obstacles to TP-Spac...
Specifies the min/max values for "k" and "n", respectively.
void loadFromConfigFile(const mrpt::config::CConfigFileBase &cfg, const std::string &sSection) override
Possible values in "params" (those in CParameterizedTrajectoryGenerator, which is called internally...
#define MRPT_LOAD_HERE_CONFIG_VAR_DEGREES_NO_DEFAULT( variableName, variableType, targetVariable, configFileObject, sectionNameStr)
#define ASSERT_BELOW_(__A, __B)
const TCollisionCell & getTPObstacle(const float obsX, const float obsY) const
For an obstacle (x,y), returns a vector with all the pairs (a,d) such as the robot collides...
double DEG2RAD(const double x)
Degrees to radians.
Trajectory points in C-Space for non-holonomic robots.
double getResolution() const
Returns the resolution of the grid map.
bool loadFromFile(mrpt::serialization::CArchive *fil, const mrpt::math::CPolygon ¤t_robotShape)
Load from file, true = OK.
void internal_deinitialize() override
This must be called to de-initialize the PTG if some parameter is to be changed.
void loadDefaultParams() override
Loads a set of default parameters; provided exclusively for the PTG-configurator tool.
A high-performance stopwatch, with typical resolution of nanoseconds.
A wrapper of a TPolygon2D class, implementing CSerializable.
void internal_readFromStream(mrpt::serialization::CArchive &in) override
double idx2x(int cx) const
Transform a cell index into a coordinate value of the cell central point.
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
void internal_shape_saveToStream(mrpt::serialization::CArchive &out) const
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
GLubyte GLubyte GLubyte GLubyte w
const uint32_t COLGRID_FILE_MAGIC
void updateTPObstacleSingle(double ox, double oy, uint16_t k, double &tp_obstacle_k) const override
Like updateTPObstacle() but for one direction only (k) in TP-Space.
#define READ_FLOAT_CHECK_IT_MATCHES_STORED(_VAR)
double idx2y(int cy) const
void internal_TPObsDistancePostprocess(const double ox, const double oy, const double new_tp_obs_dist, double &inout_tp_obs) const
To be called by implementors of updateTPObstacle() and updateTPObstacleSingle() to honor the user set...
GLsizei const GLfloat * points
mrpt::kinematics::CVehicleVelCmd::Ptr directionToMotionCommand(uint16_t k) const override
In this class, out_action_cmd contains: [0]: linear velocity (m/s), [1]: angular velocity (rad/s)...
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream, std::istream, std::ostream, std::stringstream.
void setSize(const double x_min, const double x_max, const double y_min, const double y_max, const double resolution, const T *fill_value=nullptr)
Changes the size of the grid, ERASING all previous contents.
T square(const T x)
Inline function for the square of a number.
virtual void loadDefaultParams()
Loads a set of default parameters into the PTG.
#define ASSERT_(f)
Defines an assertion mechanism.
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &o, const mrpt::nav::TCPoint &p)
void loadShapeFromConfigFile(const mrpt::config::CConfigFileBase &source, const std::string §ion)
This class allows loading and storing values and vectors of different types from a configuration text...
bool inverseMap_WS2TP(double x, double y, int &out_k, double &out_d, double tolerance_dist=0.10) const override
The default implementation in this class relies on a look-up-table.
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
double getPathDist(uint16_t k, uint32_t step) const override
Access path k ([0,N-1]=>[-pi,pi] in alpha): traversed distance at discrete step step.
void internal_shape_loadFromStream(mrpt::serialization::CArchive &in)
TCollisionCell * cellByPos(double x, double y)
Returns a pointer to the contents of a cell given by its coordinates, or nullptr if it is out of the ...
CPTG_DiffDrive_CollisionGridBased()
Constructor: possible values in "params":
bool loadColGridsFromFile(const std::string &filename, const mrpt::math::CPolygon ¤t_robotShape)
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
size_t getSizeX() const
Returns the horizontal size of grid map in cells count.
double m_stepTimeDuration
GLsizei const GLchar ** string
void updateCellInfo(const unsigned int icx, const unsigned int icy, const uint16_t k, const float dist)
Updates the info into a cell: It updates the cell only if the distance d for the path k is lower than...
void write(const std::string §ion, const std::string &name, enum_t value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment=std::string())
size_t getSizeY() const
Returns the vertical size of grid map in cells count.
uint16_t getAlphaValuesCount() const
Get the number of different, discrete paths in this family.
size_t getPathStepCount(uint16_t k) const override
Access path k ([0,N-1]=>[-pi,pi] in alpha): number of discrete "steps" along the trajectory.
#define MRPT_LOAD_CONFIG_VAR( variableName, variableType, configFileObject, sectionNameStr)
An useful macro for loading variables stored in a INI-like file under a key with the same name that t...
bool contains(const TPoint2D &point) const
Check whether a point is inside (or within geometryEpsilon of a polygon edge).
void internal_processNewRobotShape() override
Will be called whenever the robot shape is set / updated.
bool saveToFile(mrpt::serialization::CArchive *fil, const mrpt::math::CPolygon &computed_robotShape) const
Save to file, true = OK.
mrpt::containers::CDynamicGrid< TCellForLambdaFunction > m_lambdaFunctionOptimizer
This grid will contain indexes data for speeding-up the default, brute-force lambda function...
Virtual base class for "archives": classes abstracting I/O streams.
std::vector< TCPoint > TCPointVector
CCollisionGrid m_collisionGrid
The collision grid.
void loadFromConfigFile(const mrpt::config::CConfigFileBase &cfg, const std::string &sSection) override
Parameters accepted by this base class:
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
virtual void internal_readFromStream(mrpt::serialization::CArchive &in)
int x2idx(double x) const
Transform a coordinate values into cell indexes.
void saveToConfigFile(mrpt::config::CConfigFileBase &cfg, const std::string &sSection) const override
This method saves the options to a ".ini"-like file or memory-stored string list. ...
#define READ_UINT16_CHECK_IT_MATCHES_STORED(_VAR)
void getPathPose(uint16_t k, uint32_t step, mrpt::math::TPose2D &p) const override
Access path k ([0,N-1]=>[-pi,pi] in alpha): pose of the vehicle at discrete step step.
void updateTPObstacle(double ox, double oy, std::vector< double > &tp_obstacles) const override
Updates the radial map of closest TP-Obstacles given a single obstacle point at (ox,oy)
std::vector< std::pair< uint16_t, float > > TCollisionCell
A list of all the pairs (alpha,distance) such as the robot collides at that cell. ...
virtual void ptgDiffDriveSteeringFunction(float alpha, float t, float x, float y, float phi, float &v, float &w) const =0
The main method to be implemented in derived classes: it defines the differential-driven differential...
double GetVertex_y(size_t i) const
void loadDefaultParams() override
Loads a set of default parameters; provided exclusively for the PTG-configurator tool.
Saves data to a file and transparently compress the data using the given compression level...
void Tic() noexcept
Starts the stopwatch.
double turningRadiusReference
std::shared_ptr< CVehicleVelCmd > Ptr
void saveToConfigFile(mrpt::config::CConfigFileBase &cfg, const std::string &sSection) const override
This method saves the options to a ".ini"-like file or memory-stored string list. ...
#define MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT( variableName, variableType, targetVariable, configFileObject, sectionNameStr)
unsigned __int32 uint32_t
std::vector< TCPointVector > m_trajectory
mrpt::kinematics::CVehicleVelCmd::Ptr getSupportedKinematicVelocityCommand() const override
Returns an empty kinematic velocity command object of the type supported by this PTG.
Kinematic model for Ackermann-like or differential-driven vehicles.
2D polygon, inheriting from std::vector<TPoint2D>.
uint16_t m_alphaValuesCount
The number of discrete values for "alpha" between -PI and +PI.
mrpt::math::CPolygon m_robotShape
void internal_writeToStream(mrpt::serialization::CArchive &out) const override
void simulateTrajectories(float max_time, float max_dist, unsigned int max_n, float diferencial_t, float min_dist, float *out_max_acc_v=nullptr, float *out_max_acc_w=nullptr)
Numerically solve the diferential equations to generate a family of trajectories. ...
double getPathStepDuration() const override
Returns the duration (in seconds) of each "step".