13 #include "mf_farm_simulator/Alga.h" 14 #include "mf_farm_simulator/Algae.h" 15 #include <std_msgs/Float32.h> 16 #include <tf2_geometry_msgs/tf2_geometry_msgs.h> 23 void FarmNodelet::pub_rviz_markers(
float duration)
const 27 args.
stamp = ros::Time::now();
28 args.
duration = ros::Duration(duration);
35 visualization_msgs::MarkerArray markers;
36 unsigned int nbr_assets = nbr_lines_ * (5 + nbr_algae_);
37 markers.markers.reserve(nbr_assets);
41 for (
unsigned int i = 0; i < nbr_lines_; i++) {
44 markers.markers.emplace_back(
47 markers.markers.emplace_back(
54 visualization_msgs::Marker buoys_marker;
55 pop_buoys_marker(buoys_marker, args);
56 markers.markers.emplace_back(buoys_marker);
60 visualization_msgs::Marker line_marker;
61 pop_ropes_marker(line_marker, args);
62 markers.markers.emplace_back(line_marker);
66 visualization_msgs::Marker rect_marker;
67 pop_algae_marker(rect_marker, args);
68 markers.markers.emplace_back(rect_marker);
73 visualization_msgs::Marker img_marker;
74 pop_algae_heatmaps(img_marker, args);
75 markers.markers.emplace_back(img_marker);
80 rviz_pub_.publish(markers);
84 void FarmNodelet::pop_buoys_marker(visualization_msgs::Marker &marker,
88 marker.points.reserve(nbr_lines_ * nbr_buoys_);
90 for (
unsigned int i = 0; i < nbr_lines_; i++) {
92 geometry_msgs::Point point;
97 for (
unsigned int k = 0; k <
n; k++) {
98 tf2::Vector3 X = X1 + float(k)/(n-1) * (X2-X1);
99 marker.points.emplace_back(tf2::toMsg(X, point));
105 void FarmNodelet::pop_ropes_marker(visualization_msgs::Marker &marker,
109 marker.points.reserve(3 * nbr_lines_);
111 for (
unsigned int i = 0; i < nbr_lines_; i++) {
113 geometry_msgs::Point point;
115 marker.points.emplace_back(tf2::toMsg(al->
anchor1, point));
118 marker.points.emplace_back(tf2::toMsg(al->
anchor2, point));
121 marker.points.emplace_back(tf2::toMsg(al->
line.
p1, point));
122 marker.points.emplace_back(tf2::toMsg(al->
line.
p2, point));
128 tf2::Vector3 Y1 = al->
line.
p1;
129 tf2::Vector3 Y2 = al->
line.
p2;
131 for (
unsigned int k = 1; k < n-1; k++) {
132 tf2::Vector3 X = X1 + float(k)/(n-1) * (X2-X1);
133 tf2::Vector3 Y = Y1 + float(k)/(n-1) * (Y2-Y1);
134 marker.points.emplace_back(tf2::toMsg(X, point));
135 marker.points.emplace_back(tf2::toMsg(Y, point));
141 void FarmNodelet::pop_algae_marker(visualization_msgs::Marker &marker,
145 marker.points.reserve(2 * nbr_lines_ * nbr_algae_);
147 for (
unsigned int i = 0; i < nbr_lines_; i++) {
150 tf2::Vector3 X1 = al->
line.
p1;
151 tf2::Vector3 X2 = al->
line.
p2;
152 tf2::Vector3 z0(0, 0, 1);
153 tf2::Vector3 y0(X2.getX()-X1.getX(), X2.getY()-X1.getY(), 0);
154 y0 /= tf2::tf2Distance(y0, tf2::Vector3(0, 0, 0));
155 float delta = asin((X2.getZ()-X1.getZ()) / length_lines_);
157 tf2::Vector3 z1 = cos(delta)*z0 - sin(delta)*y0;
158 tf2::Vector3 y1 = (X2-X1) / tf2::tf2Distance(X1, X2);
159 tf2::Vector3 x1 = tf2::tf2Cross(y1, z1);
161 for (
unsigned int k = 0; k < al->
algae.size(); k++) {
162 tf2::Vector3 X = al->
algae[k].position;
163 float psi = al->
algae[k].orientation;
164 float H = al->
algae[k].length;
165 float W = al->
algae[k].width;
167 tf2::Vector3 p1 = X - W/2*y1;
168 tf2::Vector3 p2 = X + W/2*y1;
169 tf2::Vector3 p3 = p2 - H*(cos(psi)*z1 - sin(psi)*x1);
170 tf2::Vector3 p4 = p1 - H*(cos(psi)*z1 - sin(psi)*x1);
173 geometry_msgs::Point point;
174 marker.points.emplace_back(tf2::toMsg(p1, point));
175 marker.points.emplace_back(tf2::toMsg(p2, point));
176 marker.points.emplace_back(tf2::toMsg(p4, point));
178 marker.points.emplace_back(tf2::toMsg(p2, point));
179 marker.points.emplace_back(tf2::toMsg(p3, point));
180 marker.points.emplace_back(tf2::toMsg(p4, point));
184 std_msgs::ColorRGBA color;
189 marker.colors.resize(marker.points.size()/3, color);
193 void FarmNodelet::pop_algae_heatmaps(visualization_msgs::Marker &marker,
197 marker.points.reserve(nbr_lines_ * nbr_algae_
198 * 2 * height_disease_heatmap_ * width_disease_heatmap_);
199 marker.colors.reserve(nbr_lines_ * nbr_algae_
200 * 2 * height_disease_heatmap_ * width_disease_heatmap_);
202 for (
int k = 0; k < nbr_lines_; k++) {
205 tf2::Vector3 X1 = al->
line.
p1;
206 tf2::Vector3 X2 = al->
line.
p2;
207 tf2::Vector3 z(0, 0, 1);
208 tf2::Vector3 y = (X2-X1) / tf2::tf2Distance(X1, X2);
209 tf2::Vector3 x = tf2::tf2Cross(y, z);
211 for (
int l = 0; l < al->
algae.size(); l++) {
213 tf2::Vector3 X = al->
algae[l].position;
214 float psi = al->
algae[l].orientation;
215 float H = al->
algae[l].length;
216 float W = al->
algae[l].width;
218 vector<tf2::Vector3> coord(4);
219 coord[0] = X - W/2*y;
220 coord[1] = X + W/2*y;
221 coord[2] = coord[1] - H*(cos(psi)*z - sin(psi)*x);
222 coord[3] = coord[0] - H*(cos(psi)*z - sin(psi)*x);
225 pop_img_marker(marker, al->
algae[l].disease_heatmap, coord);
232 void FarmNodelet::pop_img_marker(visualization_msgs::Marker &marker,
233 vector<vector<float>> img,
const vector<tf2::Vector3> &coord)
const 235 int height = img.size();
239 width = img[0].size();
241 NODELET_ERROR(
"[farm_output] Dimensions of image not valid");
248 tf2::Vector3 x = (coord[3]-coord[0])/height;
249 tf2::Vector3 y = (coord[1]-coord[0])/width;
251 for (
unsigned int i = 0; i < height; i++) {
252 tf2::Vector3 p = coord[0] + i*x;
254 for (
unsigned int j = 0; j < width; j++) {
256 geometry_msgs::Point point;
257 marker.points.emplace_back(tf2::toMsg(p, point));
258 marker.points.emplace_back(tf2::toMsg(p+y, point));
259 marker.points.emplace_back(tf2::toMsg(p+y+x, point));
261 marker.points.emplace_back(tf2::toMsg(p, point));
262 marker.points.emplace_back(tf2::toMsg(p+x, point));
263 marker.points.emplace_back(tf2::toMsg(p+y+x, point));
266 std_msgs::ColorRGBA color;
271 marker.colors.emplace_back(color);
272 marker.colors.emplace_back(color);
281 void FarmNodelet::pub_algae()
283 mf_farm_simulator::Algae algae;
284 algae.algae.reserve(nbr_lines_ * nbr_algae_);
286 for (
unsigned int k = 0; k < nbr_lines_; k++) {
289 tf2::Vector3 X1 = al->
line.
p1;
290 tf2::Vector3 X2 = al->
line.
p2;
291 tf2::Vector3 z0(0, 0, 1);
292 tf2::Vector3 y0(X2.getX()-X1.getX(), X2.getY()-X1.getY(), 0);
293 y0 /= tf2::tf2Distance(y0, tf2::Vector3(0, 0, 0));
294 float delta = asin((X2.getZ()-X1.getZ()) / length_lines_);
296 tf2::Vector3 z1 = cos(delta)*z0 - sin(delta)*y0;
297 tf2::Vector3 y1 = (X2-X1) / tf2::tf2Distance(X1, X2);
298 tf2::Vector3 x1 = tf2::tf2Cross(y1, z1);
301 for (
unsigned int l = 0; l < al->
algae.size(); l++) {
302 mf_farm_simulator::Alga alga;
305 float psi = al->
algae[l].orientation;
306 float H = al->
algae[l].length;
307 float W = al->
algae[l].width;
309 tf2::Vector3 x3 = -cos(psi)*x1 + sin(-psi)*y1;
310 tf2::Vector3 y3 = y1;
311 tf2::Vector3 z3 = -cos(psi)*z1 - sin(-psi)*x1;
314 tf2::Matrix3x3 rotation(x3.getX(), x3.getY(), x3.getZ(),
315 y3.getX(), y3.getY(), y3.getZ(),
316 z3.getX(), z3.getY(), z3.getZ());
317 tf2::Quaternion quati;
319 rotation.getRPY(R, P, Y, 2);
320 quati.setRPY(R, -P, Y);
321 alga.orientation = tf2::toMsg(quati);
324 tf2::Vector3 X = al->
algae[l].position;
328 alga.dimensions.x = thickness_algae_;
329 alga.dimensions.y = W;
330 alga.dimensions.z = H;
333 int n_height = height_disease_heatmap_;
334 int n_width = width_disease_heatmap_;
336 alga.disease_heatmap.resize(n_height);
337 for (
unsigned int i = 0; i < n_height; i++)
338 alga.disease_heatmap[i].data.resize(n_width);
340 for (
unsigned int i = 0; i < n_height; i++) {
341 for (
unsigned int j = 0; j < n_width; j++) {
342 alga.disease_heatmap[i].data[j] = al->
algae[l].disease_heatmap[i][j];
347 algae.algae.emplace_back(alga);
351 algae_pub_.publish(algae);
Declaration of 2D perlin noise generator.
Rope floating_rope
Rope on wich the buoys are.
visualization_msgs::Marker rviz_marker_line(float thickness, const MarkerArgs &common_args)
Creates a blank Rviz marker to display lines.
Declaration of common structures and functions for farm simulator.
Arguments used to create a Rviz maker.
ros::Time stamp
Time stamp for the ROS message.
float anchors_diameter
Anchors diameter.
tf2::Vector3 anchor1
First anchor.
void pop_marker_ids(visualization_msgs::MarkerArray &array)
Populates id of all the markers in a marker array.
tf2::Vector3 anchor2
Second anchor.
std::string frame_id
Frame in which position/orientation of the object is.
Declaration of common Rviz display functions.
std::vector< Alga > algae
List of algae hanging on the line.
visualization_msgs::Marker rviz_marker_rect(const MarkerArgs &common_args)
Creates a blank Rviz marker to display rectangles.
Declaration of nodelet for managing the farm simulation.
geometry_msgs::Point32 vector3_to_point32(tf2::Vector3 p)
Convert a tf2::Vector3 to a geometry_msgs::Point32.
visualization_msgs::Marker rviz_marker_cylinder(tf2::Vector3 p, float diameter, float height, const MarkerArgs &common_args)
Creates a Rviz marker to display a cylinder.
tf2::Vector3 p1
Position of the first extremity of the rope.
tf2::Vector3 p2
Position of the second extremity of the rope.
Rope line
Rope on wich the algae grow.
visualization_msgs::Marker rviz_marker_triangles(const MarkerArgs &common_args)
Creates a blank Rviz marker to display triangles.
unsigned int nbr_buoys
Number of buoys on the floating rope.
ros::Duration duration
Duration of the marker (in sec)
float anchors_height
Anchors height.
Line on which algae grow.
visualization_msgs::Marker rviz_marker_spheres(float thickness, const MarkerArgs &common_args)
Creates a blank Rviz marker to display spheres.
std::string ns
Namespace for the Rviz marker.
std_msgs::ColorRGBA color
Color of the marker.