-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutomationHubv3.py
136 lines (118 loc) · 3.53 KB
/
AutomationHubv3.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
import RPi.GPIO as GPIO
import time
import socket
import struct
GPIO.setmode(GPIO.BCM)
#global variables
LEFT = (12, 13)
RIGHT = (5, 6)
FRONT = (16, 19)
FRONT_SEN_THRESH = 10
LEFT_SEN_THRESH = 20
RIGHT_SEN_THRESH = 20
ZONE1_THRESH = 14
ZONE2_THRESH = 8
ZONE3_THRESH = 14
ZONE4_THRESH = 8
ZONE5_THRESH = 15
motorServerSocket = None
motorSocket = None
##functions left to implement that are used in code already:
## turnRobotRight(Degree amount)
## turnRobotLeft(Degree amount)
## driveRobotForward(distance amount)
## driveForward(speed)
def setupSensors():
GPIO.setup(RIGHT[0],GPIO.OUT)
GPIO.setup(RIGHT[1],GPIO.IN)
GPIO.output(RIGHT[0], False)
GPIO.setup(LEFT[0],GPIO.OUT)
GPIO.setup(LEFT[1],GPIO.IN)
GPIO.output(LEFT[0], False)
GPIO.setup(FRONT[0],GPIO.OUT)
GPIO.setup(FRONT[1],GPIO.IN)
GPIO.output(FRONT[0], False)
print('Automation Hub: Settings up sensors')
time.sleep(2)
def sensorRead(senNum):
global LEFT
global RIGHT
global FRONT
distance = 0
pulse_start = time.time()
pulse_end = time.time()
GPIO.output(senNum[0], True)
time.sleep(.00001)
GPIO.output(senNum[0], False)
#print('About to read sensor')
while GPIO.input(senNum[1])==0:
pulse_start = time.time()
if pulse_start - pulse_end > .02:
distance = 100
break
while GPIO.input(senNum[1])==1:
pulse_end = time.time()
if pulse_start - pulse_end > .02:
distance = 100
break
pulse_duration = pulse_end - pulse_start
if distance != 100:
distance = pulse_duration * 17150
distance = round(distance, 2)
return distance
def setupMotorProgramConnection():
global motorServerSocket
global motorSocket
motorServerSocket = socket.socket()
hostname = socket.gethostname()
port = 4002
while(1):
print('AutomaWaiting for motor program')
try:
motorServerSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
motorServerSocket.bind((hostname, port))
motorServerSocket.listen(100)
print("Created motor program connection")
break
except:
print('Couldnt make motor program connection')
motorSocket, addr = motorServerSocket.accept()
print('Connected to motor program')
#Main code
#client
#needs set up for local
setupMotorProgramConnection()
setupSensors()
#startUp()
while (1):
front = True
right = True
left = True
back = True
rightSensor = sensorRead(RIGHT)
leftSensor = sensorRead(LEFT)
frontSensor = sensorRead(FRONT)
zone = 0
if (frontSensor <= ZONE5_THRESH):
print('front to low')
zone = 5
elif (rightSensor <= ZONE4_THRESH):
print('right way to low')
zone = 4
elif (rightSensor <= ZONE3_THRESH):
print('right to low')
zone = 3
elif (leftSensor <= ZONE2_THRESH):
print('left way to low')
zone = 2
elif (leftSensor <= ZONE1_THRESH):
print('left to low')
zone = 1
else:
print('sensors clear')
zone = 0
print('Right Sensor: ' + str(rightSensor) + '\n' + 'Left Sensor: ' + str(leftSensor) + '\n' + 'Front Sensor: ' + str(frontSensor))
data = struct.pack('i', zone)
#print('Right Sensor: ' + str(rightSensor) + '\n' + 'Left Sensor: ' + str(leftSensor) + '\n')
motorSocket.send(data)
#time.sleep(0.1)