forked from 4tronix/BitBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
349 lines (314 loc) · 9.28 KB
/
main.ts
File metadata and controls
349 lines (314 loc) · 9.28 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* Enumeration of motors.
*/
enum BBMotor {
//% block="left"
Left,
//% block="right"
Right,
//% block="all"
All
}
/**
* Enumeration of directions.
*/
enum BBRobotDirection {
//% block="left"
Left,
//% block="right"
Right
}
/**
* Enumeration of line sensors.
*/
enum BBLineSensor {
//% block="left"
Left,
//% block="right"
Right
}
/**
* Enumeration of light sensors.
*/
enum BBLightSensor {
//% block="left"
Left,
//% block="right"
Right
}
/**
* Ping unit for sesnor.
*/
enum BBPingUnit {
//% block="μs"
MicroSeconds,
//% block="cm"
Centimeters,
//% block="inches"
Inches
}
/**
* Custom blocks
*/
//% weight=10 color=#e7660b icon="\uf1b9"
namespace bitbot {
let neoStrip: neopixel.Strip;
/**
* Return a neo pixel strip.
*/
//% blockId="bitbot_neo" block="neo strip"
//% weight=5
export function neo(): neopixel.Strip {
if (!neoStrip) {
neoStrip = neopixel.create(DigitalPin.P13, 12, NeoPixelMode.RGB)
}
return neoStrip;
}
/**
* Drive robot forward (or backward) at speed.
*
* @param speed speed of motor between -1023 and 1023.
*/
//% blockId="bitbot_motor_forward" block="drive at speed %speed"
//% speed.min=-1023 speed.max=1023
//% weight=110
export function drive(speed: number): void {
motor(BBMotor.All, speed);
}
/**
* Drive robot forward (or backward) at speed for milliseconds.
*
* @param speed speed of motor between -1023 and 1023.
* @param milliseconds duration in milliseconds to drive forward for, then stop.
*/
//% blockId="bitbot_motor_forward_milliseconds" block="drive at speed %speed| for milliseconds %milliseconds"
//% speed.min=-1023 speed.max=1023
//% weight=131
export function driveMilliseconds(speed: number, milliseconds: number): void {
drive(speed);
basic.pause(milliseconds);
drive(0);
}
/**
* Turn robot in direction at speed.
*
* @param direction direction to turn.
* @param speed speed of motor between 0 and 1023.
*/
//% blockId="bitbot_turn" block="turn in direction %direction|speed %speed"
//% speed.min=0 speed.max=1023
//% weight=109
export function driveTurn(direction: BBRobotDirection, speed: number): void {
if (speed < 0) speed = 0;
if (direction == BBRobotDirection.Left) {
motor(BBMotor.Left, -speed);
motor(BBMotor.Right, speed);
} else if (direction == BBRobotDirection.Right) {
motor(BBMotor.Left, speed);
motor(BBMotor.Right, -speed);
}
}
/**
* Turn robot in direction at speed for milliseconds.
*
* @param direction direction to turn.
* @param speed speed of motor between 0 and 1023.
* @param milliseconds duration in milliseconds to turn for, then stop.
*/
//% blockId="bitbot_turn_milliseconds" block="turn in direction %direction|speed %speed| for milliseconds %milliseconds"
//% speed.min=0 speed.max=1023
//% weight=130
export function driveTurnMilliseconds(direction: BBRobotDirection, speed: number, milliseconds: number): void {
driveTurn(direction, speed)
basic.pause(milliseconds)
motor(BBMotor.All, 0)
}
/**
* Drive motor(s) forward or reverse.
*
* @param motor motor to drive.
* @param speed speed of motor
*/
//% blockId="bitbot_motor" block="drive motor %motor|speed %speed"
//% weight=100
export function motor(motor: BBMotor, speed: number): void {
let forward = (speed >= 0);
if (speed > 1023) {
speed = 1023;
} else if (speed < -1023) {
speed = -1023;
}
let realSpeed = speed;
if (!forward) {
if (realSpeed >= -200)
realSpeed = Math.idiv(realSpeed * 19, 6);
else if (realSpeed >= -400)
realSpeed = realSpeed * 2;
else if (realSpeed >= -600)
realSpeed = Math.idiv(realSpeed * 3, 2);
else if (realSpeed >= -800)
realSpeed = Math.idiv(realSpeed * 5, 4);
realSpeed = 1023 + realSpeed; // realSpeed is negative!
}
if ((motor == BBMotor.Left) || (motor == BBMotor.All)) {
pins.analogWritePin(AnalogPin.P0, realSpeed);
pins.digitalWritePin(DigitalPin.P8, forward ? 0 : 1);
}
if ((motor == BBMotor.Right) || (motor == BBMotor.All)) {
pins.analogWritePin(AnalogPin.P1, realSpeed);
pins.digitalWritePin(DigitalPin.P12, forward ? 0 : 1);
}
}
/**
* Sound a buzz.
*
* @param flag Flag to set (0) for off and (1) for on.
*/
//% blockId="bitbot_buzz" block="buzz sound %flag"
//% flag.min=0 flag.max=1
//% weight=95
export function buzz(flag: number): void {
pins.digitalWritePin(DigitalPin.P14, flag === 0 ? 0 : 1);
}
/**
* Read line sensor.
*
* @param sensor Line sensor to read.
*/
//% blockId="bitbot_read_line" block="read line sensor %sensor"
//% weight=90
export function readLine(sensor: BBLineSensor): number {
if (sensor == BBLineSensor.Left) {
return pins.digitalReadPin(DigitalPin.P11);
} else {
return pins.digitalReadPin(DigitalPin.P5);
}
}
/**
* Read light sensor.
*
* @param sensor Light sensor to read.
*/
//% blockId="bitbot_read_light" block="read light sensor %sensor"
//% weight=90
export function readLight(sensor: BBLightSensor): number {
if (sensor == BBLightSensor.Left) {
pins.digitalWritePin(DigitalPin.P16, 0);
return pins.analogReadPin(AnalogPin.P2);
} else {
pins.digitalWritePin(DigitalPin.P16, 1);
return pins.analogReadPin(AnalogPin.P2);
}
}
/**
* Shows all LEDs to a given color (range 0-255 for r, g, b).
*
* @param rgb RGB color of the LED
*/
//% blockId="bitbot_neo_set_color" block="set pixels to %rgb=neopixel_colors"
//% weight=80
export function neoSetColor(rgb: number) {
neo().showColor(rgb);
}
/**
* Set LED to a given color (range 0-255 for r, g, b).
*
* @param offset position of the NeoPixel in the strip
* @param rgb RGB color of the LED
*/
//% blockId="bitbot_neo_set_pixel_color" block="set pixel color at %offset|to %rgb=neopixel_colors"
//% weight=80
export function neoSetPixelColor(offset: number, rgb: number): void {
neo().setPixelColor(offset, rgb);
}
/**
* Show leds.
*/
//% blockId="bitbot_neo_show" block="show leds"
//% weight=76
export function neoShow(): void {
neo().show();
}
/**
* Clear leds.
*/
//% blockId="bitbot_neo_clear" block="clear leds"
//% weight=75
export function neoClear(): void {
neo().clear();
}
/**
* Shows a rainbow pattern on all LEDs.
*/
//% blockId="bitbot_neo_rainbow" block="set led rainbow"
//% weight=70
export function neoRainbow(): void {
neo().showRainbow(1, 360);
}
/**
* Shift LEDs forward and clear with zeros.
*/
//% blockId="bitbot_neo_shift" block="shift led pixels"
//% weight=66
export function neoShift(): void {
neo().shift(1);
}
/**
* Rotate LEDs forward.
*/
//% blockId="bitbot_neo_rotate" block="rotate led pixels"
//% weight=65
export function neoRotate(): void {
neo().rotate(1);
}
/**
* Set the brightness of the strip. Note this only applies to future writes to the strip.
*
* @param brightness a measure of LED brightness in 0-255. eg: 255
*/
//% blockId="bitbot_neo_brightness" block="set led brightness %brightness"
//% brightness.min=0 brightness.max=255
//% weight=10
export function neoBrightness(brightness: number): void {
neo().setBrightness(brightness);
}
/**
* Read distance from sonar module connected to accessory connector.
*
* @param unit desired conversion unit
*/
//% blockId="bitbot_sonar" block="read sonar as %unit"
//% weight=7
export function sonar(unit: BBPingUnit): number {
// send pulse
let trig = DigitalPin.P15;
let echo = DigitalPin.P15;
let maxCmDistance = 500;
pins.setPull(trig, PinPullMode.PullNone);
pins.digitalWritePin(trig, 0);
control.waitMicros(2);
pins.digitalWritePin(trig, 1);
control.waitMicros(10);
pins.digitalWritePin(trig, 0);
// read pulse
let d = pins.pulseIn(echo, PulseValue.High, maxCmDistance * 58);
switch (unit) {
case BBPingUnit.Centimeters: return d / 58;
case BBPingUnit.Inches: return d / 148;
default: return d;
}
}
/**
* Adjust opening of Talon attachment
*
* @param degrees Degrees to open Talon
*/
//% blockId="bitbot_set_talon" block="set talon %degrees"
//% weight=90
//% degrees.min=0 degrees.max=80
export function setTalon(degrees: number): void
{
pins.servoWritePin(AnalogPin.P15, degrees)
}
}