Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
graph_slam_instance.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <gtsam/geometry/Pose2.h>
4#include <gtsam/inference/Symbol.h>
5#include <gtsam/nonlinear/NonlinearFactorGraph.h>
6#include <gtsam/nonlinear/Values.h>
7
12
19protected:
20 struct TimedPose {
21 unsigned int pose_id;
22 rclcpp::Time timestamp;
23 };
24
25 gtsam::NonlinearFactorGraph
26 _factor_graph_; //< Factor graph for the graph SLAM solver (only factors, no estimates)
27 gtsam::Values
28 _graph_values_; //< Estimate for the graph SLAM solver: l_ are landmarks, x_ are poses
29 unsigned int _pose_counter_ = 0; //< Counter for the pose symbols
30 unsigned int _landmark_counter_ = 0; //< Counter for the landmark symbols
32 false; //< Flag to check if the pose was updated before adding observations
34 false; //< Flag to check if new factors were added to the graph for optimization
35 bool _locked_landmarks_ = false; //< Flag to check if landmarks are locked in the graph
36
37 SLAMParameters _params_; //< Parameters for the SLAM solver
38 std::shared_ptr<BaseOptimizer> _optimizer_; //< Optimizer for the graph SLAM solver
39
41 _pose_timestamps_; //< Buffer to hold the timestamps of the poses added to the graph
42
43 unsigned int get_landmark_id_at_time(const rclcpp::Time& timestamp) const;
44
45public:
46 GraphSLAMInstance() = default;
47
48 GraphSLAMInstance(const SLAMParameters& params, std::shared_ptr<BaseOptimizer> optimizer);
49
51
53
54 ~GraphSLAMInstance() = default;
55
63 std::shared_ptr<GraphSLAMInstance> clone() const {
64 return std::make_shared<GraphSLAMInstance>(*this);
65 }
66
72 bool new_pose_factors() const;
73
79 bool new_observation_factors() const;
80
85 unsigned int get_landmark_counter() const;
86
91 unsigned int get_pose_counter() const;
92
98 const gtsam::Pose2 get_pose() const;
99
105 const gtsam::Values& get_graph_values_reference() const;
106
112 Eigen::VectorXd get_state_vector() const;
113
119 Eigen::MatrixXd get_covariance_matrix() const;
120
126 void process_observations(const ObservationData& observation_data);
127
136 void load_initial_state(const Eigen::VectorXd& map, const Eigen::Vector3d& pose,
137 double preloaded_map_noise);
146 void process_new_pose(const Eigen::Vector3d& pose_difference, const Eigen::Vector3d& noise_vector,
147 const Eigen::Vector3d& new_pose, const rclcpp::Time& pose_timestamp);
148
156 void process_pose_difference(const Eigen::Vector3d& pose_difference,
157 const Eigen::Vector3d& noise_vector, unsigned int before_pose_id,
158 unsigned int after_pose_id);
159
167 void process_pose_difference(const Eigen::Vector3d& pose_difference,
168 const Eigen::Vector3d& noise_vector, unsigned int before_pose_id);
169
176 void process_pose_difference(const Eigen::Vector3d& pose_difference,
177 const Eigen::Vector3d& noise_vector);
178
183 void optimize();
184
189 void lock_landmarks();
190};
Graph SLAM instance class - class to hold the factor graph and the values.
unsigned int get_landmark_id_at_time(const rclcpp::Time &timestamp) const
const gtsam::Values & get_graph_values_reference() const
Get the graph values reference.
unsigned int get_landmark_counter() const
Get the number of landmarks.
GraphSLAMInstance & operator=(const GraphSLAMInstance &other)
Eigen::VectorXd get_state_vector() const
Get the state vector, EKF style.
unsigned int get_pose_counter() const
Get the number of poses.
void lock_landmarks()
Soft locks the positions of the landmarks, can be used after the first lap.
bool new_pose_factors() const
Checks if new observation factors should be added.
unsigned int _landmark_counter_
~GraphSLAMInstance()=default
void load_initial_state(const Eigen::VectorXd &map, const Eigen::Vector3d &pose, double preloaded_map_noise)
Loads a map and an initial pose into the graph.
gtsam::NonlinearFactorGraph _factor_graph_
void process_observations(const ObservationData &observation_data)
Add observation factors to the graph.
void process_pose_difference(const Eigen::Vector3d &pose_difference, const Eigen::Vector3d &noise_vector, unsigned int before_pose_id, unsigned int after_pose_id)
Process a pose difference and add the respective factor to the graph.
void process_new_pose(const Eigen::Vector3d &pose_difference, const Eigen::Vector3d &noise_vector, const Eigen::Vector3d &new_pose, const rclcpp::Time &pose_timestamp)
Process a new pose and respective pose difference.
std::shared_ptr< BaseOptimizer > _optimizer_
std::shared_ptr< GraphSLAMInstance > clone() const
Clone the graph SLAM instance.
void optimize()
Runs optimization on the graph.
const gtsam::Pose2 get_pose() const
Get the current pose estimate.
CircularBuffer< TimedPose > _pose_timestamps_
GraphSLAMInstance()=default
bool new_observation_factors() const
Checks if it is worth running optimization.
Eigen::MatrixXd get_covariance_matrix() const
Get the covariance matrix of the graph.
Struct containing observation data.
Parameters for the SLAM node.