Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
isam2_optimizer.cpp
Go to the documentation of this file.
2
3#include <gtsam/geometry/Point2.h>
4#include <gtsam/geometry/Pose2.h>
5#include <gtsam/nonlinear/ISAM2.h>
6#include <gtsam/nonlinear/ISAM2Params.h>
7#include <gtsam/slam/BearingRangeFactor.h>
8#include <gtsam/slam/BetweenFactor.h>
9
10#include <algorithm>
11#include <cmath>
12#include <memory>
13#include <rclcpp/rclcpp.hpp>
14#include <sstream>
15#include <string>
16#include <unordered_set>
17
19 gtsam::ISAM2Params isam_params;
20 isam_params.relinearizeThreshold = params.slam_isam2_relinearize_threshold_;
21 isam_params.relinearizeSkip = params.slam_isam2_relinearize_skip_;
22 isam_params.factorization = params.slam_isam2_factorization_ == "QR"
23 ? gtsam::ISAM2Params::QR
24 : gtsam::ISAM2Params::CHOLESKY;
25 _isam2_ = gtsam::ISAM2(isam_params);
26 _last_estimate_ = gtsam::Values();
27 _new_factors_ = gtsam::NonlinearFactorGraph();
28 _new_values_ = gtsam::Values();
29};
30
32 this->_isam2_ = other._isam2_;
33 this->_last_estimate_ = other._last_estimate_;
34 this->_new_values_ = other._new_values_;
35 this->_new_factors_ = other._new_factors_;
36}
37
39 if (this == &other) return *this; // Prevent self-assignment
40
41 // Copy each member individually
43 this->_isam2_ = other._isam2_;
44 this->_last_estimate_ = other._last_estimate_;
45 this->_new_values_ = other._new_values_;
46 this->_new_factors_ = other._new_factors_;
47
48 return *this;
49}
50
51std::shared_ptr<BaseOptimizer> ISAM2Optimizer::clone() const {
52 return std::make_shared<ISAM2Optimizer>(*this);
53}
54
55gtsam::Values ISAM2Optimizer::optimize(gtsam::NonlinearFactorGraph& factor_graph,
56 [[maybe_unused]] gtsam::Values& graph_values,
57 [[maybe_unused]] unsigned int pose_num,
58 [[maybe_unused]] unsigned int landmark_num) {
59 RCLCPP_DEBUG(rclcpp::get_logger("slam"), "ISAM2Optimizer - Adding %zu new factors",
60 factor_graph.size());
61
62 // Ensure all keys in factor_graph exist in either _last_estimate_ or _new_values_ DONT KNOW IF
63 // CHANGES ANYTHING
64 for (const auto& factor : factor_graph) {
65 gtsam::KeyVector keys = factor->keys(); // use KeyVector directly
66 for (gtsam::Key key : keys) {
67 if (!_last_estimate_.exists(key) && !_new_values_.exists(key)) {
68 // Insert an initial guess
69 if (gtsam::Symbol(key).chr() == 'x') {
70 _new_values_.insert(key, gtsam::Pose2(0, 0, 0));
71 } else if (gtsam::Symbol(key).chr() == 'l') {
72 _new_values_.insert(key, gtsam::Point2(0, 0));
73 }
74 }
75 }
76 }
77
78 // Add new factors and values to iSAM2
79 _isam2_.update(this->_new_factors_, this->_new_values_);
80 _new_factors_.resize(0);
81 _new_values_.clear();
82
83 _last_estimate_ = _isam2_.calculateEstimate();
84 RCLCPP_DEBUG(rclcpp::get_logger("slam"), "ISAM2Optimizer - Optimization complete");
85 return _last_estimate_;
86}
Base class for graph optimizers.
BaseOptimizer & operator=(const BaseOptimizer &other)
ISAM2 optimizer for graph SLAM.
gtsam::Values _new_values_
gtsam::Values optimize(gtsam::NonlinearFactorGraph &factor_graph, gtsam::Values &graph_values, unsigned int pose_num, unsigned int landmark_num) override
Optimize the graph.
std::shared_ptr< BaseOptimizer > clone() const
Clone the optimizer.
gtsam::NonlinearFactorGraph _new_factors_
ISAM2Optimizer & operator=(const ISAM2Optimizer &other)
gtsam::ISAM2 _isam2_
gtsam::Values _last_estimate_
ISAM2Optimizer(const SLAMParameters &params)
Parameters for the SLAM node.
double slam_isam2_relinearize_skip_
double slam_isam2_relinearize_threshold_
std::string slam_isam2_factorization_