auto_dock.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  > File Name: src/auto_dock.cpp
3  > Author:
4  > Mail:
5  > Created Time: 2018年05月31日 星期四 17时08分21秒
6  ************************************************************************/
7 
8 #include<iostream>
9 #include "../include/auto_dock.h"
10 #define MAX(x,y) (x>y)?y:x
11 
12 
13 
14 namespace xbot{
15 
16 AutoDock::AutoDock():theta_diff(100.0),vertical_theta_thresh(0.0),found_docker(false),get_middlepose(false),
17 reached_docker(false){
18 
19 }
20 
22  if(state_pub_thread){
23  state_pub_thread->join();
24  delete state_pub_thread;
25  }
27  search_dock_thread->join();
28  delete search_dock_thread;
29  }
30 
31 }
33  ros::NodeHandle nh("~");
34  laser_scan_sub = nh.subscribe("/scan",10,&AutoDock::laser_scanCB,this);
35  p[0]=-1;
36  p[1]=-1;
37  p[2]=-1;
38  launch_autodock_sub = nh.subscribe("/auto_dock/launch",10,&AutoDock::launch_autodockCB,this);
39  autodock_state_pub = nh.advertise <xbot_navi::AutodockState> ("state",100);
40  xbot_cmd_vel_pub = nh.advertise <geometry_msgs::Twist> ("/cmd_vel_mux/input/auto_dock",100);
41  autodock_state_msg.header.stamp = ros::Time::now();
42  autodock_state_msg.state = xbot_navi::AutodockState::NOT_LAUNCHED;
43 
44 
45 
46  state_pub_thread = new boost::thread(boost::bind(&AutoDock::pub_autodock_state,this));
47  search_dock_thread = new boost::thread(boost::bind(&AutoDock::search_docker,this));
48  heading_docker_thread = new boost::thread(boost::bind(&AutoDock::heading_docker,this));
49 
50 
51 
52 
53 }
55 {
56  ros::Rate autodock_state_rate(10);
57  while(ros::ok())
58  {
60  autodock_state_rate.sleep();
61 
62 
63  }
64 }
66 
67 }
68 
70 // 机器人正对中心点,并且左右两边距离相等,形成等腰三角形,即垂直正对中心点
71  return (abs(theta[1])<2*vertical_theta_thresh)&&(abs(distance[0]-distance[2])<0.05);
72 }
73 
75  ros::Rate search_rate(20);
76  int search_times=0;
77  ROS_INFO("pose1:%d;%d",p[0],p[2]);
78 
79  while((p[0]==-1||p[2]==-1)&&search_times<60*20/*&&!is_vertical()||abs(theta_diff)>0.004*/)
80  {
81 // ROS_INFO("pose1:%d;%d",p[0],p[2]);
82 
83  cmd_vel_msg.angular.z = 0.157;
85  search_rate.sleep();
86  search_times++;
87 // ROS_INFO("p[1]=%d;vertical:%d;theta_diff=%f",p[1],is_vertical(),theta_diff);
88 
89  }
90  if(search_times>=60*20){
91  ROS_INFO("searched for 30 seconds for turn 3 loops,but not found docker,please recheck!");
92  found_docker=false;
93  autodock_state_msg.state=xbot_navi::AutodockState::NOT_FOUND_DOCKER;
94 
95  }
96  else{
97  found_docker=true;
98 
99 // if(!is_vertical()){
100 // get_middlepose=false;
101 // search_times=0;
102 // while(abs(theta_diff)>0.004&&search_times<60){
103 // cmd_vel_msg.angular.z = 0.314;
104 // xbot_cmd_vel_pub.publish(cmd_vel_msg);
105 // search_rate.sleep();
106 // search_times++;
107 // }
108 // if(search_times>=60){
109 // ROS_INFO("searched for 60 seconds for turn 3 loops,but not found horizonal pose,please recheck!");
110 // found_docker=false;
111 // autodock_state_msg.state=xbot_navi::AutodockState::NOT_FOUND_DOCKER;
112 
113 // }
114 // }
115 // else{
116 // get_middlepose=true;
117 // }
118 
119  }
120 
121 }
123  ros::Rate forward_rate(20);
124  while(distance[1]>0.2){
125  cmd_vel_msg.linear.x=0.1;
127  forward_rate.sleep();
128  }
129 
130 
131 
132  reached_docker=true;
133 }
135  ros::Rate forward_rate(20);
136  while(p[0]>5&&abs(p[2]-524)<=5){
137  cmd_vel_msg.linear.x=0.1;
139  forward_rate.sleep();
140  }
141  int times=0;
142  while(!is_vertical()&&times<60){
143  cmd_vel_msg.angular.z=0.314;
145  forward_rate.sleep();
146  }
147  if(times<60)
148  {
149  get_middlepose=true;
150  }
151 
152 }
154  ros::Rate cmd_pub_rate(20);
155  while(ros::ok()&&!reached_docker){
156  if(found_docker){
157  autodock_state_msg.state=xbot_navi::AutodockState::HEADING;
158  cmd_vel_msg.angular.z=MAX(0.2*(theta[0]+theta[2])/2-(distance[0]-distance[2]),1.0);
159  cmd_vel_msg.linear.x=MAX(0.1*(distance[0]+distance[2])/2,0.5);
161  cmd_pub_rate.sleep();
162 
163 // if(get_middlepose=true){
164 // go_direct();
165 
166 // }
167 // else{
168 // go_middlepose();
169 // }
170 
171  }
172  }
173 
174 }
175 
176 void AutoDock::launch_autodockCB(const std_msgs::Bool msg){
177  if(msg.data == true){
178  if(autodock_state_msg.state%2==0){
179  ROS_INFO("launch autodock success, start searching docker around robot.");
180  autodock_state_msg.state = xbot_navi::AutodockState::SEARCHING;
181 
182 
183  }
184  else{
185  ROS_INFO("xbot autodock has already launched, please check /auto_dock/state for autodock state and try later!");
186  }
187  }
188 
189 }
190 void AutoDock::laser_scanCB(const sensor_msgs::LaserScan msg){
191  vertical_theta_thresh = msg.angle_increment;
192  int length = msg.ranges.size();
193  vector<float> a = msg.intensities;
194  int i=0,j=0;
195  p[0]=-1;
196  for(;i<a.size();i++)
197  {
198  if(a[i]>200)
199  {
200  for(;j<50;j++)
201  {
202  if(a[i+j]<200) break;
203  }
204  if(j>=20)
205  {
206  p[0]=i;
207  break;
208  }
209 
210  }
211 
212  }
213  i=i+j;
214  j=0;
215  p[2]=-1;
216  for(;i<a.size();i++)
217  {
218  if(a[i]>200)
219  {
220  for(;j<50;j++)
221  {
222  if(a[i+j]<200) break;
223  }
224  if(j>=20)
225  {
226  p[2]=i+j;
227  break;
228  }
229 
230  }
231 
232  }
233 
234 
235 
236  p[1]=-1;
237  if(p[0]!=-1&&p[2]!=-1)
238  {
239  p[1] = (p[0]+p[2])/2;
240  }
241 
242  if(p[1]!=-1){
243  for(int pii=0;pii<3;pii++){
244  theta[pii]=msg.angle_min+p[pii]*msg.angle_increment;
245  distance[pii]=msg.ranges[p[pii]];
246  }
247  theta_diff = abs(tan(theta[1]))-(abs(tan(theta[0]))+abs(tan(theta[2])))/2;
248 
249  }
250  else{
251  theta_diff = 100;
252  }
253 
254 
255 
256 // ROS_INFO("p[1]=%d;vertical:%d;theta_diff=%f",p[1],is_vertical(),theta_diff);
257  if(abs(theta_diff)<0.004)
258  {
259  ROS_INFO("found right direction!");
260  }
261 
262 
263  ROS_INFO("pose0:%d; pose2: %d",p[0],p[2]);
264 // ROS_INFO("docker pose:%d/%d",p[0],length);
265 
266 }
267 
268 
269 
270 }
271 
272 /*****************************************************************************
273 ** Main
274 *****************************************************************************/
275 
276 
277 
278 int main(int argc, char **argv)
279 {
280  ros::init(argc, argv, "auto_dock");
281  xbot::AutoDock autodock;
282  autodock.init();
283  ros::spin();
284  return 0;
285 }
286 
void search_docker()
Definition: auto_dock.cpp:74
void cal_docker_pose()
Definition: auto_dock.cpp:65
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
float vertical_theta_thresh
Definition: auto_dock.h:47
xbot_navi::AutodockState autodock_state_msg
Definition: auto_dock.h:55
void go_direct()
Definition: auto_dock.cpp:122
void heading_docker()
Definition: auto_dock.cpp:153
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
float theta[3]
Definition: auto_dock.h:43
ros::Subscriber launch_autodock_sub
Definition: auto_dock.h:52
int main(int argc, char **argv)
Definition: auto_dock.cpp:278
geometry_msgs::Twist cmd_vel_msg
Definition: auto_dock.h:56
bool is_vertical()
Definition: auto_dock.cpp:69
ROSCPP_DECL void spin(Spinner &spinner)
float theta_diff
Definition: auto_dock.h:46
bool get_middlepose
Definition: auto_dock.h:49
#define MAX(x, y)
Definition: auto_dock.cpp:10
#define ROS_INFO(...)
bool found_docker
Definition: auto_dock.h:48
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void pub_autodock_state()
Definition: auto_dock.cpp:54
bool reached_docker
Definition: auto_dock.h:50
bool sleep()
boost::thread * heading_docker_thread
Definition: auto_dock.h:61
INLINE Rall1d< T, V, S > abs(const Rall1d< T, V, S > &x)
void launch_autodockCB(const std_msgs::Bool)
Definition: auto_dock.cpp:176
static Time now()
INLINE Rall1d< T, V, S > tan(const Rall1d< T, V, S > &arg)
void laser_scanCB(const sensor_msgs::LaserScan)
Definition: auto_dock.cpp:190
Definition: auto_dock.h:21
void go_middlepose()
Definition: auto_dock.cpp:134
boost::thread * search_dock_thread
Definition: auto_dock.h:60
float distance[3]
Definition: auto_dock.h:44
boost::thread * state_pub_thread
Definition: auto_dock.h:59
ros::Publisher xbot_cmd_vel_pub
Definition: auto_dock.h:53
ros::Subscriber laser_scan_sub
Definition: auto_dock.h:51
ros::Publisher autodock_state_pub
Definition: auto_dock.h:54


xbot_navi
Author(s):
autogenerated on Sat Oct 10 2020 03:27:50