Skip to content

Correct the Jacobian calculation of GravityFactor - #1692

Merged
matlabbe merged 7 commits into
introlab:masterfrom
borongyuan:master
Apr 26, 2026
Merged

matlabbe merged 7 commits into
introlab:masterfrom
borongyuan:master

Conversation

@borongyuan

Copy link
Copy Markdown
Contributor

Hi,
We found that when gravity constraints are enabled, the accuracy of optimization using GTSAM is lower than that using g2o. GTSAM optimization may produce erroneous results, particularly when the camera is oriented upward or downward. Upon inspection, I discovered an issue with the Jacobian calculation in GravityFactor and have fixed it.

@borongyuan

Copy link
Copy Markdown
Contributor Author

I studied the design of AttitudeFactor and found that it is inherently well-suited for handling gravity constraints. We don't need to implement an additional GravityFactor. GravityFactor explicitly ignores the yaw angle, but this is unnecessary. The error object of AttitudeFactor is a unit direction vector, which has only 2 degrees of freedom. If we choose the gravity axis as the reference axis, then rotating around it by the yaw angle will not change its direction, and it will not affect the error. Using AttitudeFactor is more natural and avoids the numerical stability and singularity risks that may arise from explicit conversion to Euler angles. Early versions of AttitudeFactor contained somewhat misleading variable names and comments. In this commit, it was modified to have more reasonable semantics.
Using gravity constraints is useful, but it increases the solution cost of linear systems. Adding gravity constraints to every node is probably unnecessary. We'll see if there's a smarter way to handle it.

@matlabbe

matlabbe commented Apr 25, 2026 •

Copy link
Copy Markdown
Member

I don't remember exactly the reason why I extracted roll/pitch in AttitudeFactor explicitly. My guess is that I had a map that was rotating along yaw when optimized with original attitude factors. Probably the bug was somewhere else though (and fixed since then?).

I compared original GravityFactor, proposed GravityFactor of this PR and the official AttitudeFactor as proposed also in this PR. I created a map with RTAB-Map iOS by scanning only towards the floor (camera looking down) (EDIT: here the database and pictures below) and compared optimization with these three approaches by decreasing Optimizer/GravitySigma to see when they start diverging.

Screenshot from 2026-04-26 14-21-01 Screenshot from 2026-04-26 14-20-51

Overall:

  • AttitudeFactor is the most stable (always converging in 4-5 iterations), even with Optimizer/GravitySigma as small as 0.0001. Note also that g2o (tested with GaussNewton optimizer) performs similarly with that map with same Optimizer/GravitySigma values tested.
  • Original GravityFactor diverges badly when Optimizer/GravitySigma<=0.01
  • Proposed GravityFactor is causing an exception:
    [DEBUG] (2026-04-25 13:11:14.866) OptimizerGTSAM.cpp:782::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
    terminate called after throwing an instance of 'std::runtime_error'
      what():  Rot3::RQ : Derivative undefined at singularity (gimbal lock)
    Aborted (core dumped)
    
Here some detailed logs (click to show)
#######################
Original GravityFactor
#######################
Optimizer/GravitySigma=0.2
[DEBUG] (2026-04-25 11:34:59.039) OptimizerGTSAM.cpp:783::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:34:59.042) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =265.896653
[DEBUG] (2026-04-25 11:34:59.045) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =260.870120
[DEBUG] (2026-04-25 11:34:59.048) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =260.775222
[DEBUG] (2026-04-25 11:34:59.050) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =260.406828
[DEBUG] (2026-04-25 11:34:59.053) OptimizerGTSAM.cpp:933::optimize() iteration 5 error =260.184814
[DEBUG] (2026-04-25 11:34:59.056) OptimizerGTSAM.cpp:933::optimize() iteration 6 error =260.039233
[DEBUG] (2026-04-25 11:34:59.060) OptimizerGTSAM.cpp:933::optimize() iteration 7 error =259.958084
[DEBUG] (2026-04-25 11:34:59.063) OptimizerGTSAM.cpp:933::optimize() iteration 8 error =259.927318
[DEBUG] (2026-04-25 11:34:59.066) OptimizerGTSAM.cpp:933::optimize() iteration 9 error =259.935598
[DEBUG] (2026-04-25 11:34:59.066) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-0.008280 < 0.000010)
[DEBUG] (2026-04-25 11:34:59.069) OptimizerGTSAM.cpp:933::optimize() iteration 10 error =259.973701
[DEBUG] (2026-04-25 11:34:59.069) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-0.038103 < 0.000010)
[DEBUG] (2026-04-25 11:34:59.069) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (10 iterations done (error initial=871.508318 final=259.973701), time=0.030830 s)

Optimizer/GravitySigma=0.1
[DEBUG] (2026-04-25 11:36:47.313) OptimizerGTSAM.cpp:783::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:36:47.316) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =338.461815
[DEBUG] (2026-04-25 11:36:47.319) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =300.706999
[DEBUG] (2026-04-25 11:36:47.322) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =305.181950
[DEBUG] (2026-04-25 11:36:47.322) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-4.474951 < 0.000010)
[DEBUG] (2026-04-25 11:36:47.325) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =303.290318
[DEBUG] (2026-04-25 11:36:47.328) OptimizerGTSAM.cpp:933::optimize() iteration 5 error =303.015509
[DEBUG] (2026-04-25 11:36:47.332) OptimizerGTSAM.cpp:933::optimize() iteration 6 error =302.552041
[DEBUG] (2026-04-25 11:36:47.335) OptimizerGTSAM.cpp:933::optimize() iteration 7 error =302.207250
[DEBUG] (2026-04-25 11:36:47.338) OptimizerGTSAM.cpp:933::optimize() iteration 8 error =301.932093
[DEBUG] (2026-04-25 11:36:47.341) OptimizerGTSAM.cpp:933::optimize() iteration 9 error =301.716161
[DEBUG] (2026-04-25 11:36:47.344) OptimizerGTSAM.cpp:933::optimize() iteration 10 error =301.556139
[DEBUG] (2026-04-25 11:36:47.344) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (10 iterations done (error initial=931.590703 final=301.556139), time=0.031091 s)

Optimizer/GravitySigma=0.05
[DEBUG] (2026-04-25 11:37:45.533) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =541.256110
[DEBUG] (2026-04-25 11:37:45.540) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =376.949304
[DEBUG] (2026-04-25 11:37:45.543) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =402.417300
[DEBUG] (2026-04-25 11:37:45.543) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-25.467996 < 0.000010)
[DEBUG] (2026-04-25 11:37:45.546) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =391.263654
[DEBUG] (2026-04-25 11:37:45.549) OptimizerGTSAM.cpp:933::optimize() iteration 5 error =393.465514
[DEBUG] (2026-04-25 11:37:45.549) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-2.201859 < 0.000010)
[DEBUG] (2026-04-25 11:37:45.552) OptimizerGTSAM.cpp:933::optimize() iteration 6 error =392.391454
[DEBUG] (2026-04-25 11:37:45.555) OptimizerGTSAM.cpp:933::optimize() iteration 7 error =392.076217
[DEBUG] (2026-04-25 11:37:45.559) OptimizerGTSAM.cpp:933::optimize() iteration 8 error =391.740178
[DEBUG] (2026-04-25 11:37:45.561) OptimizerGTSAM.cpp:933::optimize() iteration 9 error =391.410734
[DEBUG] (2026-04-25 11:37:45.566) OptimizerGTSAM.cpp:933::optimize() iteration 10 error =391.152177
[DEBUG] (2026-04-25 11:37:45.566) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (10 iterations done (error initial=1171.920244 final=391.152177), time=0.036132 s)

Optimizer/GravitySigma=0.01
[DEBUG] (2026-04-25 11:38:20.309) OptimizerGTSAM.cpp:783::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:38:20.312) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =4020.978312
[DEBUG] (2026-04-25 11:38:20.315) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =6548.654273
[DEBUG] (2026-04-25 11:38:20.315) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-2527.675961 < 0.000010)
[DEBUG] (2026-04-25 11:38:20.318) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =5752.081408
[DEBUG] (2026-04-25 11:38:20.321) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =7269.434016
[DEBUG] (2026-04-25 11:38:20.321) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-1517.352608 < 0.000010)
[DEBUG] (2026-04-25 11:38:20.324) OptimizerGTSAM.cpp:933::optimize() iteration 5 error =6859.388115
[DEBUG] (2026-04-25 11:38:20.327) OptimizerGTSAM.cpp:933::optimize() iteration 6 error =8102.467482
[DEBUG] (2026-04-25 11:38:20.327) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-1243.079366 < 0.000010)
[DEBUG] (2026-04-25 11:38:20.330) OptimizerGTSAM.cpp:933::optimize() iteration 7 error =7978.333804
[DEBUG] (2026-04-25 11:38:20.332) OptimizerGTSAM.cpp:933::optimize() iteration 8 error =9067.029660
[DEBUG] (2026-04-25 11:38:20.332) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-1088.695857 < 0.000010)
[DEBUG] (2026-04-25 11:38:20.336) OptimizerGTSAM.cpp:933::optimize() iteration 9 error =9378.015149
[DEBUG] (2026-04-25 11:38:20.336) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-310.985489 < 0.000010)
[DEBUG] (2026-04-25 11:38:20.338) OptimizerGTSAM.cpp:933::optimize() iteration 10 error =10378.897627
[DEBUG] (2026-04-25 11:38:20.338) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-1000.882478 < 0.000010)
[DEBUG] (2026-04-25 11:38:20.338) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (10 iterations done (error initial=8862.466163 final=10378.897627), time=0.029168 s)

Optimizer/GravitySigma=0.001
[DEBUG] (2026-04-25 11:38:58.455) OptimizerGTSAM.cpp:783::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:38:58.459) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =7003610.121256
[DEBUG] (2026-04-25 11:38:58.463) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =15777062.786410
[DEBUG] (2026-04-25 11:38:58.463) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-8773452.665155 < 0.000010)
[DEBUG] (2026-04-25 11:38:58.465) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =28980876.449656
[DEBUG] (2026-04-25 11:38:58.465) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-13203813.663246 < 0.000010)
[DEBUG] (2026-04-25 11:38:58.468) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =44144033.002607
[DEBUG] (2026-04-25 11:38:58.468) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-15163156.552951 < 0.000010)
[DEBUG] (2026-04-25 11:38:58.473) OptimizerGTSAM.cpp:933::optimize() iteration 5 error =51108541.943972
[DEBUG] (2026-04-25 11:38:58.473) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-6964508.941364 < 0.000010)
[DEBUG] (2026-04-25 11:38:58.476) OptimizerGTSAM.cpp:933::optimize() iteration 6 error =58518767.447184
[DEBUG] (2026-04-25 11:38:58.476) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-7410225.503212 < 0.000010)
[DEBUG] (2026-04-25 11:38:58.479) OptimizerGTSAM.cpp:933::optimize() iteration 7 error =71396572.305599
[DEBUG] (2026-04-25 11:38:58.479) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-12877804.858415 < 0.000010)
[DEBUG] (2026-04-25 11:38:58.482) OptimizerGTSAM.cpp:933::optimize() iteration 8 error =70467904.054385
[DEBUG] (2026-04-25 11:38:58.484) OptimizerGTSAM.cpp:933::optimize() iteration 9 error =67702236.523415
[DEBUG] (2026-04-25 11:38:58.487) OptimizerGTSAM.cpp:933::optimize() iteration 10 error =68137451.859070
[DEBUG] (2026-04-25 11:38:58.487) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-435215.335655 < 0.000010)
[DEBUG] (2026-04-25 11:38:58.487) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (10 iterations done (error initial=801949.899642 final=68137451.859070), time=0.031989 s)


#######################
Official AttitudeFactor
#######################
Optimizer/GravitySigma=0.2
[DEBUG] (2026-04-25 11:40:48.605) OptimizerGTSAM.cpp:783::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:40:48.608) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =239.032866
[DEBUG] (2026-04-25 11:40:48.611) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =238.998287
[DEBUG] (2026-04-25 11:40:48.613) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =238.998167
[DEBUG] (2026-04-25 11:40:48.616) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =238.998165
[DEBUG] (2026-04-25 11:40:48.616) OptimizerGTSAM.cpp:955::optimize() Stop optimizing, not enough improvement (0.000002 < 0.000010)
[DEBUG] (2026-04-25 11:40:48.616) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (4 iterations done (error initial=851.557781 final=238.998167), time=0.010995 s)

Optimizer/GravitySigma=0.1
[DEBUG] (2026-04-25 11:41:16.408) OptimizerGTSAM.cpp:783::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:41:16.411) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =239.266285
[DEBUG] (2026-04-25 11:41:16.413) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =239.231104
[DEBUG] (2026-04-25 11:41:16.416) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =239.230648
[DEBUG] (2026-04-25 11:41:16.419) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =239.230637
[DEBUG] (2026-04-25 11:41:16.421) OptimizerGTSAM.cpp:933::optimize() iteration 5 error =239.230637
[DEBUG] (2026-04-25 11:41:16.421) OptimizerGTSAM.cpp:955::optimize() Stop optimizing, not enough improvement (0.000000 < 0.000010)
[DEBUG] (2026-04-25 11:41:16.421) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (5 iterations done (error initial=851.788555 final=239.230637), time=0.013422 s)

Optimizer/GravitySigma=0.05
[DEBUG] (2026-04-25 11:41:59.670) OptimizerGTSAM.cpp:783::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:41:59.673) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =239.775517
[DEBUG] (2026-04-25 11:41:59.676) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =239.735387
[DEBUG] (2026-04-25 11:41:59.679) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =239.734470
[DEBUG] (2026-04-25 11:41:59.682) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =239.734460
[DEBUG] (2026-04-25 11:41:59.682) OptimizerGTSAM.cpp:955::optimize() Stop optimizing, not enough improvement (0.000010 < 0.000010)
[DEBUG] (2026-04-25 11:41:59.683) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (4 iterations done (error initial=852.711652 final=239.734470), time=0.012013 s)

Optimizer/GravitySigma=0.01
[DEBUG] (2026-04-25 11:42:22.308) OptimizerGTSAM.cpp:783::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:42:22.311) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =241.908783
[DEBUG] (2026-04-25 11:42:22.314) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =241.873439
[DEBUG] (2026-04-25 11:42:22.316) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =241.873631
[DEBUG] (2026-04-25 11:42:22.316) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-0.000192 < 0.000010)
[DEBUG] (2026-04-25 11:42:22.319) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =241.873632
[DEBUG] (2026-04-25 11:42:22.319) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-0.000000 < 0.000010)
[DEBUG] (2026-04-25 11:42:22.322) OptimizerGTSAM.cpp:933::optimize() iteration 5 error =241.873632
[DEBUG] (2026-04-25 11:42:22.322) OptimizerGTSAM.cpp:955::optimize() Stop optimizing, not enough improvement (0.000000 < 0.000010)
[DEBUG] (2026-04-25 11:42:22.322) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (5 iterations done (error initial=882.250768 final=241.873632), time=0.013530 s)

Optimizer/GravitySigma=0.001
[DEBUG] (2026-04-25 11:42:57.558) OptimizerGTSAM.cpp:783::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:42:57.561) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =242.969220
[DEBUG] (2026-04-25 11:42:57.564) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =242.887581
[DEBUG] (2026-04-25 11:42:57.567) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =242.887815
[DEBUG] (2026-04-25 11:42:57.567) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-0.000234 < 0.000010)
[DEBUG] (2026-04-25 11:42:57.570) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =242.887816
[DEBUG] (2026-04-25 11:42:57.570) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-0.000001 < 0.000010)
[DEBUG] (2026-04-25 11:42:57.573) OptimizerGTSAM.cpp:933::optimize() iteration 5 error =242.887816
[DEBUG] (2026-04-25 11:42:57.573) OptimizerGTSAM.cpp:955::optimize() Stop optimizing, not enough improvement (0.000000 < 0.000010)
[DEBUG] (2026-04-25 11:42:57.573) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (5 iterations done (error initial=3928.471689 final=242.887816), time=0.014909 s)

Optimizer/GravitySigma=0.0001
[DEBUG] (2026-04-25 11:43:19.560) OptimizerGTSAM.cpp:783::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:43:19.563) OptimizerGTSAM.cpp:933::optimize() iteration 1 error =248.230969
[DEBUG] (2026-04-25 11:43:19.566) OptimizerGTSAM.cpp:933::optimize() iteration 2 error =242.923743
[DEBUG] (2026-04-25 11:43:19.569) OptimizerGTSAM.cpp:933::optimize() iteration 3 error =242.923979
[DEBUG] (2026-04-25 11:43:19.569) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-0.000237 < 0.000010)
[DEBUG] (2026-04-25 11:43:19.572) OptimizerGTSAM.cpp:933::optimize() iteration 4 error =242.923980
[DEBUG] (2026-04-25 11:43:19.572) OptimizerGTSAM.cpp:951::optimize() Negative improvement?! Ignore and continue optimizing... (-0.000001 < 0.000010)
[DEBUG] (2026-04-25 11:43:19.574) OptimizerGTSAM.cpp:933::optimize() iteration 5 error =242.923980
[DEBUG] (2026-04-25 11:43:19.575) OptimizerGTSAM.cpp:955::optimize() Stop optimizing, not enough improvement (0.000000 < 0.000010)
[DEBUG] (2026-04-25 11:43:19.575) OptimizerGTSAM.cpp:982::optimize() GTSAM optimizing end (5 iterations done (error initial=308550.608979 final=242.923980), time=0.014365 s)

#######################
g2o gravity factors
#######################
Optimizer/GravitySigma=0.2
[ INFO] (2026-04-25 11:48:29.807) OptimizerG2O.cpp:1057::optimize() g2o optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:48:29.811) OptimizerG2O.cpp:1186::optimize() iteration 0: 470 nodes, 1218 edges, chi2: 470.399516
[DEBUG] (2026-04-25 11:48:29.813) OptimizerG2O.cpp:1186::optimize() iteration 1: 470 nodes, 1218 edges, chi2: 465.769992
[DEBUG] (2026-04-25 11:48:29.815) OptimizerG2O.cpp:1186::optimize() iteration 2: 470 nodes, 1218 edges, chi2: 465.768774
[DEBUG] (2026-04-25 11:48:29.817) OptimizerG2O.cpp:1186::optimize() iteration 3: 470 nodes, 1218 edges, chi2: 465.768773
[ INFO] (2026-04-25 11:48:29.817) OptimizerG2O.cpp:1209::optimize() Stop optimizing, not enough improvement (0.000001 < 0.000010)
[ INFO] (2026-04-25 11:48:29.817) OptimizerG2O.cpp:1235::optimize() g2o optimizing end (4 iterations done, error=465.768773, time = 0.009690 s)

Optimizer/GravitySigma=0.1
[ INFO] (2026-04-25 11:48:55.576) OptimizerG2O.cpp:1057::optimize() g2o optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:48:55.579) OptimizerG2O.cpp:1186::optimize() iteration 0: 470 nodes, 1218 edges, chi2: 478.261698
[DEBUG] (2026-04-25 11:48:55.581) OptimizerG2O.cpp:1186::optimize() iteration 1: 470 nodes, 1218 edges, chi2: 467.780797
[DEBUG] (2026-04-25 11:48:55.583) OptimizerG2O.cpp:1186::optimize() iteration 2: 470 nodes, 1218 edges, chi2: 467.778371
[DEBUG] (2026-04-25 11:48:55.585) OptimizerG2O.cpp:1186::optimize() iteration 3: 470 nodes, 1218 edges, chi2: 467.778371
[ INFO] (2026-04-25 11:48:55.585) OptimizerG2O.cpp:1209::optimize() Stop optimizing, not enough improvement (0.000000 < 0.000010)
[ INFO] (2026-04-25 11:48:55.585) OptimizerG2O.cpp:1235::optimize() g2o optimizing end (4 iterations done, error=467.778371, time = 0.009344 s)

Optimizer/GravitySigma=0.05
[ INFO] (2026-04-25 11:49:18.633) OptimizerG2O.cpp:1057::optimize() g2o optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:49:18.636) OptimizerG2O.cpp:1186::optimize() iteration 0: 470 nodes, 1218 edges, chi2: 503.429160
[DEBUG] (2026-04-25 11:49:18.638) OptimizerG2O.cpp:1186::optimize() iteration 1: 470 nodes, 1218 edges, chi2: 469.308601
[DEBUG] (2026-04-25 11:49:18.640) OptimizerG2O.cpp:1186::optimize() iteration 2: 470 nodes, 1218 edges, chi2: 469.296336
[DEBUG] (2026-04-25 11:49:18.642) OptimizerG2O.cpp:1186::optimize() iteration 3: 470 nodes, 1218 edges, chi2: 469.296336
[ INFO] (2026-04-25 11:49:18.642) OptimizerG2O.cpp:1209::optimize() Stop optimizing, not enough improvement (0.000000 < 0.000010)
[ INFO] (2026-04-25 11:49:18.642) OptimizerG2O.cpp:1235::optimize() g2o optimizing end (4 iterations done, error=469.296336, time = 0.009469 s)

Optimizer/GravitySigma=0.01
[ INFO] (2026-04-25 11:49:41.671) OptimizerG2O.cpp:1057::optimize() g2o optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:49:41.675) OptimizerG2O.cpp:1186::optimize() iteration 0: 470 nodes, 1218 edges, chi2: 1241.107131
[DEBUG] (2026-04-25 11:49:41.677) OptimizerG2O.cpp:1186::optimize() iteration 1: 470 nodes, 1218 edges, chi2: 471.567753
[DEBUG] (2026-04-25 11:49:41.679) OptimizerG2O.cpp:1186::optimize() iteration 2: 470 nodes, 1218 edges, chi2: 471.194981
[DEBUG] (2026-04-25 11:49:41.681) OptimizerG2O.cpp:1186::optimize() iteration 3: 470 nodes, 1218 edges, chi2: 471.194978
[ INFO] (2026-04-25 11:49:41.681) OptimizerG2O.cpp:1209::optimize() Stop optimizing, not enough improvement (0.000004 < 0.000010)
[ INFO] (2026-04-25 11:49:41.681) OptimizerG2O.cpp:1235::optimize() g2o optimizing end (4 iterations done, error=471.194978, time = 0.010229 s)

Optimizer/GravitySigma=0.001
[ INFO] (2026-04-25 11:50:02.227) OptimizerG2O.cpp:1057::optimize() g2o optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:50:02.230) OptimizerG2O.cpp:1186::optimize() iteration 0: 470 nodes, 1218 edges, chi2: 85891.433338
[DEBUG] (2026-04-25 11:50:02.232) OptimizerG2O.cpp:1186::optimize() iteration 1: 470 nodes, 1218 edges, chi2: 523.688491
[DEBUG] (2026-04-25 11:50:02.234) OptimizerG2O.cpp:1186::optimize() iteration 2: 470 nodes, 1218 edges, chi2: 472.021506
[DEBUG] (2026-04-25 11:50:02.237) OptimizerG2O.cpp:1186::optimize() iteration 3: 470 nodes, 1218 edges, chi2: 472.017149
[DEBUG] (2026-04-25 11:50:02.238) OptimizerG2O.cpp:1186::optimize() iteration 4: 470 nodes, 1218 edges, chi2: 472.017149
[ INFO] (2026-04-25 11:50:02.238) OptimizerG2O.cpp:1209::optimize() Stop optimizing, not enough improvement (0.000000 < 0.000010)
[ INFO] (2026-04-25 11:50:02.239) OptimizerG2O.cpp:1235::optimize() g2o optimizing end (5 iterations done, error=472.017149, time = 0.011594 s)

Optimizer/GravitySigma=0.0001
[ INFO] (2026-04-25 11:50:31.903) OptimizerG2O.cpp:1057::optimize() g2o optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-25 11:50:31.906) OptimizerG2O.cpp:1186::optimize() iteration 0: 470 nodes, 1218 edges, chi2: 11179371.294846
[DEBUG] (2026-04-25 11:50:31.908) OptimizerG2O.cpp:1186::optimize() iteration 1: 470 nodes, 1218 edges, chi2: 20143.729783
[DEBUG] (2026-04-25 11:50:31.910) OptimizerG2O.cpp:1186::optimize() iteration 2: 470 nodes, 1218 edges, chi2: 483.311024
[DEBUG] (2026-04-25 11:50:31.912) OptimizerG2O.cpp:1186::optimize() iteration 3: 470 nodes, 1218 edges, chi2: 472.254135
[DEBUG] (2026-04-25 11:50:31.915) OptimizerG2O.cpp:1186::optimize() iteration 4: 470 nodes, 1218 edges, chi2: 472.254134
[ INFO] (2026-04-25 11:50:31.915) OptimizerG2O.cpp:1209::optimize() Stop optimizing, not enough improvement (0.000000 < 0.000010)
[ INFO] (2026-04-25 11:50:31.915) OptimizerG2O.cpp:1235::optimize() g2o optimizing end (5 iterations done, error=472.254134, time = 0.012074 s)

Another experiment was done using that map, without yaw fixed in gtsam optimization (hard-coding 2 here for yaw) and making sure the map is optimized every time a new node is added to the map with gravity constraint (changed !_memory->isOdomGravityUsed() for true here) to see better how much the optimization affects the yaw of the root node of the map. Note that RGBD/OptimizeFromGraphEnd is on and Optimizer/GravitySigma is set to 0.2.

The proposed GravityConstraint and the official AttitudeFactor perform similarly, respectively:

Peek.2026-04-25.13-40.mp4
Peek.2026-04-25.13-42.mp4

while the original GravityConstraint makes the old map drifting in yaw every time a new gravity constraint is added.

Peek.2026-04-25.13-45.mp4

Conclusion

Based on the results above, I'ld suggest to delete GravityFactor.h and GravityFactor.cpp files in this PR, and use Pose3AttitudeFactor as suggested.

EDIT: All experiments in this post were done on ROS2 Humble with gtsam ros binaries (4.2.0)

@borongyuan

Copy link
Copy Markdown
Contributor Author

I don't remember exactly the reason why I extracted roll/pitch in AttitudeFactor explicitly. My guess is that I had a map that was rotating along yaw when optimized with original attitude factors. Probably the bug was somewhere else though (and fixed since then?).

This may be because the variable names in the AttitudeFactor were misleading, leading to the incorrect use of the measurement and reference terms. I initially referred to the GTSAM 4.2 code, which I also misunderstood, so I first modified the Jacobian calculation in the GravityFactor. However, after reviewing the naming changes in GTSAM 4.3, I realized that nZ should actually be the fixed reference term, while bRef is the measurement term. Therefore nZ and bRef were later renamed to nRef and bMeasured respectively. The error is zero when: nRb * bMeasured == nRef. As long as nRef takes the gravity vector under the navigation frame, yaw will naturally be in the null space of the Jacobian.

  • Proposed GravityFactor is causing an exception:
    [DEBUG] (2026-04-25 13:11:14.866) OptimizerGTSAM.cpp:782::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
    terminate called after throwing an instance of 'std::runtime_error'
      what():  Rot3::RQ : Derivative undefined at singularity (gimbal lock)
    Aborted (core dumped)
    

I also encountered this problem when the changes were applied to the latest master branch, but it did not occur when applied to the humble-devel branch.

I haven't removed the GravityFactor code from the PR yet, but this part is no longer in use. I was planning to wait for your review before removing it. GravityFactor.h contains the directive "using namespace gtsam", which was previously required for some other code to compile. I added the namespace prefix to other code so that the GravityFactor code can be safely removed.

@matlabbe

matlabbe commented Apr 26, 2026 •

Copy link
Copy Markdown
Member

As long as nRef takes the gravity vector under the navigation frame, yaw will naturally be in the null space of the Jacobian.

That is something that I've may overlooked the first time, but looking at it now it makes sense that yaw doesn't matter.

I haven't removed the GravityFactor code from the PR yet, but this part is no longer in use. I was planning to wait for your review before removing it. GravityFactor.h contains the directive "using namespace gtsam", which was previously required for some other code to compile. I added the namespace prefix to other code so that the GravityFactor code can be safely removed.

That's fine!

I updated my previous post with the GTSAM version that I used, which was 4.2.0 from ROS2 humble. GTSAM 4.2.0 was also used for ROS2 Jazzy and latest ROS noetic, so the results above would still stand for people/robots still on ROS1. I am gonna redo the same experiments I did with GTSAM 4.3a0-ros (used by ROS2 Kilted and ROS2 Rolling), and latest GTSAM built from source. If it works, I'll update your PR with the changes, and remove GravityFactor class.

…rce. Require c++17 when building with GTSAM>=4.3.0.
@matlabbe

matlabbe commented Apr 26, 2026 •

Copy link
Copy Markdown
Member

I confirmed that on 4.3.0a-ros and latest GTSAM (version of april 12 2026) work as expected, here with Optimizer/GravitySigma=0.0001.

4.3.0a-ros
[DEBUG] (2026-04-26 11:44:17.366) OptimizerGTSAM.cpp:782::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-26 11:44:17.369) OptimizerGTSAM.cpp:932::optimize() iteration 1 error =181.313990
[DEBUG] (2026-04-26 11:44:17.371) OptimizerGTSAM.cpp:932::optimize() iteration 2 error =157.591881
[DEBUG] (2026-04-26 11:44:17.373) OptimizerGTSAM.cpp:932::optimize() iteration 3 error =157.489250
[DEBUG] (2026-04-26 11:44:17.376) OptimizerGTSAM.cpp:932::optimize() iteration 4 error =157.489046
[DEBUG] (2026-04-26 11:44:17.378) OptimizerGTSAM.cpp:932::optimize() iteration 5 error =157.489043
[DEBUG] (2026-04-26 11:44:17.378) OptimizerGTSAM.cpp:954::optimize() Stop optimizing, not enough improvement (0.000003 < 0.000010)
[DEBUG] (2026-04-26 11:44:17.378) OptimizerGTSAM.cpp:981::optimize() GTSAM optimizing end (5 iterations done (error initial=504922.421264 final=157.489046), time=0.012214 s)

April 12 2026 (511952c52c42637e8f5719d38917aebf29c004fd)
[DEBUG] (2026-04-26 19:49:38.387) OptimizerGTSAM.cpp:782::optimize() GTSAM optimizing begin (max iterations=10, robust=0)
[DEBUG] (2026-04-26 19:49:38.390) OptimizerGTSAM.cpp:932::optimize() iteration 1 error =181.313990
[DEBUG] (2026-04-26 19:49:38.393) OptimizerGTSAM.cpp:932::optimize() iteration 2 error =157.591881
[DEBUG] (2026-04-26 19:49:38.395) OptimizerGTSAM.cpp:932::optimize() iteration 3 error =157.489250
[DEBUG] (2026-04-26 19:49:38.396) OptimizerGTSAM.cpp:932::optimize() iteration 4 error =157.489046
[DEBUG] (2026-04-26 19:49:38.398) OptimizerGTSAM.cpp:932::optimize() iteration 5 error =157.489043
[DEBUG] (2026-04-26 19:49:38.398) OptimizerGTSAM.cpp:954::optimize() Stop optimizing, not enough improvement (0.000003 < 0.000010)
[DEBUG] (2026-04-26 19:49:38.398) OptimizerGTSAM.cpp:981::optimize() GTSAM optimizing end (5 iterations done (error initial=504922.421264 final=157.489046), time=0.010502 s)

@matlabbe
matlabbe merged commit ebba7e4 into introlab:master Apr 26, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants