12 #include <nav_msgs/Path.h> 13 #include <nav_msgs/Odometry.h> 14 #include <geometry_msgs/PoseStamped.h> 15 #include <geometry_msgs/Pose.h> 16 #include <std_srvs/Empty.h> 17 #include <ros/package.h> 19 #include <eigen3/Eigen/Dense> 53 string transition_file_name;
55 bool initially_disabled;
58 nh_.param<
string>(
"path_frame", path_frame,
"ocean");
59 nh_.param<
string>(
"wp_file_name", wp_file_name,
"resources/path.txt");
60 nh_.param<
string>(
"transition_file_name", transition_file_name,
"resources/transition.txt");
61 nh_.param<
float>(
"plan_res", plan_res, 1.0);
62 nh_.param<
bool>(
"initially_disabled", initially_disabled,
false);
70 path_.header.frame_id = path_frame;
74 if (initially_disabled) {
96 ROS_ERROR(
"[planning_logic] Couldn't read transition path, shutting down...");
108 Eigen::Vector3f pos(pose.position.x, pose.position.y, pose.position.z);
111 if (distance < 0.5) {
124 ROS_WARN(
"[planning_logic] Enable/Disable call to planner failed");
130 path_.header.stamp = ros::Time::now();
142 string package_path = ros::package::getPath(
"mf_navigation");
143 string file_path = package_path +
'/' + file_name;
145 std::ifstream file(file_path.c_str());
147 if (!file.is_open()) {
148 ROS_WARN(
"[planning_logic] Path file couldn't be opened at: %s", file_path.c_str());
153 vector<Eigen::Vector3f> p(0);
154 vector<Eigen::Vector3f> o(0);
157 for (
string line; std::getline(file, line); ) {
159 p.emplace_back(Eigen::Vector3f());
160 o.emplace_back(Eigen::Vector3f());
161 std::istringstream in(line);
162 in >> p[k](0) >> p[k](1) >> p[k](2) >> o[k](0) >> o[k](1) >> o[k](2);
171 path_.poses.resize(0);
172 bool last_reached =
false;
175 while (!last_reached) {
179 spline.
evaluate(t, p, o, last_reached);
182 geometry_msgs::PoseStamped pose;
183 pose.pose.position.x = p(0);
184 pose.pose.position.y = p(1);
185 pose.pose.position.z = p(2);
186 to_quaternion(0.0, -asin(o(2)), atan2(o(1), o(0)), pose.pose.orientation);
188 path_.poses.emplace_back(pose);
201 string package_path = ros::package::getPath(
"mf_navigation");
202 string file_path = package_path +
'/' + file_name;
204 std::ifstream file(file_path.c_str());
206 if (!file.is_open()) {
207 ROS_WARN(
"[planning_logic] Path file couldn't be opened at: %s", file_path.c_str());
215 for (
string line; std::getline(file, line); ) {
218 std::istringstream in(line);
232 const std::vector<geometry_msgs::PoseStamped> &path,
233 const geometry_msgs::Pose &pose)
235 if (path.size() == 0)
236 return geometry_msgs::Pose();
238 float min_dist =
distance2(path[0].pose.position, pose.position);
241 for (
int k = 1; k < path.size(); k++) {
242 float d =
distance2(path[k].pose.position, pose.position);
250 return path[best_k].pose;
268 int main(
int argc,
char **argv)
270 ros::init(argc, argv,
"planning_logic");
Base class for a node managing high level planning logic.
void load_transition_pts(std::string file_name)
Loads the points delimiting the transitions.
bool transition_pts_loaded_
Whether the transition points have been loaded.
nav_msgs::Odometry odom_
Odometry of the robot.
std::vector< Eigen::Vector3f > transition_pts_
Points marking start and end of transition paths.
ros::ServiceClient enable_planner_client_
Client for enabling the path planner.
ros::Subscriber odom_sub_
Subscriber for robot's odometry.
void evaluate(float t, Eigen::Vector3f &position, Eigen::Vector3f &orientation, bool &last_reached)
Evaluates the spline at a specific time instant (assuming constant speed)
void to_quaternion(double roll, double pitch, double yaw, tf2::Quaternion &quat)
Converts Roll-Pitch-Yaw Euler angles to a quaternion.
void run_node()
Runs the node.
Declaration of a base class node managing high level planning logic.
void odom_cb(const nav_msgs::Odometry msg)
Callback for the odometry.
void init_node()
Initialises the node and its parameters.
void load_path(std::string file_name, float resolution, nav_msgs::Path &path)
Loads a path from a text file.
Declaration of common functions.
ros::Publisher path_pub_
Publisher for a path.
Declaration of a class for 3D spline interpolation.
int main(int argc, char **argv)
Main function called before node initialisation.
float distance(const geometry_msgs::Point &p1, const geometry_msgs::Point &p2)
Computes the the euclidean distance between two positions.
bool transition_
Whether the robot is following a transition path.
ros::ServiceClient disable_planner_client_
Client for disabling the path planner.
bool odom_received_
Whether the odometry has been received.
float distance2(const geometry_msgs::Point &p1, const geometry_msgs::Point &p2)
Computes the square of the euclidean distance between two positions.
ros::NodeHandle nh_
Node handler.
geometry_msgs::Pose find_closest(const std::vector< geometry_msgs::PoseStamped > &path, const geometry_msgs::Pose &pose)
Finds the closest pose in a path.
bool path_loaded_
Whether the path was loaded with success.
Class for spline interpolation.
nav_msgs::Path path_
Path to publish.