OdometryFLOAM.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
30 #include "rtabmap/core/util2d.h"
32 #include "rtabmap/utilite/UTimer.h"
33 #include "rtabmap/utilite/UStl.h"
34 #include "rtabmap/core/util3d.h"
35 #include <pcl/common/transforms.h>
36 
37 #ifdef RTABMAP_FLOAM
38 #include <laserProcessingClass.h>
39 #include <odomEstimationClass.h>
40 #endif
41 
42 namespace rtabmap {
43 
49  Odometry(parameters)
50 #ifdef RTABMAP_FLOAM
51  ,laserProcessing_(new LaserProcessingClass())
52  ,odomEstimation_(new OdomEstimationClass())
53  ,lastPose_(Transform::getIdentity())
54  ,lost_(false)
55 #endif
56 {
57 #ifdef RTABMAP_FLOAM
58  int sensor = Parameters::defaultOdomLOAMSensor();
59  double vertical_angle = 2.0; // seems not used by floam (https://github.com/wh200720041/floam/issues/31)
60  float scan_period= Parameters::defaultOdomLOAMScanPeriod();
61  float max_dis = Parameters::defaultIcpRangeMax();
62  float min_dis = Parameters::defaultIcpRangeMin();
63  float map_resolution = Parameters::defaultOdomLOAMResolution();
64  linVar_ = Parameters::defaultOdomLOAMLinVar();
65  angVar_ = Parameters::defaultOdomLOAMAngVar();
66 
67  Parameters::parse(parameters, Parameters::kOdomLOAMSensor(), sensor);
68  Parameters::parse(parameters, Parameters::kOdomLOAMScanPeriod(), scan_period);
69  Parameters::parse(parameters, Parameters::kIcpRangeMax(), max_dis);
70  Parameters::parse(parameters, Parameters::kIcpRangeMin(), min_dis);
71  Parameters::parse(parameters, Parameters::kOdomLOAMResolution(), map_resolution);
72 
73  Parameters::parse(parameters, Parameters::kOdomLOAMLinVar(), linVar_);
74  UASSERT(linVar_>0.0f);
75  Parameters::parse(parameters, Parameters::kOdomLOAMAngVar(), angVar_);
76  UASSERT(angVar_>0.0f);
77 
78  lidar::Lidar lidar_param;
79  lidar_param.setScanPeriod(scan_period);
80  lidar_param.setVerticalAngle(vertical_angle);
81  lidar_param.setLines(sensor==2?64:sensor==1?32:16);
82  lidar_param.setMaxDistance(max_dis<=0?200:max_dis);
83  lidar_param.setMinDistance(min_dis);
84 
85  laserProcessing_->init(lidar_param);
86  odomEstimation_->init(lidar_param, map_resolution);
87 #endif
88 }
89 
91 {
92 #ifdef RTABMAP_FLOAM
93  delete laserProcessing_;
94  delete odomEstimation_;
95 #endif
96 }
97 
98 void OdometryFLOAM::reset(const Transform & initialPose)
99 {
100  Odometry::reset(initialPose);
101 #ifdef RTABMAP_FLOAM
102  lastPose_.setIdentity();
103  lost_ = false;
104 #endif
105 }
106 
107 // return not null transform if odometry is correctly computed
109  SensorData & data,
110  const Transform & guess,
111  OdometryInfo * info)
112 {
113  Transform t;
114 #ifdef RTABMAP_FLOAM
115  UTimer timer;
116  UTimer timerTotal;
117 
118  if(data.laserScanRaw().isEmpty())
119  {
120  UERROR("LOAM works only with laser scans and the current input is empty. Aborting odometry update...");
121  return t;
122  }
123  else if(data.laserScanRaw().is2d())
124  {
125  UERROR("LOAM version used works only with 3D laser scans from Velodyne. Aborting odometry update...");
126  return t;
127  }
128 
129  cv::Mat covariance = cv::Mat::eye(6,6,CV_64FC1)*9999;
130  if(!lost_)
131  {
132  pcl::PointCloud<pcl::PointXYZI>::Ptr laserCloudInPtr = util3d::laserScanToPointCloudI(data.laserScanRaw());
133 
134  UDEBUG("Scan conversion: %fs", timer.ticks());
135 
136  pcl::PointCloud<pcl::PointXYZI>::Ptr pointcloud_edge(new pcl::PointCloud<pcl::PointXYZI>());
137  pcl::PointCloud<pcl::PointXYZI>::Ptr pointcloud_surf(new pcl::PointCloud<pcl::PointXYZI>());
138 
139  laserProcessing_->featureExtraction(laserCloudInPtr,pointcloud_edge,pointcloud_surf);
140  UDEBUG("Feature extraction: %fs", timer.ticks());
141 
142  // Put back the laser scan filtered
143  pcl::PointCloud<pcl::PointXYZI>::Ptr pointcloud_filtered(new pcl::PointCloud<pcl::PointXYZI>());
144  *pointcloud_filtered+=*pointcloud_edge;
145  *pointcloud_filtered+=*pointcloud_surf;
146  data.setLaserScan(util3d::laserScanFromPointCloud(*pointcloud_filtered));
147 
148  if(this->framesProcessed() == 0){
149  odomEstimation_->initMapWithPoints(pointcloud_edge, pointcloud_surf);
150  }else{
151  odomEstimation_->updatePointsToMap(pointcloud_edge, pointcloud_surf);
152  }
153  UDEBUG("Update: %fs", timer.ticks());
154 
155  Transform pose = Transform::fromEigen3d(odomEstimation_->odom);
156 
157  if(!pose.isNull())
158  {
159  covariance = cv::Mat::eye(6,6,CV_64FC1);
160  covariance(cv::Range(0,3), cv::Range(0,3)) *= linVar_;
161  covariance(cv::Range(3,6), cv::Range(3,6)) *= angVar_;
162 
163  t = lastPose_.inverse() * pose; // incremental
164  lastPose_ = pose;
165 
166  const Transform & localTransform = data.laserScanRaw().localTransform();
167  if(!t.isNull() && !t.isIdentity() && !localTransform.isIdentity() && !localTransform.isNull())
168  {
169  // from laser frame to base frame
170  t = localTransform * t * localTransform.inverse();
171  }
172 
173  if(info)
174  {
175  info->type = (int)kTypeLOAM;
176  if(covariance.cols == 6 && covariance.rows == 6 && covariance.type() == CV_64FC1)
177  {
178  info->reg.covariance = covariance;
179  }
180 
181  if(this->isInfoDataFilled())
182  {
183  pcl::PointCloud<pcl::PointXYZI>::Ptr localMap(new pcl::PointCloud<pcl::PointXYZI>());
184  odomEstimation_->getMap(localMap);
185  info->localScanMapSize = localMap->size();
187  UDEBUG("Fill info data: %fs", timer.ticks());
188  }
189  }
190  }
191  else
192  {
193  lost_ = true;
194  UWARN("FLOAM failed to register the latest scan, odometry should be reset.");
195  }
196  }
197  UINFO("Odom update time = %fs, lost=%s", timerTotal.elapsed(), lost_?"true":"false");
198 
199 #else
200  UERROR("RTAB-Map is not built with FLOAM support! Select another odometry approach.");
201 #endif
202  return t;
203 }
204 
205 } // namespace rtabmap
static bool parse(const ParametersMap &parameters, const std::string &key, bool &value)
Definition: Parameters.cpp:497
Definition: UTimer.h:46
void setLaserScan(const LaserScan &laserScan, bool clearPreviousData=true)
Definition: SensorData.cpp:381
double elapsed()
Definition: UTimer.h:75
f
virtual void reset(const Transform &initialPose=Transform::getIdentity())
Definition: Odometry.cpp:203
unsigned int framesProcessed() const
Definition: Odometry.h:81
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:43
OdometryFLOAM(const rtabmap::ParametersMap &parameters=rtabmap::ParametersMap())
bool isEmpty() const
Definition: LaserScan.h:125
bool is2d() const
Definition: LaserScan.h:128
float rangeMax() const
Definition: LaserScan.h:118
#define UASSERT(condition)
Wrappers of STL for convenient functions.
bool isInfoDataFilled() const
Definition: Odometry.h:77
virtual void reset(const Transform &initialPose=Transform::getIdentity())
LaserScan laserScanFromPointCloud(const PointCloud2T &cloud, bool filterNaNs, bool is2D, const Transform &transform)
Definition: util3d.hpp:37
virtual Transform computeTransform(SensorData &image, const Transform &guess=Transform(), OdometryInfo *info=0)
bool isNull() const
Definition: Transform.cpp:107
static Transform fromEigen3d(const Eigen::Affine3d &matrix)
Definition: Transform.cpp:425
#define false
Definition: ConvertUTF.c:56
const LaserScan & laserScanRaw() const
Definition: SensorData.h:185
#define UDEBUG(...)
#define UERROR(...)
ULogger class and convenient macros.
#define UWARN(...)
double ticks()
Definition: UTimer.cpp:117
RegistrationInfo reg
Definition: OdometryInfo.h:97
pcl::PointCloud< pcl::PointXYZI >::Ptr RTABMAP_EXP laserScanToPointCloudI(const LaserScan &laserScan, const Transform &transform=Transform(), float intensity=0.0f)
Definition: util3d.cpp:2250
Transform localTransform() const
Definition: LaserScan.h:122
bool isIdentity() const
Definition: Transform.cpp:136
Transform inverse() const
Definition: Transform.cpp:178
#define UINFO(...)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:29