-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.py
72 lines (60 loc) · 2.22 KB
/
logic.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
######################################
# Author: Ethan Reker
# Date: April 10, 2017
# Purpose: Provide a unit to perform basic logic
# from data read by sensors
######################################
class LogicalUnit:
sensorList = [] #A list that will contain all of the sensors
motorController
sensorData
stopDistance = 20 #distance in centimeters
decisionList = []
def readSensors(self):
for sensor in sensorList:
sensorData[sensor.name] = sensor.read()
def start(self):
decision = ""
while true:
readSensors()
#Check if front sensor is less than 20cm from an object
if(sensorData["FRONT"] < 20 and sensorData["FRONT"] != 0):
decision = "STOP"
motorController.setMode(decision)
#If distance on left > distance on right
if(sensorData["LEFT"] > sensorData["RIGHT"]):
if(sensorData["LEFT"] != 0):
decision = "LEFT"
motorController.setMode(decision)
#If distance on right > distance on left
else:
if(sensorData["RIGHT"] != 0):
decision = "RIGHT"
motorController.setMode(decision)
#Check if Left sensor reads less than 10cm
elif(sensorData["LEFT"] < 10 and sensorData["LEFT"] != 0):
decision = "STOP"
motorController.setMode(decision)
#Check if right sensor reads greater than 10 : if so turn right
if(sensorData["RIGHT"] > 10 and sensorData["RIGHT"] != 0):
decision = "RIGHT"
motorController.setMode(decision)
#Otherwise back up
else:
decision = "BACKWARD"
motorController.setMode(decision)
#Check if Right sensor reads less than 10cm
elif(sensorData["RIGHT"] < 10 and sensorData["RIGHT"] != 0):
decision = "STOP"
motorController.setMode(decision)
if(sensorData["LEFT"] > 10):
decision = "LEFT"
motorController.setMode(decision)
else:
decision = "BACKWARD"
motorController.setMode(decision)
else:
decision = "FORWARD"
motorController.setMode(decision)
decisionList.append(decision)
time.sleep(0.1)