-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrite.py
executable file
·379 lines (312 loc) · 10.3 KB
/
rewrite.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python
import time
import serial
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
import math
import rospy
from geometry_msgs.msg import Twist, Vector3
from sensor_msgs.msg import LaserScan
from std_msgs.msg import *
import Adafruit_TCS34725
import smbus
import RPi.GPIO as GPIO
turn = None
drive = None
angle_increment = None
vel = None
tolerance = .30
tSize = 5
distances = []
detectOpening = [];
forwardSpeed = 0.1 #m/s
pGain = -0.000
acceptTime = 1
justTurned = False
soundStart = False
tcs = Adafruit_TCS34725.TCS34725()
inRoom = False
ard = None
def read_flame () :
global ard
return ard.readline().decode().strip()
def toggle_extinguisher(state):
if(state):
GPIO.output(21, GPIO.HIGH)
else:
GPIO.output(21, GPIO.LOW)
def alignToWall(n) :
global distances, angle_increment, detectOpening
print("Align to Wall")
minDist = distances[n]
minDistAngle = n
# this is bad rn
#print(distances)
for i in range(n-25, n+25):
if distances[i] < minDist:
minDist = distances[i]
minDistAngle = i
print("Moving to " + str(minDistAngle) + "degrees")
if minDistAngle < 0:
turnLeftDegrees(n-math.fabs(minDistAngle))
else:
turnRightDegrees(n-minDistAngle)
rospy.sleep(1)
def colorHandler(data) :
global inRoom
if (data.data) :
inRoom = 1
else:
inRoom = 0
print("in color handler: "+str(inRoom))
def setup() :
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
global distances, angle_increment, turn, vel, drive, fireReading, ard
for port, desc, hwid in sorted(ports):
print("{}: {} [{}]".format(port, desc, hwid))
if 'USB2.0-Serial' in desc:
print('arduino connected')
ard = serial.Serial(port, 9600, timeout=0)
while True:
curr = ard.readline().decode().strip()
# print(curr)
if curr == 'sound':
print('got sound')
break
time.sleep(1)
rospy.init_node('wallfollower', anonymous=False)
rospy.Subscriber("/scan", LaserScan, scanHandler, queue_size=1, buff_size=1)
rospy.Subscriber("/color", Bool, colorHandler)
vel = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
turn = rospy.Publisher('turn', Float64, queue_size=10)
drive = rospy.Publisher('drive', Float64, queue_size=10)
time.sleep(1)
# turnLeftDegrees(31)
# time.sleep(2)
# turnRightDegrees(180)
# time.sleep(2)
# moveForwardDistance(.5)
# time.sleep(2)
# moveForwardDistance(-.5)
# time.sleep(2)
# time.sleep(1)
# turnAndMove(0, 0)
# alignToWall(0)
# time.sleep(1)
#wallfollower()
rospy.spin()
def removeInf(distances) :
d = []
count = 0
for dist in range(len(distances)) :
#print(distances[dist])
if (not math.isinf(distances[dist])) :
d.append(distances[dist])
else :
count += 1
print(dist)
d.append(-1)
print("Number of infinities removed: " + str(count))
return d
def sweepRoom () :
oldRead = read_flame()
latestRead = 0
for i2 in range(0, 12):
turnRightDegrees(30)
rospy.sleep(1)
latestRead = read_flame()
if (latestRead > 100) :
if (oldRead < latestRead):
while (oldRead < latestRead) :
turnRightDegrees(30)
rospy.sleep(1)
oldRead = latestRead
latestRead = read_flame()
oldRead, latestRead = latestRead, oldRead
while (oldRead < latestRead) :
turnLeftDegrees(5)
rospy.sleep(1)
oldRead = latestRead
latestRead = read_flame()
turnRightDegrees(5) # ?
toggle_extinguisher(True)
rospy.sleep(10)
toggle_extinguisher(False)
else:
oldRead, latestRead = latestRead, oldRead
while (oldRead < latestRead) :
turnLeftDegrees(30)
rospy.sleep(1)
oldRead = latestRead
latestRead = read_flame()
oldRead, latestRead = latestRead, oldRead
while (oldRead < latestRead) :
turnRightDegrees(5)
rospy.sleep(1)
oldRead = latestRead
latestRead = read_flame()
turnLeftDegrees(5) # ?
toggle_extinguisher(True)
rospy.sleep(10)
toggle_extinguisher(False)
break
oldRead = latestRead
def planeDistance (angle) :
global distances
hypo = distances[angle]
if hypo != -1 :
x = hypo * math.cos(angle * angle_increment) # convert to radians
y = hypo * math.sin(angle * angle_increment)
else :
x = -1
y = -1
return [x, y]
def scanHandler(scan) :
global distances, detectOpening, angle_increment, justTurned, tcs, inRoom, acceptTime
if rospy.get_time()-scan.header.stamp.secs<acceptTime:
distances = scan.ranges
print("scanhandler")
angle_increment = scan.angle_increment
r,g,b,c = tcs.get_raw_data()
distances = removeInf(distances)
# For the room detection
print('inRoom', inRoom)
if (inRoom) :#r+g+b>300
print ("Now in room")
turnAndMove(0, 0)
moveForward(0.2)
rospy.sleep(1)
print("Turning back to the door")
turnLeftDegrees(180)
rospy.sleep(1)
print("Sweeping")
sweepRoom()
alignToWall(0)
while (inRoom) :
print("getting out of room")
moveForwardDistance(0.05)
rospy.sleep(0.7)
moveForwardDistance(0.05)
rospy.sleep(0.7)
print("Got out of room")
print("inRoom should be false, and is " + str(inRoom))
turnAndMove(0, 0)
justTurned = True
#turnRightDegrees(180)
#rospy.sleep(0.5)
#moveForwardDistance(0)
elif (justTurned):
turnAndMove(0, 0)
if(distances[0] < distances[180]):
alignToWall(0)
else:
alignToWall(180)
rospy.sleep(0.7)
justTurned = False
else:
# This determines if there is an opening to the right
#if (newDist > (sum(detectOpening)/len(detectOpening)) + tolerance) :
print("0: " + str(distances[0]))
print("90: " + str(distances[90]))
print("180: " + str(distances[180]))
if (distances[0] > 0.65 or distances[0] == -1) :
print("Turn right")
turnAndMove(0,0)
xy315 = planeDistance(315)
print("Initial: " + str(xy315))
while (xy315[1] > -0.1) :
moveForwardDistance(0.01) # Arbitrary, to make sure robot clears opening
rospy.sleep(0.5)
xy315 = planeDistance(315)
print("After initial: " + str(xy315))
rospy.sleep(1)
turnRightDegrees(90)
rospy.sleep(1)
print("Moving into opening")
# Distance will have to be determined through testing
moveForwardDistance(0.3)
rospy.sleep(1)
justTurned = True
turnAndMove(0,0)
# alignToWall(0)
elif distances[90] > 0.4 or distances[90] == -1:
print("Go Forward")
moveForward(0.05)
rospy.sleep(1)
else:
print("Turn left")
turnAndMove(0, 0)
moveForwardDistance(0.1) # Arbitrary, to make sure robot clears opening
rospy.sleep(1)
turnLeftDegrees(90)
rospy.sleep(1)
print("Moving into opening")
# Distance will have to be determined through testing
moveForwardDistance(0.3)
rospy.sleep(1)
justTurned = True
turnAndMove(0,0)
#alignToWall(0)
# This aligns regularly
#print("distances[0]: " + str(distances[0]) + ", distances[90]: " + str(distances[90]) + ", distances[180]: " + str(distances[180]) + ", distances[270]: " + str(distances[270]))
# Moves forward m meters at .20
def moveForwardDistance(distance):
global drive
drive.publish(Float64(distance))
# Turns left at 1 radians (? degrees) per second
def turnLeftDegrees(degrees):
global turn
radians = - degrees * (math.pi / (180))
turn.publish(Float64(radians))
# Turns right at 1 radians (? degrees) per second
def turnRightDegrees(degrees):
global turn
radians = degrees * (math.pi/180)
turn.publish(Float64(radians))
#SPEED IS IN M/S
def moveForward(speed):
global vel
msg = Twist()
msg.linear = Vector3(speed, 0, 0)
msg.angular = Vector3(0, 0, 0)
vel.publish(msg)
def moveBackward(speed):
global vel
msg = Twist()
msg.linear = Vector3(-speed, 0, 0)
msg.angular = Vector3(0, 0, 0)
vel.publish(msg)
def turnLeft(speed):
global vel
msg = Twist()
msg.linear = Vector3(0, 0, 0)
msg.angular = Vector3(0, 0, -speed)
vel.publish(msg)
def turnRight(speed):
global vel
msg = Twist()
msg.linear = Vector3(0, 0, 0)
msg.angular = Vector3(0, 0, speed)
vel.publish(msg)
def turnAndMove(speedForward, speedClockwise):
global vel
msg = Twist()
msg.linear = Vector3(speedForward, 0, 0)
msg.angular = Vector3(0, 0, speedClockwise)
vel.publish(msg)
def stopeverything():
print('exiting')
vel = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
msg = Twist()
msg.linear = Vector3(0, 0, 0)
msg.angular = Vector3(0, 0, 0)
vel.publish(msg)
print('done exiting')
if __name__ == '__main__':
print(rospy.get_published_topics())
while not (['/motor_listener_listening', 'std_msgs/String'] in rospy.get_published_topics()):
pass
print('starting')
rospy.on_shutdown(stopeverything)
setup()