30 turningRadiusReference(.10),
32 m_stepTimeDuration(0.01),
33 m_collisionGrid(-1,1,-1,1,0.5,this)
61 const int WN = 25, WV = 30;
65 cfg.
write(sSection,
"resolution",
m_resolution, WN,WV,
"Resolution of the collision-check look-up-table [m].");
66 cfg.
write(sSection,
"v_max_mps",
V_MAX, WN,WV,
"Maximum linear velocity for trajectories [m/s].");
68 cfg.
write(sSection,
"turningRadiusReference",
turningRadiusReference, WN,WV,
"An approximate dimension of the robot (not a critical parameter) [m].");
78 o <<
p.x<<
p.y<<
p.phi<<
p.t<<
p.dist<<
p.v<<
p.w;
83 i >>
p.x>>
p.y>>
p.phi>>
p.t>>
p.dist>>
p.v>>
p.w;
100 float *out_max_acc_w)
111 const float radio_max_robot=1.0f;
116 float ult_dist, ult_dist1, ult_dist2;
119 float x_min = 1e3f, x_max = -1e3;
120 float y_min = 1e3f, y_max = -1e3;
123 float max_acc_lin, max_acc_ang;
125 max_acc_lin = max_acc_ang = 0;
136 float t = .0f, dist = .0f, girado = .0f;
137 float x = .0f,
y = .0f, phi = .0f,
v = .0f,
w = .0f, _x = .0f, _y = .0f, _phi = .0f;
140 float last_vs[2] = {.0f,.0f}, last_ws[2] = {.0f,.0f};
146 while (
t < max_time && dist < max_dist &&
points.size() < max_n && fabs(girado) < 1.95 *
M_PI )
151 float acc_lin = fabs( (last_vs[0]-last_vs[1])/diferencial_t);
152 float acc_ang = fabs( (last_ws[0]-last_ws[1])/diferencial_t);
161 last_vs[1]=last_vs[0];
162 last_ws[1]=last_ws[0];
168 x += cos(phi)*
v * diferencial_t;
169 y += sin(phi)*
v * diferencial_t;
170 phi+=
w * diferencial_t;
173 girado +=
w * diferencial_t;
177 dist += v_inTPSpace * diferencial_t;
183 ult_dist2 = fabs( radio_max_robot* ( _phi - phi ) );
184 ult_dist = std::max( ult_dist1, ult_dist2 );
186 if (ult_dist > min_dist)
202 x_min =
std::min(x_min,
x); x_max = std::max(x_max,
x);
203 y_min =
std::min(y_min,
y); y_max = std::max(y_max,
y);
217 if (out_max_acc_v) *out_max_acc_v = max_acc_lin;
218 if (out_max_acc_w) *out_max_acc_w = max_acc_ang;
225 x_min-0.5f,x_max+0.5f,
226 y_min-0.5f,y_max+0.5f, 0.25f,
246 std::cout <<
format(
"[CPTG_DiffDrive_CollisionGridBased::simulateTrajectories] Simulation aborted: unexpected exception!\n");
260 return mrpt::kinematics::CVehicleVelCmdPtr(cmd);
267 const float obsX,
const float obsY)
const 271 return cell!=NULL ? *cell : emptyCell;
279 const unsigned int icx,
280 const unsigned int icy,
296 if (itK==cell->end())
298 cell->push_back(std::make_pair(k,dist) );
302 if (dist<itK->second)
316 if (!fo.fileOpenCorrectly())
return false;
340 if (
n!=1)
return false;
359 if (!f)
return false;
361 const uint8_t serialize_version = 2;
367 *f << computed_robotShape;
370 *f << m_parent->getDescription()
371 << m_parent->getAlphaValuesCount()
372 <<
static_cast<float>(m_parent->getMax_V())
373 << static_cast<float>(m_parent->getMax_W());
375 *f << m_x_min << m_x_max << m_y_min << m_y_max;
386 *f << m_map[i][k].
first << m_map[i][k].second;
404 if (!f)
return false;
415 *f >> serialized_version;
417 switch (serialized_version)
424 const bool shapes_match =
425 ( stored_shape.size()==current_robotShape.size() &&
426 std::equal(stored_shape.begin(),stored_shape.end(), current_robotShape.begin() ) );
428 if (!shapes_match)
return false;
439 const std::string expected_desc = m_parent->getDescription();
442 if (desc!=expected_desc)
return false;
445 #define READ_UINT16_CHECK_IT_MATCHES_STORED(_VAR) { uint16_t ff; *f >> ff; if (ff!=_VAR) return false; } 446 #define READ_FLOAT_CHECK_IT_MATCHES_STORED(_VAR) { float ff; *f >> ff; if (std::abs(ff-_VAR)>1e-4f) return false; } 447 #define READ_DOUBLE_CHECK_IT_MATCHES_STORED(_VAR) { double ff; *f >> ff; if (std::abs(ff-_VAR)>1e-6) return false; } 471 *f >> m_map[i][k].
first >> m_map[i][k].second;
476 catch(std::exception &e)
478 std::cerr <<
"[CCollisionGrid::loadFromFile] " << e.what();
501 bool at_least_one =
false;
508 for (
int cx=cx0-1;cx<=cx0+1;cx++)
510 for (
int cy=cy0-1;cy<=cy0+1;cy++)
537 float selected_dist = std::numeric_limits<float>::max();
542 for (
int k=k_min;k<=k_max;k++)
545 const uint32_t n_max_this =
std::min( static_cast<uint32_t>(n_real ? n_real-1 : 0), n_max);
550 if (dist_a_punto<selected_dist)
552 selected_dist = dist_a_punto;
564 return (selected_dist <=
square(tolerance_dist));
574 selected_dist = std::numeric_limits<float>::max();
580 if (dist_a_punto<selected_dist)
582 selected_dist = dist_a_punto;
584 selected_d = dist_a_punto;
588 selected_d = std::sqrt(selected_d);
595 const float target_dist = std::sqrt(
x*
x+
y*
y );
596 return (target_dist>target_dist);
601 ASSERTMSG_(
m_trajectory.empty(),
"Changing reference distance not allowed in this class after initialization!");
622 cout << endl <<
"[CPTG_DiffDrive_CollisionGridBased::initialize] Starting... *** THIS MAY TAKE A WHILE, BUT MUST BE COMPUTED ONLY ONCE!! **" << endl;
635 if (verbose) cout <<
"Initializing PTG '" << cacheFilename <<
"'...";
638 const float min_dist = 0.015f;
656 ASSERTMSG_(Ki>0,
"The PTG seems to be not initialized!");
662 cout <<
"loaded from file OK" << endl;
675 std::vector<mrpt::math::TPoint2D> transf_shape(nVerts);
679 for (
size_t k=0;k<Ki;k++)
684 for (
size_t n=0;
n<(nPoints-1);
n++)
690 mrpt::math::TPoint2D bb_min(std::numeric_limits<double>::max(),std::numeric_limits<double>::max());
691 mrpt::math::TPoint2D bb_max(-std::numeric_limits<double>::max(),-std::numeric_limits<double>::max());
693 for (
size_t m = 0;m<nVerts;m++)
710 for (
int ix=ix_min;ix<ix_max;ix++)
714 for (
int iy=iy_min;iy<iy_max;iy++)
733 cout << k <<
"/" << Ki <<
",";
737 cout <<
format(
"Done! [%.03f sec]\n",tictac.
Tac() );
779 for (
size_t n=0;
n < numPoints-1;
n++)
788 out_step = numPoints-1;
793 double ox,
double oy,
794 std::vector<double> &tp_obstacles)
const 800 const double dist = i->second;
812 const double dist = i->second;
double index2alpha(uint16_t k) const
Alpha value for the discrete corresponding value.
void updateTPObstacle(double ox, double oy, std::vector< double > &tp_obstacles) const MRPT_OVERRIDE
Updates the radial map of closest TP-Obstacles given a single obstacle point at (ox,oy)
void internal_shape_saveToStream(mrpt::utils::CStream &out) const
double GetVertex_x(size_t i) const
Methods for accessing the vertices.
double getPathDist(uint16_t k, uint32_t step) const MRPT_OVERRIDE
Access path k ([0,N-1]=>[-pi,pi] in alpha): traversed distance at discrete step step.
GLclampf GLclampf GLclampf alpha
#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:
Specifies the min/max values for "k" and "n", respectively.
double DEG2RAD(const double x)
Degrees to radians.
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...
Trajectory points in C-Space for non-holonomic robots.
mrpt::utils::CDynamicGrid< TCellForLambdaFunction > m_lambdaFunctionOptimizer
This grid will contain indexes data for speeding-up the default, brute-force lambda function...
int x2idx(double x) const
Transform a coordinate values into cell indexes.
#define ASSERT_BELOW_(__A, __B)
virtual void loadFromConfigFile(const mrpt::utils::CConfigFileBase &cfg, const std::string &sSection) MRPT_OVERRIDE
Parameters accepted by this base class:
virtual void saveToConfigFile(mrpt::utils::CConfigFileBase &cfg, const std::string &sSection) const MRPT_OVERRIDE
This method saves the options to a ".ini"-like file or memory-stored string list. ...
A wrapper of a TPolygon2D class, implementing CSerializable.
size_t getSizeY() const
Returns the vertical size of grid map in cells count.
std::vector< TCPoint > TCPointVector
const Scalar * const_iterator
void Tic()
Starts the stopwatch.
void loadShapeFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string §ion)
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...
GLubyte GLubyte GLubyte GLubyte w
const uint32_t COLGRID_FILE_MAGIC
#define READ_FLOAT_CHECK_IT_MATCHES_STORED(_VAR)
virtual void internal_writeToStream(mrpt::utils::CStream &out) const
This class allows loading and storing values and vectors of different types from a configuration text...
T square(const T x)
Inline function for the square of a number.
int y2idx(double y) const
void internal_readFromStream(mrpt::utils::CStream &in) MRPT_OVERRIDE
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
TCollisionCell * cellByPos(double x, double y)
Returns a pointer to the contents of a cell given by its coordinates, or NULL if it is out of the map...
double idx2y(int cy) const
virtual void loadDefaultParams()
Loads a set of default parameters into the PTG.
bool getPathStepForDist(uint16_t k, double dist, uint32_t &out_step) const MRPT_OVERRIDE
Access path k ([0,N-1]=>[-pi,pi] in alpha): largest step count for which the traversed distance is < ...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
double RAD2DEG(const double x)
Radians to degrees.
virtual void internal_readFromStream(mrpt::utils::CStream &in)
void internal_shape_loadFromStream(mrpt::utils::CStream &in)
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
This class implements a high-performance stopwatch.
size_t getSizeX() const
Returns the horizontal size of grid map in cells count.
virtual void loadFromConfigFile(const mrpt::utils::CConfigFileBase &cfg, const std::string &sSection) MRPT_OVERRIDE
Possible values in "params" (those in CParameterizedTrajectoryGenerator, which is called internally...
CPTG_DiffDrive_CollisionGridBased()
Constructor: possible values in "params":
virtual void saveToConfigFile(mrpt::utils::CConfigFileBase &cfg, const std::string &sSection) const MRPT_OVERRIDE
This method saves the options to a ".ini"-like file or memory-stored string list. ...
double ang_vel
Angular velocity (rad/s)
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void internal_processNewRobotShape() MRPT_OVERRIDE
Will be called whenever the robot shape is set / updated.
virtual void setRefDistance(const double refDist) MRPT_OVERRIDE
Launches an exception in this class: it is not allowed in numerical integration-based PTGs to change ...
bool loadColGridsFromFile(const std::string &filename, const mrpt::math::CPolygon ¤t_robotShape)
double getPathStepDuration() const MRPT_OVERRIDE
Returns the duration (in seconds) of each "step".
double m_stepTimeDuration
GLsizei const GLchar ** string
void simulateTrajectories(float max_time, float max_dist, unsigned int max_n, float diferencial_t, float min_dist, float *out_max_acc_v=NULL, float *out_max_acc_w=NULL)
Numerically solve the diferential equations to generate a family of trajectories. ...
double idx2x(int cx) const
Transform a cell index into a coordinate value of the cell central point.
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...
std::vector< std::pair< uint16_t, float > > TCollisionCell
A list of all the pairs (alpha,distance) such as the robot collides at that cell. ...
size_t getPathStepCount(uint16_t k) const MRPT_OVERRIDE
Access path k ([0,N-1]=>[-pi,pi] in alpha): number of discrete "steps" along the trajectory.
void updateTPObstacleSingle(double ox, double oy, uint16_t k, double &tp_obstacle_k) const MRPT_OVERRIDE
Like updateTPObstacle() but for one direction only (k) in TP-Space.
uint16_t getAlphaValuesCount() const
Get the number of different, discrete paths in this family.
double lin_vel
Linear velocity (m/s)
virtual mrpt::kinematics::CVehicleVelCmdPtr directionToMotionCommand(uint16_t k) const MRPT_OVERRIDE
In this class, out_action_cmd contains: [0]: linear velocity (m/s), [1]: angular velocity (rad/s)...
void saveToConfigFile(mrpt::utils::CConfigFileBase &cfg, const std::string &sSection) const MRPT_OVERRIDE
This method saves the options to a ".ini"-like file or memory-stored string list. ...
bool contains(const TPoint2D &point) const
Check whether a point is inside (or within geometryEpsilon of a polygon edge). This works for concave...
#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...
#define CFileGZOutputStream
Saves data to a file and transparently compress the data using the given compression level...
CCollisionGrid m_collisionGrid
The collision grid.
double getResolution() const
Returns the resolution of the grid map.
void write(const std::string §ion, const std::string &name, const data_t &value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment=std::string())
bool loadFromFile(mrpt::utils::CStream *fil, const mrpt::math::CPolygon ¤t_robotShape)
Load from file, true = OK.
bool saveToFile(mrpt::utils::CStream *fil, const mrpt::math::CPolygon &computed_robotShape) const
Save to file, true = OK.
virtual bool inverseMap_WS2TP(double x, double y, int &out_k, double &out_d, double tolerance_dist=0.10) const MRPT_OVERRIDE
The default implementation in this class relies on a look-up-table.
T square(const T x)
Inline function for the square of a number.
double Tac()
Stops the stopwatch.
::mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CAbstractHolonomicReactiveMethodPtr &pObj)
void internal_initialize(const std::string &cacheFilename=std::string(), const bool verbose=true) MRPT_OVERRIDE
Must be called after setting all PTG parameters and before requesting converting obstacles to TP-Spac...
#define MRPT_LOAD_HERE_CONFIG_VAR_DEGREES_NO_DEFAULT(variableName, variableType, targetVariable, configFileObject, sectionNameStr)
void internal_writeToStream(mrpt::utils::CStream &out) const MRPT_OVERRIDE
#define READ_UINT16_CHECK_IT_MATCHES_STORED(_VAR)
virtual void loadDefaultParams() MRPT_OVERRIDE
Loads a set of default parameters; provided exclusively for the PTG-configurator tool.
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=NULL)
Changes the size of the grid, ERASING all previous contents.
void getPathPose(uint16_t k, uint32_t step, mrpt::math::TPose2D &p) const MRPT_OVERRIDE
Access path k ([0,N-1]=>[-pi,pi] in alpha): pose of the vehicle at discrete step step.
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...
mrpt::utils::CStream NAV_IMPEXP & operator<<(mrpt::utils::CStream &o, const mrpt::nav::TCPoint &p)
double GetVertex_y(size_t i) const
double turningRadiusReference
#define MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT(variableName, variableType, targetVariable, configFileObject, sectionNameStr)
unsigned __int32 uint32_t
void loadDefaultParams() MRPT_OVERRIDE
Loads a set of default parameters; provided exclusively for the PTG-configurator tool.
#define ASSERTMSG_(f, __ERROR_MSG)
std::vector< TCPointVector > m_trajectory
void internal_deinitialize() MRPT_OVERRIDE
This must be called to de-initialize the PTG if some parameter is to be changed.
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...
virtual mrpt::kinematics::CVehicleVelCmdPtr getSupportedKinematicVelocityCommand() const MRPT_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