Main MRPT website > C++ reference
MRPT logo
CPRRTNavigator.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CPRRTNavigator_H
29 #define CPRRTNavigator_H
30 
31 #include <mrpt/maps.h>
32 #include <mrpt/poses.h>
33 #include <mrpt/synch.h>
34 #include <mrpt/system/threads.h>
36 
38 
39 namespace mrpt
40 {
41  namespace reactivenav
42  {
43  using namespace mrpt;
44  using namespace mrpt::slam;
45  using namespace mrpt::math;
46  using namespace mrpt::synch;
47  using namespace mrpt::poses;
48 
49  /** This class is a multi-threaded mobile robot navigator, implementing an (anytime) PTG-based Rapidly-exploring Random Tree (PRRT) search algorithm.
50  *
51  * <b>Usage:</b><br>
52  * - Create an instance of the CPRRTNavigator class (an object on the heap, i.e. no 'new', is preferred, but just for the convenience of the user).
53  * - Set all the parameters in CPRRTNavigator::params
54  * - Call CPRRTNavigator::initialize. If all the params are OK, true is returned and you can go on.
55  * - Start a navigation by calling CPRRTNavigator::navigate.
56  * - From your application, you must update all the sensory data (from the real robot or a simulator) on a timely-fashion. The navigator will stop the robot if the last sensory data is too old.
57  *
58  * Note that all the public methods are thread-safe.
59  *
60  * <b>About the algorithm:</b><br>
61  *
62  *
63  *
64  *
65  *
66  * <b>Changes history</b>
67  * - 05/JUL/2009: Creation (JLBC). This is an evolution from leassons learnt from the pure reactive navigator based on PTGs.
68  * \ingroup mrpt_reactivenav_grp
69  */
71  {
72  public:
73  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
74 
75  public:
76  CPRRTNavigator(); //!< Constructor
77  virtual ~CPRRTNavigator(); //!< Destructor
78 
79  /** @name Navigation control methods
80  @{*/
81  /** Initialize the navigator using the parameters in "params"; read the usage instructions in CPRRTNavigator.
82  * This method should be called only once at the beginning of the robot operation.
83  * \return true on sucess, false on any error preparing the navigator (and no navigation command will be processed until that's fixed).
84  */
85  bool initialize();
86 
87  /** Starts a navigation to a 2D pose (including a desired heading at the target).
88  * \note CPRRTNavigator::initialize must be called first.
89  */
90  void navigate( const mrpt::math::TPose2D &target_pose);
91 
92  /** Starts a navigation to a given 2D point (any heading at the target).
93  * \note CPRRTNavigator::initialize must be called first.
94  */
95  void navigate( const mrpt::math::TPoint2D &target_point);
96 
97  /** Cancel current navegacion.
98  * \note CPRRTNavigator::initialize must be called first.
99  */
100  void cancel();
101 
102  /** Continues with suspended navigation.
103  * \sa suspend
104  * \note CPRRTNavigator::initialize must be called first.
105  */
106  void resume();
107 
108  /** Suspend current navegation
109  * \sa resume
110  * \note CPRRTNavigator::initialize must be called first.
111  */
112  void suspend();
113 
114  /** @} */
115 
116  /** @name Navigator data structures
117  @{*/
118  /** Each data point in m_planned_path */
120  {
122  p(0,0,0),
123  max_v(0.1),max_w(0.2),
124  trg_v(0.1)
125  {}
126 
127  TPose2D p; //!< Coordinates are "global"
128  double max_v, max_w; //!< Maximum velocities along this path segment.
129  double trg_v; //!< Desired linear velocity at the target point, ie: the robot should program its velocities such as after this arc the speeds are the given ones.
130  };
131 
132  typedef std::list<TPathData> TPlannedPath; //!< An ordered list of path poses.
133 
135  {
136  public:
137  TOptions(); //!< Initial values
138 
139  /** This method load the options from a ".ini"-like file or memory-stored string list.
140  */
141  void loadFromConfigFile(
142  const mrpt::utils::CConfigFileBase &source,
143  const std::string &section);
144 
145  /** This method displays clearly all the contents of the structure in textual form, sending it to a CStream. */
146  void dumpToTextStream( CStream &out) const;
147 
148  // Data:
149  double absolute_max_v; //!< Max linear speed (m/s)
150  double absolute_max_w; //!< Max angular speed (rad/s)
151  double max_accel_v; //!< Max desired linear speed acceleration (m/s^2)
152  double max_accel_w; //!< Max desired angular speed acceleration (rad/s^2)
153 
154  double max_age_observations; //!< Max age (in seconds) for an observation to be considered invalid for navigation purposes.
155 
156  /** The robot shape used when computing collisions; it's loaded from the
157  * config file/text as a single 2xN matrix in MATLAB format, first row are Xs, second are Ys, e.g:
158  * \code
159  * robot_shape = [-0.2 0.2 0.2 -0.2; -0.1 -0.1 0.1 0.1]
160  * \endcode
161  */
163 
165  {
166  double radius_checkpoints; //!< Radius of each checkpoint in the path, ie: when the robot get closer than this distance, the point is considered as visited and the next one is processed.
167  };
169 
171  {
172  double max_time_expend_planning; //!< Maximum time to spend when planning, in seconds.
173  };
175 
176  };
177 
178  TOptions params; //!< Change here all the parameters of the navigator.
179 
180  /** @} */
181 
182  /** @name Debug and logging
183  @{*/
184  /** Manually sets the short-time path to be followed by the robot (use 'navigate' instead, this method is for debugging mainly)
185  */
186  void setPathToFollow(const TPlannedPath &path );
187 
188  /** Returns the current planned path the robot is following */
189  void getCurrentPlannedPath(TPlannedPath &path ) const;
190 
191 
192  /** @} */
193 
194 
195  /** @name Sensory data methods: call them to update the navigator knowledge on the outside world.
196  @{*/
197  /** Updates the navigator with a low or high-rate estimate from a localization (or SLAM) algorithm running somewhere else.
198  * \param new_robot_pose The (global) coordinates of the mean robot pose as estimated by some external module.
199  * \param new_robot_cov The 3x3 covariance matrix of that estimate.
200  * \param timestamp The associated timestamp of the data.
201  */
202  void processNewLocalization(const TPose2D &new_robot_pose, const CMatrixDouble33 &new_robot_cov, TTimeStamp timestamp );
203 
204  /** Updates the navigator with high-rate odometry from the mobile base.
205  * The odometry poses are dealed internally as increments only, so there is no problem is
206  * arbitrary mismatches between localization (from a particle filter or SLAM) and the odometry.
207  * \param newOdoPose The global, cummulative odometry as computed by the robot.
208  * \param timestamp The associated timestamp of the measurement.
209  * \param hasVelocities If false, the next arguments are ignored.
210  * \param v Measured linear speed, in m/s.
211  * \param w Measured angular speed, in rad/s.
212  */
213  void processNewOdometryInfo( const TPose2D &newOdoPose, TTimeStamp timestamp, bool hasVelocities =false, float v =0, float w=0 );
214 
215  /** Must be called in a timely fashion to let the navigator know about the obstacles in the environment.
216  * \param obstacles
217  * \param timestamp The associated timestamp of the sensed points.
218  */
219  void processNewObstaclesData(const mrpt::slam::CPointsMap* obstacles, TTimeStamp timestamp );
220 
221  /** @} */
222 
223  /** @name Virtual methods to be implemented by the user.
224  @{*/
225  /** This is the most important method the user must provide: to send an instantaneous velocity command to the robot.
226  * \param v Desired linear speed, in meters/second.
227  * \param w Desired angular speed, in rads/second.
228  * \return false on any error. In that case, the navigator will immediately stop the navigation and announce the error.
229  */
230  virtual bool onMotionCommand(float v, float w ) = 0;
231 
232  /** Re-implement this method if you want to know when a new navigation has started.
233  */
234  virtual void onNavigationStart( ) { }
235 
236  /** Re-implement this method if you want to know when and why a navigation has ended.
237  * \param targetReachedOK Will be false if the navigation failed.
238  */
239  virtual void onNavigationEnd( bool targetReachedOK ) { }
240 
241  /** Re-implement this method if you want to know when the robot is approaching the target:
242  * this event is raised before onNavigationEnd, when the robot is closer than a certain distance to the target.
243  */
244  virtual void onApproachingTarget( ) { }
245 
246  /** @} */
247 
248  static const double INVALID_HEADING; //!< Used to refer to undefined or "never mind" headings values.
249 
250  private:
251  // ----------- Internal methods & threads -----------
252 
253  TThreadHandle m_thr_planner; //!< Thread handle
254  TThreadHandle m_thr_testcol; //!< Thread handle
255  TThreadHandle m_thr_pathtrack; //!< Thread handle
256 
257  void thread_planner(); //!< Thread function
258  void thread_test_collision(); //!< Thread function
259  void thread_path_tracking(); //!< Thread function
260 
261 
262  // ----------- Internal data -----------
263 
264  bool m_initialized; //!< The object is ready to navigate. Users must call "initialize" first.
265  bool m_closingThreads; //!< set to true at destructor.
266 
267  TPose2D m_target_pose; //!< Heading may be INVALID_HEADING to indicate "don't mind"
270 
271  // Instead of a CSimplePointsMap, just store the X & Y vectors, since
272  // this is a temporary variable. We'll let the planning thread to build
273  // a CSimplePointsMap object with these points.
274  //mrpt::slam::CSimplePointsMap m_last_obstacles;
275  std::vector<float> m_last_obstacles_x,m_last_obstacles_y;
278 
279  // The planned path, to be followed:
281  TTimeStamp m_planned_path_time; //!< The last modification time. INVALID_TIMESTAMP means there is no path.
283 
284  /** The list of PTGs used by the anytime planner to explore the free-space. */
287 
288  public:
289 
290  mrpt::poses::CRobot2DPoseEstimator m_robotStateFilter; //!< Object maintained by the robot-tracking thread (All methods are thread-safe).
291 
292 
293 
294  };
295  }
296 }
297 
298 
299 #endif
300 



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013