-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlocalize.py
More file actions
553 lines (427 loc) · 16.6 KB
/
localize.py
File metadata and controls
553 lines (427 loc) · 16.6 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
from Raveen.motorRotating import *
from Raveen.tofsensorreadings import *
from Raveen.cuboidToF import *
from Raveen.servo_COntrol_rasberry import *
from Neo.align import *
import cv2
import numpy as np
from PIL import Image
import math
from Nidula.irSensors import *
def get_limits(color):
c = np.uint8([[color]]) # BGR values
hsvC = cv2.cvtColor(c, cv2.COLOR_BGR2HSV)
hue = hsvC[0][0][0] # Get the hue value
# Handle red hue wrap-around
if hue >= 165: # Upper limit for divided red hue
lowerLimit = np.array([hue - 10, 100, 100], dtype=np.uint8)
upperLimit = np.array([180, 255, 255], dtype=np.uint8)
elif hue <= 15: # Lower limit for divided red hue
lowerLimit = np.array([0, 100, 100], dtype=np.uint8)
upperLimit = np.array([hue + 10, 255, 255], dtype=np.uint8)
else:
lowerLimit = np.array([hue - 10, 100, 100], dtype=np.uint8)
upperLimit = np.array([hue + 10, 255, 255], dtype=np.uint8)
return lowerLimit, upperLimit
def wall_follow(sensor, distance_right, distance_left, baseSpeed, ob_detect=True):
kp = 0.4
kd = 0.4
last_error = 0
while True:
if sensor == "sensor_right":
instant_distance = tof2Readings()
error = instant_distance - distance_right
leftSpeed = baseSpeed + (error * kp + (last_error - error) * kd)
rightSpeed = baseSpeed - (error * kp + (last_error - error) * kd)
last_error = error
elif sensor == "sensor_left":
instant_distance = tof3Readings() # This should be tof3Readings
error = instant_distance - distance_left
leftSpeed = baseSpeed - error * kp
rightSpeed = baseSpeed + error * kp
else:
pass
if leftSpeed >= 60:
leftSpeed = 60
if rightSpeed >= 60:
rightSpeed = 60
if rightSpeed < 0:
rightSpeed = 0
if leftSpeed < 0:
leftSpeed = 0
leftrightMotor_Forward(leftSpeed, rightSpeed)
if ob_detect == True:
if tof3Readings() < (distance_left - 100):
return "left"
elif tof2Readings() < (distance_right - 100):
return "right"
elif tof1Readings() <= 150:
return "end"
elif ob_detect == False:
if tof1Readings() <= right_cons:
return "forward"
def wall_follow_back(sensor, distance_right, distance_left, baseSpeed, ob_detect=True):
kp = 0.1
kd = 0.4
last_error = 0
global right_cons
while True:
if sensor == "sensor_right":
instant_distance = tof2Readings()
error = instant_distance - distance_right
leftSpeed = baseSpeed - (error * kp + (last_error - error) * kd)
rightSpeed = baseSpeed + (error * kp + (last_error - error) * kd)
last_error = error
elif sensor == "sensor_left":
instant_distance = tof3Readings() # This should be tof3Readings
error = instant_distance - distance_left
leftSpeed = baseSpeed - error * kp
rightSpeed = baseSpeed + error * kp
else:
pass
if leftSpeed >= 60:
leftSpeed = 60
if rightSpeed >= 60:
rightSpeed = 60
if rightSpeed < 0:
rightSpeed = 0
if leftSpeed < 0:
leftSpeed = 0
leftrightMotor_Backward(rightSpeed,leftSpeed)
if ob_detect == True:
if tof3Readings() < (distance_left - 100):
return "left"
elif tof2Readings() < (distance_right - 100):
return "right"
if ob_detect == False:
if tof1Readings() <= right_cons:
return "forward"
def init_measure():
"""Getting the initial measurements(Length and width) of the trash yard"""
left_right_sensor_dis = 140 # Distance between the left and right tof sensors
front_dis = tof1Readings()
left_dis = tof3Readings()
right_dis = tof2Readings()
length = left_dis + right_dis + left_right_sensor_dis
width = front_dis
return front_dis, left_dis, right_dis, length, width
def find_white(sensor, distance_right, distance_left, baseSpeed):
kp = 0.4
kd = 0.4
last_error = 0
video_capture = cv2.VideoCapture(0, cv2.CAP_V4L2)
video_capture.set(4, 480) # Set the height of the frame
video_capture.set(3, 640) # Set the width of the frame
video_capture.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1) # manual mode
video_capture.set(cv2.CAP_PROP_EXPOSURE, 150)
while True:
ret, image = video_capture.read()
image = cv2.flip(image, 0)
image = cv2.flip(image, 1)
width = int(640)
height = int(480)
dimensions = (width, height)
image = cv2.resize(image, dimensions, interpolation=cv2.INTER_AREA)
image = image[120:350, 0:640]
Blackline = cv2.inRange(image, (140,140,140), (255,255,255))
kernel = np.ones((3,3), np.uint8)
Blackline = cv2.erode(Blackline, kernel, iterations=5)
Blackline = cv2.dilate(Blackline, kernel, iterations=9)
contours_blk, hierarchy_blk = cv2.findContours(Blackline.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
if sensor == "sensor_right":
instant_distance = tof2Readings()
error = instant_distance - distance_right
leftSpeed = baseSpeed + (error * kp + (last_error - error) * kd)
rightSpeed = baseSpeed - (error * kp + (last_error - error) * kd)
last_error = error
elif sensor == "sensor_left":
instant_distance = tof3Readings() # This should be tof3Readings
error = instant_distance - distance_left
leftSpeed = baseSpeed - error * kp
rightSpeed = baseSpeed + error * kp
else:
pass
if leftSpeed >= 60:
leftSpeed = 60
if rightSpeed >= 60:
rightSpeed = 60
if rightSpeed < 0:
rightSpeed = 0
if leftSpeed < 0:
leftSpeed = 0
leftrightMotor_Forward(leftSpeed, rightSpeed)
if len(contours_blk) > 0:
c = max(contours_blk, key=cv2.contourArea)
M = cv2.moments(c)
try:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
except:
continue
print(cy)
if cy < 160:
goForward(40)
sleep(0.2)
stop()
break
return 0
cv2.imshow("Frame",Blackline)
if cv2.waitKey(10) & 0xFF == ord("q"):
break
front_dis = None
left_dis = None
right_dis = None
length = None
width = None
width_cons = None
length_cons = None
right_cons = None
left_cons = None
ob_direction = None
orientation = None
def yard():
global front_dis, left_dis, right_dis, length, width, width_cons, length_cons, right_cons, left_cons, ob_direction, orientation
servo_3_rotate(-40)
height_list = [10]
front_dis, left_dis, right_dis, length, width = init_measure() # Getting the initial measurements of the yard
# Defining the initial measurements
width_cons = width
length_cons = length
right_cons = right_dis
left_cons = left_dis
ob_direction = wall_follow("sensor_right", right_dis, left_dis, 40) # Giving the direction of the object
orientation = None
if ob_direction == "end":
turnLeft(40)
sleep(3.85)
stop()
elif ob_direction == "left": # If the object is placed left
# front_dis, left_dis, right_dis, length, width = init_measure()
goForward(40) # Moving forward to align with the object
sleep(1.2)
stop()
turnLeft(40) # Turn 90 degrees left
sleep(1.95)
stop()
# orientation = 180
servo_3_rotate(0) # Setting the camera to find the tower position
front_dis, left_dis, right_dis, length, width = init_measure() # Getting the measurements to wall follow
find_white("sensor_right", right_dis, left_dis, 40)
sleep(1)
height_list.append(findHeight()) # Finding the height of the object
turnLeft(40) # Turning 180 degrees
sleep(3.9)
stop()
front_dis, left_dis, right_dis, length, width = init_measure() # Getting the measurements to wall follow
ob_direction = wall_follow("sensor_left", right_dis, left_dis, 40, False)
if ob_direction == "forward":
stop()
turnLeft(40)
sleep(1.95)
stop()
goForward(30)
sleep(0.5)
stop()
front_dis, left_dis, right_dis, length, width = init_measure()
ob_direction = wall_follow("sensor_right", right_dis, left_dis, 40)
orientation = None
if ob_direction == "end":
turnLeft(40)
sleep(3.85)
stop()
elif ob_direction == "left":
front_dis, left_dis, right_dis, length, width = init_measure()
goForward(40)
sleep(1.2)
stop()
turnLeft(40) # Turn 90 degrees left
sleep(1.95)
stop()
# while tof1Readings() < (left_dis - 100):
# goRight(40)
# sleep(0.01)
orientation = 180
servo_3_rotate(0)
front_dis, left_dis, right_dis, length, width = init_measure()
find_white("sensor_right", right_dis, left_dis, 40)
# align_robot()
sleep(1)
height_list.append(findHeight())
turnLeft(40)
sleep(3.9)
stop()
front_dis, left_dis, right_dis, length, width = init_measure()
ob_direction = wall_follow("sensor_left", right_dis, left_dis, 40, False)
if ob_direction == "forward":
stop()
turnRight(40)
sleep(1.95)
stop()
# servo_3_rotate(-47)
# front_dis, left_dis, right_dis, length, width = init_measure()
# find_white("sensor_left", right_dis, left_dis, 40)
# stop()
servo_3_rotate(-47)
front_dis, left_dis, right_dis, length, width = init_measure()
find_white("sensor_left", right_dis, left_dis, 40)
stop()
# elif ob_direction == "right":
# turnRight(40) # Turn 90 degrees right
# sleep(1.9)
# orientation = 90
servo_3_rotate(-47)
front_dis, left_dis, right_dis, length, width = init_measure()
find_white("sensor_left", right_dis, left_dis, 40)
stop()
stop()
align_robot()
max_height = max(height_list)
if max_height == 10:
return 10
elif max_height == 15:
return 20
elif max_height == 20:
return 30
def findHeight():
video_capture = cv2.VideoCapture(0, cv2.CAP_V4L2)
video_capture.set(4, 480) # Set the height of the frame
video_capture.set(3, 640) # Set the width of the frame
video_capture.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1) # manual mode
video_capture.set(cv2.CAP_PROP_EXPOSURE, 180)
servo_ang = -20
while True:
servo_3_rotate(servo_ang)
ret, frame = video_capture.read()
frame = cv2.flip(frame, 0)
frame = cv2.flip(frame, 1)
width = int(640)
height = int(480)
dimensions = (width, height)
frame = cv2.resize(frame, dimensions, interpolation=cv2.INTER_AREA)
frame = frame[120:350, 0:640]
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Gaussian blur
blur = cv2.GaussianBlur(gray, (5, 5), 0)
# Color thresholding
ret, thresh = cv2.threshold(
blur, 155, 255, cv2.THRESH_BINARY
) # For the white line
# Find the contours of the frame
contours, hierarchy = cv2.findContours(
thresh.copy(), 1, cv2.CHAIN_APPROX_NONE
)
if len(contours) > 0:
c = max(contours, key=cv2.contourArea)
M = cv2.moments(c)
try:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
except:
continue
print(cy)
if cy > 160:
dis = tof1Readings() - 15
dis = dis - 30
print("Distance: ", dis)
print(servo_ang)
# if servo_ang > 0:
# return math.tan(math.radians(servo_ang)) * dis + 130
# else:
# return 130 - math.tan(math.radians(-servo_ang)) * dis
if servo_ang < 20 :
servo_3_rotate(-40)
return 10
elif servo_ang < 40:
servo_3_rotate(-40)
return 15
else:
servo_3_rotate(-40)
return 20
cv2.line(frame, (cx, 0), (cx, 480), (255, 0, 0), 1)
cv2.line(frame, (0, cy), (640, cy), (255, 0, 0), 1)
cv2.drawContours(frame, contours, -1, (0, 255, 0), 1)
servo_ang += 1
sleep(0.05)
# if servo_ang > 40:
# break
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
return None
def go_yard():
base_speed = 37
video_capture = cv2.VideoCapture(0, cv2.CAP_V4L2)
# video_capture = cv2.VideoCapture(0)
video_capture.set(3, 640) # Set the width of the frame
video_capture.set(4, 480) # Set the height of the frame
video_capture.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1) # manual mode
video_capture.set(cv2.CAP_PROP_EXPOSURE, 250)
# print(video_capture.get(cv2.CAP_PROP_EXPOSURE))
count = 0
kp = 0.13
kd = 0.01
prev_error = 0
servo_3_rotate(-47)
gripper_down()
while True:
if sensor_LEFT() == 0 and sensor_RIGHT() == 0:
# print("junction detected")
if count == 0 or count == 1:
goForward(37)
sleep(1)
count += 1
ret, frame = video_capture.read()
frame = cv2.flip(frame, 0)
frame = cv2.flip(frame, 1)
width = int(640)
height = int(480)
dimensions = (width, height)
frame = cv2.resize(frame, dimensions, interpolation=cv2.INTER_AREA)
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Gaussian blur
blur = cv2.GaussianBlur(gray, (5, 5), 0)
# Color thresholding
ret, thresh = cv2.threshold(
blur, 150, 255, cv2.THRESH_BINARY
) # For the white line
# Find the contours of the frame
contours, hierarchy = cv2.findContours(
thresh.copy(), 1, cv2.CHAIN_APPROX_NONE
)
if len(contours) > 0:
c = max(contours, key=cv2.contourArea)
M = cv2.moments(c)
try:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
except:
continue
# PID control
error = 368 - cx
speed = error * kp + (prev_error - error) * kd
prev_error = error
left_speed = base_speed - speed
right_speed = base_speed + speed
if left_speed > 100:
left_speed = 100
elif left_speed < 0:
left_speed = 0
if right_speed > 100:
right_speed = 100
elif right_speed < 0:
right_speed = 0
leftrightMotor_Forward(left_speed, right_speed)
# Drawing the lines
cv2.line(frame, (cx, 0), (cx, 480), (255, 0, 0), 1)
cv2.line(frame, (0, cy), (640, cy), (255, 0, 0), 1)
cv2.drawContours(frame, contours, -1, (0, 255, 0), 1)
cv2.imshow("frame", frame)
cv2.imshow("threshold", thresh)
if cv2.waitKey(10) & 0xFF == ord("q"):
break
else:
if sensor_FRONT() == 1:
stop()
break
yard()