path_follower/main.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <igvc_msgs/action_path.h>
3 #include <igvc_msgs/velocity_pair.h>
4 #include <mutex>
5 #include <chrono>
6 
7 using namespace igvc_msgs;
8 using namespace std;
9 using namespace chrono;
10 
11 action_pathConstPtr path;
12 mutex path_mutex;
13 
14 int path_index = 0;
15 bool path_reset = false;
16 
17 void newPath(const action_pathConstPtr &msg)
18 {
19  lock_guard<mutex> lock(mutex);
20  path = msg;
21  path_index = 0;
22  path_reset = true;
23 }
24 
25 int main(int argc, char** argv)
26 {
27 
28  ros::init(argc, argv, "path_follower");
29 
30  time_point<high_resolution_clock> cmd_start_time;
31 
32  ros::NodeHandle n;
33 
34  ros::Publisher cmd_pub = n.advertise<velocity_pair>("/motors", 1);
35 
36  ros::Subscriber path_sub = n.subscribe("path", 1, newPath);
37 
38  ros::Rate rate(120);
39  while(ros::ok())
40  {
41  ros::spinOnce();
42 
43  { //scope for mutex lock
44  lock_guard<mutex> lock(mutex);
45  if(path.get() != nullptr)
46  {
47  if(path_reset)
48  {
49  cmd_pub.publish(path->actions[path_index]);
50  cmd_start_time = high_resolution_clock::now();
51  path_reset = false;
52  }
53  auto elapsed_time = high_resolution_clock::now() - cmd_start_time;
54  if(duration_cast<seconds>(elapsed_time).count() > path->actions[path_index].duration)
55  {
56  if(path_index < path->actions.size() - 1)
57  {
58  path_index++;
59  cmd_pub.publish(path->actions[path_index]);
60  cmd_start_time = high_resolution_clock::now();
61  } else {
62  velocity_pair vel;
63  vel.left_velocity = 0.;
64  vel.right_velocity = 0.;
65  cmd_pub.publish(vel);
66  path.reset();
67  }
68  }
69  }
70  }
71 
72  rate.sleep();
73  }
74 
75  return 0;
76 }
int path_index
void newPath(const action_pathConstPtr &msg)
action_pathConstPtr path
ros::Publisher cmd_pub
int main(int argc, char **argv)
mutex path_mutex
bool path_reset


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