multipoint_navigation_receiver.cpp
Go to the documentation of this file.
1 #include <move_base_msgs/MoveBaseAction.h>
3 #include <queue>
4 #include <multipoint_navigation_receiver/obstacle_srv.h>
5 #include <multipoint_navigation_receiver/set_goal_srv.h>
6 #include <signal.h>
7 #include <pthread.h>
9 
10 using namespace std;
13 void* send_goals(void* goals);
14 pthread_t send_thread;
16 void MySigintHandler(int sig)
17 {
18  //这里主要进行退出前的数据保存、内存清理、告知其他节点等工作
19  ROS_INFO("shutting down!");
20  ros::shutdown();
21 }
22 
23 bool goalsCB(multipoint_navigation_receiver::set_goal_srv::Request &req,multipoint_navigation_receiver::set_goal_srv::Response &res)
24 {
25  if (req.positions.size() == 0)
26  {
27  if (thread_isexist)
28  {
29  pthread_cancel(send_thread);
30  while (pthread_kill(send_thread,0) == 0)
31  {
32  sleep(0.2);
33  }
34  }
35  client->cancelGoal();
36  thread_isexist = false;
37  res.result = "cancel received";
38  return true;
39  }
40  queue<move_base_msgs::MoveBaseGoal>* goals = new queue<move_base_msgs::MoveBaseGoal>();
41  for (size_t i = 0; i < req.positions.size(); i++)
42  {
43  move_base_msgs::MoveBaseGoal goal;
44  goal.target_pose.header.frame_id = "map";
45  goal.target_pose.pose.position.x = req.positions[i].x;
46  goal.target_pose.pose.position.y = req.positions[i].y;
47  goal.target_pose.pose.position.z = req.positions[i].z;
48  goal.target_pose.pose.orientation.x = req.quaternions[i].x;
49  goal.target_pose.pose.orientation.y = req.quaternions[i].y;
50  goal.target_pose.pose.orientation.z = req.quaternions[i].z;
51  goal.target_pose.pose.orientation.w = req.quaternions[i].w;
52  ROS_INFO("mapdata:px=%lf,py=%lf,pz=%lf,qx=%lf,qy=%lf,pz=%lf,pw=%lf",goal.target_pose.pose.position.x,goal.target_pose.pose.position.y,goal.target_pose.pose.position.z,
53  goal.target_pose.pose.orientation.x,goal.target_pose.pose.orientation.y,goal.target_pose.pose.orientation.z,goal.target_pose.pose.orientation.w);
54  goals->push(goal);
55  }
56  res.result = "goals received";
57  if (!thread_isexist)
58  {
59  pthread_create(&send_thread,NULL,send_goals,goals);
60  }else
61  {
62  pthread_cancel(send_thread);
63  while (pthread_kill(send_thread,0) == 0)
64  {
65  sleep(2);
66  }
67  pthread_create(&send_thread,NULL,send_goals,goals);
68  }
69  return true;
70 }
71 
72 int main(int argc, char** argv)
73 {
74  thread_isexist = false;
75  ros::init(argc, argv, "multipoint_navigation_receiver");
76  n = new ros::NodeHandle();
77  ROS_INFO("start");
78  ros::service::waitForService("/obstacle_information");
79  ros::ServiceServer goals_server = n->advertiseService("/goals_server",goalsCB);
80  client = new Client("move_base", true); // true -> don't need ros::spin()
81  client->waitForServer(ros::Duration(60.0));
82  signal(SIGINT, MySigintHandler);
83  ros::spin();
84  return 0;
85 }
86 void* send_goals(void* data)
87 {
88  thread_isexist = true;
89  queue<move_base_msgs::MoveBaseGoal>* goals;
90  goals =(queue<move_base_msgs::MoveBaseGoal>*) data;
91  ros::ServiceClient obstacle_client = n->serviceClient<multipoint_navigation_receiver::obstacle_srv>("/obstacle_information");
92  client->sendGoal(goals->front());
93  int count = 0;
94  while (ros::ok())
95  {
96  pthread_testcancel();
97  bool Arrivals = client->waitForResult(ros::Duration(60.0));
98  if (!Arrivals)
99  {
100  client->cancelGoal();
101  multipoint_navigation_receiver::obstacle_srv obstacle;
102  obstacle.request.positionx = goals->front().target_pose.pose.position.x;
103  obstacle.request.positiony = goals->front().target_pose.pose.position.y;
104  obstacle_client.call(obstacle);
105  int8_t obstacle_probability = obstacle.response.obstacle_probability;
106  printf("obstacle is %d \n",obstacle_probability);
107  if(obstacle_probability > 0)
108  {
109  printf("Goal have obatacle! \n");
110 
111  goals->pop();
112  if (goals->empty())
113  {
114  printf("You got all goals \n");
115  delete(goals);
116  break;
117  }else
118  {
119  client->sendGoal(goals->front());
120  count = 0;
121  }
122  printf("Timed out achieving goal, go next \n");
123  }
124  else if(obstacle_probability == 0)
125  {
126  client->sendGoal(goals->front());
127  count = 0;
128  printf("You resent one goal\n");
129  }
130  }
131  else
132  {
133  count ++ ;
135  {
136  printf("Yay! One goal OK!\n");
137  goals->pop();
138  if (goals->empty())
139  {
140  printf("You got all goals\n");
141  delete(goals);
142  break;
143  }
144  else
145  {
146  client->sendGoal(goals->front());
147  count = 0;
148  }
149  }
150  else if(count > 3)
151  {
152  client->sendGoal(goals->front());
153  count = 0;
154  printf("You resent one goal\n");
155  }
156  printf("Current State: %s\n", client->getState().toString().c_str());
157  }
158  }
159  thread_isexist = false;
160  return 0;
161 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
bool goalsCB(multipoint_navigation_receiver::set_goal_srv::Request &req, multipoint_navigation_receiver::set_goal_srv::Response &res)
bool waitForServer(const ros::Duration &timeout=ros::Duration(0, 0)) const
void MySigintHandler(int sig)
bool waitForResult(const ros::Duration &timeout=ros::Duration(0, 0))
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
actionlib::SimpleActionClient< move_base_msgs::MoveBaseAction > Client
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
ROSCPP_DECL void spin(Spinner &spinner)
std::string toString() const
int main(int argc, char **argv)
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
ros::NodeHandle * n
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
ROSCPP_DECL void shutdown()
SimpleClientGoalState getState() const
pthread_t send_thread
void * send_goals(void *goals)
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)


multipoint_navigation_receiver
Author(s): qiuan ren
autogenerated on Sat Aug 22 2020 03:28:37