Corentin Chauvin-Hameau – 2019-2020
Coverage Path Planning for an underwater robot surveying a marine farm
gp_output.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  *
4  * \brief Definition of Gaussian Process functions
5  * \author Corentin Chauvin-Hameau
6  * \date 2019
7  */
8 
9 #include "gp_nodelet.hpp"
10 #include "mf_common/Array2D.h"
11 #include "mf_common/Float32Array.h"
12 #include <sensor_msgs/Image.h>
13 #include <sensor_msgs/image_encodings.h>
14 #include <ros/ros.h>
15 #include <eigen3/Eigen/Dense>
16 #include <cmath>
17 #include <vector>
18 
19 using namespace std;
20 using Eigen::VectorXf;
21 using Eigen::MatrixXf;
22 
23 
24 namespace mfcpp {
25 
26 void GPNodelet::publish_gp_state()
27 {
28  // Publish GP mean
29  mf_common::Float32Array mean_msg;
30  mean_msg.data = vector<float>(gp_mean_.data(), gp_mean_.data() + gp_mean_.size());
31  gp_mean_pub_.publish(mean_msg);
32 
33  // Publish GP covariance
34  mf_common::Array2D cov_msg;
35  cov_msg.data.resize(size_gp_);
36 
37  for (int i = 0; i < size_gp_; i++) {
38  cov_msg.data[i].data.resize(size_gp_);
39 
40  for (int j = 0; j <= i; j++) {
41  cov_msg.data[i].data[j] = gp_cov_(i, j);
42  cov_msg.data[j].data[i] = gp_cov_(i, j);
43  }
44  }
45 
46  gp_cov_pub_.publish(cov_msg);
47 }
48 
49 
50 void GPNodelet::publish_gp_eval()
51 {
52  // Preparing evaluation of the Gaussian Process
53  int size_obs = idx_obs_.size();
54  VectorXf x_obs; // x coordinates of the observed state
55  VectorXf y_obs; // y coordinates of the observed state
56  VectorXf W; // vector needed for evaluation
57 
58  prepare_eval(idx_obs_, gp_mean_, x_obs, y_obs, W);
59 
60  // Evaluate the Gaussian Process on the wall
61  float delta_x = size_wall_x_ / size_img_x_;
62  float delta_y = size_wall_y_ / size_img_y_;
63 
64  unsigned int k = 0;
65  float x = 0 , y = 0;
66 
67  for (unsigned int i = 0; i < size_img_x_; i++) {
68  for (unsigned int j = 0; j < size_img_y_; j++) {
69  if (changed_pxl_[k]) {
70  out_values_[k] = eval_gp(x, y, x_obs, y_obs, W);
71  changed_pxl_[k] = false;
72  }
73 
74  y += delta_y;
75  k++;
76  }
77  x += delta_x;
78  y = 0;
79  }
80 
81  // Fill evaluated raw output message
82  mf_common::Array2D out_msg;
83  out_msg.data.resize(size_img_x_);
84 
85  for (int k = 0; k < size_img_x_; k++) {
86  out_msg.data[k].data.resize(size_img_y_);
87 
88  for (int l = 0; l < size_img_y_; l++) {
89  out_msg.data[k].data[l] = out_values_[k*size_img_y_ + l];
90  }
91  }
92 
93  gp_eval_pub_.publish(out_msg);
94 
95  // Fill output image
96  sensor_msgs::Image img;
97  img.header.stamp = ros::Time::now();
98  img.height = size_img_x_;
99  img.width = size_img_y_;
100  img.encoding = sensor_msgs::image_encodings::MONO8;
101  img.is_bigendian = false;
102  img.step = size_img_y_;
103  img.data.resize(size_img_);
104 
105  for (unsigned int k = 0; k < size_img_; k++) {
106  img.data[k] = 255 * out_values_[k];
107  }
108 
109  wall_img_pub_.publish(img);
110 
111  // Fill covariance image
112  img.header.stamp = ros::Time::now();
113  img.height = size_gp_x_;
114  img.width = size_gp_y_;
115  img.encoding = sensor_msgs::image_encodings::MONO8;
116  img.is_bigendian = false;
117  img.step = size_gp_y_;
118  img.data.resize(size_gp_);
119 
120  for (unsigned int k = 0; k < size_gp_; k++) {
121  img.data[k] = (1 - gp_cov_(k, k)*gp_noise_var_) * 255;
122  }
123 
124  cov_img_pub_.publish(img);
125 }
126 
127 
128 } // namespace mfcpp
Definition: common.hpp:23
Declaration of a nodelet for Gaussian Process mapping of an algae wall.