-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafeDrive.py
60 lines (49 loc) · 1.46 KB
/
SafeDrive.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
import time
class SafeDrive:
def __init__(self,motors,distance_gen):
self.BACKWARDS = 'BACKWARDS'
self.SAFE= 'SAFE'
self.UNSAFE = 'UNSAFE'
self.state = self.UNSAFE
self.motors = motors
self.time_of_state_change = None
self.running = True
self.distance_gen = distance_gen
def updateBackwards(self):
finished = (time.time()-self.time_of_state_change)>1
if(finished):
self.motors.stop()
self.state = self.UNSAFE
def startBackwards(self,speed):
self.state = self.BACKWARDS
self.time_of_state_change = time.time()
self.motors.backward(speed)
def handleNewData(self,left,right):
if (self.state == self.BACKWARDS):
self.updateBackwards()
elif(left<40 or right <40):
self.motors.stop()
self.state = self.UNSAFE
else:
self.state = self.SAFE
def stop(self):
self.motors.stop()
def forward(self,speed):
if(self.state == self.SAFE):
self.motors.forward(speed)
def backward(self,speed):
self.startBackwards(speed)
def pivot_left(self,speed):
if(self.state == self.SAFE):
self.motors.pivot_left(speed)
def pivot_right(self,speed):
if(self.state == self.SAFE):
self.motors.pivot_right(speed)
async def start(self):
self.running = True
async for distance in self.distance_gen():
if self.running == False:
self.motors.stop()
self.reset()
break
self.handleNewData(distance['left'],distance['right'])