-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqnx_navigation.py
105 lines (80 loc) · 2.8 KB
/
qnx_navigation.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
import rpi_gpio as GPIO
import time
#17, 27
sensor_input = [17,27,22,5,6,26,25,16]
movement_output = [23,24]
for i in sensor_input:
GPIO.setup(i, GPIO.IN)
for i in movement_output:
GPIO.setup(i, GPIO.OUT)
latest_data = {
"front_sensor": -1,
"left_sensor": -1,
"right_sensor": -1,
"back_sensor": -1,
}
def moveForward():
print("Moving forward")
GPIO.output(movement_output[0], GPIO.HIGH)
GPIO.output(movement_output[1], GPIO.HIGH)
def rotateRight():
print("Rotating right")
GPIO.output(movement_output[0], GPIO.HIGH)
GPIO.output(movement_output[1], GPIO.LOW)
def rotateLeft():
print("Rotating left")
GPIO.output(movement_output[0], GPIO.LOW)
GPIO.output(movement_output[1], GPIO.HIGH)
def rotateAround():
print("Rotating around")
GPIO.output(movement_output[0], GPIO.LOW)
GPIO.output(movement_output[1], GPIO.HIGH)
def stopRumba():
print("Stopping")
GPIO.output(movement_output[0], GPIO.LOW)
GPIO.output(movement_output[1], GPIO.LOW)
while True:
input_data = []
for i in sensor_input:
input_data.append(GPIO.input(i))
time.sleep(1)
print(input_data)
sixBits = ""
for i in input_data[2:]:
sixBits += str(i)
sixBits+="000"
decimal = int(sixBits, 2)
print(decimal)
if (input_data[0] == 0 and input_data[1] == 0):
print("Forward sensor packet")
latest_data["front_sensor"] = decimal
# GPIO.output(movement_output[0], GPIO.HIGH)
# GPIO.output(movement_output[1], GPIO.HIGH)
elif (input_data[0] == 0 and input_data[1] == 1):
print("Right sensor packet")
latest_data["right_sensor"] = decimal
# GPIO.output(movement_output[0], GPIO.HIGH)
# GPIO.output(movement_output[1], GPIO.LOW)
elif (input_data[0] == 1 and input_data[1] == 0):
print("Back sensor packet")
latest_data["back_sensor"] = decimal
# GPIO.output(movement_output[0], GPIO.LOW)
# GPIO.output(movement_output[1], GPIO.LOW)
elif (input_data[0] == 1 and input_data[1] == 1):
print("Left sensor packet")
latest_data["left_sensor"] = decimal
# GPIO.output(movement_output[0], GPIO.LOW)
# GPIO.output(movement_output[1], GPIO.HIGH)
max_direction = max(latest_data, key=latest_data.get)
print(f"Biggest direction: {max_direction}")
if (abs(latest_data["front_sensor"] - latest_data["back_sensor"]) < 50) and (abs(latest_data["right_sensor"] - latest_data["left_sensor"]) < 50):
stopRumba()
elif max_direction == "front_sensor":
moveForward()
elif max_direction == "right_sensor":
rotateRight()
elif max_direction == "left_sensor":
rotateLeft()
elif max_direction == "back_sensor":
rotateAround()
# print(sixBits)