-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
160 lines (136 loc) · 5.08 KB
/
example.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
import signal
import ev3dev.ev3 as ev3
btn = ev3.Button()
mL = ev3.LargeMotor('outA')
mR = ev3.LargeMotor('outB')
mL.run_direct()
mR.run_direct()
mL.polarity = "normal"
mR.polarity = "normal"
speedL = mL.duty_cycle_sp
speedR = mR.duty_cycle_sp
BASE_SPEED = 30
TURN_SPEED = -10
sL = ev3.ColorSensor('in1')
sR = ev3.ColorSensor('in2')
sL.mode = 'COL-REFLECT'
sR.mode = 'COL-REFLECT'
assert sL.connected, "ColorSensorLeft(ColorSensor) is not connected"
assert sR.connected, "ColorSensorRight(ColorSensor) is not connected"
def signal_handler(sig, frame):
print('Shutting down gracefully')
mL.duty_cycle_sp = 0
mR.duty_cycle_sp = 0
exit(0)
signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C to exit')
def calculate_direction(next_direction):
global current_direction
current_direction_index = find_direction_index(current_direction)
next_direction_index = find_direction_index(next_direction)
if current_direction_index == next_direction_index:
return 0 # Go straight
elif abs(current_direction_index - next_direction_index) == 2:
current_direction_print = current_direction
current_direction = next_direction
return 1 # Turn around
elif current_direction_index - next_direction_index == 1 or current_direction_index - next_direction_index == -3:
current_direction_print = current_direction
current_direction = next_direction
return 2 # Turn left
elif current_direction_index - next_direction_index == -1 or current_direction_index - next_direction_index == 3:
current_direction_print = current_direction
current_direction = next_direction
return 3 # Turn right
def find_direction_index(direction):
index = 0
for value in DIRECTIONS:
if value.lower() == direction.lower():
return index
index += 1
def increment_counter():
global counter_index
global finished
counter_index += 1
if counter_index < len(solution):
print('If')
else:
print('Else')
finished = True
DIRECTIONS = ['u', 'r', 'd', 'l']
# solution = ['L', 'd', 'l', 'l', 'l', 'u', 'u', 'u', 'u', 'R', 'R', 'd', 'r', 'U', 'U', 'U', 'U', 'd', 'd', 'd',
# 'l', 'l', 'l', 'd', 'd', 'r', 'U', 'l', 'u', 'R', 'R', 'd', 'r', 'U', 'U', 'U', 'r', 'u', 'L', 'L',
# 'L', 'u', 'l', 'D', 'r', 'r', 'r', 'd', 'd', 'd', 'd', 'l', 'l', 'l', 'd', 'd', 'd', 'r', 'U', 'U',
# 'U', 'l', 'u', 'R', 'R', 'd', 'r', 'U', 'U', 'U', 'r', 'u', 'L', 'd', 'd', 'd', 'd', 'l', 'l', 'd',
# 'd', 'd', 'r', 'r', 'u', 'L', 'd', 'l', 'U', 'U', 'U', 'l', 'u', 'R', 'R', 'd', 'r', 'U', 'U', 'U',
# 'r', 'u', 'u', 'L', 'L', 'L', 'r', 'D', 'R', 'u', 'r', 'D']
solution = ['u', 'l', 'l', 'l']
STATE = 0
current_direction = 'l'
counter_index = 0
mL.duty_cycle_sp = BASE_SPEED
mR.duty_cycle_sp = BASE_SPEED
on_line = False
direction = -1
finished = False
while not finished:
while STATE == 0:
if sL.value() <= 20 and sR.value() <= 20:
mL.duty_cycle_sp = BASE_SPEED
mR.duty_cycle_sp = BASE_SPEED
STATE = 1
if sL.value() <= 60 <= sR.value():
mL.duty_cycle_sp = TURN_SPEED
mR.duty_cycle_sp = BASE_SPEED
elif sL.value() >= 60 >= sR.value():
mR.duty_cycle_sp = TURN_SPEED
mL.duty_cycle_sp = BASE_SPEED
elif sL.value() >= 60 and sR.value() >= 60:
mL.duty_cycle_sp = BASE_SPEED
mR.duty_cycle_sp = BASE_SPEED
if sL.value() >= 60 and sR.value() >= 60 and STATE == 1:
direction = calculate_direction(solution[counter_index])
print(solution[counter_index])
increment_counter()
STATE = 2
while STATE == 2:
print('commencing turning')
if direction == 0:
mR.duty_cycle_sp = BASE_SPEED
mL.duty_cycle_sp = BASE_SPEED
STATE = 0
elif direction == 1:
# do nothing
print('should turn around')
elif direction == 2:
print('Direction = 2')
STATE = 3
mL.duty_cycle_sp = TURN_SPEED
mR.duty_cycle_sp = BASE_SPEED
while STATE == 3:
if sL.value() <= 20:
on_line = True
if on_line is True and sL.value() >= 60:
print('passed line')
on_line = False
mL.duty_cycle_sp = BASE_SPEED
mR.duty_cycle_sp = BASE_SPEED
STATE = 0
elif direction == 3:
print('Direction = 3')
STATE = 3
mR.duty_cycle_sp = TURN_SPEED
mL.duty_cycle_sp = BASE_SPEED
while STATE == 3:
if sR.value() <= 20:
on_line = True
if on_line is True and sR.value() >= 60:
print('passed line')
on_line = False
mL.duty_cycle_sp = BASE_SPEED
mR.duty_cycle_sp = BASE_SPEED
STATE = 0
print('Program finishing')
mL.duty_cycle_sp = 0
mR.duty_cycle_sp = 0
exit()