Repository navigation
Correct the Jacobian calculation of GravityFactor - #1692
Conversation
|
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. |
|
I don't remember exactly the reason why I extracted roll/pitch in I compared original
Overall:
Here some detailed logs (click to show)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 The proposed Peek.2026-04-25.13-40.mp4Peek.2026-04-25.13-42.mp4while the original Peek.2026-04-25.13-45.mp4ConclusionBased on the results above, I'ld suggest to delete EDIT: All experiments in this post were done on ROS2 Humble with gtsam ros binaries (4.2.0) |
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.
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. |
That is something that I've may overlooked the first time, but looking at it now it makes sense that yaw doesn't matter.
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 |
…rce. Require c++17 when building with GTSAM>=4.3.0.
|
I confirmed that on 4.3.0a-ros and latest GTSAM (version of april 12 2026) work as expected, here with |


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.