Swarm-SLAM  1.0.0
C-SLAM Framework
logger.h
Go to the documentation of this file.
1 #ifndef _CSLAM_LOGGER_H_
2 #define _CSLAM_LOGGER_H_
3 
4 #include <vector>
5 #include <string>
6 #include <fstream>
7 
8 #include <rclcpp/rclcpp.hpp>
9 
10 #include <gtsam/geometry/Pose3.h>
11 #include <gtsam/inference/Key.h>
12 #include <gtsam/inference/LabeledSymbol.h>
13 #include <gtsam/linear/NoiseModel.h>
14 #include <gtsam/nonlinear/GncOptimizer.h>
15 #include <gtsam/nonlinear/NonlinearFactorGraph.h>
16 #include <gtsam/nonlinear/Values.h>
17 #include <gtsam/slam/BetweenFactor.h>
18 #include <gtsam/slam/dataset.h>
19 
20 #include <ctime>
21 #include <chrono>
22 #include <iomanip>
23 
24 #include <sensor_msgs/msg/nav_sat_fix.hpp>
25 #include <diagnostic_msgs/msg/key_value.hpp>
26 
27 #include <cslam_common_interfaces/msg/pose_graph.hpp>
28 #include <cslam_common_interfaces/msg/inter_robot_matches.hpp>
29 
30 namespace cslam
31 {
32 
33  class Logger
34  {
39  public:
40  Logger(std::shared_ptr<rclcpp::Node> &node, const unsigned int &robot_id, const unsigned int &max_nb_robots, const std::string &log_folder);
41 
42  void add_pose_graph_log_info(const cslam_common_interfaces::msg::PoseGraph &msg);
43 
44  void log_initial_global_pose_graph(const gtsam::NonlinearFactorGraph::shared_ptr &graph,
45  const gtsam::Values::shared_ptr &initial);
46 
47  void log_optimized_global_pose_graph(const gtsam::NonlinearFactorGraph::shared_ptr &graph,
48  const gtsam::Values &result,
49  const unsigned int &origin_robot_id);
50 
51  void start_timer();
52 
53  void stop_timer();
54 
55  void write_logs();
56 
62  void log_callback(const diagnostic_msgs::msg::KeyValue::ConstSharedPtr msg);
63 
69  void log_matches_callback(const cslam_common_interfaces::msg::InterRobotMatches::ConstSharedPtr msg);
70 
71  void fill_msg(cslam_common_interfaces::msg::PoseGraph & msg);
72 
73  void log_pose_timestamp(const gtsam::LabeledSymbol & symbol, const int& sec, const int& nanosec);
74 
75  private:
76 
77  double compute_error(const gtsam::NonlinearFactorGraph::shared_ptr &graph,
78  const gtsam::Values::shared_ptr &result);
79 
80  std::vector<std::pair<std::pair<gtsam::LabeledSymbol, gtsam::LabeledSymbol>, double>> compute_inter_robot_loop_closure_errors(const gtsam::NonlinearFactorGraph::shared_ptr &graph,
81  const gtsam::Values::shared_ptr &result);
82 
83  std::shared_ptr<rclcpp::Node> node_;
84 
85  std::string log_folder_;
86  unsigned int robot_id_, origin_robot_id_, max_nb_robots_;
87  std::chrono::steady_clock::time_point start_time_;
88  uint64_t elapsed_time_, total_pgo_time_;
89 
90  std::map<unsigned int, std::map<unsigned int, sensor_msgs::msg::NavSatFix>> gps_values_;
91  std::pair<gtsam::NonlinearFactorGraph::shared_ptr, gtsam::Values::shared_ptr> initial_global_pose_graph_;
92  std::pair<gtsam::NonlinearFactorGraph::shared_ptr, gtsam::Values::shared_ptr> optimized_global_pose_graph_;
93  std::vector<cslam_common_interfaces::msg::PoseGraph> pose_graphs_log_info_;
94 
95  rclcpp::Subscription<diagnostic_msgs::msg::KeyValue>::SharedPtr
96  logger_subscriber_;
97 
98  rclcpp::Subscription<cslam_common_interfaces::msg::InterRobotMatches>::SharedPtr
99  inter_robot_matches_subscriber_;
100 
101  cslam_common_interfaces::msg::InterRobotMatches spectral_matches_;
102 
103  unsigned int log_nb_matches_, log_nb_failed_matches_, log_nb_vertices_transmitted_, log_nb_matches_selected_, log_detection_cumulative_communication_, log_local_descriptors_cumulative_communication_;
104  float log_sparsification_cumulative_computation_time_;
105 
106  std::map<gtsam::LabeledSymbol, std::pair<int, int>> pose_time_map_;
107  };
108 
109 } // namespace cslam
110 
111 #endif // _CSLAM_LOGGER_H_
void stop_timer()
Definition: logger.cpp:64
void log_initial_global_pose_graph(const gtsam::NonlinearFactorGraph::shared_ptr &graph, const gtsam::Values::shared_ptr &initial)
Definition: logger.cpp:44
void add_pose_graph_log_info(const cslam_common_interfaces::msg::PoseGraph &msg)
Definition: logger.cpp:39
void log_matches_callback(const cslam_common_interfaces::msg::InterRobotMatches::ConstSharedPtr msg)
Receive log messages.
Definition: logger.cpp:310
void fill_msg(cslam_common_interfaces::msg::PoseGraph &msg)
Definition: logger.cpp:325
void log_optimized_global_pose_graph(const gtsam::NonlinearFactorGraph::shared_ptr &graph, const gtsam::Values &result, const unsigned int &origin_robot_id)
Definition: logger.cpp:50
void start_timer()
Definition: logger.cpp:59
void write_logs()
Definition: logger.cpp:71
void log_pose_timestamp(const gtsam::LabeledSymbol &symbol, const int &sec, const int &nanosec)
Definition: logger.cpp:34
Logger(std::shared_ptr< rclcpp::Node > &node, const unsigned int &robot_id, const unsigned int &max_nb_robots, const std::string &log_folder)
Logger class to log various metrics.
Definition: logger.cpp:6
void log_callback(const diagnostic_msgs::msg::KeyValue::ConstSharedPtr msg)
Receive log messages.
Definition: logger.cpp:274
Definition: __init__.py:1