Corentin Chauvin-Hameau – 2019-2020
Coverage Path Planning for an underwater robot surveying a marine farm
experiment_stats.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  *
4  * \brief Declaration of a node to measure statistics about experiments.
5  * \author Corentin Chauvin-Hameau
6  * \date 2020
7  */
8 
9 #ifndef EXPERIMENT_STATS_HPP
10 #define EXPERIMENT_STATS_HPP
11 
12 #include "mf_common/EulerPose.h"
13 #include "mf_common/Array2D.h"
14 #include "mf_common/Float32Array.h"
15 #include "mf_farm_simulator/Algae.h"
16 #include <nav_msgs/Odometry.h>
17 #include <nav_msgs/Path.h>
18 #include <geometry_msgs/Pose.h>
19 #include <tf2_ros/transform_listener.h>
20 #include <ros/ros.h>
21 #include <fstream>
22 #include <string>
23 #include <vector>
24 
25 
26 namespace mfcpp {
27 
28 /**
29  * \brief Class to measure statistics about experiments
30  *
31  * Measures different statistics and publishes it to ROS and to a text file.
32  * Statistics measured:
33  * - control tracking error
34  * - trace of the Gaussian Process covariance
35  * - information (weighted sum of the diagonal coefficients of the covariance of the GP)
36  * - mapping error
37  */
39  public:
42 
43  /**
44  * \brief Runs the node
45  */
46  void run_node();
47 
48  private:
49  // Private members
50  ros::NodeHandle nh_; ///< Node handler
51  ros::Subscriber odom_sub_; ///< Subscriber for robot odometry
52  ros::Subscriber path_sub_; ///< Subscriber for the reference path
53  ros::Subscriber gp_mean_sub_; ///< Subscriber for mean of the GP
54  ros::Subscriber gp_cov_sub_; ///< Subscriber for covariance of the GP
55  ros::Subscriber gp_eval_sub_; ///< Subscriber for the evaluated GP
56  ros::Subscriber algae_sub_; ///< Subscriber for the farm algae
57  ros::Publisher ref_pub_; ///< Publisher for the reference pose
58  ros::Publisher error_pub_; ///< Publisher for the tracking error
59  ros::Publisher diff_img_pub_; ///< Publisher for the image of the difference between the mapped GP and the algae heatmap
60  ros::ServiceClient load_gp_client_; ///< Service client for loading the GP
61  tf2_ros::Buffer tf_buffer_; ///< Buffer for tf2
62  tf2_ros::TransformListener tf_listener_; ///< Transform listener for tf2
63 
64  nav_msgs::Odometry odom_; ///< Last odometry message received
65  nav_msgs::Path path_; ///< Reference path to follow
66  std::vector<float> gp_mean_; ///< Mean of the Gaussian Process
67  std::vector<float> gp_cov_; ///< Diagonal of the covariance of the Gaussian Proces
68  std::vector<float> init_gp_cov_; ///< Diagonal of the initial covariance of the GP
69  std::vector<std::vector<float>> gp_eval_; ///< Evaluated GP
70  std::vector<std::vector<std::vector<float>>> algae_heatmaps_; ///< Disease heatmaps of the algae
71  bool odom_received_; ///< Whether odometry has been received
72  bool path_received_; ///< Whether the reference path has been received
73  bool gp_mean_received_; ///< Whether the mean of the GP has been received
74  bool gp_cov_received_; ///< Whether the covariance of the GP has been received
75  bool gp_eval_received_; ///< Whether the evaluated GP has been received
76  bool algae_received_; ///< Whether the farm algae have been received
77  float moved_distance_; ///< Distance moved by the robot since the start
78 
79  double start_time_; ///< Start time of the test
80  std::ofstream out_file_; ///< Output CSV file containg data of the experiment
81 
82  /// \name ROS parameters
83  ///@{
84  float main_freq_; ///< Frequency of the main loop
85  float gp_threshold_; ///< Threshold to consider a GP component in information gain computation
86  bool save_gp_; ///< Whether to save the Gaussian Process at the end
87  bool load_gp_; ///< Whether to load the Gaussian Process at the beginning
88  std::string gp_file_name_; ///< Name/path of the file where to save the GP
89  ///@}
90 
91 
92  /**
93  * \brief Initialises the node and its parameters
94  */
95  void init_node();
96 
97  /**
98  * \brief Finds the closest pose in a path
99  *
100  * \param[in] path List of poses
101  * \param[in] pose Current pose
102  * \return Closest pose to `pose` in `path`
103  */
104  geometry_msgs::Pose find_closest(
105  const std::vector<geometry_msgs::PoseStamped> &path,
106  const geometry_msgs::Pose &pose
107  );
108 
109  /**
110  * \brief Computes the difference between two arrays
111  *
112  * Computes the 2-norm of the difference between the two arrays, and
113  * publishes an image corresponding to the difference.
114  *
115  * The arrays don't need to be of the same size (but of same proportions).
116  * The values will be scaled out by their maximum value.
117  *
118  * \param array1 First array
119  * \param array2 Second array
120  * \return 2-norm of the difference of the two arrays
121  */
122  float compute_diff(
123  const std::vector<std::vector<float>> &array1,
124  const std::vector<std::vector<float>> &array2
125  );
126 
127  /**
128  * \brief Writes the output statistics in the out file
129  *
130  * \param pose Current pose
131  * \param reference Reference pose
132  * \param error Error between current pose and reference
133  * \param gp_cov_trace Trace of the covariance of the Gaussian Process
134  * \param information Information (weighted sum of the diagonal coefficients of the covariance of the GP)
135  * \param gp_diff_norm Norm of the difference between the evaluated GP and the algae disease heatmaps
136  */
137  void write_output(
138  const geometry_msgs::Pose &pose,
139  const geometry_msgs::Pose &reference,
140  const mf_common::EulerPose &error,
141  float gp_cov_trace,
142  float information,
143  float gp_diff_norm
144  );
145 
146  /**
147  * \brief Loads the GP mean from a file
148  *
149  * \param[in] file_name Path of the file where to load the GP
150  * \param[out] gp_mean Loaded gp_mean
151  * \return Whether the file could be read
152  */
153  bool load_gp(
154  std::string file_name,
155  std::vector<float> &gp_mean
156  );
157 
158  /**
159  * \brief Saves the mean of the Gaussian Process in a file
160  */
161  void save_gp();
162 
163  /**
164  * \brief Callback for odometry
165  *
166  * Increments distance moved by the robot
167  */
168  void odom_cb(const nav_msgs::Odometry msg);
169 
170  /**
171  * \brief Callback for the reference path
172  */
173  void path_cb(const nav_msgs::Path msg);
174 
175  /**
176  * \brief Callback for the mean of the Gaussian Process
177  */
178  void gp_mean_cb(const mf_common::Float32Array msg);
179 
180  /**
181  * \brief Callback for the covariance of the Gaussian Process
182  */
183  void gp_cov_cb(const mf_common::Array2D msg);
184 
185  /**
186  * \brief Callback for the evaluated GP
187  */
188  void gp_eval_cb(const mf_common::Array2D msg);
189 
190  /**
191  * \brief Callback for farm algae
192  */
193  void algae_cb(const mf_farm_simulator::Algae msg);
194 
195 
196 };
197 
198 
199 } // namespace mfcpp
200 
201 #endif
void save_gp()
Saves the mean of the Gaussian Process in a file.
void algae_cb(const mf_farm_simulator::Algae msg)
Callback for farm algae.
float moved_distance_
Distance moved by the robot since the start.
Definition: common.hpp:23
ros::Subscriber path_sub_
Subscriber for the reference path.
bool gp_cov_received_
Whether the covariance of the GP has been received.
std::vector< float > init_gp_cov_
Diagonal of the initial covariance of the GP.
float gp_threshold_
Threshold to consider a GP component in information gain computation.
ros::Subscriber odom_sub_
Subscriber for robot odometry.
float main_freq_
Frequency of the main loop.
void gp_mean_cb(const mf_common::Float32Array msg)
Callback for the mean of the Gaussian Process.
void gp_eval_cb(const mf_common::Array2D msg)
Callback for the evaluated GP.
std::vector< float > gp_cov_
Diagonal of the covariance of the Gaussian Proces.
double start_time_
Start time of the test.
ros::Publisher ref_pub_
Publisher for the reference pose.
ros::Publisher diff_img_pub_
Publisher for the image of the difference between the mapped GP and the algae heatmap.
nav_msgs::Path path_
Reference path to follow.
bool odom_received_
Whether odometry has been received.
ros::Subscriber gp_cov_sub_
Subscriber for covariance of the GP.
std::vector< std::vector< std::vector< float > > > algae_heatmaps_
Disease heatmaps of the algae.
std::vector< float > gp_mean_
Mean of the Gaussian Process.
void gp_cov_cb(const mf_common::Array2D msg)
Callback for the covariance of the Gaussian Process.
bool load_gp(std::string file_name, std::vector< float > &gp_mean)
Loads the GP mean from a file.
bool path_received_
Whether the reference path has been received.
Class to measure statistics about experiments.
tf2_ros::TransformListener tf_listener_
Transform listener for tf2.
ros::Subscriber algae_sub_
Subscriber for the farm algae.
void odom_cb(const nav_msgs::Odometry msg)
Callback for odometry.
void path_cb(const nav_msgs::Path msg)
Callback for the reference path.
bool gp_mean_received_
Whether the mean of the GP has been received.
float compute_diff(const std::vector< std::vector< float >> &array1, const std::vector< std::vector< float >> &array2)
Computes the difference between two arrays.
bool save_gp_
Whether to save the Gaussian Process at the end.
ros::NodeHandle nh_
Node handler.
std::vector< std::vector< float > > gp_eval_
Evaluated GP.
std::ofstream out_file_
Output CSV file containg data of the experiment.
bool load_gp_
Whether to load the Gaussian Process at the beginning.
geometry_msgs::Pose find_closest(const std::vector< geometry_msgs::PoseStamped > &path, const geometry_msgs::Pose &pose)
Finds the closest pose in a path.
ros::Subscriber gp_mean_sub_
Subscriber for mean of the GP.
ros::Subscriber gp_eval_sub_
Subscriber for the evaluated GP.
ros::Publisher error_pub_
Publisher for the tracking error.
bool gp_eval_received_
Whether the evaluated GP has been received.
nav_msgs::Odometry odom_
Last odometry message received.
void init_node()
Initialises the node and its parameters.
bool algae_received_
Whether the farm algae have been received.
void write_output(const geometry_msgs::Pose &pose, const geometry_msgs::Pose &reference, const mf_common::EulerPose &error, float gp_cov_trace, float information, float gp_diff_norm)
Writes the output statistics in the out file.
void run_node()
Runs the node.
ros::ServiceClient load_gp_client_
Service client for loading the GP.
tf2_ros::Buffer tf_buffer_
Buffer for tf2.