Corentin Chauvin-Hameau – 2019-2020
Coverage Path Planning for an underwater robot surveying a marine farm
traj_publisher.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  *
4  * \brief Definition of a node to generate hard-coded trajectories
5  * \author Corentin Chauvin-Hameau
6  * \date 2020
7  */
8 
9 #include "traj_publisher.hpp"
10 #include "mf_common/common.hpp"
11 #include "mf_common/spline.hpp"
12 #include <nav_msgs/Path.h>
13 #include <geometry_msgs/PoseStamped.h>
14 #include <geometry_msgs/Pose.h>
15 #include <ros/package.h>
16 #include <ros/ros.h>
17 #include <eigen3/Eigen/Dense>
18 #include <fstream>
19 #include <iostream>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 using std::cout;
25 using std::endl;
26 using std::string;
27 using std::vector;
28 
29 
30 namespace mfcpp {
31 
32 
34  nh_("~")
35 {
36  init_node();
37 }
38 
39 
41 {
42 
43 }
44 
45 
47 {
48  // ROS parameters
49  string path_frame; // frame in which the path is expressed
50  string wp_file_name; // relative path of the file containing the waypoints
51  float plan_speed; // planned speed (m/s) of the robot
52  float plan_res; // spatial resolution (m) of the planned trajectory
53 
54  nh_.param<float>("path_freq", path_freq_, 1.0);
55  nh_.param<string>("path_frame", path_frame, "ocean");
56  nh_.param<string>("wp_file_name", wp_file_name, "resources/path.txt");
57  nh_.param<float>("plan_res", plan_res, 1.0);
58 
59  // Other variables
60  path_loaded_ = false;
61  path_.header.frame_id = path_frame;
62  load_path(wp_file_name, plan_res, path_);
63 
64  // ROS publishers
65  path_pub_ = nh_.advertise<nav_msgs::Path>("path", 0);
66 }
67 
68 
70 {
71  ros::Rate loop_rate(path_freq_);
72 
73  while (ros::ok()) {
74  ros::spinOnce();
75 
76  if (path_loaded_) {
77  path_.header.stamp = ros::Time::now();
78  path_pub_.publish(path_);
79  }
80 
81  loop_rate.sleep();
82  }
83 }
84 
85 
86 void TrajPublisherNode::load_path(string file_name, float resolution, nav_msgs::Path &path)
87 {
88  path_loaded_ = false;
89 
90  // Open path file
91  string package_path = ros::package::getPath("mf_experiments");
92  string file_path = package_path + '/' + file_name;
93 
94  std::ifstream file(file_path.c_str());
95 
96  if (!file.is_open()) {
97  ROS_WARN("[control_tester] Path file couldn't be opened at: %s", file_path.c_str());
98  return;
99  }
100 
101  // Read the waypoints
102  vector<Eigen::Vector3f> p(0); // waypoints positions
103  vector<Eigen::Vector3f> o(0); // waypoints orientations
104  int k = 0;
105 
106  for (string line; std::getline(file, line); ) {
107  if (!line.empty()) {
108  p.emplace_back(Eigen::Vector3f());
109  o.emplace_back(Eigen::Vector3f());
110  std::istringstream in(line);
111  in >> p[k](0) >> p[k](1) >> p[k](2) >> o[k](0) >> o[k](1) >> o[k](2);
112 
113  k++;
114  }
115  }
116 
117  // Interpolate the path
118  Spline spline(p, o, 1.0);
119 
120  path_.poses.resize(0);
121  bool last_reached = false;
122  float t = 0;
123 
124  while (!last_reached) {
125  Eigen::Vector3f p;
126  Eigen::Vector3f o;
127 
128  spline.evaluate(t, p, o, last_reached);
129  o = o / o.norm();
130 
131  geometry_msgs::PoseStamped pose;
132  pose.pose.position.x = p(0);
133  pose.pose.position.y = p(1);
134  pose.pose.position.z = p(2);
135  to_quaternion(0.0, -asin(o(2)), atan2(o(1), o(0)), pose.pose.orientation);
136 
137  path_.poses.emplace_back(pose);
138  t += resolution;
139  }
140 
141  // Cleaning
142  file.close();
143  path_loaded_ = true;
144 }
145 
146 
147 } // namespace mfcpp
148 
149 
150 
151 /**
152  * \brief Main function called before node initialisation
153  */
154 int main(int argc, char **argv)
155 {
156  ros::init(argc, argv, "traj_publisher");
157  mfcpp::TrajPublisherNode traj_publisher_node;
158  traj_publisher_node.run_node();
159 
160  return 0;
161 }
Definition: common.hpp:23
void evaluate(float t, Eigen::Vector3f &position, Eigen::Vector3f &orientation, bool &last_reached)
Evaluates the spline at a specific time instant (assuming constant speed)
Definition: spline.cpp:63
Declaration of a node to generate hard-coded trajectories.
void to_quaternion(double roll, double pitch, double yaw, tf2::Quaternion &quat)
Converts Roll-Pitch-Yaw Euler angles to a quaternion.
Definition: common.hpp:207
ros::NodeHandle nh_
Node handler.
int main(int argc, char **argv)
Main function called before node initialisation.
Declaration of common functions.
Declaration of a class for 3D spline interpolation.
Class to publish hard-coded trajectories.
void load_path(std::string file_name, float resolution, nav_msgs::Path &path)
Loads a path from a text file.
void run_node()
Runs the node.
nav_msgs::Path path_
Path to publish.
void init_node()
Initialises the node and its parameters.
ros::Publisher path_pub_
Publisher for a path.
bool path_loaded_
Whether the path was loaded with success.
Class for spline interpolation.
Definition: spline.hpp:24