|
4 | 4 | from pybricks.tools import wait |
5 | 5 |
|
6 | 6 | # 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) |
9 | 9 | drive_base = DriveBase(motorL, motorR, wheel_diameter=56, axle_track=80) |
10 | 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) |
| 11 | +# Demo 1: Using straight() to move Fwd & Rev in mm |
16 | 12 | drive_base.straight(200, wait=False) |
17 | 13 | wait(500) |
18 | | -print( |
19 | | - "Test 1 - Drive Fwd - motorL.speed() = {}, motorR.speed() = {}".format( |
20 | | - motorL.speed(), motorR.speed() |
21 | | - ) |
22 | | -) |
23 | 14 | assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve" |
24 | 15 | assert motorR.speed() > 0, "When driving Fwd in straight line, right speed should be +ve" |
25 | 16 | while not drive_base.done(): |
|
28 | 19 |
|
29 | 20 | drive_base.straight(-200, wait=False) |
30 | 21 | wait(500) |
31 | | -print( |
32 | | - "Test 2 -Drive Rev - motorL.speed() = {}, motorR.speed() = {}".format( |
33 | | - motorL.speed(), motorR.speed() |
34 | | - ) |
35 | | -) |
36 | 22 | assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve" |
37 | 23 | assert motorR.speed() < 0, "When driving Rev in straight line, right speed should be -ve" |
38 | 24 | while not drive_base.done(): |
39 | 25 | wait(10) |
40 | 26 | wait(250) |
41 | 27 |
|
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) |
44 | 29 | drive_base.turn(90, wait=False) |
45 | 30 | 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" |
53 | 33 | while not drive_base.done(): |
54 | 34 | wait(10) |
55 | 35 | wait(500) |
56 | 36 |
|
57 | 37 | drive_base.turn(180, wait=False) |
58 | 38 | 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" |
66 | 41 | while not drive_base.done(): |
67 | 42 | wait(10) |
68 | 43 | wait(500) |
69 | 44 |
|
70 | 45 | drive_base.turn(-270, wait=False) |
71 | 46 | 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" |
79 | 49 | while not drive_base.done(): |
80 | 50 | wait(10) |
81 | 51 | wait(500) |
82 | 52 |
|
83 | 53 | drive_base.turn(1080, wait=False) |
84 | 54 | 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" |
92 | 57 | while not drive_base.done(): |
93 | 58 | wait(10) |
94 | 59 | wait(500) |
95 | 60 |
|
96 | 61 | drive_base.turn(-1440, wait=False) |
97 | 62 | 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" |
105 | 65 | while not drive_base.done(): |
106 | 66 | wait(10) |
107 | 67 | wait(500) |
108 | 68 |
|
109 | 69 |
|
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 |
113 | 73 | 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 | 74 | assert motorL.speed() > 0, "When driving Fwd in CW Curve, left speed should be +ve" |
120 | 75 | assert motorR.speed() > 0, "When driving Fwd in CW Curve, right speed should be +ve" |
121 | 76 | assert ( |
|
125 | 80 | wait(10) |
126 | 81 | wait(500) |
127 | 82 |
|
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 |
129 | 84 | 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 | 85 | assert motorL.speed() < 0, "When driving Rev in CCW Curve, left speed should be -ve" |
136 | 86 | assert motorR.speed() < 0, "When driving Rev in CCW Curve, right speed should be -ve" |
137 | 87 | assert ( |
138 | 88 | 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" |
140 | 90 | while not drive_base.done(): |
141 | 91 | wait(10) |
142 | 92 | wait(500) |
143 | 93 |
|
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 |
145 | 95 | 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 | 96 | assert motorL.speed() > 0, "When driving Fwd in CCW Curve, left speed should be +ve" |
152 | 97 | assert motorR.speed() > 0, "When driving Fwd in CCW Curve, right speed should be +ve" |
153 | 98 | assert ( |
|
157 | 102 | wait(10) |
158 | 103 | wait(500) |
159 | 104 |
|
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 |
161 | 106 | 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 | 107 | assert motorL.speed() < 0, "When driving Rev in CW Curve, left speed should be -ve" |
168 | 108 | assert motorR.speed() < 0, "When driving Rev in CW Curve, right speed should be -ve" |
169 | 109 | assert ( |
|
173 | 113 | wait(10) |
174 | 114 | wait(500) |
175 | 115 |
|
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 |
179 | 118 | wait(1000) |
180 | | -print( |
181 | | - "Test 12 - .drive() - Drive Fwd - motorL.speed() = {}, motorR.speed() = {}".format( |
182 | | - motorL.speed(), motorR.speed() |
183 | | - ) |
184 | | -) |
185 | 119 | assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve" |
186 | 120 | assert motorR.speed() > 0, "When driving Fwd in straight line, right speed should be +ve" |
187 | 121 | wait(500) |
188 | 122 | drive_base.stop() |
189 | 123 | wait(250) |
190 | 124 |
|
191 | | -drive_base.drive(-200, 0) # Drive Rev |
| 125 | +drive_base.drive(-200, 0) # Drive straight Rev |
192 | 126 | wait(1000) |
193 | | -print( |
194 | | - "Test 13 - .drive() - Drive Rev - motorL.speed() = {}, motorR.speed() = {}".format( |
195 | | - motorL.speed(), motorR.speed() |
196 | | - ) |
197 | | -) |
198 | 127 | assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve" |
199 | 128 | assert motorR.speed() < 0, "When driving Rev in straight line, right speed should be -ve" |
200 | 129 | wait(500) |
|
203 | 132 |
|
204 | 133 | drive_base.drive(200, 90) # Drives CW Fwd |
205 | 134 | 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 | 135 | assert motorL.speed() > 0, "When driving CW Curve Fwd, left speed should be +ve" |
212 | 136 | assert motorR.speed() > 0, "When driving CW Curve Fwd, right speed should be +ve" |
213 | 137 | assert ( |
214 | 138 | motorL.speed() > motorR.speed() |
215 | 139 | ), "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 |
217 | 141 | drive_base.stop() |
218 | 142 | wait(250) |
219 | 143 |
|
220 | | -drive_base.drive(-200, 90) # In corrected version it should Drive CCW in Rev |
| 144 | +drive_base.drive(-200, 90) # Drives CW Rev |
221 | 145 | 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 | 146 | assert motorL.speed() < 0, "When driving CCW Curve Rev, left speed should be -ve" |
228 | 147 | assert motorR.speed() < 0, "When driving CCW Curve Rev, right speed should be -ve" |
229 | 148 | 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" |
232 | 151 | wait(3000) # 4 seconds x 90 deg/s = full circle |
233 | 152 | drive_base.stop() |
234 | 153 | wait(250) |
235 | 154 |
|
236 | | -drive_base.drive(200, -90) # In corrected version it should Drive CCW Fwd |
| 155 | +drive_base.drive(200, -90) # Drives CCW Fwd |
237 | 156 | 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 | 157 | assert motorL.speed() > 0, "When driving CCW Curve Fwd, left speed should be +ve" |
244 | 158 | assert motorR.speed() > 0, "When driving CCW Curve Fwd, right speed should be +ve" |
245 | 159 | assert ( |
|
251 | 165 |
|
252 | 166 | drive_base.drive(-200, -90) # In corrected version it should drive CW in Rev |
253 | 167 | 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 | 168 | assert motorL.speed() < 0, "When driving CW Curve Rev, left speed should be +ve" |
260 | 169 | assert motorR.speed() < 0, "When driving CW Curve Rev, right speed should be +ve" |
261 | 170 | assert ( |
262 | | - motorL.speed() > motorR.speed() |
| 171 | + motorL.speed() < motorR.speed() |
263 | 172 | ), "When driving Rev in CW Curve, motorL should be less than motorR" |
264 | 173 | wait(3000) # 4 seconds x 90 deg/s = full circle |
265 | 174 | drive_base.stop() |
|
0 commit comments