isam2 优化pose graph

isam2 优化pose graphgtsam 里面只有一个 isam2 的例子 那个例子里面没有添加位姿闭环约束 主要是视觉 BA 而通过闭环优化位姿的 gtsam 程序主要是 Pose2SLAMExa cpp 等 这种用法类似 g2o 不能体现 isam2 的增量优化特性 因此我仿照 Pose2SLAMExa 里的数据写了一个增量优化位姿的 isam2 程序 用法上还是有 isam2 的特性 特别注意 graph 里的只有 isam2 优化以后新加的约束

gtsam里面只有一个isam2的例子,那个例子里面没有添加位姿闭环约束,主要是视觉BA。而通过闭环优化位姿的gtsam程序主要是Pose2SLAMExample.cpp等,这种用法类似g2o,不能体现isam2的增量优化特性,因此我仿照Pose2SLAMExample里的数据写了一个增量优化位姿的isam2程序,用法上还是有isam2的特性,特别注意graph里的只有isam2优化以后新加的约束,具体见代码。

/ * A simple 2D pose slam example * - The robot moves in a 2 meter square * - The robot moves 2 meters each step, turning 90 degrees after each step * - The robot initially faces along the X axis (horizontal, to the right in 2D) * - We have full odometry between pose * - We have a loop closure constraint when the robot returns to the first position * x5 -- x4 * | | * x1 -- x2 -- x3 */ // Each variable in the system (poses and landmarks) must be identified with a unique key. // We can either use simple integer keys (1, 2, 3, ...) or symbols (X1, X2, L1). // Here we will use Symbols #include 
    // We want to use iSAM2 to solve the pose optimazation problem incrementally, so // include iSAM2 here #include 
    // iSAM2 requires as input a set set of new factors to be added stored in a factor graph, // and initial guesses for any new variables used in the added factors #include 
    #include 
    #include 
    // In planar SLAM example we use Pose2 variables (x, y, theta) to represent the robot poses #include 
    // We will use simple integer Keys to refer to the robot poses. #include 
    // In GTSAM, measurement functions are represented as 'factors'. Several common factors // have been provided with the library for solving robotics/SLAM/Bundle Adjustment problems. // Here we will use Between factors for the relative motion described by odometry measurements. // We will also use a Between Factor to encode the loop closure constraint // Also, we will initialize the robot at the origin using a Prior factor. #include 
    #include 
    // The nonlinear solvers within GTSAM are iterative solvers, meaning they linearize the // nonlinear functions around an initial linearization point, then solve the linear system // to update the linearization point. This happens repeatedly until the solver converges // to a consistent set of variable values. This requires us to specify an initial guess // for each variable, held in a Values container. #include 
    using namespace std; using namespace gtsam; int main() { // 向量保存好模拟的位姿和测量,到时候一个个往isam2里填加 std::vector< BetweenFactor 
    > gra; std::vector< Pose2 > initPose; // For simplicity, we will use the same noise model for odometry and loop closures noiseModel::Diagonal::shared_ptr model = noiseModel::Diagonal::Sigmas(Vector3(0.2, 0.2, 0.1)); gra.push_back(BetweenFactor 
  
    ( 
   1, 
   2, Pose2( 
   2, 
   0, 
   0 ), model)); gra.push_back(BetweenFactor 
   
     ( 
    2, 
    3, Pose2( 
    2, 
    0, M_PI_2), model)); gra.push_back(BetweenFactor 
    
      ( 
     3, 
     4, Pose2( 
     2, 
     0, M_PI_2), model)); gra.push_back(BetweenFactor 
     
       ( 
      4, 
      5, Pose2( 
      2, 
      0, M_PI_2), model)); gra.push_back(BetweenFactor 
      
        ( 
       5, 
       2, Pose2( 
       2, 
       0, M_PI_2), model)); initPose.push_back(Pose2( 
       0.5, 
       0.0, 
       0.2 )); initPose.push_back( Pose2( 
       2.3, 
       0.1, - 
       0.2 )); initPose.push_back( Pose2( 
       4.1, 
       0.1, M_PI_2)); initPose.push_back( Pose2( 
       4.0, 
       2.0, M_PI )); initPose.push_back( Pose2( 
       2.1, 
       2.1, -M_PI_2)); 
       // Create an iSAM2 object. Unlike iSAM1, which performs periodic batch steps to maintain proper linearization 
       // and efficient variable ordering, iSAM2 performs partial relinearization/reordering at each step. A parameter 
       // structure is available that allows the user to set various properties, such as the relinearization threshold 
       // and type of linear solver. For this example, we we set the relinearization threshold small so the iSAM2 result 
       // will approach the batch result. ISAM2Params parameters; parameters.relinearizeThreshold = 
       0.01; parameters.relinearizeSkip = 
       1; ISAM2 isam(parameters); 
       // Create a Factor Graph and Values to hold the new data 
       // 注意isam2的graph里只添加isam2更新状态以后新测量到的约束 NonlinearFactorGraph graph; Values initialEstimate; 
       // the first pose don't need to update 
       for( 
       int i = 
       0; i< 
       5 ;i++) { 
       // Add an initial guess for the current pose initialEstimate.insert(i+ 
       1, initPose[i]); 
       if(i == 
       0) { 
       // Add a prior on the first pose, setting it to the origin 
       // A prior factor consists of a mean and a noise model (covariance matrix) noiseModel::Diagonal:: 
       shared_ptr priorNoise = noiseModel::Diagonal::Sigmas(Vector3( 
       0.3, 
       0.3, 
       0.1)); graph.push_back(PriorFactor 
       
         ( 
        1, Pose2( 
        0, 
        0, 
        0), priorNoise)); } 
        else { graph.push_back(gra[i- 
        1]); 
        // ie: when i = 1 , robot at pos2, there is a edge gra[0] between pos1 and pos2 
        if(i == 
        4) { graph.push_back(gra[ 
        4]); 
        // when robot at pos5, there two edge, one is pos4 ->pos5, another is pos5->pos2 (grad[4]) } isam.update(graph, initialEstimate); isam.update(); Values currentEstimate = isam.calculateEstimate(); 
        cout << 
        "" << endl; 
        cout << 
        "Frame " << i << 
        ": " << endl; currentEstimate.print( 
        "Current estimate: "); 
        // Clear the factor graph and values for the next iteration 
        // 特别重要,update以后,清空原来的约束。已经加入到isam2的那些会用bayes tree保管,你不用操心了。 graph.resize( 
        0); initialEstimate.clear(); } } 
        return 
        0; } 
        
       
      
     
    
  

这个程序的优化结果和Pose2SLAMExample.cpp几乎一样,但是使用的是增量优化的方式,有其独特的优点。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/201871.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月20日 上午8:25
下一篇 2026年3月20日 上午8:25


相关推荐

  • 硬盘安装Fedora12「建议收藏」

    硬盘安装Fedora12「建议收藏」从fedora官网上下载Fedora-12-i386-DVD.iso,live的貌似不容易安装成功。把iso中的images文件夹里的install.img和isolinux文件夹里的vmlinuz、initrd.img解压出来,和iso安如下目录机构放在FAT32分区的根目录下。目录结构:C:/images/install.imgC:/vmlinuzC:/initrd.imgC:/Fedor

    2026年2月4日
    4
  • 三极管工作原理分析,精辟、透彻,看后你就懂

    三极管工作原理分析,精辟、透彻,看后你就懂说明 内容与之前那篇一样 由于之前那篇是转载百度的 现在图片受限 无法阅读 这篇自己添加了图片资源 随着科学技的发展 电子技术的应用几乎渗透到了人们生产生活的方方面面 晶体三极管作为电子技术中一个最为基本的常用器件 其原理对于学习电子技术的人自然应该是一个重点 三极管原理的关键是要说明以下三点 1 集电结为何会发生反偏导通并产生 Ic 这看起来与二极管原理强调的 PN 结单向导电性相矛盾 2 放大状态下集

    2026年3月26日
    2
  • pycharm恢复初始设置windows_pycharm中恢复索引

    pycharm恢复初始设置windows_pycharm中恢复索引在windows下,pycharm恢复初始设置C:\User\(用户名)\.pycharmxxxx

    2025年7月30日
    6
  • 学会理解并编辑/etc/fstab

    学会理解并编辑/etc/fstabfstab etc fstab 是 Linux 下比较重要的配置文件 它包含了系统在启动时挂载文件系统和存储设备的详细信息 下面是我机子上的 fstab 文件 nbsp LABEL nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp ext3 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp defaults nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 11 nbsp LABEL boot

    2026年3月17日
    2
  • 线上FGC调优案例三则

    线上FGC调优案例三则前言闲鱼服务端应用广泛使用Java技术栈,基于JVM提供的托管式堆内存管理,开发者无需过多关心对象创建/回收时的内存分配/释放动作,垃圾回收器(GarbageCollector)会在…

    2022年6月19日
    35
  • 软件测试基础知识——全[通俗易懂]

    软件测试基础知识——全[通俗易懂]目录1、请你分别介绍一下单元测试、集成测试、系统测试、验收测试、回归测试。2、请你回答一下单元测试、集成测试、系统测试、验收测试、回归测试这几步中最重要的是哪一步?3、请回答集成测试和系统测试的区别,以及它们的应用场景主要是什么?4请问测试开发需要哪些知识?需要具备什么能力?5、请说一说黑盒与白盒的测试方法。6、请说一下手动测试与自动化测试的优缺点。7、请问你怎么看待软件测试的潜力和挑战。8、你觉得软件测试的核心竞争力是什么?9、你觉得测试和开发需要怎么结合才能使软件的质量得到更好的保障?10、你觉得

    2022年6月18日
    32

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号