-
Notifications
You must be signed in to change notification settings - Fork 0
/
lidar_publisher2.py
144 lines (112 loc) · 3.13 KB
/
lidar_publisher2.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
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
#!/usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan
#from std_msgs.msg import String
from std_msgs.msg import Int32MultiArray
import sys
drive_data=Int32MultiArray()
#pub = rospy.Publisher("drive", Int32MultiArray, queue_size=10)
def callback(data):
pub = rospy.Publisher("drive", Int32MultiArray, queue_size=10)
c0 = []
c1 = []
c2 = []
c3_1 = []
c3_2 = []
c3 = []
c4 = []
c5 = []
drive_data.data=[]
value=data.ranges
speed=10
angle=96
#print (value)
for x in range(60,300):
c0.append(value[x])
c0 = min(c0)
print (c0)
for x in range(45,60):
c1.append(value[x])
c1 = min(c1)
print (c1)
for x in range(15,45):
c2.append(value[x])
c2 = min(c2)
print (c2)
for x in range(0,15):
c3_1.append(value[x])
c3_1 = min(c3_1)
for x in range(345,360):
c3_2.append(value[x])
c3_2 = min(c3_2)
c3 = min(c3_1,c3_2)
print (c3)
for x in range(315,345):
c4.append(value[x])
c4 = min(c4)
print (c4)
for x in range(300,315):
c5.append(value[x])
c5 = min(c5)
print (c5)
if c3 <= 0.22:
speed = 0
angle = 96
else:
if c1 > 0.25 and c5 > 0.25 and c2 > 0.5 and c4 > 0.5:
speed = 18
angle = 96
print('Go Straight!')
elif c1 <0.4 or 0.4< c2 < 0.55 and c4 > 0.5 and c5 > 0.4:
speed = 15
angle = 96 + (8/c1)+(4/c2)
if angle > 140: angle = 140
else: angle = angle
print('Turn Right')
elif c5 < 0.4 or 0.4< c4 < 0.55 and c2 > 0.5 and c1 > 0.4:
speed = 15
angle = 96 - (8/c5) - (4/c4)
if angle < 55: angle = 55
else: angle = angle
print('Turn Left')
elif c2 < 0.4 and c4 > 0.5 and c5 > 0.5:
speed = 15
angle = 96 + 12/(c2)
print('Take a Sharp Right')
elif c4 < 0.4 and c2 > 0.5 and c1 > 0.5:
speed = 15
angle = 96 - 12/(c4)
print('Take a Sharp Left')
elif c1 < 0.3 or c2 < 0.5 and not c4 > 0.5 and not c5 > 0.4:
speed = 0
angle = 96
print('STOP!!')
elif c5 < 0.3 or c4 < 0.5 and not c2 > 0.5 and not c1 > 0.4:
speed = 0
angle = 96
print('STOP!!')
elif c2 < 0.3 and not c4 > 0.5 and not c5 > 0.5:
speed = 0
angle = 96
print('STOP!!')
elif c4 < 0.3 and not c2 > 0.5 and not c1 > 0.5:
speed = 0
angle = 96
print('STOP!!')
drive_data.data=[speed,angle]
rospy.loginfo(drive_data)
pub.publish(drive_data)
def talker():
rospy.init_node('talker', anonymous=True)
rospy.Subscriber("/scan", LaserScan, callback, queue_size=10)
rospy.spin()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pub = rospy.Publisher("drive", Int32MultiArray, queue_size=10)
speed = 0
angle = 100
drive_data.data=[speed,angle]
rospy.loginfo(drive_data)
pub.publish(drive_data)