Skip to content

Commit cf36ff1

Browse files
committed
tests/virtualhub/motor: Make drive base test CI ready.
MicroPython tests can run for only 10 seconds.
1 parent 9b02e79 commit cf36ff1

File tree

1 file changed

+70
-62
lines changed

1 file changed

+70
-62
lines changed

tests/virtualhub/motor/drivebase_turns.py

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,168 +8,176 @@
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+
# Wait on a real robot but not in the simulator.
19+
def delay(ms):
20+
if virtual:
21+
return
22+
wait(ms)
23+
24+
25+
def wait_until_done():
26+
# Stop immediately if virtual.
27+
if virtual:
28+
drive_base.straight(0)
29+
wait(30)
30+
return
31+
32+
# Wait until the drive base command completes normally.
33+
while not drive_base.done():
34+
wait(10)
35+
wait(250)
36+
37+
38+
STARTUP_DELAY = 500
39+
STARTUP_DELAY_LONG = 500
40+
1141
# Demo 1: Using straight() to move Fwd & Rev in mm
1242
drive_base.straight(200, wait=False)
13-
wait(500)
43+
wait(STARTUP_DELAY)
1444
assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve"
1545
assert motorR.speed() > 0, "When driving Fwd in straight line, right speed should be +ve"
16-
while not drive_base.done():
17-
wait(10)
18-
wait(250)
46+
wait_until_done()
1947

2048
drive_base.straight(-200, wait=False)
21-
wait(500)
49+
wait(STARTUP_DELAY)
2250
assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve"
2351
assert motorR.speed() < 0, "When driving Rev in straight line, right speed should be -ve"
24-
while not drive_base.done():
25-
wait(10)
26-
wait(250)
52+
wait_until_done()
2753

2854
# Demo 2: Using turn() for in-place turns by angle (+ve = CW, -ve = CCW)
2955
drive_base.turn(90, wait=False)
30-
wait(500)
56+
wait(STARTUP_DELAY)
3157
assert motorL.speed() > 0, "For CW in-place turn 90 degrees, left speed should be +ve"
3258
assert motorR.speed() < 0, "For CW in-place turn 90 degrees, right speed should be -ve"
33-
while not drive_base.done():
34-
wait(10)
35-
wait(500)
59+
wait_until_done()
3660

3761
drive_base.turn(180, wait=False)
38-
wait(500)
62+
wait(STARTUP_DELAY)
3963
assert motorL.speed() > 0, "For CW in-place turn 180 degrees, left speed should be +ve"
4064
assert motorR.speed() < 0, "For CW in-place turn 180 degrees, right speed should be -ve"
41-
while not drive_base.done():
42-
wait(10)
43-
wait(500)
65+
wait_until_done()
4466

4567
drive_base.turn(-270, wait=False)
46-
wait(500)
68+
wait(STARTUP_DELAY)
4769
assert motorL.speed() < 0, "For CCW in-place turn 270 degrees, left speed should be -ve"
4870
assert motorR.speed() > 0, "For CCW in-place turn 270 degrees, right speed should be +ve"
49-
while not drive_base.done():
50-
wait(10)
51-
wait(500)
71+
wait_until_done()
5272

5373
drive_base.turn(1080, wait=False)
54-
wait(500)
74+
wait(STARTUP_DELAY)
5575
assert motorL.speed() > 0, "For CW in-place turn 1080 degrees, left speed should be +ve"
5676
assert motorR.speed() < 0, "For CW in-place turn 1080 degrees, right speed should be -ve"
57-
while not drive_base.done():
58-
wait(10)
59-
wait(500)
77+
wait_until_done()
6078

6179
drive_base.turn(-1440, wait=False)
62-
wait(500)
80+
wait(STARTUP_DELAY)
6381
assert motorL.speed() < 0, "For CCW in-place turn 1440 degrees, left speed should be -ve"
6482
assert motorR.speed() > 0, "For CCW in-place turn 1440 degrees, right speed should be +ve"
65-
while not drive_base.done():
66-
wait(10)
67-
wait(500)
83+
wait_until_done()
6884

6985

7086
# Demo 3: Using curve() drives the 'center point' between the wheels a full
7187
# circle (360 degrees or part thereof) around a circle of 12cm radius.
7288
drive_base.curve(120, 360, wait=False) # Drives forward along circle to robot's right
73-
wait(500)
89+
wait(STARTUP_DELAY)
7490
assert motorL.speed() > 0, "When driving Fwd in CW Curve, left speed should be +ve"
7591
assert motorR.speed() > 0, "When driving Fwd in CW Curve, right speed should be +ve"
7692
assert (
7793
motorL.speed() > motorR.speed()
7894
), "When driving Fwd in CW Curve, motorL should be greater than motorR"
79-
while not drive_base.done():
80-
wait(10)
81-
wait(500)
95+
wait_until_done()
8296

8397
drive_base.curve(-140, 360, wait=False) # Drives backward along circle to robot's right
84-
wait(500)
98+
wait(STARTUP_DELAY)
8599
assert motorL.speed() < 0, "When driving Rev in CCW Curve, left speed should be -ve"
86100
assert motorR.speed() < 0, "When driving Rev in CCW Curve, right speed should be -ve"
87101
assert (
88102
motorL.speed() < motorR.speed()
89103
), "When driving Rev in CCW Curve, motorL should be less (i.e. faster) than motorR"
90-
while not drive_base.done():
91-
wait(10)
92-
wait(500)
104+
wait_until_done()
93105

94106
drive_base.curve(120, -360, wait=False) # Drives forward along circle to robot's left
95-
wait(500)
107+
wait(STARTUP_DELAY)
96108
assert motorL.speed() > 0, "When driving Fwd in CCW Curve, left speed should be +ve"
97109
assert motorR.speed() > 0, "When driving Fwd in CCW Curve, right speed should be +ve"
98110
assert (
99111
motorL.speed() < motorR.speed()
100112
), "When driving Fwd in CCW Curve, motorL should be less than motorR"
101-
while not drive_base.done():
102-
wait(10)
103-
wait(500)
113+
wait_until_done()
104114

105115
drive_base.curve(-120, -360, wait=False) # Drives in reverse along circle to robot's left
106-
wait(500)
116+
wait(STARTUP_DELAY)
107117
assert motorL.speed() < 0, "When driving Rev in CW Curve, left speed should be -ve"
108118
assert motorR.speed() < 0, "When driving Rev in CW Curve, right speed should be -ve"
109119
assert (
110120
motorL.speed() > motorR.speed()
111121
), "When driving Rev in CW Curve, motorL should be greater than motorR"
112-
while not drive_base.done():
113-
wait(10)
114-
wait(500)
122+
wait_until_done()
115123

116124
# Demo 4: Using drive() to control the robot in various ways.
117125
drive_base.drive(200, 0) # Drive straight Fwd
118-
wait(1000)
126+
wait(STARTUP_DELAY_LONG)
119127
assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve"
120128
assert motorR.speed() > 0, "When driving Fwd in straight line, right speed should be +ve"
121-
wait(500)
129+
delay(500)
122130
drive_base.stop()
123-
wait(250)
131+
delay(250)
124132

125133
drive_base.drive(-200, 0) # Drive straight Rev
126-
wait(1000)
134+
wait(STARTUP_DELAY_LONG)
127135
assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve"
128136
assert motorR.speed() < 0, "When driving Rev in straight line, right speed should be -ve"
129-
wait(500)
137+
delay(500)
130138
drive_base.stop()
131-
wait(250)
139+
delay(250)
132140

133141
drive_base.drive(200, 90) # Drives CW Fwd
134-
wait(1000)
142+
wait(STARTUP_DELAY_LONG)
135143
assert motorL.speed() > 0, "When driving CW Curve Fwd, left speed should be +ve"
136144
assert motorR.speed() > 0, "When driving CW Curve Fwd, right speed should be +ve"
137145
assert (
138146
motorL.speed() > motorR.speed()
139147
), "When driving Fwd in CW Curve, motorL should be greater than motorR"
140-
wait(3000) # 4 seconds x 90 deg/s approx full circle
148+
delay(3000) # 4 seconds x 90 deg/s approx full circle
141149
drive_base.stop()
142-
wait(250)
150+
delay(250)
143151

144152
drive_base.drive(-200, 90) # Drives CW Rev
145-
wait(1000)
153+
wait(STARTUP_DELAY_LONG)
146154
assert motorL.speed() < 0, "When driving CCW Curve Rev, left speed should be -ve"
147155
assert motorR.speed() < 0, "When driving CCW Curve Rev, right speed should be -ve"
148156
assert (
149157
motorR.speed() < motorL.speed()
150158
), "When driving Rev in CCW Curve, motorR should be less than motorL"
151-
wait(3000) # 4 seconds x 90 deg/s = full circle
159+
delay(3000) # 4 seconds x 90 deg/s = full circle
152160
drive_base.stop()
153-
wait(250)
161+
delay(250)
154162

155163
drive_base.drive(200, -90) # Drives CCW Fwd
156-
wait(1000)
164+
wait(STARTUP_DELAY_LONG)
157165
assert motorL.speed() > 0, "When driving CCW Curve Fwd, left speed should be +ve"
158166
assert motorR.speed() > 0, "When driving CCW Curve Fwd, right speed should be +ve"
159167
assert (
160168
motorL.speed() < motorR.speed()
161169
), "When driving Fwd in CCW Curve, motorL should be less than motorR"
162-
wait(3000) # 4 seconds x 90 deg/s = full circle
170+
delay(3000) # 4 seconds x 90 deg/s = full circle
163171
drive_base.stop()
164-
wait(250)
172+
delay(250)
165173

166174
drive_base.drive(-200, -90) # In corrected version it should drive CW in Rev
167-
wait(1000)
175+
wait(STARTUP_DELAY_LONG)
168176
assert motorL.speed() < 0, "When driving CW Curve Rev, left speed should be +ve"
169177
assert motorR.speed() < 0, "When driving CW Curve Rev, right speed should be +ve"
170178
assert (
171179
motorL.speed() < motorR.speed()
172180
), "When driving Rev in CW Curve, motorL should be less than motorR"
173-
wait(3000) # 4 seconds x 90 deg/s = full circle
181+
delay(3000) # 4 seconds x 90 deg/s = full circle
174182
drive_base.stop()
175-
wait(250)
183+
delay(250)

0 commit comments

Comments
 (0)