首页 > 解决方案 > MRPT Graph Slam 最小示例

问题描述

我试图想出一种使用 MRPT 运行图形 slam 应用程序的“最小”方式。传感器数据(LaserScan / Odometry)将由类似于 ROS 的自定义中间件提供。在广泛阅读文档和源代码(MRPT 和 ROS 桥)之后,我想出了以下代码片段:

std::string config_file = "../../../laser_odometry.ini";   
std::string rawlog_fname = "";   
std::string fname_GT = "";   
auto node_reg = mrpt::graphslam::deciders::CICPCriteriaNRD<mrpt::graphs::CNetworkOfPoses2DInf>{}; 
auto edge_reg = mrpt::graphslam::deciders::CICPCriteriaERD<mrpt::graphs::CNetworkOfPoses2DInf>{}; 
auto optimizer = mrpt::graphslam::optimizers::CLevMarqGSO<mrpt::graphs::CNetworkOfPoses2DInf>{};

auto win3d = mrpt::gui::CDisplayWindow3D{"Slam", 800, 600};
auto win_observer = mrpt::graphslam::CWindowObserver{};
auto win_manager = mrpt::graphslam::CWindowManager{&win3d, &win_observer};

auto engine = mrpt::graphslam::CGraphSlamEngine<mrpt::graphs::CNetworkOfPoses2DInf>{
  config_file, rawlog_fname, fname_GT, &win_manager, &node_reg, &edge_reg, &optimizer};

for (size_t measurement_count = 0;;) {
   // grab laser scan from the network, then fill it (hardcoded values for now), e.g:
   auto scan_ptr = mrpt::obs::CObservation2DRangeScan::Create();
   scan_ptr->timestamp = std::chrono::system_clock::now().time_since_epoch().count();
   scan_ptr->rightToLeft = true;
   scan_ptr->sensorLabel = "";
   scan_ptr->aperture = 3.14;  // rad (max-min)
   scan_ptr->maxRange = 3.0;   // m
   scan_ptr->sensorPose = mrpt::poses::CPose3D{};

   scan_ptr->resizeScan(30);
   for (int i = 0; i < 30; ++i) {
     scan_ptr->setScanRange(i, 0.5);
     scan_ptr->setScanRangeValidity(i, true);
   }

   { // Send LaserScan measurement to the slam engine
     auto obs_ptr = std::dynamic_pointer_cast<mrpt::obs::CObservation>(scan_ptr);
     engine.execGraphSlamStep(obs_ptr, measurement_count);
     ++measurement_count;
   }


   // grab odometry from the network, then fill it (hardcoded values for now), e.g:
   auto odometry_ptr = mrpt::obs::CObservationOdometry::Create();
   odometry_ptr->timestamp = std::chrono::system_clock::now().time_since_epoch().count();
   odometry_ptr->hasVelocities = false;
   odometry_ptr->odometry.x(0);
   odometry_ptr->odometry.y(0);
   odometry_ptr->odometry.phi(0);

   { // Send Odometry measurement to the slam engine
     auto obs_ptr = std::dynamic_pointer_cast<mrpt::obs::CObservation>(odometry_ptr);
     engine.execGraphSlamStep(obs_ptr, measurement_count);
     ++measurement_count;
   }

   // Get pose estimation from the engine
   auto pose = engine.getCurrentRobotPosEstimation();

}

我在这里的方向正确吗?我错过了什么?

标签: c++mobile-robot-toolkit

解决方案


嗯,乍一看,脚本看起来不错,您正在以两个不同的步骤和观察形式提供里程计和激光扫描。

  • 小注

    auto node_reg = mrpt::graphslam::deciders::CICPCriteriaNRD{};

如果您想使用里程计 + 激光扫描运行,请改用 CFixedIntervalsNRD。它经过了更好的测试,并且实际上利用了这些测量值。

目前 MRPT 中没有最小的 graphslam-engine 示例,但这里是使用数据集运行 graph-slam 的主要方法:

https://github.com/MRPT/mrpt/blob/26ee0f2d3a9366c50faa5f78d0388476ae886808/libs/graphslam/include/mrpt/graphslam/apps_related/CGraphSlamHandler_impl.h#L395

template <class GRAPH_T>
void CGraphSlamHandler<GRAPH_T>::execute()
{
    using namespace mrpt::obs;
    ASSERTDEB_(m_engine);

    // Variables initialization
    mrpt::io::CFileGZInputStream rawlog_stream(m_rawlog_fname);
    CActionCollection::Ptr action;
    CSensoryFrame::Ptr observations;
    CObservation::Ptr observation;
    size_t curr_rawlog_entry;
    auto arch = mrpt::serialization::archiveFrom(rawlog_stream);

    // Read the dataset and pass the measurements to CGraphSlamEngine
    bool cont_exec = true;
    while (CRawlog::getActionObservationPairOrObservation(
               arch, action, observations, observation, curr_rawlog_entry) &&
           cont_exec)
    {
        // actual call to the graphSLAM execution method
        // Exit if user pressed C-c
        cont_exec = m_engine->_execGraphSlamStep(
            action, observations, observation, curr_rawlog_entry);
    }
    m_logger->logFmt(mrpt::system::LVL_WARN, "Finished graphslam execution.");
}

您基本上抓取数据,然后通过 execGraphSlamStep 或 _execGraphSlamStep 方法不断地将它们提供给 CGraphSlamEngine。

这也是在相应的 ROS 包装器中处理测量值的相关片段,该包装器使用来自 ROS 主题的测量值进行操作:

https://github.com/mrpt-ros-pkg/mrpt_slam/blob/8b32136e2a381b1759eb12458b4adba65e2335da/mrpt_graphslam_2d/include/mrpt_graphslam_2d/CGraphSlamHandler_ROS_impl.h#L719

template<class GRAPH_T>
void CGraphSlamHandler_ROS<GRAPH_T>::processObservation(
        mrpt::obs::CObservation::Ptr& observ) {
  this->_process(observ);
}

template<class GRAPH_T>
void CGraphSlamHandler_ROS<GRAPH_T>::_process(
        mrpt::obs::CObservation::Ptr& observ) {
  using namespace mrpt::utils;
  if (!this->m_engine->isPaused()) {
        this->m_engine->execGraphSlamStep(observ, m_measurement_cnt);
        m_measurement_cnt++;
  }
}

推荐阅读