Skip to content

Commit 8a2ac99

Browse files
committed
tests/virtualhub/motor: Make drivebase tests actually run.
These used to be artificially shortened on the virtual hub, so never ran in full. Split it in two parts to get under the test timeout instead.
1 parent fd165dd commit 8a2ac99

File tree

3 files changed

+79
-101
lines changed

3 files changed

+79
-101
lines changed

tests/virtualhub/motor/drivebase_turns.py

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,8 @@
88
motorR = Motor(Port.B)
99
drive_base = DriveBase(motorL, motorR, wheel_diameter=56, axle_track=80)
1010

11-
try:
12-
import ffi
13-
14-
virtual = True
15-
except ImportError:
16-
virtual = False
17-
18-
19-
# Wait on a real robot but not in the simulator.
20-
def delay(ms):
21-
if virtual:
22-
return
23-
wait(ms)
24-
2511

2612
def wait_until_done():
27-
# Stop immediately if virtual.
28-
if virtual:
29-
drive_base.straight(0)
30-
wait(30)
31-
return
32-
3313
# Wait until the drive base command completes normally.
3414
while not drive_base.done():
3515
wait(10)
@@ -77,25 +57,12 @@ def wait_until_done():
7757
)
7858
wait_until_done()
7959

80-
drive_base.turn(1080, wait=False)
60+
drive_base.turn(540, wait=False)
8161
wait(STARTUP_DELAY)
82-
assert motorL.speed() > 0, "For CW in-place turn 1080 degrees, left speed should be +ve"
83-
assert motorR.speed() < 0, (
84-
"For CW in-place turn 1080 degrees, right speed should be -ve"
85-
)
62+
assert motorL.speed() > 0, "For CW in-place turn 540 degrees, left speed should be +ve"
63+
assert motorR.speed() < 0, "For CW in-place turn 540 degrees, right speed should be -ve"
8664
wait_until_done()
8765

88-
drive_base.turn(-1440, wait=False)
89-
wait(STARTUP_DELAY)
90-
assert motorL.speed() < 0, (
91-
"For CCW in-place turn 1440 degrees, left speed should be -ve"
92-
)
93-
assert motorR.speed() > 0, (
94-
"For CCW in-place turn 1440 degrees, right speed should be +ve"
95-
)
96-
wait_until_done()
97-
98-
9966
# Demo 3: Using curve() drives the 'center point' between the wheels a full
10067
# circle (360 degrees or part thereof) around a circle of 12cm radius.
10168
drive_base.curve(120, 360, wait=False) # Drives forward along circle to robot's right
@@ -135,68 +102,3 @@ def wait_until_done():
135102
"When driving Rev in CW Curve, motorL should be greater than motorR"
136103
)
137104
wait_until_done()
138-
139-
# Demo 4: Using drive() to control the robot in various ways.
140-
drive_base.drive(200, 0) # Drive straight Fwd
141-
wait(STARTUP_DELAY_LONG)
142-
assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve"
143-
assert motorR.speed() > 0, (
144-
"When driving Fwd in straight line, right speed should be +ve"
145-
)
146-
delay(500)
147-
drive_base.stop()
148-
delay(250)
149-
150-
drive_base.drive(-200, 0) # Drive straight Rev
151-
wait(STARTUP_DELAY_LONG)
152-
assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve"
153-
assert motorR.speed() < 0, (
154-
"When driving Rev in straight line, right speed should be -ve"
155-
)
156-
delay(500)
157-
drive_base.stop()
158-
delay(250)
159-
160-
drive_base.drive(200, 90) # Drives CW Fwd
161-
wait(STARTUP_DELAY_LONG)
162-
assert motorL.speed() > 0, "When driving CW Curve Fwd, left speed should be +ve"
163-
assert motorR.speed() > 0, "When driving CW Curve Fwd, right speed should be +ve"
164-
assert motorL.speed() > motorR.speed(), (
165-
"When driving Fwd in CW Curve, motorL should be greater than motorR"
166-
)
167-
delay(3000) # 4 seconds x 90 deg/s approx full circle
168-
drive_base.stop()
169-
delay(250)
170-
171-
drive_base.drive(-200, 90) # Drives CW Rev
172-
wait(STARTUP_DELAY_LONG)
173-
assert motorL.speed() < 0, "When driving CCW Curve Rev, left speed should be -ve"
174-
assert motorR.speed() < 0, "When driving CCW Curve Rev, right speed should be -ve"
175-
assert motorR.speed() < motorL.speed(), (
176-
"When driving Rev in CCW Curve, motorR should be less than motorL"
177-
)
178-
delay(3000) # 4 seconds x 90 deg/s = full circle
179-
drive_base.stop()
180-
delay(250)
181-
182-
drive_base.drive(200, -90) # Drives CCW Fwd
183-
wait(STARTUP_DELAY_LONG)
184-
assert motorL.speed() > 0, "When driving CCW Curve Fwd, left speed should be +ve"
185-
assert motorR.speed() > 0, "When driving CCW Curve Fwd, right speed should be +ve"
186-
assert motorL.speed() < motorR.speed(), (
187-
"When driving Fwd in CCW Curve, motorL should be less than motorR"
188-
)
189-
delay(3000) # 4 seconds x 90 deg/s = full circle
190-
drive_base.stop()
191-
delay(250)
192-
193-
drive_base.drive(-200, -90) # In corrected version it should drive CW in Rev
194-
wait(STARTUP_DELAY_LONG)
195-
assert motorL.speed() < 0, "When driving CW Curve Rev, left speed should be +ve"
196-
assert motorR.speed() < 0, "When driving CW Curve Rev, right speed should be +ve"
197-
assert motorL.speed() < motorR.speed(), (
198-
"When driving Rev in CW Curve, motorL should be less than motorR"
199-
)
200-
delay(3000) # 4 seconds x 90 deg/s = full circle
201-
drive_base.stop()
202-
delay(250)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.A, Direction.COUNTERCLOCKWISE)
8+
motorR = Motor(Port.B)
9+
drive_base = DriveBase(motorL, motorR, wheel_diameter=56, axle_track=80)
10+
11+
STARTUP_DELAY = 500
12+
13+
# Demo 4: Using drive() to control the robot in various ways.
14+
drive_base.drive(200, 0) # Drive straight Fwd
15+
wait(STARTUP_DELAY)
16+
assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve"
17+
assert motorR.speed() > 0, (
18+
"When driving Fwd in straight line, right speed should be +ve"
19+
)
20+
wait(500)
21+
drive_base.stop()
22+
wait(250)
23+
24+
drive_base.drive(-200, 0) # Drive straight Rev
25+
wait(STARTUP_DELAY)
26+
assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve"
27+
assert motorR.speed() < 0, (
28+
"When driving Rev in straight line, right speed should be -ve"
29+
)
30+
wait(500)
31+
drive_base.stop()
32+
wait(250)
33+
34+
drive_base.drive(200, 90) # Drives CW Fwd
35+
wait(STARTUP_DELAY)
36+
assert motorL.speed() > 0, "When driving CW Curve Fwd, left speed should be +ve"
37+
assert motorR.speed() > 0, "When driving CW Curve Fwd, right speed should be +ve"
38+
assert motorL.speed() > motorR.speed(), (
39+
"When driving Fwd in CW Curve, motorL should be greater than motorR"
40+
)
41+
wait(3000) # 4 seconds x 90 deg/s approx full circle
42+
drive_base.stop()
43+
wait(250)
44+
45+
drive_base.drive(-200, 90) # Drives CW Rev
46+
wait(STARTUP_DELAY)
47+
assert motorL.speed() < 0, "When driving CCW Curve Rev, left speed should be -ve"
48+
assert motorR.speed() < 0, "When driving CCW Curve Rev, right speed should be -ve"
49+
assert motorR.speed() < motorL.speed(), (
50+
"When driving Rev in CCW Curve, motorR should be less than motorL"
51+
)
52+
wait(3000) # 4 seconds x 90 deg/s = full circle
53+
drive_base.stop()
54+
wait(250)
55+
56+
drive_base.drive(200, -90) # Drives CCW Fwd
57+
wait(STARTUP_DELAY)
58+
assert motorL.speed() > 0, "When driving CCW Curve Fwd, left speed should be +ve"
59+
assert motorR.speed() > 0, "When driving CCW Curve Fwd, right speed should be +ve"
60+
assert motorL.speed() < motorR.speed(), (
61+
"When driving Fwd in CCW Curve, motorL should be less than motorR"
62+
)
63+
wait(3000) # 4 seconds x 90 deg/s = full circle
64+
drive_base.stop()
65+
wait(250)
66+
67+
drive_base.drive(-200, -90) # In corrected version it should drive CW in Rev
68+
wait(STARTUP_DELAY)
69+
assert motorL.speed() < 0, "When driving CW Curve Rev, left speed should be +ve"
70+
assert motorR.speed() < 0, "When driving CW Curve Rev, right speed should be +ve"
71+
assert motorL.speed() < motorR.speed(), (
72+
"When driving Rev in CW Curve, motorL should be less than motorR"
73+
)
74+
wait(3000) # 4 seconds x 90 deg/s = full circle
75+
drive_base.stop()
76+
wait(250)

tests/virtualhub/motor/drivebase_turns2.py.exp

Whitespace-only changes.

0 commit comments

Comments
 (0)