forked from LiHongbo97/Ros-Formation-and-Obstacle-Avoidance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtb_follower2
209 lines (180 loc) · 6.87 KB
/
tb_follower2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
#################################################################################
# Copyright 2018 IWIN, SJTU
#
# https://iwin-fins.com
#################################################################################
# Authors: Hongbo Li, Han Wang#
import rospy
from geometry_msgs.msg import Twist, Point, Quaternion
import tf
from math import radians, copysign, sqrt, pow, pi, atan2,cos,sin
from tf.transformations import euler_from_quaternion
import numpy as np
from sensor_msgs.msg import LaserScan
msg = """
control your Turtlebot3!
-----------------------
this is follower2
-----------------------
"""
tb_leader_pos=Point()
tb_follower1_pos=Point()
tb_follower2_pos=Point()
tb_leader_vel=Twist()
tb_follower2_vel=Twist()
obstacle_pos=Point()
K0=0.3
K1=0.1
K2=0.3
K3=0.1
detect_R=0.6
safe_r=0.2
ID=2
dx=-0.6
dy=-0.5
class GotoPoint():
def __init__(self):
rospy.init_node('follower2', anonymous=False)
#turtlebot3_model = rospy.get_param("model")
rospy.on_shutdown(self.shutdown)
self.cmd_vel = rospy.Publisher('cmd_vel', Twist, queue_size=5)
self.tb_follower2_positon=rospy.Publisher('/tb_follower2_pos',Point,queue_size=5)
position = Point()
move_cmd = Twist()
r = rospy.Rate(10)
self.tf_listener = tf.TransformListener()
self.odom_frame = '/follower2/odom'
try:
self.tf_listener.waitForTransform(self.odom_frame, '/follower2/base_footprint', rospy.Time(), rospy.Duration(1.0))
self.base_frame = '/follower2/base_footprint'
except (tf.Exception, tf.ConnectivityException, tf.LookupException):
try:
self.tf_listener.waitForTransform(self.odom_frame, '/follower2/base_link', rospy.Time(), rospy.Duration(1.0))
self.base_frame = '/follower2/base_link'
except (tf.Exception, tf.ConnectivityException, tf.LookupException):
rospy.loginfo("Cannot find transform between odom and base_link or base_footprint")
rospy.signal_shutdown("tf Exception")
print 66
(position, rotation) = self.get_odom()
rospy.Subscriber('/tb_leader_pos',Point,point_callback_1)
rospy.Subscriber('/tb_follower1_pos',Point,point_callback_2)
# rospy.Subscriber('/tb_follower2_pos',Point,point_callback_3)
rospy.Subscriber('/tb_leader_vel',Twist,vel_callback_4)
self.tb_follower2_positon.publish(position)
global tb_leader_vel
global tb_follower1_pos
global tb_follower2_pos
global tb_follower2_vel
global obstacle_pos
delta_x=(tb_leader_pos.x-position.x+dx)
delta_y=(tb_leader_pos.y-position.y+dy)
# print ('deltax of Node 2 is %f'%(delta_x))
avoid_delta=0
tb_follower2_pos=position
obstacle_pos=self.lidar(position)
# print ('follower 2 obstacle x=%f y=%f'%(obstacle_pos.x,obstacle_pos.y))
# theta=self.compute_theta(tb_leader_pos.z,rotation)
r=sqrt(pow(obstacle_pos.x-position.x,2)+pow(obstacle_pos.y-position.y,2))
print ('follower 2 obstacle d=%f ang=%f'%(r,obstacle_pos.z))
if r<detect_R and (obstacle_pos.z<70 or obstacle_pos.z>290):
theta2=self.compute_theta(obstacle_pos.z,0)
if(obstacle_pos.z<70):
tb_follower2_vel.angular.z=-0.4*(pi/4-theta2)-0.1
tb_follower2_vel.linear.x=0.2/(r+0.3)
else:
tb_follower2_vel.angular.z=0.4*(pi/4+theta2)+0.1
tb_follower2_vel.linear.x=0.2/(r+0.3)
#tb_follower2_vel.angular.z=tb_follower2_vel.angular.z*(-1);
self.cmd_vel.publish(tb_follower2_vel)
else:
theta=tb_leader_pos.z-rotation
tb_follower2_vel.linear.x=K0*tb_leader_vel.linear.x*cos(theta)+K1*delta_x
print ('follower 2 velecity=%f'%(tb_follower2_vel.linear.x))
tb_follower2_vel.angular.z=K0*tb_leader_vel.angular.z+K2*theta+1.3*tb_leader_vel.linear.x*delta_y*(sin(theta)/theta)
self.cmd_vel.publish(tb_follower2_vel)
def lidar(self,tb_pos):
msg = rospy.wait_for_message("scan", LaserScan)
LIDAR_ERR = 0.05
LIDAR_MAX = 2
obstacle=[]
min_dis=3
min_ang=0
min_point=Point()
for i in range(360):
if i <= 90 or i > 270:
obstacle_pos=Point()
if msg.ranges[i] >= LIDAR_ERR and msg.ranges[i]<=LIDAR_MAX:
obstacle_pos.x=tb_pos.x+msg.ranges[i]*cos(i*2*pi/360)
obstacle_pos.y=tb_pos.y+msg.ranges[i]*sin(i*2*pi/360)
obstacle.append(obstacle_pos)
if msg.ranges[i] < min_dis:
min_dis = msg.ranges[i]
min_ang = i
if min_dis<3:
min_point.x=tb_pos.x+min_dis*cos(i*2*pi/360)
min_point.y=tb_pos.y+min_dis*sin(i*2*pi/360)
min_point.z=min_ang
else:
min_point.x=-1000
min_point.y=-1000
return min_point
def compute_theta(self,theta,rotation1):
# if theta*rotation1<0:
# if theta>0:
# if abs(rotation1)+theta<=pi:
# w=abs(rotation1)+theta
# else:
# w=-(2*pi+rotation1-theta)
# else:
# if rotation1+abs(theta)<=pi:
# w=-(abs(theta)+rotation1)
# else:
# w=(2*pi-rotation1+theta)
# else:
# w=theta-rotation1
theta=theta%(2*pi)
if theta>pi:
theta=theta-2*pi
rotation1=rotation1%(2*pi)
if rotation1>pi:
rotation1=rotation1-2*pi
w=theta-rotation1
return w
def get_odom(self):
try:
(trans, rot) = self.tf_listener.lookupTransform(self.odom_frame, self.base_frame, rospy.Time(0))
rotation = euler_from_quaternion(rot)
except (tf.Exception, tf.ConnectivityException, tf.LookupException):
rospy.loginfo("TF Exception")
return
return (Point(*trans), rotation[2])
def shutdown(self):
self.cmd_vel.publish(Twist())
rospy.sleep(1)
def point_callback_1(data):
global tb_leader_pos
tb_leader_pos.x=data.x
tb_leader_pos.y=data.y
tb_leader_pos.z=data.z
def point_callback_2(data):
global tb_follower1_pos
tb_follower1_pos.x=data.x
tb_follower1_pos.y=data.y
def point_callback_3(data):
global tb_follower2_pos
tb_follower2_pos.x=data.x
tb_follower2_pos.y=data.y
def vel_callback_4(data):
global tb_leader_vel
tb_leader_vel=data
if __name__ == '__main__':
try:
timer=0
while not rospy.is_shutdown():
print(msg)
GotoPoint()
# timer=timer+1
# print ('follower2 timer= %d'%(timer))
except:
rospy.loginfo("shutdown program.")