-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample_1.py
More file actions
57 lines (40 loc) · 1.24 KB
/
Copy pathexample_1.py
File metadata and controls
57 lines (40 loc) · 1.24 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
import sys
import numpy as np
import cv2
sys.path.append('../src')
from robot_simulation import Robot
def main():
# create the instance of the
# robor
# windows
robot = Robot('../maps/map_1.png')
# linux
robot = Robot('../maps/map_1.png', top_view_enable=False)
while True:
# get the image
img = robot.get_camera_view()
# convert it to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# apply a threshold
ret, gray = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# count the black pixels to the right and to the left
black_pixels_dx = np.count_nonzero(255-gray[:, 32:])
black_pixels_sx = np.count_nonzero(255 - gray[:, :32])
delta = black_pixels_dx-black_pixels_sx
print(delta)
v_dx = 100 - delta/10,
v_sx = 100 + delta/10
v_dx = np.clip(v_dx, -255, 255)
v_sx = np.clip(v_sx, -255, 255)
robot.set_motors_speeds(
v_dx,
v_sx
)
cv2.imshow("view", cv2.resize(gray, (500, 500)))
cv2.waitKey(1)
# only in linux
#robot.update_top_view()
# delete the robot, to stop the treads
robot.__del__()
if __name__ == '__main__':
main()