|
8 | 8 | motorR = Motor(Port.B) |
9 | 9 | drive_base = DriveBase(motorL, motorR, wheel_diameter=56, axle_track=80) |
10 | 10 |
|
| 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 | + |
11 | 41 | # Demo 1: Using straight() to move Fwd & Rev in mm |
12 | 42 | drive_base.straight(200, wait=False) |
13 | | -wait(500) |
| 43 | +wait(STARTUP_DELAY) |
14 | 44 | assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve" |
15 | 45 | 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() |
19 | 47 |
|
20 | 48 | drive_base.straight(-200, wait=False) |
21 | | -wait(500) |
| 49 | +wait(STARTUP_DELAY) |
22 | 50 | assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve" |
23 | 51 | 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() |
27 | 53 |
|
28 | 54 | # Demo 2: Using turn() for in-place turns by angle (+ve = CW, -ve = CCW) |
29 | 55 | drive_base.turn(90, wait=False) |
30 | | -wait(500) |
| 56 | +wait(STARTUP_DELAY) |
31 | 57 | assert motorL.speed() > 0, "For CW in-place turn 90 degrees, left speed should be +ve" |
32 | 58 | 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() |
36 | 60 |
|
37 | 61 | drive_base.turn(180, wait=False) |
38 | | -wait(500) |
| 62 | +wait(STARTUP_DELAY) |
39 | 63 | assert motorL.speed() > 0, "For CW in-place turn 180 degrees, left speed should be +ve" |
40 | 64 | 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() |
44 | 66 |
|
45 | 67 | drive_base.turn(-270, wait=False) |
46 | | -wait(500) |
| 68 | +wait(STARTUP_DELAY) |
47 | 69 | assert motorL.speed() < 0, "For CCW in-place turn 270 degrees, left speed should be -ve" |
48 | 70 | 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() |
52 | 72 |
|
53 | 73 | drive_base.turn(1080, wait=False) |
54 | | -wait(500) |
| 74 | +wait(STARTUP_DELAY) |
55 | 75 | assert motorL.speed() > 0, "For CW in-place turn 1080 degrees, left speed should be +ve" |
56 | 76 | 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() |
60 | 78 |
|
61 | 79 | drive_base.turn(-1440, wait=False) |
62 | | -wait(500) |
| 80 | +wait(STARTUP_DELAY) |
63 | 81 | assert motorL.speed() < 0, "For CCW in-place turn 1440 degrees, left speed should be -ve" |
64 | 82 | 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() |
68 | 84 |
|
69 | 85 |
|
70 | 86 | # Demo 3: Using curve() drives the 'center point' between the wheels a full |
71 | 87 | # circle (360 degrees or part thereof) around a circle of 12cm radius. |
72 | 88 | drive_base.curve(120, 360, wait=False) # Drives forward along circle to robot's right |
73 | | -wait(500) |
| 89 | +wait(STARTUP_DELAY) |
74 | 90 | assert motorL.speed() > 0, "When driving Fwd in CW Curve, left speed should be +ve" |
75 | 91 | assert motorR.speed() > 0, "When driving Fwd in CW Curve, right speed should be +ve" |
76 | 92 | assert ( |
77 | 93 | motorL.speed() > motorR.speed() |
78 | 94 | ), "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() |
82 | 96 |
|
83 | 97 | drive_base.curve(-140, 360, wait=False) # Drives backward along circle to robot's right |
84 | | -wait(500) |
| 98 | +wait(STARTUP_DELAY) |
85 | 99 | assert motorL.speed() < 0, "When driving Rev in CCW Curve, left speed should be -ve" |
86 | 100 | assert motorR.speed() < 0, "When driving Rev in CCW Curve, right speed should be -ve" |
87 | 101 | assert ( |
88 | 102 | motorL.speed() < motorR.speed() |
89 | 103 | ), "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() |
93 | 105 |
|
94 | 106 | drive_base.curve(120, -360, wait=False) # Drives forward along circle to robot's left |
95 | | -wait(500) |
| 107 | +wait(STARTUP_DELAY) |
96 | 108 | assert motorL.speed() > 0, "When driving Fwd in CCW Curve, left speed should be +ve" |
97 | 109 | assert motorR.speed() > 0, "When driving Fwd in CCW Curve, right speed should be +ve" |
98 | 110 | assert ( |
99 | 111 | motorL.speed() < motorR.speed() |
100 | 112 | ), "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() |
104 | 114 |
|
105 | 115 | drive_base.curve(-120, -360, wait=False) # Drives in reverse along circle to robot's left |
106 | | -wait(500) |
| 116 | +wait(STARTUP_DELAY) |
107 | 117 | assert motorL.speed() < 0, "When driving Rev in CW Curve, left speed should be -ve" |
108 | 118 | assert motorR.speed() < 0, "When driving Rev in CW Curve, right speed should be -ve" |
109 | 119 | assert ( |
110 | 120 | motorL.speed() > motorR.speed() |
111 | 121 | ), "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() |
115 | 123 |
|
116 | 124 | # Demo 4: Using drive() to control the robot in various ways. |
117 | 125 | drive_base.drive(200, 0) # Drive straight Fwd |
118 | | -wait(1000) |
| 126 | +wait(STARTUP_DELAY_LONG) |
119 | 127 | assert motorL.speed() > 0, "When driving Fwd in straight line, left speed should be +ve" |
120 | 128 | assert motorR.speed() > 0, "When driving Fwd in straight line, right speed should be +ve" |
121 | | -wait(500) |
| 129 | +delay(500) |
122 | 130 | drive_base.stop() |
123 | | -wait(250) |
| 131 | +delay(250) |
124 | 132 |
|
125 | 133 | drive_base.drive(-200, 0) # Drive straight Rev |
126 | | -wait(1000) |
| 134 | +wait(STARTUP_DELAY_LONG) |
127 | 135 | assert motorL.speed() < 0, "When driving Rev in straight line, left speed should be -ve" |
128 | 136 | assert motorR.speed() < 0, "When driving Rev in straight line, right speed should be -ve" |
129 | | -wait(500) |
| 137 | +delay(500) |
130 | 138 | drive_base.stop() |
131 | | -wait(250) |
| 139 | +delay(250) |
132 | 140 |
|
133 | 141 | drive_base.drive(200, 90) # Drives CW Fwd |
134 | | -wait(1000) |
| 142 | +wait(STARTUP_DELAY_LONG) |
135 | 143 | assert motorL.speed() > 0, "When driving CW Curve Fwd, left speed should be +ve" |
136 | 144 | assert motorR.speed() > 0, "When driving CW Curve Fwd, right speed should be +ve" |
137 | 145 | assert ( |
138 | 146 | motorL.speed() > motorR.speed() |
139 | 147 | ), "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 |
141 | 149 | drive_base.stop() |
142 | | -wait(250) |
| 150 | +delay(250) |
143 | 151 |
|
144 | 152 | drive_base.drive(-200, 90) # Drives CW Rev |
145 | | -wait(1000) |
| 153 | +wait(STARTUP_DELAY_LONG) |
146 | 154 | assert motorL.speed() < 0, "When driving CCW Curve Rev, left speed should be -ve" |
147 | 155 | assert motorR.speed() < 0, "When driving CCW Curve Rev, right speed should be -ve" |
148 | 156 | assert ( |
149 | 157 | motorR.speed() < motorL.speed() |
150 | 158 | ), "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 |
152 | 160 | drive_base.stop() |
153 | | -wait(250) |
| 161 | +delay(250) |
154 | 162 |
|
155 | 163 | drive_base.drive(200, -90) # Drives CCW Fwd |
156 | | -wait(1000) |
| 164 | +wait(STARTUP_DELAY_LONG) |
157 | 165 | assert motorL.speed() > 0, "When driving CCW Curve Fwd, left speed should be +ve" |
158 | 166 | assert motorR.speed() > 0, "When driving CCW Curve Fwd, right speed should be +ve" |
159 | 167 | assert ( |
160 | 168 | motorL.speed() < motorR.speed() |
161 | 169 | ), "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 |
163 | 171 | drive_base.stop() |
164 | | -wait(250) |
| 172 | +delay(250) |
165 | 173 |
|
166 | 174 | drive_base.drive(-200, -90) # In corrected version it should drive CW in Rev |
167 | | -wait(1000) |
| 175 | +wait(STARTUP_DELAY_LONG) |
168 | 176 | assert motorL.speed() < 0, "When driving CW Curve Rev, left speed should be +ve" |
169 | 177 | assert motorR.speed() < 0, "When driving CW Curve Rev, right speed should be +ve" |
170 | 178 | assert ( |
171 | 179 | motorL.speed() < motorR.speed() |
172 | 180 | ), "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 |
174 | 182 | drive_base.stop() |
175 | | -wait(250) |
| 183 | +delay(250) |
0 commit comments