-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathblocks.ts
321 lines (303 loc) · 9.93 KB
/
blocks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Robot
*/
//% color="#ff6800" icon="\uf1b9" weight=15
//% groups='["Robot", "Motors", "Accessories", "Lines", "Obstacles", "Configuration"]'
namespace robot {
/**
* Steers the robot.
*/
//% weight=97
//% group="Motors"
//% block="robot motor steer $turnRatio at $speed \\% || for $duration ms"
//% blockid="mbitrobotmotorsteer"
//% expandableArgumentMode="toggle"
//% speed.defl=100
//% speed.min=-100
//% speed.max=100
//% speed.shadow=speedPicker
//% turnRatio.shadow=turnRatioPicker
//% turnRatio.min=-200
//% turnRatio.max=200
//% duration.shadow=timePicker
export function motorSteer(
turnRatio: number = 0,
speed: number = 100,
duration?: number
) {
const robot = RobotDriver.instance()
robot.motorSteer(turnRatio, speed, duration)
}
/**
* Tanks the robot.
*/
//% weight=98
//% group="Motors"
//% block="robot motor tank $left \\% $right \\% || for $duration ms"
//% expandableArgumentMode="toggle"
//% blockid="mbitrobotmotortank"
//% left.defl=100
//% left.min=-100
//% left.max=100
//% left.shadow=speedPicker
//% right.defl=100
//% right.min=-100
//% right.max=100
//% right.shadow=speedPicker
//% duration.shadow=timePicker
export function motorTank(
left: number = 80,
right: number = 80,
duration?: number
) {
left = clampSpeed(left, 100)
right = clampSpeed(right, 100)
const speed = Math.abs(left) > Math.abs(right) ? left : right
let turnRatio: number
if (speed === 0) {
turnRatio = 0
} else {
turnRatio = ((left - right) / speed) * 100
}
const robot = RobotDriver.instance()
robot.motorSteer(turnRatio, speed, duration)
}
/**
* Stops the robot.
*/
//% weight=50
//% group="Motors"
//% block="robot motor stop"
//% blockid="mbitrobotmotorstop"
export function motorStop() {
const robot = RobotDriver.instance()
robot.motorSteer(0, 0)
}
/**
* Opens or closes a claw (if available).
* @param opening the opening of the claw, from 0 (closed) to 100 (open)
*/
//% weight=5
//% group="Accessories"
//% block="robot arm $index open $opening \\%"
//% blockid="mbitrobotarmopen"
//% index.min=0
//% index.max=1
//% opening.min=0
//% opening.max=100
export function armOpen(index: number, opening: number) {
const robot = RobotDriver.instance()
robot.armOpen(index, opening)
}
/**
* Sets the LED color
*/
//% blockid="mbitrobotsetcolor" block="robot set color $rgb"
//% group="Accessories"
//% weight=98
//% rgb.shadow=colorNumberPicker
export function setColor(rgb: number) {
const robot = RobotDriver.instance()
robot.setColor(rgb)
}
/**
* Play a tone through the robot speaker
*/
//% blockid="mbitrobotplaytone" block="robot play tone $frequency for $duration"
//% group="Accessories"
//% weight=10
//% frequency.shadow=device_note
//% duration.shadow=device_beat
export function playTone(frequency: number, duration: number) {
const robot = RobotDriver.instance()
robot.playTone(frequency, duration)
}
/**
* Gets the distance reported by the distance sensor
*/
//% block="robot obstacle distance (cm)"
//% blockId=microcoderobotobstacledistance
//% group="Obstacles"
export function obstacleDistance(): number {
const robot = RobotDriver.instance()
robots.sensorsUsed |= robots.Sensors.Sonar
return robot.obstacleDistance
}
/**
* Register a handler to run when the obstacle distance
* changes by a minimum threshold
*/
//% block="robot on obstacle distance changed"
//% blockId=microcoderobotobstacledistancechanged
//% group="Obstacles"
export function onObstacleDistanceChanged(handler: () => void) {
robots.sensorsUsed |= robots.Sensors.Sonar
messages.onEvent(
messages.RobotEvents.ObstacleDistance,
robot.robots.RobotCompactCommand.ObstacleState,
handler
)
}
/**
* Checks the state of line detectors. Always returns false if the line detector is not available on the hardware
*/
//% weight=40
//% block="robot detect line $detector"
//% blockId=microcoderobotdetectlines
//% group="Lines"
export function detectLine(detector: RobotLineDetector): boolean {
robots.sensorsUsed |= robots.Sensors.LineDetector
const r = RobotDriver.instance()
const threshold = r.robot.lineHighThreshold
const current = r.currentLineState
return current[detector] >= threshold // returns false for missing
}
/**
* Registers an event to run when any line detector
* changes state
*/
//% weight=50
//% block="robot on line detected"
//% blockId=microcoderobotondetectlines
//% group="Lines"
export function onLineDetected(handler: () => void) {
robots.sensorsUsed |= robots.Sensors.LineDetector
const msg = robot.robots.RobotCompactCommand.LineAnyState
messages.onEvent(messages.RobotEvents.LineAny, msg, handler)
}
/**
* Registers an event to run when the left or right detectors
* changes state
*/
//% weight=99
//% block="robot on line $left $right"
//% blockId=microcoderobotondetectlinesleftright
//% group="Lines"
//% left.shadow=toggleOnOff
//% right.shadow=toggleOnOff
export function onLineLeftRightDetected(
left: boolean,
right: boolean,
handler: () => void
) {
robots.sensorsUsed |= robots.Sensors.LineDetector
let msg = robot.robots.RobotCompactCommand.LineLeftRightState
if (left) msg |= 1 << RobotLineDetector.Left
if (right) msg |= 1 << RobotLineDetector.Right
messages.onEvent(messages.RobotEvents.LineLeftRight, msg, handler)
}
/**
* Registers an event to run when the left, middle or right detectors
* changes state
*/
//% weight=61
//% block="robot on line $left $middle $right"
//% blockId=microcoderobotondetectlinesleftrightmid
//% group="Lines"
//% left.shadow=toggleOnOff
//% middle.shadow=toggleOnOff
//% right.shadow=toggleOnOff
export function onLineLeftMiddleRightDetected(
left: boolean,
middle: boolean,
right: boolean,
handler: () => void
) {
robots.sensorsUsed |= robots.Sensors.LineDetector
let msg = robot.robots.RobotCompactCommand.LineLeftRightMiddleState
if (left) msg |= 1 << RobotLineDetector.Left
if (middle) msg |= 1 << RobotLineDetector.Middle
if (right) msg |= 1 << RobotLineDetector.Right
messages.onEvent(messages.RobotEvents.LineLeftMiddleRight, msg, handler)
}
/**
* Registers an event to run when the outer left, left, right, outerRight detectors
* changes state
*/
//% weight=60
//% block="robot on line $outerLeft $left $right $outerRight"
//% blockId=microcoderobotondetectlinesouterleftleftrightouterright
//% group="Lines"
//% outerLeft.shadow=toggleOnOff
//% right.shadow=toggleOnOff
//% left.shadow=toggleOnOff
//% outerRight.shadow=toggleOnOff
export function onLineOuterLeftLeftOuterRightDetected(
outerLeft: boolean,
left: boolean,
right: boolean,
outerRight: boolean,
handler: () => void
) {
robots.sensorsUsed |= robots.Sensors.LineDetector
let msg =
robot.robots.RobotCompactCommand
.LineOuterLeftLeftRightOuterRightState
if (outerLeft) msg |= 1 << RobotLineDetector.OuterLeft
if (left) msg |= 1 << RobotLineDetector.Left
if (right) msg |= 1 << RobotLineDetector.Right
if (outerRight) msg |= 1 << RobotLineDetector.OuterRight
messages.onEvent(
messages.RobotEvents.LineOuterLeftLeftRightOuterRight,
msg,
handler
)
}
/**
* Sets a value that corrects the ratio of power between the left and the right motor to account for hardware differences.
*/
//% block="robot set motor drift to %drift"
//% blockid="mbitrobotsetmotordrift"
//% group="Configuration"
//% weight=10
//% drift.min=-25
//% drift.max=25
export function setMotorDrift(drift: number) {
const robot = RobotDriver.instance()
drift = Math.clamp(-25, 25, drift)
robot.setRunDrift(drift)
}
/**
* Enables or disables the display of the robot state on the LED matrix.
* @param enabled
*/
//% block="robot set $assist $enabled"
//% blockid="mbitrobotsetassist"
//% group="Configuration"
//% enabled.shadow=toggleOnOff
export function setAssist(assist: RobotAssist, enabled: boolean) {
const robot = RobotDriver.instance()
robot.setAssist(assist, enabled)
}
/**
* Sets the group used for radio communication and stores it in flash.
* @param group group id
*/
//% block="robot set radio group $group"
//% blockid="mbitrobotsetradiogroup"
//% group="Configuration"
//% group.min=1
//% group.max=25
//% group.defl=1
export function setRadioGroup(group: number) {
const robot = RobotDriver.instance()
group = (group >> 0) & 0xff
if (group === 0) return // not allowed
while (group < 0) group += configuration.MAX_GROUPS
group = group % configuration.MAX_GROUPS
robot.setRadioGroup(group)
}
/**
* Registers button A, B, A+B to change the radio group and drift.
* Puts motors to 80%.
*/
//% block="robot calibrate"
//% blockid="mbitrobotcalibrate"
//% group="Configuration"
//% weight=100
export function calibrate() {
robot.motorStop()
robot.startCalibrationButtons(true)
robot.motorSteer(0, 80)
}
}