Corentin Chauvin-Hameau – 2019-2020
Coverage Path Planning for an underwater robot surveying a marine farm
farm_nodelet.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  *
4  * \brief Declaration of nodelet for managing the farm simulation
5  * \author Corentin Chauvin-Hameau
6  * \date 2019
7  */
8 
9 #ifndef FARM_NODELET_HPP
10 #define FARM_NODELET_HPP
11 
12 #include "farm_common.hpp"
13 #include "rviz_visualisation.hpp"
14 #include "mf_farm_simulator/FarmSimulatorConfig.h"
16 #include <visualization_msgs/Marker.h>
17 #include <visualization_msgs/MarkerArray.h>
18 #include <dynamic_reconfigure/server.h>
19 #include <nodelet/nodelet.h>
20 #include <ros/ros.h>
21 #include <string>
22 #include <vector>
23 #include <cstdlib>
24 #include <csignal>
25 
26 
27 namespace mfcpp {
28  /**
29  * \brief Nodelet class for farm simulation
30  */
31  class FarmNodelet: public nodelet::Nodelet {
32  public:
33  FarmNodelet();
34  ~FarmNodelet();
35 
36  /**
37  * \brief Function called at beginning of nodelet execution
38  */
39  virtual void onInit();
40 
41  private:
42  // Static members
43  // Note: the timers need to be static since stopped by the SIGINT callback
44  static sig_atomic_t volatile b_sigint_; ///< Whether SIGINT signal has been received
45  static ros::Timer init_timer_; ///< Timer callback for the init function
46  static ros::Timer main_timer_; ///< Timer callback for the main function
47 
48  // Private members
49  ros::NodeHandle nh_; ///< Node handler (for topics and services)
50  ros::NodeHandle private_nh_; ///< Private node handler (for parameters)
51  dynamic_reconfigure::Server<mf_farm_simulator::FarmSimulatorConfig> reconf_srv_; ///< Dynamic reconfigure server
52  ros::Publisher rviz_pub_; ///< ROS publisher for Rviz
53  ros::Publisher algae_pub_; ///< ROS publisher for the algae
54  std::vector<AlgaeLine> algae_lines_; ///< Vector of all the algae in the farm
55  bool reconfigure_initialised_; ///< Whether the dynamic reconfigure callback has been called once
56  bool init_done_; ///< Whether the farm initialisation has been done
57  std::random_device random_device_; ///< Seed initialiser for random number generation
58  std::mt19937 random_generator_; ///< Random number initialiser
59  PerlinNoiseGenerator perlin_; ///< For randomising the heatmaps
60 
61 
62  /// \name ROS parameters
63  ///@{
64  float main_loop_freq_; ///< Frequency of the main loop
65  int random_seed_; ///< Seed for random numbers (0 for random seed)
66  int nbr_lines_; ///< Number of algae lines
67  float offset_lines_; ///< Lateral distance (m) between each line
68  float length_lines_; ///< Length (m) of each line
69  float thickness_ropes_; ///< Diameter (m) of each line
70  float depth_lines_; ///< Distance (m) between water surface and line
71  float depth_water_; ///< Distance (m) between water surface and seafloor
72  float anchors_diameter_; ///< Diameter (m) of the cylindrical anchors
73  float anchors_height_; ///< Height (m) of the cylindrical anchors
74  int nbr_buoys_; ///< Number of buoys on each floating rope
75  float buoys_diameter_; ///< Diameter (m) of each buoy
76 
77  bool randomise_lines_; ///< Whether to randomise the position of each line
78  float alga_miss_rate_; ///< Probability to have a missing alga
79  float phi_lines_; ///< Mean of phi angle for algae line generation
80  float theta_lines_; ///< Mean of theta angle for algae line generation
81  float bnd_phi_lines_; ///< Bound such that phi is sampled in [mean-bnd, mean+bnd]
82  float bnd_theta_lines_; ///< Bound such that theta is sampled in [mean-bnd, mean+bnd]
83  float bnd_gamma_lines_; ///< Bound such that gamma is sampled in [mean-bnd, mean+bnd]
84 
85  bool randomise_algae_; ///< Whether to randomise size and orientation of algae
86  int nbr_algae_; ///< Number of algae per line
87  float width_algae_; ///< Mean of the width of an alga
88  float length_algae_; ///< Mean of the length of an alga
89  float thickness_algae_; ///< Thickness of an alga (for collision detection)
90  float psi_algae_; ///< Mean of the algae orientation
91  float std_width_algae_; ///< Standard deviation on algae width
92  float std_length_algae_; ///< Standard deviation on algae length
93  float std_psi_algae_; ///< Standard deviation on algae orientation
94 
95  bool disp_disease_; ///< Whether to display the disease heatmaps
96  float disease_ratio_; ///< Ratio of alga disease (0->fully sane, 1->fully sick)
97  int height_disease_heatmap_; ///< Height of the algae disease heatmap
98  int width_disease_heatmap_; ///< Width of the algae disease heatmap
99  int height_grid_heatmap_; ///< Height of the grid for perlin noise generation
100  int width_grid_heatmap_; ///< Width of the grid for perlin noise generation
101  ///@}
102 
103 
104  /**
105  * \brief Main callback which is called by a timer
106  *
107  * \param timer_event Timer event information
108  */
109  void main_cb(const ros::TimerEvent& timer_event);
110 
111  /**
112  * \brief SINGINT (Ctrl+C) callback to stop the nodelet properly
113  */
114  static void sigint_handler(int s);
115 
116  /**
117  * \brief Callback for dynamic reconfigure
118  *
119  * \param New configuration
120  * \param Change level
121  */
122  void reconfigure_cb(mf_farm_simulator::FarmSimulatorConfig &config,
123  uint32_t level);
124 
125  /**
126  * \brief Publishes the position and heatmap of the algae
127  */
128  void pub_algae();
129 
130 
131  /// \name Initialisation functions
132  ///@{
133  /**
134  * \brief Callback for the oneshot initialisation timer
135  *
136  * \param timer_event Timer event information
137  */
138  void init_cb(const ros::TimerEvent &timer_event);
139 
140  /**
141  * \brief Initialise the algae lines
142  */
143  void init_algae_lines();
144 
145  /**
146  * \brief Draw a random number from a Gaussian distribution
147  *
148  * \param mu Mean of the distribution
149  * \param sigma Standard deviation of the distribution
150  * \return Random number following a normal law (mu, sigma)
151  */
152  template <class T>
153  inline T rand_gaussian(T mu, T sigma);
154 
155  /**
156  * \brief Draw a random number from a uniform distribution
157  *
158  * \param a Lower bound
159  * \param b Upper bound
160  * \return Random number following a uniform law in [a, b]
161  */
162  template <class T>
163  inline T rand_uniform(T a, T b);
164 
165  /**
166  * \brief Draw a random number from a bernoulli distribution
167  *
168  * \param p Probability to get true
169  * \return Random number following a uniform law in [a, b]
170  */
171  bool rand_bernoulli(double p);
172 
173  /**
174  * \brief Init anchors of a specific algae line
175  *
176  * \param line Concerned algae line
177  * \param i Index of the algae line
178  */
179  void init_anchors(AlgaeLine &line, unsigned int i);
180 
181  /**
182  * \brief Init ropes of an algae line
183  *
184  * \param line Concerned algae line
185  */
186  void init_ropes(AlgaeLine &line);
187 
188  /**
189  * \brief Init algae of an algae line
190  *
191  * \param line Concerned algae line
192  */
193  void init_algae(AlgaeLine &line);
194 
195 
196  ///@}
197 
198  /// \name Visualisation functions
199  ///@{
200  /**
201  * \brief Displays objects by publishing Rviz markers
202  *
203  * \param duration Duration of the marker (in sec)
204  */
205  void pub_rviz_markers(float duration) const;
206 
207  /**
208  * \brief Populates a marker for displaying the buoys
209  *
210  * \param marker Marker to populate
211  * \param args Common arguments to fill ROS message
212  */
213  void pop_buoys_marker(visualization_msgs::Marker &marker,
214  MarkerArgs args) const;
215 
216  /**
217  * \brief Populates a marker for displaying the ropes
218  *
219  * \param marker Marker to populate
220  * \param args Common arguments to fill ROS message
221  */
222  void pop_ropes_marker(visualization_msgs::Marker &marker,
223  MarkerArgs args) const;
224 
225  /**
226  * \brief Populates a marker for displaying the algae
227  *
228  * \param marker Marker to populate
229  * \param args Common arguments to fill ROS message
230  */
231  void pop_algae_marker(visualization_msgs::Marker &marker,
232  MarkerArgs args) const;
233 
234  /**
235  * \brief Populates a marker for displaying the disease heatmaps
236  *
237  * \param marker Marker to populate
238  * \param args Common arguments to fill ROS message
239  */
240  void pop_algae_heatmaps(visualization_msgs::Marker &marker,
241  MarkerArgs args) const;
242 
243  /**
244  * \brief Populates a triangle marker for displaying images
245  *
246  * coord[0] | coord[1]
247  * -------------------
248  * coord[3] | coord[2]
249  *
250  * \note For speed issues, space for the concerned vectors should
251  * already be reserved.
252  *
253  * \param marker Marker to populate
254  * \param img Black and white image to add to the marker
255  * \param coord Four 3D coordinates of the corners of the image
256  */
257  void pop_img_marker(visualization_msgs::Marker &marker,
258  std::vector<std::vector<float>> img,
259  const std::vector<tf2::Vector3> &coord) const;
260  ///@}
261 
262  };
263 
264  /*
265  * Inline functions
266  */
267 
268  template <class T>
269  inline T FarmNodelet::rand_gaussian(T mu, T sigma)
270  {
271  std::normal_distribution<T> distribution(mu, sigma);
272  return distribution(random_generator_);
273  }
274 
275 
276  template <class T>
277  inline T FarmNodelet::rand_uniform(T a, T b)
278  {
279  std::uniform_real_distribution<T> distribution(a, b);
280  return distribution(random_generator_);
281  }
282 
283 
284  inline bool FarmNodelet::rand_bernoulli(double p)
285  {
286  std::bernoulli_distribution distribution(p);
287  return distribution(random_generator_);
288  }
289 
290 
291 
292 } // namespace mfcpp
293 
294 #endif
ros::NodeHandle private_nh_
Private node handler (for parameters)
float std_width_algae_
Standard deviation on algae width.
Definition: common.hpp:23
float buoys_diameter_
Diameter (m) of each buoy.
int nbr_lines_
Number of algae lines.
Declaration of 2D perlin noise generator.
T rand_gaussian(T mu, T sigma)
Draw a random number from a Gaussian distribution.
ros::Publisher rviz_pub_
ROS publisher for Rviz.
std::mt19937 random_generator_
Random number initialiser.
float anchors_diameter_
Diameter (m) of the cylindrical anchors.
float offset_lines_
Lateral distance (m) between each line.
float bnd_gamma_lines_
Bound such that gamma is sampled in [mean-bnd, mean+bnd].
int height_disease_heatmap_
Height of the algae disease heatmap.
PerlinNoiseGenerator perlin_
For randomising the heatmaps.
float std_length_algae_
Standard deviation on algae length.
bool reconfigure_initialised_
Whether the dynamic reconfigure callback has been called once.
Declaration of common structures and functions for farm simulator.
void init_algae_lines()
Initialise the algae lines.
Arguments used to create a Rviz maker.
void pub_rviz_markers(float duration) const
Displays objects by publishing Rviz markers.
Definition: farm_output.cpp:23
ros::NodeHandle nh_
Node handler (for topics and services)
void pub_algae()
Publishes the position and heatmap of the algae.
float bnd_phi_lines_
Bound such that phi is sampled in [mean-bnd, mean+bnd].
float depth_lines_
Distance (m) between water surface and line.
int nbr_buoys_
Number of buoys on each floating rope.
float anchors_height_
Height (m) of the cylindrical anchors.
int width_disease_heatmap_
Width of the algae disease heatmap.
float phi_lines_
Mean of phi angle for algae line generation.
Declaration of common Rviz display functions.
float psi_algae_
Mean of the algae orientation.
Perlin noise generator.
float thickness_algae_
Thickness of an alga (for collision detection)
void pop_img_marker(visualization_msgs::Marker &marker, std::vector< std::vector< float >> img, const std::vector< tf2::Vector3 > &coord) const
Populates a triangle marker for displaying images.
bool randomise_algae_
Whether to randomise size and orientation of algae.
float theta_lines_
Mean of theta angle for algae line generation.
bool randomise_lines_
Whether to randomise the position of each line.
void reconfigure_cb(mf_farm_simulator::FarmSimulatorConfig &config, uint32_t level)
Callback for dynamic reconfigure.
float alga_miss_rate_
Probability to have a missing alga.
std::random_device random_device_
Seed initialiser for random number generation.
float main_loop_freq_
Frequency of the main loop.
void main_cb(const ros::TimerEvent &timer_event)
Main callback which is called by a timer.
bool disp_disease_
Whether to display the disease heatmaps.
void init_anchors(AlgaeLine &line, unsigned int i)
Init anchors of a specific algae line.
void pop_algae_marker(visualization_msgs::Marker &marker, MarkerArgs args) const
Populates a marker for displaying the algae.
int height_grid_heatmap_
Height of the grid for perlin noise generation.
int nbr_algae_
Number of algae per line.
float depth_water_
Distance (m) between water surface and seafloor.
float width_algae_
Mean of the width of an alga.
void pop_algae_heatmaps(visualization_msgs::Marker &marker, MarkerArgs args) const
Populates a marker for displaying the disease heatmaps.
void pop_buoys_marker(visualization_msgs::Marker &marker, MarkerArgs args) const
Populates a marker for displaying the buoys.
Definition: farm_output.cpp:84
virtual void onInit()
Function called at beginning of nodelet execution.
std::vector< AlgaeLine > algae_lines_
Vector of all the algae in the farm.
float thickness_ropes_
Diameter (m) of each line.
void pop_ropes_marker(visualization_msgs::Marker &marker, MarkerArgs args) const
Populates a marker for displaying the ropes.
float length_lines_
Length (m) of each line.
int random_seed_
Seed for random numbers (0 for random seed)
static ros::Timer main_timer_
Timer callback for the main function.
ros::Publisher algae_pub_
ROS publisher for the algae.
void init_cb(const ros::TimerEvent &timer_event)
Callback for the oneshot initialisation timer.
float disease_ratio_
Ratio of alga disease (0->fully sane, 1->fully sick)
Line on which algae grow.
Definition: farm_common.hpp:58
void init_ropes(AlgaeLine &line)
Init ropes of an algae line.
float length_algae_
Mean of the length of an alga.
float std_psi_algae_
Standard deviation on algae orientation.
T rand_uniform(T a, T b)
Draw a random number from a uniform distribution.
static void sigint_handler(int s)
SINGINT (Ctrl+C) callback to stop the nodelet properly.
static ros::Timer init_timer_
Timer callback for the init function.
Nodelet class for farm simulation.
dynamic_reconfigure::Server< mf_farm_simulator::FarmSimulatorConfig > reconf_srv_
Dynamic reconfigure server.
static sig_atomic_t volatile b_sigint_
Whether SIGINT signal has been received.
bool init_done_
Whether the farm initialisation has been done.
void init_algae(AlgaeLine &line)
Init algae of an algae line.
float bnd_theta_lines_
Bound such that theta is sampled in [mean-bnd, mean+bnd].
bool rand_bernoulli(double p)
Draw a random number from a bernoulli distribution.