waypoint_source/main.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <geometry_msgs/PoseWithCovarianceStamped.h>
3 #include <geometry_msgs/PointStamped.h>
4 #include <fstream>
5 #include <igvc/StringUtils.hpp>
6 #include <gps_common/conversions.h>
7 #include <string>
8 #include <mutex>
9 
10 using namespace std;
11 using namespace geometry_msgs;
12 using namespace ros;
13 
14 ros::Publisher waypoint_pub;
15 vector<PointStamped> waypoints;
16 PointStamped current_waypoint;
17 Point map_origin;
18 
20 
21 void loadWaypointsFile(string path, vector<PointStamped>& waypoints)
22 {
23  if(path.empty())
24  {
25  ROS_ERROR_STREAM("Could not load empty file path.");
26  return;
27  }
28 
29  ifstream file;
30  file.open(path.c_str());
31 
32  if(!file.is_open())
33  {
34  ROS_INFO_STREAM("Could not open file: " << path);
35  return;
36  }
37 
38  string line = "";
39  auto lineIndex = 1;
40  while(!file.eof())
41  {
42  getline(file, line);
43 
44  if(!line.empty())
45  {
46  vector<string> tokens = split(line, ',');
47 
48  if(tokens.size() != 2)
49  {
50  ROS_ERROR_STREAM(path << ":" << lineIndex << " - " << tokens.size() << " tokens instead of 2.");
51  return;
52  }
53 
54  auto lat = stof(tokens[0]);
55  auto lon = stof(tokens[1]);
56 
57  PointStamped p;
58 
59  gps_common::UTM(lat, lon, &(p.point.x), &(p.point.y));
60 
61  waypoints.push_back(p);
62  }
63 
64  lineIndex++;
65  }
66 }
67 
68 double distanceBetweenPoints(const Point &p1, const Point &p2)
69 {
70  return sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y));
71 }
72 
73 void positionCallback(const geometry_msgs::PoseWithCovarianceStampedConstPtr& msg)
74 {
75  lock_guard<mutex> lock(current_mutex);
76  if(distanceBetweenPoints(msg->pose.pose.position, current_waypoint.point) < 1.0)
77  {
78  // advance to next waypoint.
79  current_waypoint = waypoints.front();
80  current_waypoint.point.x -= map_origin.x;
81  current_waypoint.point.y -= map_origin.y;
82  if(waypoints.size() > 1)
83  waypoints.erase(waypoints.begin());
84  }
85 }
86 
87 void originCallback(const geometry_msgs::PointStampedConstPtr& msg)
88 {
89  lock_guard<mutex> lock(current_mutex);
90  map_origin = msg->point;
91 }
92 
93 int main(int argc, char** argv)
94 {
95  ros::init(argc, argv, "waypoint_source");
96 
97  ros::NodeHandle nh;
98  ros::NodeHandle nhp("~");
99 
100  ROS_INFO_STREAM("Has param: " << nhp.hasParam("file"));
101 
102  string path;
103  nhp.getParam("file", path);
104 
105  ROS_INFO_STREAM("Loading waypoints from " << path);
106 
107  waypoint_pub = nh.advertise<PointStamped>("/waypoint", 1);
108 
109  ros::Subscriber odom_sub = nh.subscribe("/robot_pose_ekf/odom_combined", 1, positionCallback);
110 
111  ros::Subscriber origin_sub = nh.subscribe("/map_origin", 1, originCallback);
112 
114 
115  if(!waypoints.empty())
116  {
117  ROS_INFO_STREAM(waypoints.size() << " waypoints found.");
118  current_waypoint = waypoints.front();
119  Rate rate(1); // 1 Hz
120  while(ros::ok())
121  {
122  ROS_INFO("publishing current waypoint...");
123  lock_guard<mutex> lock(current_mutex);
124  current_waypoint.header.stamp = ros::Time::now();
125  current_waypoint.header.seq++;
126  current_waypoint.header.frame_id = "map";
127  waypoint_pub.publish(current_waypoint);
128 
129  ros::spinOnce();
130  rate.sleep();
131  }
132  } else {
133  ROS_ERROR("No valid waypoint entries found.");
134  }
135 
136  return 0;
137 }
mutex current_mutex
action_pathConstPtr path
vector< PointStamped > waypoints
int main(int argc, char **argv)
PointStamped current_waypoint
void positionCallback(const geometry_msgs::PoseWithCovarianceStampedConstPtr &msg)
ros::NodeHandle * nhp
std::vector< std::string > split(const std::string &s, const char &delim)
Definition: StringUtils.hpp:5
void loadWaypointsFile(string path, vector< PointStamped > &waypoints)
ros::Publisher waypoint_pub
double distanceBetweenPoints(const Point &p1, const Point &p2)
Point map_origin
void originCallback(const geometry_msgs::PointStampedConstPtr &msg)


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45