RTAB-Map 0.23.10
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
RTAB-Map C++ API

RTAB-Map (Real-Time Appearance-Based Mapping) is a RGB-D, stereo and lidar graph-based SLAM library built around an incremental appearance-based loop closure detector, with memory management that keeps the online constraints satisfiable on large-scale, long-term maps.

These pages document the public C++ API of the rtabmap_core and rtabmap_utilite libraries. For installation, tutorials and the ROS packages, see the project website and the wiki.

Start here

rtabmap::Rtabmap is the entry point: it owns the map and runs one full SLAM iteration per call to rtabmap::Rtabmap::process(). A minimal loop feeds it a rtabmap::SensorData and the odometry pose that goes with it:

#include <rtabmap/core/Rtabmap.h>
#include <rtabmap/core/Odometry.h>
rtabmap.init(); // optionally: init(parameters, databasePath)
const double mapUpdateRate = 1.0; // Hz, i.e. Rtabmap/DetectionRate
double lastProcessStamp = -1.0;
while(/* frames available */)
{
rtabmap::SensorData data = camera.takeImage();
// Odometry sees every frame: dropping any would break the motion tracking.
rtabmap::Transform odomPose = odometry->process(data);
// The map is updated at a lower rate. The frames skipped here are not lost
// work: the motion they carry is already integrated in the pose above, so
// the next accepted frame arrives with an up-to-date odometry pose.
if(lastProcessStamp < 0.0 ||
data.stamp() - lastProcessStamp >= 1.0/mapUpdateRate)
{
lastProcessStamp = data.stamp();
if(rtabmap.process(data, odomPose)) // true when a new node was added
{
const rtabmap::Statistics & stats = rtabmap.getStatistics();
// A loop closure or a proximity detection re-optimizes the graph,
// which shifts the map frame under the odometry frame.
if(!stats.mapCorrection().isNull())
{
mapToOdom = stats.mapCorrection();
}
if(rtabmap.getLoopClosureId() > 0)
{
// a loop closure was accepted on this iteration
}
}
}
// Robot pose in the map frame, on every frame and always with the matching
// correction: composed after the block above, so an iteration that just
// re-optimized the graph uses its new correction rather than the previous
// one. In ROS terms: (/map -> /odom) * (/odom -> /base_link).
rtabmap::Transform mapPose = mapToOdom * odomPose;
}
Abstract base class for visual, lidar and visual-inertial odometry backends.
Definition Odometry.h:57
Transform process(SensorData &data, OdometryInfo *info=0)
Processes a sensor frame and updates the integrated pose.
static Odometry * create(const ParametersMap &parameters=ParametersMap())
Creates an odometry instance from Parameters::kOdomStrategy() in parameters.
Top-level RTAB-Map SLAM pipeline (mapping, localization and loop closure).
Definition Rtabmap.h:194
Container class for all sensor data captured at a specific time.
Definition SensorData.h:97
double stamp() const
Returns the timestamp.
Definition SensorData.h:527
Collects and manages runtime statistics for RTAB-Map.
Definition Statistics.h:108
const Transform & mapCorrection() const
Returns the map correction transform.
Definition Statistics.h:657
Represents a 3D rigid body transformation (rotation + translation).
Definition Transform.h:53
bool isNull() const
Checks whether the transform is null (all zeros).
static Transform getIdentity()
Returns identity transform.

Throttling is the caller's job here: rtabmap::Rtabmap::process() maps every frame it is given. rtabmap::RtabmapThread does this same stamp comparison internally, from Rtabmap/DetectionRate, so the threaded pipeline only needs the parameter to be set.

Odometry drifts and the graph gets re-optimized, so the odometry pose is not a map pose. rtabmap::Statistics::mapCorrection() is what reconciles the two, and since it only changes on a map update it can be applied to every incoming frame – which is how the pose stays available at full rate while the map is built at 1 Hz. This is the transform published as /map/odom by the ROS wrapper, and what the MapBuilder of each example composes with the live odometry pose to place the clouds. It is also readable outside the statistics, as rtabmap::Rtabmap::getMapCorrection().

Complete programs live under examples/ in the source tree:

Example What it shows
BOWMapping The smallest useful loop: images from disk into rtabmap::Rtabmap, appearance-only loop closure detection (no odometry, no GUI)
NoEventsExample Driving the pipeline by direct calls – camera, rtabmap::Odometry and rtabmap::Rtabmap in one explicit loop, without the event system
RGBDMapping The threaded event-based pipeline (rtabmap::SensorCaptureThreadrtabmap::OdometryThreadrtabmap::RtabmapThread) with any supported RGB-D or stereo camera
LidarMapping The same threaded pipeline driven by a 3D lidar (rtabmap::LidarVLP16) instead of a camera

What each iteration does, and which parameters influence it, is documented on rtabmap::Rtabmap itself – memory update, loop-closure hypothesis, hypothesis selection, retrieval, proximity detection and transfer to long-term memory.

Occupancy grid

With RGBD/CreateOccupancyGrid enabled, every node carries a local occupancy grid computed from its depth images or laser scan (rtabmap::LocalGridMaker). Assembling those into a global grid is left to the caller, so that the result always follows the optimized poses:

#include <rtabmap/core/global_map/OccupancyGrid.h>
rtabmap::OccupancyGrid grid(&localGrids, parameters); // reads the Grid/... parameters
// ... inside the "if(rtabmap.process(data, odomPose))" block of the loop above:
if(node.sensorData().gridCellSize() > 0.0f &&
grid.addedNodes().find(node.id()) == grid.addedNodes().end())
{
// Local grid of the new node, as stored in the database (compressed).
cv::Mat ground, obstacles, empty;
node.sensorData().uncompressDataConst(0, 0, 0, 0, &ground, &obstacles, &empty);
localGrids.add(node.id(), ground, obstacles, empty,
}
// Draws the nodes that are not assembled yet. If the last optimization moved
// poses by more than GridGlobal/UpdateError, the grid is cleared first and
// redrawn entirely from the cache -- which is why the cache is kept around.
grid.update(stats.poses());
float xMin, yMin; // grid origin (m), in the map frame
cv::Mat map = grid.getMap(xMin, yMin); // CV_8S: -1 unknown, 0 free, 100 occupied
Cache of LocalGrid entries keyed by map node id.
Definition LocalGrid.h:98
void add(int nodeId, const cv::Mat &ground, const cv::Mat &obstacles, const cv::Mat &empty, float cellSize, const cv::Point3f &viewPoint=cv::Point3f(0, 0, 0))
Inserts or replaces the grid for nodeId (from separate cell mats).
void uncompressDataConst(cv::Mat *imageRaw, cv::Mat *depthOrRightRaw, LaserScan *laserScanRaw=0, cv::Mat *userDataRaw=0, cv::Mat *groundCellsRaw=0, cv::Mat *obstacleCellsRaw=0, cv::Mat *emptyCellsRaw=0, cv::Mat *depthConfidenceRaw=0) const
Uncompresses compressed data into provided output buffers (const version)
const cv::Point3f & gridViewPoint() const
Returns the occupancy grid viewpoint.
Definition SensorData.h:812
float gridCellSize() const
Returns the occupancy grid cell size.
Definition SensorData.h:806
Represents a node in RTAB-Map's pose graph.
Definition Signature.h:84
SensorData & sensorData()
Returns mutable access to the sensor data.
Definition Signature.h:597
int id() const
Returns the signature ID.
Definition Signature.h:168
const Signature & getLastSignatureData() const
Returns the last signature data.
Definition Statistics.h:630
const std::map< int, Transform > & poses() const
Returns the pose graph.
Definition Statistics.h:642

For the node that was just added, the cells are already there uncompressed, and rtabmap::SensorData::uncompressDataConst() returns them as they are – it only decompresses what comes back empty, which is what makes the same code work for a node retrieved from the database. The occupancy grid is kept on the published copy even with Rtabmap/PublishLastSignature disabled (only images, scans and user data are dropped), precisely so that the global grid can still be assembled; statistics themselves must be published (Rtabmap/PublishStats, on by default). rtabmap::OccupancyGrid is one of the rtabmap::GlobalMap back-ends: rtabmap::OctoMap, rtabmap::CloudMap and rtabmap::GridMap consume the same cache the same way. Each example's MapBuilder does exactly this, then hands the result to the viewer.

For a 3D map, rtabmap::CloudMap assembles the very same cells into PCL clouds instead of a 2D grid. It shares the cache, so both can be kept up to date from one set of local grids:

#include <rtabmap/core/global_map/CloudMap.h>
#include <pcl/io/pcd_io.h>
rtabmap::CloudMap cloudMap(&localGrids, parameters); // same cache as above
// ... right after the localGrids.add() of the block above:
cloudMap.update(stats.poses());
pcl::PointCloud<pcl::PointXYZRGB>::Ptr ground = cloudMap.getMapGround();
pcl::PointCloud<pcl::PointXYZRGB>::Ptr obstacles = cloudMap.getMapObstacles();
pcl::PointCloud<pcl::PointXYZ>::Ptr emptySpace = cloudMap.getMapEmptyCells();
pcl::io::savePCDFileBinary("obstacles.pcd", *obstacles);

The clouds are in the map frame and voxelized at Grid/CellSize. Points keep the colour of the local grid when it has one, otherwise ground is green and obstacles red. Note that this assembles the cells, not the raw sensor clouds: with Grid/3D disabled they are flattened onto the xy plane, so it must stay enabled for a 3D result.

For a full-resolution cloud, assemble the nodes themselves rather than their cells. Ask rtabmap::Rtabmap::getGraph() for the optimized poses along with the node data, then rebuild a cloud per node and transform it to its pose:

#include <rtabmap/core/util3d.h>
#include <rtabmap/core/util3d_filtering.h>
#include <rtabmap/core/util3d_transforms.h>
std::map<int, rtabmap::Transform> poses;
std::multimap<int, rtabmap::Link> links;
std::map<int, rtabmap::Signature> nodes;
rtabmap.getGraph(poses, links, true, true, &nodes, true); // optimized, global, with images
pcl::PointCloud<pcl::PointXYZRGB>::Ptr assembled(new pcl::PointCloud<pcl::PointXYZRGB>);
for(std::map<int, rtabmap::Transform>::const_iterator iter=poses.begin(); iter!=poses.end(); ++iter)
{
rtabmap::SensorData data = nodes.at(iter->first).sensorData();
pcl::IndicesPtr indices(new std::vector<int>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud = rtabmap::util3d::cloudRGBFromSensorData(
data,
4, // image decimation
4.0f, // max depth (m), 0 = no limit
0.0f, // min depth (m)
indices.get());
cloud = rtabmap::util3d::voxelize(cloud, indices, 0.01f); // 1 cm
*assembled += *rtabmap::util3d::transformPointCloud(cloud, iter->second);
}
assembled = rtabmap::util3d::voxelize(assembled, 0.01f); // one last pass over the overlaps
pcl::io::savePCDFileBinary("cloud.pcd", *assembled);
void uncompressData()
Uncompresses all compressed data in-place.
pcl::PointCloud< pcl::PointXYZ >::Ptr RTABMAP_CORE_EXPORT transformPointCloud(const pcl::PointCloud< pcl::PointXYZ >::Ptr &cloud, const Transform &transform)
Transforms pcl::PointXYZ point cloud type.
pcl::PointCloud< pcl::PointXYZ >::Ptr RTABMAP_CORE_EXPORT voxelize(const pcl::PointCloud< pcl::PointXYZ >::Ptr &cloud, const pcl::IndicesPtr &indices, float voxelSize)
Performs voxel grid downsampling on a point cloud of type pcl::PointXYZ on provided indices.
pcl::PointCloud< pcl::PointXYZRGB >::Ptr RTABMAP_CORE_EXPORT cloudRGBFromSensorData(const SensorData &sensorData, int decimation=1, float maxDepth=0.0f, float minDepth=0.0f, std::vector< int > *validIndices=0, const ParametersMap &stereoParameters=ParametersMap(), const std::vector< float > &roiRatios=std::vector< float >(), unsigned char confidenceThr=0)
Generates a point cloud of type pcl::PointXYZRGB from sensor data.
Note
Do this once the session is over, not on every iteration. It decompresses and re-projects every node, so the cost grows with the whole map, and the poses are only worth exporting once the graph has been optimized – the same cloud assembled mid-session would carry the drift that later loop closures correct. This is what rtabmap-export does, with more filtering options.

Memory management

RTAB-Map keeps the map in three tiers (rtabmap::Memory): a short-term memory of the last Mem/STMSize nodes, where neighbours are too similar to be loop closure candidates; a working memory holding everything loop closure detection compares against; and a long-term memory, the part of the map that stays in the database and is not searched.

By default nothing leaves the working memory, so the iteration time grows with the map. Memory management caps it, and is enabled by setting a budget – either one, or both:

// Keep each update under 700 ms...
// ... and/or keep at most 500 nodes in the working memory.
rtabmap.init(parameters, "map.db");
static std::string kRtabmapMemoryThr()
Key of parameter Rtabmap/MemoryThr : uFormat("Maximum nodes in the Working Memory (0 means infinity)....
Definition Parameters.h:193
static std::string kRtabmapTimeThr()
Key of parameter Rtabmap/TimeThr : "Maximum time allowed for map update (ms) (0 means infinity)....
Definition Parameters.h:192
std::pair< std::string, std::string > ParametersPair
A single parameter key/value pair, the entry type of ParametersMap.
Definition Parameters.h:46
std::map< std::string, std::string > ParametersMap
Parameter keys mapped to their values, as used by every configurable class (see Parameters).
Definition Parameters.h:44

When an iteration goes over budget, the nodes of lowest weight are moved to the long-term memory at the end of it – age only breaks ties between equal weights, so it is not simply the oldest that go. Weight is how often a place has been seen: while a node is still in the short-term memory, a new node similar enough to it (Mem/RehearsalSimilarity) is merged into it and raises its weight – the rehearsal mechanism. Places the robot dwells on or revisits therefore stay in the working memory, while views seen once leave first. They are not lost: when a loop closure is found, their neighbours are brought back into the working memory for the next iterations, up to Rtabmap/MaxRetrieved nodes (plus RGBD/MaxLocalRetrieved around the current pose and along a planned path). This is what makes long-term mapping practical: the robot keeps a bounded, relevant working set and pulls the rest back as it recognizes where it is. Retrieval and node immunization only run when memory management is on.

Which nodes go first is controlled by three parameters: Mem/RecentWmRatio protects the most recent part of the working memory, RGBD/LocalImmunizationRatio protects the nodes around the current pose, and Mem/TransferSortingByWeightId selects the ordering. The step-by-step behaviour is documented on rtabmap::Rtabmap (steps 4 and 6), and the Memory/Working_memory_size and Memory/Signatures_retrieved entries of rtabmap::Statistics report what happens at runtime.

Configuration

Every parameter is a string key/value pair in a rtabmap::ParametersMap, declared with its default and description in Parameters.h (for example Parameters::kMemSTMSize(), Parameters::kRGBDLinearUpdate()). The same keys are used by the applications, the ROS wrappers and the --Param value command-line arguments of the tools, so a setting found here applies everywhere.

The Parameter reference lists all of them, grouped, with their type, default value and description.

rtabmap.init(parameters, "map.db");
static std::string kMemSTMSize()
Key of parameter Mem/STMSize : "Short-term memory size." Default value: 10 ( unsigned int ).
Definition Parameters.h:226

The main classes

Doxygen lists the classes alphabetically; this is the same set arranged by the role they play, as a starting point into the API.

The map structure

Class Role
rtabmap::Rtabmap The entry point: one SLAM iteration per call, owning everything below
rtabmap::Memory Three-tiered memory (STM / WM / LTM) holding the map and deciding what stays online
rtabmap::Signature One node: sensor data, visual words, pose and links
rtabmap::Link One edge: neighbour, loop closure, landmark or prior constraint
rtabmap::DBDriver Persistence of the map to the database (see rtabmap::DBDriverSqlite3)
rtabmap::Statistics Everything the pipeline reports about an iteration

Inputs

Class Role
rtabmap::SensorData An observation: images, depth, laser scan, IMU, GPS, landmarks
rtabmap::CameraModel, rtabmap::StereoCameraModel Intrinsics, extrinsics and rectification
rtabmap::LaserScan Point cloud / laser scan container and its formats
rtabmap::Transform The 3D rigid transform used everywhere in the API
rtabmap::SensorCapture, rtabmap::SensorCaptureThread Drivers and the thread that pumps them

Building blocks

Class Role
rtabmap::Odometry Visual / lidar odometry front-ends
rtabmap::Registration, rtabmap::RegistrationVis, rtabmap::RegistrationIcp Relative transform between two nodes
rtabmap::Optimizer Graph optimization back-ends (g2o, GTSAM, Ceres, TORO)
rtabmap::Feature2D, rtabmap::VWDictionary Keypoint detectors/descriptors and the bag-of-words dictionary
rtabmap::BayesFilter Loop-closure hypothesis estimation
rtabmap::LocalGridMaker, rtabmap::GlobalMap Occupancy grid generation and assembly

Free functions for point cloud, image and geometry processing are grouped in util2d.h, util3d.h, util3d_filtering.h, util3d_registration.h, util3d_surface.h, util3d_transforms.h and util3d_mapping.h.