-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchange_line_plot2.py
More file actions
150 lines (119 loc) · 3.55 KB
/
change_line_plot2.py
File metadata and controls
150 lines (119 loc) · 3.55 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
import time
import cv2 as cv
import pypot.dynamixel
from robot import *
from image_processing import processing, init_values
# 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
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)
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
BLUE_SPEED = 500
RED_SPEED = 400
BLUE_TIME = 34
RED_TIME_1 = 8
RED_TIME_2 = 45
RED_TIME_3 = 46
robot = Robot()
coordinates_blue = ([],[])
coordinates_red = ([],[])
try:
initial_time = time.time()
init_values("blue")
speed = (-BLUE_SPEED, BLUE_SPEED)
while True:
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?).")
continue
vari, _, _ = processing(frame)
if vari is None:
print("No Line")
else:
if vari == 0:
speed = (
- BLUE_SPEED,
BLUE_SPEED
)
else:
speed = (
- BLUE_SPEED + 0.5 * vari + 0.001 * (vari / abs(vari)) * vari ** 2,
BLUE_SPEED + 0.5 * vari + 0.001 * (vari / abs(vari)) * vari ** 2
)
dxl_io.set_moving_speed({
1: speed[0],
2: speed[1]
})
robot.odom(direct_kinematics(speed[0], speed[1]))
coordinates_blue[0].append(robot.x)
coordinates_blue[1].append(robot.y)
if time.time() - initial_time > BLUE_TIME:
break
initial_time = time.time()
init_values("red")
speed = (-RED_SPEED, RED_SPEED)
while True:
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?).")
continue
vari, _, _ = processing(frame)
if RED_TIME_1 < time.time() - initial_time < RED_TIME_1 + 2:
speed = (
- RED_SPEED,
RED_SPEED - 250
)
elif RED_TIME_2 < time.time() - initial_time < RED_TIME_2 + 1:
speed = (
- RED_SPEED,
RED_SPEED
)
elif RED_TIME_3 < time.time() - initial_time < RED_TIME_3 + 3:
speed = (
- RED_SPEED + 200,
RED_SPEED
)
else:
if vari == 0:
speed = (
- RED_SPEED,
RED_SPEED
)
else:
speed = (
- RED_SPEED + 0.6 * vari + 0.0005 * (vari / abs(vari)) * vari ** 2,
RED_SPEED + 0.6 * vari + 0.0005 * (vari / abs(vari)) * vari ** 2
)
dxl_io.set_moving_speed({
1: speed[0],
2: speed[1]
})
robot.odom(direct_kinematics(speed[0], speed[1]))
coordinates_red[0].append(robot.x)
coordinates_red[1].append(robot.y)
plt.plot(coordinates_blue[0],coordinates_blue[1],'b')
plt.plot(coordinates_red[0],coordinates_red[1],'r')
plt.savefig('trace_line.png')
except KeyboardInterrupt:
speed = {1: 0, 2: 0}
dxl_io.set_moving_speed(speed)
cap.release()
exit()