00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2010 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef CPRRTNavigator_H 00029 #define CPRRTNavigator_H 00030 00031 #include <mrpt/maps.h> 00032 #include <mrpt/poses.h> 00033 #include <mrpt/synch.h> 00034 #include <mrpt/system/threads.h> 00035 #include <mrpt/reactivenav/CParameterizedTrajectoryGenerator.h> 00036 00037 #include <mrpt/reactivenav/link_pragmas.h> 00038 00039 namespace mrpt 00040 { 00041 namespace reactivenav 00042 { 00043 using namespace mrpt; 00044 using namespace mrpt::slam; 00045 using namespace mrpt::math; 00046 using namespace mrpt::synch; 00047 using namespace mrpt::poses; 00048 00049 /** This class is a multi-threaded mobile robot navigator, implementing an (anytime) PTG-based Rapidly-exploring Random Tree (PRRT) search algorithm. 00050 * 00051 * <b>Usage:</b><br> 00052 * - 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). 00053 * - Set all the parameters in CPRRTNavigator::params 00054 * - Call CPRRTNavigator::initialize. If all the params are OK, true is returned and you can go on. 00055 * - Start a navigation by calling CPRRTNavigator::navigate. 00056 * - 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. 00057 * 00058 * Note that all the public methods are thread-safe. 00059 * 00060 * <b>About the algorithm:</b><br> 00061 * 00062 * 00063 * 00064 * 00065 * 00066 * <b>Changes history</b> 00067 * - 05/JUL/2009: Creation (JLBC). This is an evolution from leassons learnt from the pure reactive navigator based on PTGs. 00068 */ 00069 class REACTIVENAV_IMPEXP CPRRTNavigator 00070 { 00071 public: 00072 CPRRTNavigator(); //!< Constructor 00073 virtual ~CPRRTNavigator(); //!< Destructor 00074 00075 /** @name Navigation control methods 00076 @{*/ 00077 /** Initialize the navigator using the parameters in "params"; read the usage instructions in CPRRTNavigator. 00078 * This method should be called only once at the beginning of the robot operation. 00079 * \return true on sucess, false on any error preparing the navigator (and no navigation command will be processed until that's fixed). 00080 */ 00081 bool initialize(); 00082 00083 /** Starts a navigation to a 2D pose (including a desired heading at the target). 00084 * \note CPRRTNavigator::initialize must be called first. 00085 */ 00086 void navigate( const mrpt::math::TPose2D &target_pose); 00087 00088 /** Starts a navigation to a given 2D point (any heading at the target). 00089 * \note CPRRTNavigator::initialize must be called first. 00090 */ 00091 void navigate( const mrpt::math::TPoint2D &target_point); 00092 00093 /** Cancel current navegacion. 00094 * \note CPRRTNavigator::initialize must be called first. 00095 */ 00096 void cancel(); 00097 00098 /** Continues with suspended navigation. 00099 * \sa suspend 00100 * \note CPRRTNavigator::initialize must be called first. 00101 */ 00102 void resume(); 00103 00104 /** Suspend current navegation 00105 * \sa resume 00106 * \note CPRRTNavigator::initialize must be called first. 00107 */ 00108 void suspend(); 00109 00110 /** @} */ 00111 00112 /** @name Navigator data structures 00113 @{*/ 00114 /** Each data point in m_planned_path */ 00115 struct REACTIVENAV_IMPEXP TPathData 00116 { 00117 TPathData() : 00118 p(0,0,0), 00119 max_v(0.1),max_w(0.2), 00120 trg_v(0.1) 00121 {} 00122 00123 TPose2D p; //!< Coordinates are "global" 00124 double max_v, max_w; //!< Maximum velocities along this path segment. 00125 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. 00126 }; 00127 00128 typedef std::list<TPathData> TPlannedPath; //!< An ordered list of path poses. 00129 00130 class REACTIVENAV_IMPEXP TOptions : public mrpt::utils::CLoadableOptions 00131 { 00132 public: 00133 TOptions(); //!< Initial values 00134 00135 /** This method load the options from a ".ini"-like file or memory-stored string list. 00136 */ 00137 void loadFromConfigFile( 00138 const mrpt::utils::CConfigFileBase &source, 00139 const std::string §ion); 00140 00141 /** This method displays clearly all the contents of the structure in textual form, sending it to a CStream. */ 00142 void dumpToTextStream( CStream &out) const; 00143 00144 // Data: 00145 double absolute_max_v; //!< Max linear speed (m/s) 00146 double absolute_max_w; //!< Max angular speed (rad/s) 00147 double max_accel_v; //!< Max desired linear speed acceleration (m/s^2) 00148 double max_accel_w; //!< Max desired angular speed acceleration (rad/s^2) 00149 00150 double max_age_observations; //!< Max age (in seconds) for an observation to be considered invalid for navigation purposes. 00151 00152 /** The robot shape used when computing collisions; it's loaded from the 00153 * config file/text as a single 2xN matrix in MATLAB format, first row are Xs, second are Ys, e.g: 00154 * \code 00155 * robot_shape = [-0.2 0.2 0.2 -0.2; -0.1 -0.1 0.1 0.1] 00156 * \endcode 00157 */ 00158 TPolygon2D robot_shape; 00159 00160 struct REACTIVENAV_IMPEXP TPathTrackingOpts 00161 { 00162 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. 00163 }; 00164 TPathTrackingOpts pathtrack; 00165 00166 struct REACTIVENAV_IMPEXP TPlannerOpts 00167 { 00168 double max_time_expend_planning; //!< Maximum time to spend when planning, in seconds. 00169 }; 00170 TPlannerOpts planner; 00171 00172 }; 00173 00174 TOptions params; //!< Change here all the parameters of the navigator. 00175 00176 /** @} */ 00177 00178 /** @name Debug and logging 00179 @{*/ 00180 /** Manually sets the short-time path to be followed by the robot (use 'navigate' instead, this method is for debugging mainly) 00181 */ 00182 void setPathToFollow(const TPlannedPath &path ); 00183 00184 /** Returns the current planned path the robot is following */ 00185 void getCurrentPlannedPath(TPlannedPath &path ) const; 00186 00187 00188 /** @} */ 00189 00190 00191 /** @name Sensory data methods: call them to update the navigator knowledge on the outside world. 00192 @{*/ 00193 /** Updates the navigator with a low or high-rate estimate from a localization (or SLAM) algorithm running somewhere else. 00194 * \param new_robot_pose The (global) coordinates of the mean robot pose as estimated by some external module. 00195 * \param new_robot_cov The 3x3 covariance matrix of that estimate. 00196 * \param timestamp The associated timestamp of the data. 00197 */ 00198 void processNewLocalization(const TPose2D &new_robot_pose, const CMatrixDouble33 &new_robot_cov, TTimeStamp timestamp ); 00199 00200 /** Updates the navigator with high-rate odometry from the mobile base. 00201 * The odometry poses are dealed internally as increments only, so there is no problem is 00202 * arbitrary mismatches between localization (from a particle filter or SLAM) and the odometry. 00203 * \param newOdoPose The global, cummulative odometry as computed by the robot. 00204 * \param timestamp The associated timestamp of the measurement. 00205 * \param hasVelocities If false, the next arguments are ignored. 00206 * \param v Measured linear speed, in m/s. 00207 * \param w Measured angular speed, in rad/s. 00208 */ 00209 void processNewOdometryInfo( const TPose2D &newOdoPose, TTimeStamp timestamp, bool hasVelocities =false, float v =0, float w=0 ); 00210 00211 /** Must be called in a timely fashion to let the navigator know about the obstacles in the environment. 00212 * \param obstacles 00213 * \param timestamp The associated timestamp of the sensed points. 00214 */ 00215 void processNewObstaclesData(const mrpt::slam::CPointsMap* obstacles, TTimeStamp timestamp ); 00216 00217 /** @} */ 00218 00219 /** @name Virtual methods to be implemented by the user. 00220 @{*/ 00221 /** This is the most important method the user must provide: to send an instantaneous velocity command to the robot. 00222 * \param v Desired linear speed, in meters/second. 00223 * \param w Desired angular speed, in rads/second. 00224 * \return false on any error. In that case, the navigator will immediately stop the navigation and announce the error. 00225 */ 00226 virtual bool onMotionCommand(float v, float w ) = 0; 00227 00228 /** Re-implement this method if you want to know when a new navigation has started. 00229 */ 00230 virtual void onNavigationStart( ) { } 00231 00232 /** Re-implement this method if you want to know when and why a navigation has ended. 00233 * \param targetReachedOK Will be false if the navigation failed. 00234 */ 00235 virtual void onNavigationEnd( bool targetReachedOK ) { } 00236 00237 /** Re-implement this method if you want to know when the robot is approaching the target: 00238 * this event is raised before onNavigationEnd, when the robot is closer than a certain distance to the target. 00239 */ 00240 virtual void onApproachingTarget( ) { } 00241 00242 /** @} */ 00243 00244 static const double INVALID_HEADING; //!< Used to refer to undefined or "never mind" headings values. 00245 00246 private: 00247 // ----------- Internal methods & threads ----------- 00248 00249 TThreadHandle m_thr_planner; //!< Thread handle 00250 TThreadHandle m_thr_testcol; //!< Thread handle 00251 TThreadHandle m_thr_pathtrack; //!< Thread handle 00252 00253 void thread_planner(); //!< Thread function 00254 void thread_test_collision(); //!< Thread function 00255 void thread_path_tracking(); //!< Thread function 00256 00257 00258 // ----------- Internal data ----------- 00259 00260 bool m_initialized; //!< The object is ready to navigate. Users must call "initialize" first. 00261 bool m_closingThreads; //!< set to true at destructor. 00262 00263 TPose2D m_target_pose; //!< Heading may be INVALID_HEADING to indicate "don't mind" 00264 TTimeStamp m_target_pose_time; 00265 CCriticalSection m_target_pose_cs; 00266 00267 // Instead of a CSimplePointsMap, just store the X & Y vectors, since 00268 // this is a temporary variable. We'll let the planning thread to build 00269 // a CSimplePointsMap object with these points. 00270 //mrpt::slam::CSimplePointsMap m_last_obstacles; 00271 vector_float m_last_obstacles_x,m_last_obstacles_y; 00272 TTimeStamp m_last_obstacles_time; 00273 CCriticalSection m_last_obstacles_cs; 00274 00275 // The planned path, to be followed: 00276 CCriticalSection m_planned_path_cs; 00277 TTimeStamp m_planned_path_time; //!< The last modification time. INVALID_TIMESTAMP means there is no path. 00278 TPlannedPath m_planned_path; 00279 00280 /** The list of PTGs used by the anytime planner to explore the free-space. */ 00281 TListPTGs m_PTGs; 00282 CCriticalSection m_PTGs_cs; 00283 00284 public: 00285 00286 mrpt::poses::CRobot2DPoseEstimator m_robotStateFilter; //!< Object maintained by the robot-tracking thread (All methods are thread-safe). 00287 00288 00289 00290 }; 00291 } 00292 } 00293 00294 00295 #endif 00296
| Page generated by Doxygen 1.7.1 for MRPT 0.9.1 SVN:exported at Fri Aug 6 10:31:35 UTC 2010 |
