-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautomove.py
44 lines (40 loc) · 1.08 KB
/
automove.py
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
#!/usr/bin/env python
import rospy
import time
from math import pi
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
def scan_callback(msg):
global dis_ahead, mindis_index, mid_index,min_dis
ranges=msg.ranges[90:270]
dis_ahead=ranges[len(ranges)/2]
mindis_index=ranges.index(min(ranges))
min_dis=min(ranges)
mid_index=len(ranges)/2
cmd_vel_pub=rospy.Publisher('cmd_vel',Twist,queue_size=1)
scan_sub=rospy.Subscriber('scan',LaserScan,scan_callback)
rospy.init_node('automove')
twist=Twist()
dis_ahead=1
min_dis=1
mindis_index=1
mid_index=1
rate=rospy.Rate(10)
while not rospy.is_shutdown():
print 'dis ahead:%.2f'%dis_ahead
if dis_ahead<0.2:
twist.linear.x=0
twist.angular.z=3*pi/4
cmd_vel_pub.publish(twist)
time.sleep(2)
continue
else:
twist.linear.x=-0.05
if mindis_index>mid_index+10:
twist.angular.z=pi/4
elif mindis_index<mid_index-10:
twist.angular.z=-pi/4
else:
twist.angular.z=0
cmd_vel_pub.publish(twist)
rate.sleep()