/
3_29_Test.py
280 lines (239 loc) · 10 KB
/
3_29_Test.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import smbus
import time
import startup
import math
import RPi.GPIO as GPIO
from vision import vision
from gpiozero import Button
from picamera import PiCamera
import jsonread
import copyjson
from NanoManager import NanoManager
import os
import sys
# IEEE competition code
address1 = 10 # Address to the drive nano
address2 = 20 # Address to the arm nano
BUTTON_PIN = 14
GREEN_LED_PIN = 18
RED_LED_PIN = 15
VIEWING_ANGLE = 150
bus = smbus.SMBus(1)
camera = PiCamera()
camera.resolution = (600, 600)
camera.rotation = 180
myvision = vision()
myNano = NanoManager()
movedLeft = 0
movedRight = 0
movedBack = 0
found_cube = True
back_counter = 0
distance = 0
# List of info to send. Format is [forward, side, distance]
#data = []cd
GPIO.setmode(GPIO.BCM)
GPIO.setup(GREEN_LED_PIN, GPIO.OUT)
GPIO.setup(RED_LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.OUT)
GPIO.output(GREEN_LED_PIN, GPIO.LOW)
GPIO.output(RED_LED_PIN, GPIO.LOW)
# Forwards and Backwards
while True:
start = GPIO.input(BUTTON_PIN)
if start == True:
break;
myNano.driveRobot(address1, 1, 0, 12, 0)
myNano.driveRobot(address1, -1, 0, 12, 0)
GPIO.output(RED_LED_PIN, GPIO.HIGH)
time.sleep(10000)
while True:
print('begin')
#check if file is in location
#if true, proceed to round code (and button)
#if not, run through jsonread
if not os.path.isfile("/home/pi/locations/mars1.json"):
copy = False
while (not copy):
copy = copyjson.copyjson()
print('copied')
GPIO.output(GREEN_LED_PIN, GPIO.HIGH)
time.sleep(3)
print('green led')
sys.exit(0)
print('you shouldnt be seeing this')
# button pressed = round code starting
while True:
start = GPIO.input(BUTTON_PIN)
if start == True:
break;
# Get JSON file location
data = jsonread.jsonread()
if (data == 0):
myNano.driveRobot(address1, 1, 0, 12, 0)
myNano.driveRobot(address1, -1, 0, 12, 0)
GPIO.output(RED_LED_PIN, GPIO.HIGH)
time.sleep(10000)
# If for some reason json is empty just go forwards and backwards
#if len(data) <= 0:
#myNano.driveRobot(address1, 1, 0, 12, 0)
#myNano.driveRobot(address1, -1, 0, 12, 0)
#GPIO.output(RED_LED_PIN, GPIO.HIGH)
#time.sleep(10000)
size = data["size"]
# no way block is at 4,4 (starting location of robot)
# no way closest block is 5 feet away
minDist = 5 # feet
minX = 4
minY = 4
fuck = False
while (size > 0):
if (data["y coords"][size - 1] > 4):
fuck = True
size = size - 1
size = data["size"]
# interates through all coordinates and finds minimum distance and at what coordinates
while (size > 0):
print(size)
print("\n")
dist = abs(math.sqrt(math.pow((data["x coords"][size - 1] - 4), 2)+math.pow((data["y coords"][size - 1] - 4), 2)))
#if all cubes are behind bot and there's one cube on the same x coordinate as robot, then just do forwards and backwards
if (data["x coords"][size - 1] == 4 and data["y coords"][size - 1] <= 4 and not fuck):
myNano.driveRobot(address1, 1, 0, 12, 0)
myNano.driveRobot(address1, -1, 0, 12, 0)
GPIO.output(RED_LED_PIN, GPIO.HIGH)
os.remove("/home/pi/locations/mars1.json")
time.sleep(100000)
if (not (data["y coords"][size - 1] <= 1) and not (data["x coords"][size - 1] == 4 and data["y coords"][size - 1] <= 4)):
if (dist < minDist):
minX = data["x coords"][size - 1]
minY = data["y coords"][size - 1]
minDist = dist
size = size - 1
print("MinX:", minX)
print("\n")
print("MinY:", minY)
print("\n")
# Drive to one location
# if y coordinate is < 4 turn 180, > 4 nothing, = 4 turn 90 or -90 depending on x coor
# if x coordinate is < 4 strafe left, > 4 strafe right, else nothing
# stop one block away from actual location maybe?
#drive up to cube
# if y != 4, drive (abs(y coord - 4ft) - 1 ft)
# if y == 4, drive (abs(x coord - 4ft) - 1 ft)
#if (minY == 4):
# if (minX < 4):
# #turn -90
# myNano.driveRobot(address1, 0, 0, 0, -90)
# else:
# #turn 90
# myNano.driveRobot(address1, 0, 0, 0, 90)
# #drive to cube
# dist = 12*(minX - 4 - 1)
# myNano.driveRobot(1, 0, dist, 0)
if (minY > 4):
dist = 12*abs(minX - 4)
if (minX < 4):
#strafe left
myNano.driveRobot(address1, 0, -1, dist, 0)
elif (minX > 4):
# strafe right
myNano.driveRobot(address1, 0, 1, dist, 0)
dist = 12*(minY - 4 - 1)
print(str(dist))
myNano.driveRobot(address1, 1, 0, 12, 0)
elif (minY <= 4):
#drive back
myNano.driveRobot(address1, -1, 0, 12*(4 - minY + 1), 0)
#myNano.driveRobot(address1, 0, 0, 0, 90)
#myNano.driveRobot(address1, 0, 0, 0, 90)
dist = 12*abs(minX - 4)
print(dist);
if (minX < 4):
#strafe left
myNano.driveRobot(address1, 0, -1, dist, 0)
elif (minX > 4):
#strafe right
myNano.driveRobot(address1, 0, 1, dist, 0)
#dist = 12*(minY - 4 - 1)
#myNano.driveRobot(address1, 1, 0, dist, 0)
# optimal angle for looking for cube
myNano.moveArm(address2, 3, VIEWING_ANGLE)
# Align to the cube
while True:
camera.capture("center.jpg")
center = myvision.getCenter("center.jpg");
# Cube is to the right
if (center == 1):
myNano.driveRobot(address1, 0, 1, 2, 0)
movedRight += 1
# Cube is to the left
elif (center == -1):
myNano.driveRobot(address1, 0, -1, 2, 0)
movedLeft += 1
elif (center == -2):
print("Block not found")
print(back_counter)
if back_counter >1:
found_cube = False
break
myNano.driveRobot(address1, -1, 0, 3, 0)
movedBack += 1
back_counter = back_counter + 1
# Cube is centered. Move forward to pick cube up.
else:
#distance = myvision.getDist2Cube("center.jpg")
myNano.moveArm(address2, 3, 60)
#print("distance to cube: " + str(distance))
myNano.driveRobot(address1, 1, 0, 12, 0)
# Move arm and pick it up
break;
# pick up block
myNano.moveArm(address2, 2, 0)
myNano.moveArm(address2, 3, VIEWING_ANGLE)
while(movedRight != 0):
myNano.driveRobot(address1, 0, -1, 2, 0)
movedRight -= 1
while(movedLeft != 0):
myNano.driveRobot(address1, 0, 1, 2, 0)
movedLeft -= 1
while(movedBack != 0):
myNano.driveRobot(address1, 1, 0, 3, 0)
movedBack -= 1
if found_cube:
myNano.driveRobot(address1, -1, 0, 12, 0)
#myNano.driveRobot(address1, 0, 0, 0, 90)
#myNano.driveRobot(address1, 0, 0, 0, 90)
# return to start by using the same movements as before
#if (minY == 4):
# dist = 12*(minX - 4 - 1)
# myNano.driveRobot(-1, 0, dist)
if (minY > 4):
dist = 12*abs(minX - 4)
if (minX < 4):
#strafe right
myNano.driveRobot(address1, 0, 1, dist, 0)
elif (minX > 4):
# strafe left
myNano.driveRobot(address1, 0, -1, dist, 0)
dist = 12*(minY - 4 - 1)
print(str(dist))
myNano.driveRobot(address1, -1, 0, 12, 0)
elif (minY <= 4):
dist = 12*abs(minX - 4)
if (minX < 4):
#strafe right
myNano.driveRobot(address1, 0, 1, dist, 0)
elif (minX > 4):
#strafe left
myNano.driveRobot(address1, 0, -1, dist, 0)
dist = 12*(abs(minY - 4) + 1)
print(dist)
myNano.driveRobot(address1, 1, 0, dist, 0)
myNano.moveArm(address2, 2, 180)
myNano.moveArm(address2, 3, 70)
#end of code, delete json file
#os.remove("/home/pi/locations/mars1.json")
GPIO.output(RED_LED_PIN, GPIO.HIGH)
time.sleep(10000)
#repeat code for remaining cubes