-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollow_line.py
More file actions
89 lines (64 loc) · 1.66 KB
/
follow_line.py
File metadata and controls
89 lines (64 loc) · 1.66 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
import cv2 as cv
from image_processing import processing, init_color
import pypot.dynamixel
import sys
import numpy as np
# def mean(list_speed):
# sum_1 = 0
# sum_2 = 0
# l = len(list_speed)
# for i in range(l):
# sum_1 += list_speed[i][0]
# sum_2 += list_speed[i][1]
# return sum_1/l, sum_2/l
line_color = "blue"
nb_args = len(sys.argv)
if nb_args > 1:
line_color = sys.argv[1]
init_color(line_color)
ports = pypot.dynamixel.get_available_ports()
if not ports:
exit('No port')
port = ports[0]
print('Using the first on the list', port)
dxl_io = pypot.dynamixel.DxlIO(port)
print('Connected!')
found_ids = dxl_io.scan([1,2])
print('Found ids:', found_ids)
ids = found_ids[:2]
dxl_io.enable_torque(ids)
default_speed = 200
speed = (-default_speed, default_speed)
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
try:
while True:
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?).")
continue
x, y = processing(frame)
if x == None:
s = 20
mean_speed = {
1: -default_speed + s,
2: default_speed + s
}
dxl_io.set_moving_speed(mean_speed)
else:
speed = (
-default_speed + 0.4 * x,
default_speed + 0.4 * x
)
print(speed)
dxl_io.set_moving_speed({
1: speed[0],
2: speed[1]
})
except KeyboardInterrupt:
speed = {1:0, 2:0}
dxl_io.set_moving_speed(speed)
cap.release()
exit()