Skip to content

Conversation

mkielo3
Copy link
Contributor

@mkielo3 mkielo3 commented Feb 20, 2025

Fixed the elaboratePoint2KalmanFilter.cpp with:

  • Direct JacobianFactor creation
  • Updated vector creation
  • Updated data structure management. insert() is now push_back()

Additionally fixed minor bug in easyPoint2KalmanFilter which predicted with wrong factor.

X1 Predict[
1;
0
]
X1 Update[
1;
0
]
X2 Predict[
2;
0
]
X2 Update[
2;
0
]
X3 Predict[
3;
0
]
X3 Update[
3;
0
]

Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. I left a bunch of comments with recommendations, and it would be nice to include the output in the Pr comment, but I am approving. I trust you to implement comments where you agree with them.

@@ -55,17 +55,21 @@ int main() {

// Create new state variable
Symbol x0('x',0);
ordering->insert(x0, 0);
ordering->push_back(x0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use X(k) everywhere.

using symbol_shorthand::X;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I mean: kill x0, x1, x2 variables. Use X(k) in-place everywhere. Slightly less efficient but good practice for copy/paste in other code.


// Initialize state x0 (2D point) at origin by adding a prior factor, i.e., Bayes net P(x0)
// This is equivalent to x_0 and P_0
Point2 x_initial(0,0);
SharedDiagonal P_initial = noiseModel::Diagonal::Sigmas((Vec(2) << 0.1, 0.1));
PriorFactor<Point2> factor1(x0, x_initial, P_initial);
SharedDiagonal P_initial = noiseModel::Diagonal::Sigmas((gtsam::Vector2() << 0.1, 0.1).finished());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Isotropic

// Create a JacobianFactor directly - this represents the prior constraint on x0
JacobianFactor::shared_ptr factor1(
new JacobianFactor(x0, P_initial->R(), Vector::Zero(2),
noiseModel::Unit::Create(2)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure but you might even omit noise model if Unit

// Linearize the factor and add it to the linear factor graph
linearizationPoints.insert(x0, x_initial);
linearFactorGraph->push_back(factor1.linearize(linearizationPoints, *ordering));

linearFactorGraph->push_back(factor1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use emplace_shared if you’re creating the shared pointer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> emplace_shared(args)
OR
-> add(args)

so make_shared above is no longer needed.


Point2 difference(1,0);
SharedDiagonal Q = noiseModel::Diagonal::Sigmas((Vec(2) << 0.1, 0.1));
SharedDiagonal Q = noiseModel::Diagonal::Sigmas((gtsam::Vector2() << 0.1, 0.1).finished());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isotropic

@@ -88,14 +92,14 @@ int main() {
// = (F - I)*x_{t} + B*u_{t}
// = B*u_{t} (for our example)
Symbol x1('x',1);
ordering->insert(x1, 1);
ordering->push_back(x1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X(1)

SharedDiagonal P_initial = noiseModel::Diagonal::Sigmas((gtsam::Vector2() << 0.1, 0.1).finished());

// Create a JacobianFactor directly - this represents the prior constraint on x0
JacobianFactor::shared_ptr factor1(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto factor1 = std::make_shared….

@@ -169,10 +183,10 @@ int main() {
// = (x_{t} - z_{t}) * R^-1 * (x_{t} - z_{t})^T
// This can be modeled using the PriorFactor, where the mean is z_{t} and the covariance is R.
Point2 z1(1.0, 0.0);
SharedDiagonal R1 = noiseModel::Diagonal::Sigmas((Vec(2) << 0.25, 0.25));
SharedDiagonal R1 = noiseModel::Diagonal::Sigmas((gtsam::Vector2() << 0.25, 0.25).finished());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isotropic

assert(cg1->nrParents() == 0);
JacobianFactor tmpPrior1 = JacobianFactor(*cg1);
linearFactorGraph->add(0, tmpPrior1.getA(tmpPrior1.begin()), tmpPrior1.getb() - tmpPrior1.getA(tmpPrior1.begin()) * result[ordering->at(x1)], tmpPrior1.get_model());
JacobianFactor::shared_ptr updatedPrior(new JacobianFactor(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto make_shared is the recommended style now

Point2 x3_update = linearizationPoints.at<Point2>(x3).retract(result[ordering->at(x3)]);
x3_update.print("X3 Update");
VectorValues updatedResult3 = updatedBayesNet3->optimize();
Point2 x3_update = linearizationPoints.at<Point2>(x3) + Point2(updatedResult3[x3]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think you need the Point2()

@mkielo3
Copy link
Contributor Author

mkielo3 commented Feb 20, 2025

Thanks! I've added your suggestions. Waiting for CI checks to complete.

@mkielo3
Copy link
Contributor Author

mkielo3 commented Feb 20, 2025

Adding for reference, expected output is:

X1 Predict[
1;
0
]
X1 Update[
1;
0
]
X2 Predict[
2;
0
]
X2 Update[
2;
0
]
X3 Predict[
3;
0
]
X3 Update[
3;
0
]

@dellaert
Copy link
Member

Two more comments. And, request: update the PR comment with the actual output in a block. Do this by edit button at the top.

@mkielo3
Copy link
Contributor Author

mkielo3 commented Feb 20, 2025

Added

@dellaert
Copy link
Member

Very cool, I will merge after CI passes! Next, Python version? We are moving to have Python examples as notebooks, so maybe immediately make a notebook? Will require exposing in wrapper of course.

@mkielo3
Copy link
Contributor Author

mkielo3 commented Feb 21, 2025

Great, and yes- working on this!

@dellaert dellaert merged commit 08147c7 into borglab:develop Feb 21, 2025
36 checks passed
@dellaert
Copy link
Member

Congrats on first PR :-)

@mkielo3 mkielo3 deleted the fix-ekf-examples branch February 21, 2025 14:32
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