Main MRPT website > C++ reference
MRPT logo
CAbstractPTGBasedReactive.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2014, 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 #ifndef CAbstractPTGBasedReactive_H
10 #define CAbstractPTGBasedReactive_H
11 
18 #include <mrpt/utils/CTimeLogger.h>
19 #include <mrpt/system/datetime.h>
21 
22 namespace mrpt
23 {
24  namespace reactivenav
25  {
26  using namespace mrpt;
27  using namespace mrpt::slam;
28  using namespace mrpt::poses;
29 
30  /** Base class for reactive navigator systems based on TP-Space, with an arbitrary holonomic
31  * reactive method running on it and any number of PTGs for transforming the navigation space.
32  * Both, the holonomic method and the PTGs can be customized by the apropriate user derived classes.
33  *
34  * How to use:
35  * - Instantiate a reactive navigation object (one of the derived classes of this virtual class).
36  * - A class with callbacks must be defined by the user and provided to the constructor (derived from CReactiveInterfaceImplementation)
37  * - loadConfigFile() must be called to set up the bunch of parameters from a config file (could be a memory-based virtual config file).
38  * - navigationStep() must be called periodically in order to effectively run the navigation. This method will internally call the callbacks to gather sensor data and robot positioning data.
39  *
40  * For working examples, refer to the source code of the apps:
41  * - [ReactiveNavigationDemo](http://www.mrpt.org/list-of-mrpt-apps/application-reactivenavigationdemo/)
42  * - [ReactiveNav3D-Demo](http://www.mrpt.org/list-of-mrpt-apps/application-reactivenav3d-demo/)
43  *
44  * Publications:
45  * - See derived classes for papers on each specific method.
46  *
47  * \sa CReactiveNavigationSystem, CReactiveNavigationSystem3D
48  * \ingroup mrpt_reactivenav_grp
49  */
51  {
52  public:
54 
55  /** The struct for configuring navigation requests to CAbstractPTGBasedReactive and derived classes. */
57  {
58  /** (Default=empty) Optionally, a list of PTG indices can be sent such that
59  * the navigator will restrict itself to only employ those PTGs. */
60  std::vector<size_t> restrict_PTG_indices;
61 
63  virtual ~TNavigationParamsPTG() { }
64  virtual std::string getAsText() const;
65  virtual TNavigationParams* clone() const { return new TNavigationParamsPTG(*this); }
66  };
67 
68 
69  /** Constructor.
70  * \param[in] react_iterf_impl An instance of an object that implement all the required interfaces to read from and control a robot.
71  * \param[in] enableConsoleOutput Can be set to false to reduce verbosity.
72  * \param[in] enableLogFile Set to true to enable creation of navigation log files, useful for inspection and debugging.
73  */
75  CReactiveInterfaceImplementation &react_iterf_impl,
76  bool enableConsoleOutput = true,
77  bool enableLogFile = false);
78 
79  virtual ~CAbstractPTGBasedReactive();
80 
81  /** Must be called for loading collision grids, or the first navigation command may last a long time to be executed.
82  * Internally, it just calls STEP1_CollisionGridsBuilder().
83  */
84  void initialize();
85 
86  /** Selects which one from the set of available holonomic methods will be used
87  * into transformed TP-Space, and sets its configuration from a configuration file.*/
88  void setHolonomicMethod(
89  THolonomicMethod method,
90  const mrpt::utils::CConfigFileBase & cfgBase);
91 
92  /** Just loads the holonomic method params from the given config source \sa setHolonomicMethod */
93  void loadHolonomicMethodConfig(
95  const std::string &section );
96 
97 
98  /** Start navigation:
99  * \param[in] params Pointer to structure with navigation info (its contents will be copied, so the original can be freely destroyed upon return.)
100  */
101  virtual void navigate( const TNavigationParams *params );
102 
103  /** Provides a copy of the last log record with information about execution.
104  * \param o An object where the log will be stored into.
105  * \note Log records are not prepared unless either "enableLogFile" is enabled in the constructor or "enableKeepLogRecords()" has been called.
106  */
107  void getLastLogRecord( CLogFileRecord &o );
108 
109  /** Enables keeping an internal registry of navigation logs that can be queried with getLastLogRecord() */
110  void enableKeepLogRecords(bool enable=true) { m_enableKeepLogRecords=enable; }
111 
112  /** Enables/disables saving log files. */
113  void enableLogFile(bool enable);
114 
115  /** Enables/disables the detailed time logger (default:disabled upon construction)
116  * When enabled, a report will be dumped to std::cout upon destruction.
117  * \sa getTimeLogger
118  */
119  void enableTimeLog(bool enable=true) {
120  MRPT_UNUSED_PARAM(enable);
121  m_timelogger.enable(true);
122  }
123 
124  /** Gives access to a const-ref to the internal time logger \sa enableTimeLog */
125  const mrpt::utils::CTimeLogger & getTimeLogger() const { return m_timelogger; }
126 
127  /** Returns the number of different PTGs that have been setup */
128  virtual size_t getPTG_count() const = 0;
129 
130  /** Gets the i'th PTG */
131  virtual CParameterizedTrajectoryGenerator* getPTG(size_t i) = 0;
132 
133  protected:
134  // ------------------------------------------------------
135  // INTERNAL DEFINITIONS
136  // ------------------------------------------------------
137  /** The structure used for storing a movement generated by a holonomic-method. */
139  CParameterizedTrajectoryGenerator *PTG; //!< The associated PTG
140  double direction, speed; //!< The holonomic movement
141  double evaluation; //!< An evaluation in the range [0,1] for the goodness of the movement
142 
143  THolonomicMovement() : PTG(NULL),direction(0),speed(0),evaluation(0) {}
144  };
145 
146  // ------------------------------------------------------
147  // PRIVATE METHODS
148  // ------------------------------------------------------
149  /** The main method for the navigator */
150  void performNavigationStep( );
151 
152  // ------------------------------------------------------
153  // PRIVATE VARIABLES
154  // ------------------------------------------------------
155  std::vector<CAbstractHolonomicReactiveMethod*> m_holonomicMethod; //!< The holonomic navigation algorithm (one object per PTG, so internal states are maintained)
156  mrpt::utils::CStream *m_logFile; //!< The current log file stream, or NULL if not being used
157  bool m_enableKeepLogRecords; //!< See enableKeepLogRecords
158  CLogFileRecord lastLogRecord; //!< The last log
159  float last_cmd_v,last_cmd_w, new_cmd_v, new_cmd_w; //!< Speed actual and last commands:
160  bool navigationEndEventSent; //!< Will be false until the navigation end is sent, and it is reset with each new command
161  synch::CCriticalSection m_critZoneLastLog,m_critZoneNavigating; //!< Critical zones
162 
163  bool m_enableConsoleOutput; //!< Enables / disables the console debug output.
164  bool m_init_done; //!< Whether \a loadConfigFile() has been called or not.
166 
167  // PTG params loaded from INI file:
168  std::string robotName; //!< Robot name
169  float refDistance; //!< "D_{max}" in papers.
170  float colGridRes; //!< CollisionGrid resolution
171  float robotMax_V_mps; //!< Max. linear speed (m/s)
172  float robotMax_W_degps; //!< Max. angular speed (deg/s)
173  float SPEEDFILTER_TAU; //!< Time constant for the low-pass filter applied to the speed commands
174  std::vector<float> weights; //!< length: 6 [0,5]
175 
176  /** In normalized distances, the start and end of a ramp function that scales the velocity
177  * output from the holonomic navigator:
178  *
179  * \code
180  * velocity scale
181  * ^
182  * | _____________
183  * | /
184  * 1 | /
185  * | /
186  * 0 +-------+---|----------------> normalized distance
187  * Start
188  * End
189  * \endcode
190  *
191  */
192  float secureDistanceStart,secureDistanceEnd;
193 
194 
196  float meanExecutionPeriod; //!< Runtime estimation of execution period of the method.
197  mrpt::utils::CTimeLogger m_timelogger; //!< A complete time logger \sa enableTimeLog()
198 
199  /** For sending an alarm (error event) when it seems that we are not approaching toward the target in a while... */
203 
205 
206  /** Stops the robot and set navigation state to error */
207  void doEmergencyStop( const char *msg );
208 
209  /** @name Variables for CReactiveNavigationSystem::performNavigationStep
210  @{ */
211  mrpt::utils::CTicTac totalExecutionTime, executionTime, tictac;
212  float meanExecutionTime, meanTotalExecutionTime;
213  /** @} */
214 
215  // Steps for the reactive navigation sytem.
216  // ----------------------------------------------------------------------------
217  virtual void STEP1_CollisionGridsBuilder() = 0;
218 
219  /** Return false on any fatal error */
220  virtual bool STEP2_SenseObstacles() = 0;
221 
222  /** Builds TP-Obstacles from Workspace obstacles for the given PTG.
223  * "out_TPObstacles" is already initialized to the proper length and maximum collision-free distance for each "k" trajectory index.
224  * Distances are in "pseudo-meters". They will be normalized automatically to [0,1] upon return. */
225  virtual void STEP3_WSpaceToTPSpace(const size_t ptg_idx,std::vector<float> &out_TPObstacles) = 0;
226 
227  /** Generates a pointcloud of obstacles, and the robot shape, to be saved in the logging record for the current timestep */
228  virtual void loggingGetWSObstaclesAndShape(CLogFileRecord &out_log) = 0;
229 
230 
231  /** Scores \a holonomicMovement */
232  void STEP5_PTGEvaluator(
233  THolonomicMovement & holonomicMovement,
234  const std::vector<float> & in_TPObstacles,
235  const mrpt::math::TPose2D & WS_Target,
236  const mrpt::math::TPoint2D & TP_Target,
238 
239  virtual void STEP7_GenerateSpeedCommands(const THolonomicMovement &in_movement);
240 
241 
242  void preDestructor(); //!< To be called during children destructors to assure thread-safe destruction, and free of shared objects.
243 
244 
245  private:
246  bool m_closing_navigator; //!< Signal that the destructor has been called, so no more calls are accepted from other threads
247 
248  struct TInfoPerPTG
249  {
250  bool valid_TP; //!< For each PTG, whether the target falls into the PTG domain.
251  mrpt::math::TPoint2D TP_Target; //!< The Target, in TP-Space (x,y)
252  float target_alpha,target_dist; //!< TP-Target
253  int target_k;
254 
255  std::vector<float> TP_Obstacles; //!< One distance per discretized alpha value, describing the "polar plot" of TP obstacles.
256  };
257 
258  std::vector<TInfoPerPTG> m_infoPerPTG; //!< Temporary buffers for working with each PTG during a navigationStep()
259 
260 
261  void deleteHolonomicObjects(); //!< Delete m_holonomicMethod
262 
263 
264  }; // end of CAbstractPTGBasedReactive
265  }
266 }
267 
268 
269 #endif
270 
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1...
Definition: datetime.h:30
float secureDistanceStart
In normalized distances, the start and end of a ramp function that scales the velocity output from th...
A class for storing, saving and loading a reactive navigation log record for the CReactiveNavigationS...
This class provides simple critical sections functionality.
The struct for configuring navigation requests to CAbstractPTGBasedReactive and derived classes...
std::vector< float > weights
length: 6 [0,5]
The pure virtual class that a user of CAbstractReactiveNavigationSystem-derived classes must implemen...
The structure used to store all relevant information about each transformation into TP-Space...
#define MRPT_MAKE_ALIGNED_OPERATOR_NEW
Definition: memory.h:112
Base class for reactive navigator systems based on TP-Space, with an arbitrary holonomic reactive met...
bool m_closing_navigator
Signal that the destructor has been called, so no more calls are accepted from other threads...
void enableKeepLogRecords(bool enable=true)
Enables keeping an internal registry of navigation logs that can be queried with getLastLogRecord() ...
bool m_init_done
Whether loadConfigFile() has been called or not.
double evaluation
An evaluation in the range [0,1] for the goodness of the movement.
std::vector< float > TP_Obstacles
One distance per discretized alpha value, describing the "polar plot" of TP obstacles.
mrpt::utils::CStream * m_logFile
The current log file stream, or NULL if not being used.
CParameterizedTrajectoryGenerator * PTG
The associated PTG.
std::vector< size_t > restrict_PTG_indices
(Default=empty) Optionally, a list of PTG indices can be sent such that the navigator will restrict i...
This namespace contains algorithms for SLAM, localization, map building, representation of robot's ac...
std::vector< CAbstractHolonomicReactiveMethod * > m_holonomicMethod
The holonomic navigation algorithm (one object per PTG, so internal states are maintained) ...
This is the base class for any user-defined PTG.
This class allows loading and storing values and vectors of different types from a configuration text...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
float badNavAlarm_minDistTarget
For sending an alarm (error event) when it seems that we are not approaching toward the target in a w...
bool valid_TP
For each PTG, whether the target falls into the PTG domain.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
synch::CCriticalSection m_critZoneNavigating
Critical zones.
This class implements a high-performance stopwatch.
Definition: CTicTac.h:24
float SPEEDFILTER_TAU
Time constant for the low-pass filter applied to the speed commands.
The structure used for storing a movement generated by a holonomic-method.
mrpt::utils::CTimeLogger m_timelogger
A complete time logger.
const mrpt::utils::CTimeLogger & getTimeLogger() const
Gives access to a const-ref to the internal time logger.
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
std::vector< TInfoPerPTG > m_infoPerPTG
Temporary buffers for working with each PTG during a navigationStep()
bool m_enableConsoleOutput
Enables / disables the console debug output.
float new_cmd_w
Speed actual and last commands:
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
THolonomicMethod
The implemented reactive navigation methods.
Lightweight 2D pose.
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
Definition: CTimeLogger.h:35
float meanExecutionPeriod
Runtime estimation of execution period of the method.
bool navigationEndEventSent
Will be false until the navigation end is sent, and it is reset with each new command.
This is the base class for any reactive navigation system.
Lightweight 2D point.
mrpt::math::TPoint2D TP_Target
The Target, in TP-Space (x,y)
void enableTimeLog(bool enable=true)
Enables/disables the detailed time logger (default:disabled upon construction) When enabled...



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014