Skip to content

Commit 0946b1f

Browse files
JDwyer009laurensvalk
authored andcommitted
tests/virtualhub/motor: Add drivebase turn tests.
1 parent 35ab6fa commit 0946b1f

File tree

1 file changed

+266
-0
lines changed

1 file changed

+266
-0
lines changed
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
from pybricks.pupdevices import Motor
2+
from pybricks.parameters import Direction, Port
3+
from pybricks.robotics import DriveBase
4+
from pybricks.tools import wait
5+
6+
# Initialize the drive base.
7+
motorL = Motor(Port.C, Direction.COUNTERCLOCKWISE)
8+
motorR = Motor(Port.D)
9+
drive_base = DriveBase(motorL, motorR, wheel_diameter=56, axle_track=80)
10+
11+
# User's can override the default speeds with .setting() (Defaults are 40% of it's max speed)
12+
# .settings(straight_speed, straight_acceleration, turn_rate, turn_acceleration)
13+
14+
# Demo 1 - Using .straight() to move Fwd & Rev in mm
15+
# .straight(distance, then=Stop.HOLD, wait=True)
16+
drive_base.straight(200, wait=False)
17+
wait(500)
18+
print(
19+
"Test 1 - Drive Fwd - motorL.speed() = {}, motorR.speed() = {}".format(
20+
motorL.speed(), motorR.speed()
21+
)
22+
)
23+
assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve"
24+
assert motorR.speed() > 0, "When driving Fwd in straight line, right speed should be +ve"
25+
while not drive_base.done():
26+
wait(10)
27+
wait(250)
28+
29+
drive_base.straight(-200, wait=False)
30+
wait(500)
31+
print(
32+
"Test 2 -Drive Rev - motorL.speed() = {}, motorR.speed() = {}".format(
33+
motorL.speed(), motorR.speed()
34+
)
35+
)
36+
assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve"
37+
assert motorR.speed() < 0, "When driving Rev in straight line, right speed should be -ve"
38+
while not drive_base.done():
39+
wait(10)
40+
wait(250)
41+
42+
# Demo 2 - Using .turn() to 'Point Turn' by angle (+ve = CW, -ve = CCW)
43+
# .turn(angle, then=Stop.HOLD, wait=True)
44+
drive_base.turn(90, wait=False)
45+
wait(500)
46+
print(
47+
"Test 3 - CW Point Turn 90 degrees - motorL.speed() = {}, motorR.speed() = {}".format(
48+
motorL.speed(), motorR.speed()
49+
)
50+
)
51+
assert motorL.speed() > 0, "For CW Point Turn 90 Degrees, left speed should be +ve"
52+
assert motorR.speed() < 0, "For CW Point Turn 90 Degrees, right speed should be -ve"
53+
while not drive_base.done():
54+
wait(10)
55+
wait(500)
56+
57+
drive_base.turn(180, wait=False)
58+
wait(500)
59+
print(
60+
"Test 4 - CW Point Turn 180 degrees - motorL.speed() = {}, motorR.speed() = {}".format(
61+
motorL.speed(), motorR.speed()
62+
)
63+
)
64+
assert motorL.speed() > 0, "For CW Point Turn 180 Degrees, left speed should be +ve"
65+
assert motorR.speed() < 0, "For CW Point Turn 180 Degrees, right speed should be -ve"
66+
while not drive_base.done():
67+
wait(10)
68+
wait(500)
69+
70+
drive_base.turn(-270, wait=False)
71+
wait(500)
72+
print(
73+
"Test 5 - CCW Point Turn 270 degrees - motorL.speed() = {}, motorR.speed() = {}".format(
74+
motorL.speed(), motorR.speed()
75+
)
76+
)
77+
assert motorL.speed() < 0, "For CCW Point Turn 270 Degrees, left speed should be -ve"
78+
assert motorR.speed() > 0, "For CCW Point Turn 270 Degrees, right speed should be +ve"
79+
while not drive_base.done():
80+
wait(10)
81+
wait(500)
82+
83+
drive_base.turn(1080, wait=False)
84+
wait(500)
85+
print(
86+
"Test 6 - CW Point Turn 1080 degrees - motorL.speed() = {}, motorR.speed() = {}".format(
87+
motorL.speed(), motorR.speed()
88+
)
89+
)
90+
assert motorL.speed() > 0, "For CW Point Turn 1080 Degrees, left speed should be +ve"
91+
assert motorR.speed() < 0, "For CW Point Turn 1080 Degrees, right speed should be -ve"
92+
while not drive_base.done():
93+
wait(10)
94+
wait(500)
95+
96+
drive_base.turn(-1440, wait=False)
97+
wait(500)
98+
print(
99+
"Test 7 - CCW Point Turn 1440 degrees - motorL.speed() = {}, motorR.speed() = {}".format(
100+
motorL.speed(), motorR.speed()
101+
)
102+
)
103+
assert motorL.speed() < 0, "For CCW Point Turn 270 Degrees, left speed should be -ve"
104+
assert motorR.speed() > 0, "For CCW Point Turn 270 Degrees, right speed should be +ve"
105+
while not drive_base.done():
106+
wait(10)
107+
wait(500)
108+
109+
110+
# Demo 3 - Using .curve() drives the 'center point' between the wheels a full circle (360 degrees or part thereof) around a circle of 12cm radius
111+
# .curve(radius, angle, then=Stop.HOLD, wait=True)
112+
drive_base.curve(120, 360, wait=False) # Drives Clockwise Fwd
113+
wait(500)
114+
print(
115+
"Test 8 - CW Curve Fwd (radius 12cm) - motorL.speed() = {}, motorR.speed() = {}".format(
116+
motorL.speed(), motorR.speed()
117+
)
118+
)
119+
assert motorL.speed() > 0, "When driving Fwd in CW Curve, left speed should be +ve"
120+
assert motorR.speed() > 0, "When driving Fwd in CW Curve, right speed should be +ve"
121+
assert (
122+
motorL.speed() > motorR.speed()
123+
), "When driving Fwd in CW Curve, motorL should be greater than motorR"
124+
while not drive_base.done():
125+
wait(10)
126+
wait(500)
127+
128+
drive_base.curve(-140, 360, wait=False) # Drives CounterClockwise in Rev
129+
wait(500)
130+
print(
131+
"Test 9 - CCW Curve Rev (radius 12cm) - motorL.speed() = {}, motorR.speed() = {}".format(
132+
motorL.speed(), motorR.speed()
133+
)
134+
)
135+
assert motorL.speed() < 0, "When driving Rev in CCW Curve, left speed should be -ve"
136+
assert motorR.speed() < 0, "When driving Rev in CCW Curve, right speed should be -ve"
137+
assert (
138+
motorL.speed() < motorR.speed()
139+
), "When driving Rev in CCW Curve, motorL should be less than motorR"
140+
while not drive_base.done():
141+
wait(10)
142+
wait(500)
143+
144+
drive_base.curve(120, -360, wait=False) # Drives Counterclockwise Fwd
145+
wait(500)
146+
print(
147+
"Test 10 - CCW Curve Fwd (radius 12cm) - motorL.speed() = {}, motorR.speed() = {}".format(
148+
motorL.speed(), motorR.speed()
149+
)
150+
)
151+
assert motorL.speed() > 0, "When driving Fwd in CCW Curve, left speed should be +ve"
152+
assert motorR.speed() > 0, "When driving Fwd in CCW Curve, right speed should be +ve"
153+
assert (
154+
motorL.speed() < motorR.speed()
155+
), "When driving Fwd in CCW Curve, motorL should be less than motorR"
156+
while not drive_base.done():
157+
wait(10)
158+
wait(500)
159+
160+
drive_base.curve(-120, -360, wait=False) # Drives Clockwise in Rev
161+
wait(500)
162+
print(
163+
"Test 11 - CW Curve Rev (radius 12cm) - motorL.speed() = {}, motorR.speed() = {}".format(
164+
motorL.speed(), motorR.speed()
165+
)
166+
)
167+
assert motorL.speed() < 0, "When driving Rev in CW Curve, left speed should be -ve"
168+
assert motorR.speed() < 0, "When driving Rev in CW Curve, right speed should be -ve"
169+
assert (
170+
motorL.speed() > motorR.speed()
171+
), "When driving Rev in CW Curve, motorL should be greater than motorR"
172+
while not drive_base.done():
173+
wait(10)
174+
wait(500)
175+
176+
# Demo 4 - Using .drive() to control the robot in various ways
177+
# .drive(speed, turn_rate)
178+
drive_base.drive(200, 0) # Drive Fwd
179+
wait(1000)
180+
print(
181+
"Test 12 - .drive() - Drive Fwd - motorL.speed() = {}, motorR.speed() = {}".format(
182+
motorL.speed(), motorR.speed()
183+
)
184+
)
185+
assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve"
186+
assert motorR.speed() > 0, "When driving Fwd in straight line, right speed should be +ve"
187+
wait(500)
188+
drive_base.stop()
189+
wait(250)
190+
191+
drive_base.drive(-200, 0) # Drive Rev
192+
wait(1000)
193+
print(
194+
"Test 13 - .drive() - Drive Rev - motorL.speed() = {}, motorR.speed() = {}".format(
195+
motorL.speed(), motorR.speed()
196+
)
197+
)
198+
assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve"
199+
assert motorR.speed() < 0, "When driving Rev in straight line, right speed should be -ve"
200+
wait(500)
201+
drive_base.stop()
202+
wait(250)
203+
204+
drive_base.drive(200, 90) # Drives CW Fwd
205+
wait(1000)
206+
print(
207+
"Test 14 - .drive() - CW Curve Fwd (turning 90/sec) - motorL.speed() = {}, motorR.speed() = {}".format(
208+
motorL.speed(), motorR.speed()
209+
)
210+
)
211+
assert motorL.speed() > 0, "When driving CW Curve Fwd, left speed should be +ve"
212+
assert motorR.speed() > 0, "When driving CW Curve Fwd, right speed should be +ve"
213+
assert (
214+
motorL.speed() > motorR.speed()
215+
), "When driving Fwd in CW Curve, motorL should be greater than motorR"
216+
wait(3000) # 4 seconds x 90 deg/s = full circle
217+
drive_base.stop()
218+
wait(250)
219+
220+
drive_base.drive(-200, 90) # In corrected version it should Drive CCW in Rev
221+
wait(1000)
222+
print(
223+
"Test 15 - .drive() - CCW Curve Rev (turning 90/sec) - motorL.speed() = {}, motorR.speed() = {}".format(
224+
motorL.speed(), motorR.speed()
225+
)
226+
)
227+
assert motorL.speed() < 0, "When driving CCW Curve Rev, left speed should be -ve"
228+
assert motorR.speed() < 0, "When driving CCW Curve Rev, right speed should be -ve"
229+
assert (
230+
motorL.speed() < motorR.speed()
231+
), "When driving Rev in CCW Curve, motorL should be less than motorR"
232+
wait(3000) # 4 seconds x 90 deg/s = full circle
233+
drive_base.stop()
234+
wait(250)
235+
236+
drive_base.drive(200, -90) # In corrected version it should Drive CCW Fwd
237+
wait(1000)
238+
print(
239+
"Test 16 - .drive() - CCW Curve Fwd (turning 90/sec) - motorL.speed() = {}, motorR.speed() = {}".format(
240+
motorL.speed(), motorR.speed()
241+
)
242+
)
243+
assert motorL.speed() > 0, "When driving CCW Curve Fwd, left speed should be +ve"
244+
assert motorR.speed() > 0, "When driving CCW Curve Fwd, right speed should be +ve"
245+
assert (
246+
motorL.speed() < motorR.speed()
247+
), "When driving Fwd in CCW Curve, motorL should be less than motorR"
248+
wait(3000) # 4 seconds x 90 deg/s = full circle
249+
drive_base.stop()
250+
wait(250)
251+
252+
drive_base.drive(-200, -90) # In corrected version it should drive CW in Rev
253+
wait(1000)
254+
print(
255+
"Test 17 - .drive() - CW Curve Rev (turning 90/sec) - motorL.speed() = {}, motorR.speed() = {}".format(
256+
motorL.speed(), motorR.speed()
257+
)
258+
)
259+
assert motorL.speed() < 0, "When driving CW Curve Rev, left speed should be +ve"
260+
assert motorR.speed() < 0, "When driving CW Curve Rev, right speed should be +ve"
261+
assert (
262+
motorL.speed() > motorR.speed()
263+
), "When driving Rev in CW Curve, motorL should be less than motorR"
264+
wait(3000) # 4 seconds x 90 deg/s = full circle
265+
drive_base.stop()
266+
wait(250)

0 commit comments

Comments
 (0)