Skip to content

Commit 9b02e79

Browse files
committed
tests/virtualhub/motor: Update drive base tests.
Make them work for current version of the drive base. Remove prints since values are nondeterministic. Use same ports as virtual hub.
1 parent 0946b1f commit 9b02e79

File tree

2 files changed

+30
-121
lines changed

2 files changed

+30
-121
lines changed

tests/virtualhub/motor/drivebase_turns.py

Lines changed: 30 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,13 @@
44
from pybricks.tools import wait
55

66
# Initialize the drive base.
7-
motorL = Motor(Port.C, Direction.COUNTERCLOCKWISE)
8-
motorR = Motor(Port.D)
7+
motorL = Motor(Port.A, Direction.COUNTERCLOCKWISE)
8+
motorR = Motor(Port.B)
99
drive_base = DriveBase(motorL, motorR, wheel_diameter=56, axle_track=80)
1010

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)
11+
# Demo 1: Using straight() to move Fwd & Rev in mm
1612
drive_base.straight(200, wait=False)
1713
wait(500)
18-
print(
19-
"Test 1 - Drive Fwd - motorL.speed() = {}, motorR.speed() = {}".format(
20-
motorL.speed(), motorR.speed()
21-
)
22-
)
2314
assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve"
2415
assert motorR.speed() > 0, "When driving Fwd in straight line, right speed should be +ve"
2516
while not drive_base.done():
@@ -28,94 +19,58 @@
2819

2920
drive_base.straight(-200, wait=False)
3021
wait(500)
31-
print(
32-
"Test 2 -Drive Rev - motorL.speed() = {}, motorR.speed() = {}".format(
33-
motorL.speed(), motorR.speed()
34-
)
35-
)
3622
assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve"
3723
assert motorR.speed() < 0, "When driving Rev in straight line, right speed should be -ve"
3824
while not drive_base.done():
3925
wait(10)
4026
wait(250)
4127

42-
# Demo 2 - Using .turn() to 'Point Turn' by angle (+ve = CW, -ve = CCW)
43-
# .turn(angle, then=Stop.HOLD, wait=True)
28+
# Demo 2: Using turn() for in-place turns by angle (+ve = CW, -ve = CCW)
4429
drive_base.turn(90, wait=False)
4530
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"
31+
assert motorL.speed() > 0, "For CW in-place turn 90 degrees, left speed should be +ve"
32+
assert motorR.speed() < 0, "For CW in-place turn 90 degrees, right speed should be -ve"
5333
while not drive_base.done():
5434
wait(10)
5535
wait(500)
5636

5737
drive_base.turn(180, wait=False)
5838
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"
39+
assert motorL.speed() > 0, "For CW in-place turn 180 degrees, left speed should be +ve"
40+
assert motorR.speed() < 0, "For CW in-place turn 180 degrees, right speed should be -ve"
6641
while not drive_base.done():
6742
wait(10)
6843
wait(500)
6944

7045
drive_base.turn(-270, wait=False)
7146
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"
47+
assert motorL.speed() < 0, "For CCW in-place turn 270 degrees, left speed should be -ve"
48+
assert motorR.speed() > 0, "For CCW in-place turn 270 degrees, right speed should be +ve"
7949
while not drive_base.done():
8050
wait(10)
8151
wait(500)
8252

8353
drive_base.turn(1080, wait=False)
8454
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"
55+
assert motorL.speed() > 0, "For CW in-place turn 1080 degrees, left speed should be +ve"
56+
assert motorR.speed() < 0, "For CW in-place turn 1080 degrees, right speed should be -ve"
9257
while not drive_base.done():
9358
wait(10)
9459
wait(500)
9560

9661
drive_base.turn(-1440, wait=False)
9762
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"
63+
assert motorL.speed() < 0, "For CCW in-place turn 1440 degrees, left speed should be -ve"
64+
assert motorR.speed() > 0, "For CCW in-place turn 1440 degrees, right speed should be +ve"
10565
while not drive_base.done():
10666
wait(10)
10767
wait(500)
10868

10969

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
70+
# Demo 3: Using curve() drives the 'center point' between the wheels a full
71+
# circle (360 degrees or part thereof) around a circle of 12cm radius.
72+
drive_base.curve(120, 360, wait=False) # Drives forward along circle to robot's right
11373
wait(500)
114-
print(
115-
"Test 8 - CW Curve Fwd (radius 12cm) - motorL.speed() = {}, motorR.speed() = {}".format(
116-
motorL.speed(), motorR.speed()
117-
)
118-
)
11974
assert motorL.speed() > 0, "When driving Fwd in CW Curve, left speed should be +ve"
12075
assert motorR.speed() > 0, "When driving Fwd in CW Curve, right speed should be +ve"
12176
assert (
@@ -125,29 +80,19 @@
12580
wait(10)
12681
wait(500)
12782

128-
drive_base.curve(-140, 360, wait=False) # Drives CounterClockwise in Rev
83+
drive_base.curve(-140, 360, wait=False) # Drives backward along circle to robot's right
12984
wait(500)
130-
print(
131-
"Test 9 - CCW Curve Rev (radius 12cm) - motorL.speed() = {}, motorR.speed() = {}".format(
132-
motorL.speed(), motorR.speed()
133-
)
134-
)
13585
assert motorL.speed() < 0, "When driving Rev in CCW Curve, left speed should be -ve"
13686
assert motorR.speed() < 0, "When driving Rev in CCW Curve, right speed should be -ve"
13787
assert (
13888
motorL.speed() < motorR.speed()
139-
), "When driving Rev in CCW Curve, motorL should be less than motorR"
89+
), "When driving Rev in CCW Curve, motorL should be less (i.e. faster) than motorR"
14090
while not drive_base.done():
14191
wait(10)
14292
wait(500)
14393

144-
drive_base.curve(120, -360, wait=False) # Drives Counterclockwise Fwd
94+
drive_base.curve(120, -360, wait=False) # Drives forward along circle to robot's left
14595
wait(500)
146-
print(
147-
"Test 10 - CCW Curve Fwd (radius 12cm) - motorL.speed() = {}, motorR.speed() = {}".format(
148-
motorL.speed(), motorR.speed()
149-
)
150-
)
15196
assert motorL.speed() > 0, "When driving Fwd in CCW Curve, left speed should be +ve"
15297
assert motorR.speed() > 0, "When driving Fwd in CCW Curve, right speed should be +ve"
15398
assert (
@@ -157,13 +102,8 @@
157102
wait(10)
158103
wait(500)
159104

160-
drive_base.curve(-120, -360, wait=False) # Drives Clockwise in Rev
105+
drive_base.curve(-120, -360, wait=False) # Drives in reverse along circle to robot's left
161106
wait(500)
162-
print(
163-
"Test 11 - CW Curve Rev (radius 12cm) - motorL.speed() = {}, motorR.speed() = {}".format(
164-
motorL.speed(), motorR.speed()
165-
)
166-
)
167107
assert motorL.speed() < 0, "When driving Rev in CW Curve, left speed should be -ve"
168108
assert motorR.speed() < 0, "When driving Rev in CW Curve, right speed should be -ve"
169109
assert (
@@ -173,28 +113,17 @@
173113
wait(10)
174114
wait(500)
175115

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
116+
# Demo 4: Using drive() to control the robot in various ways.
117+
drive_base.drive(200, 0) # Drive straight Fwd
179118
wait(1000)
180-
print(
181-
"Test 12 - .drive() - Drive Fwd - motorL.speed() = {}, motorR.speed() = {}".format(
182-
motorL.speed(), motorR.speed()
183-
)
184-
)
185119
assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve"
186120
assert motorR.speed() > 0, "When driving Fwd in straight line, right speed should be +ve"
187121
wait(500)
188122
drive_base.stop()
189123
wait(250)
190124

191-
drive_base.drive(-200, 0) # Drive Rev
125+
drive_base.drive(-200, 0) # Drive straight Rev
192126
wait(1000)
193-
print(
194-
"Test 13 - .drive() - Drive Rev - motorL.speed() = {}, motorR.speed() = {}".format(
195-
motorL.speed(), motorR.speed()
196-
)
197-
)
198127
assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve"
199128
assert motorR.speed() < 0, "When driving Rev in straight line, right speed should be -ve"
200129
wait(500)
@@ -203,43 +132,28 @@
203132

204133
drive_base.drive(200, 90) # Drives CW Fwd
205134
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-
)
211135
assert motorL.speed() > 0, "When driving CW Curve Fwd, left speed should be +ve"
212136
assert motorR.speed() > 0, "When driving CW Curve Fwd, right speed should be +ve"
213137
assert (
214138
motorL.speed() > motorR.speed()
215139
), "When driving Fwd in CW Curve, motorL should be greater than motorR"
216-
wait(3000) # 4 seconds x 90 deg/s = full circle
140+
wait(3000) # 4 seconds x 90 deg/s approx full circle
217141
drive_base.stop()
218142
wait(250)
219143

220-
drive_base.drive(-200, 90) # In corrected version it should Drive CCW in Rev
144+
drive_base.drive(-200, 90) # Drives CW Rev
221145
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-
)
227146
assert motorL.speed() < 0, "When driving CCW Curve Rev, left speed should be -ve"
228147
assert motorR.speed() < 0, "When driving CCW Curve Rev, right speed should be -ve"
229148
assert (
230-
motorL.speed() < motorR.speed()
231-
), "When driving Rev in CCW Curve, motorL should be less than motorR"
149+
motorR.speed() < motorL.speed()
150+
), "When driving Rev in CCW Curve, motorR should be less than motorL"
232151
wait(3000) # 4 seconds x 90 deg/s = full circle
233152
drive_base.stop()
234153
wait(250)
235154

236-
drive_base.drive(200, -90) # In corrected version it should Drive CCW Fwd
155+
drive_base.drive(200, -90) # Drives CCW Fwd
237156
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-
)
243157
assert motorL.speed() > 0, "When driving CCW Curve Fwd, left speed should be +ve"
244158
assert motorR.speed() > 0, "When driving CCW Curve Fwd, right speed should be +ve"
245159
assert (
@@ -251,15 +165,10 @@
251165

252166
drive_base.drive(-200, -90) # In corrected version it should drive CW in Rev
253167
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-
)
259168
assert motorL.speed() < 0, "When driving CW Curve Rev, left speed should be +ve"
260169
assert motorR.speed() < 0, "When driving CW Curve Rev, right speed should be +ve"
261170
assert (
262-
motorL.speed() > motorR.speed()
171+
motorL.speed() < motorR.speed()
263172
), "When driving Rev in CW Curve, motorL should be less than motorR"
264173
wait(3000) # 4 seconds x 90 deg/s = full circle
265174
drive_base.stop()

tests/virtualhub/motor/drivebase_turns.py.exp

Whitespace-only changes.

0 commit comments

Comments
 (0)