-
Notifications
You must be signed in to change notification settings - Fork 65
/
main.ts
613 lines (550 loc) · 19 KB
/
main.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
Riven
modified from pxt-servo/servodriver.ts
load dependency
"robotbit": "file:../pxt-robotbit"
*/
//% color="#31C7D5" weight=10 icon="\uf1d0"
namespace robotbit {
const PCA9685_ADDRESS = 0x40
const MODE1 = 0x00
const MODE2 = 0x01
const SUBADR1 = 0x02
const SUBADR2 = 0x03
const SUBADR3 = 0x04
const PRESCALE = 0xFE
const LED0_ON_L = 0x06
const LED0_ON_H = 0x07
const LED0_OFF_L = 0x08
const LED0_OFF_H = 0x09
const ALL_LED_ON_L = 0xFA
const ALL_LED_ON_H = 0xFB
const ALL_LED_OFF_L = 0xFC
const ALL_LED_OFF_H = 0xFD
const STP_CHA_L = 2047
const STP_CHA_H = 4095
const STP_CHB_L = 1
const STP_CHB_H = 2047
const STP_CHC_L = 1023
const STP_CHC_H = 3071
const STP_CHD_L = 3071
const STP_CHD_H = 1023
// HT16K33 commands
const HT16K33_ADDRESS = 0x70
const HT16K33_BLINK_CMD = 0x80
const HT16K33_BLINK_DISPLAYON = 0x01
const HT16K33_BLINK_OFF = 0
const HT16K33_BLINK_2HZ = 1
const HT16K33_BLINK_1HZ = 2
const HT16K33_BLINK_HALFHZ = 3
const HT16K33_CMD_BRIGHTNESS = 0xE0
export enum Servos {
S1 = 0x01,
S2 = 0x02,
S3 = 0x03,
S4 = 0x04,
S5 = 0x05,
S6 = 0x06,
S7 = 0x07,
S8 = 0x08
}
export enum Motors {
M1A = 0x1,
M1B = 0x2,
M2A = 0x3,
M2B = 0x4
}
export enum Steppers {
M1 = 0x1,
M2 = 0x2
}
export enum SonarVersion {
V1 = 0x1,
V2 = 0x2
}
export enum Turns {
//% blockId="T1B4" block="1/4"
T1B4 = 90,
//% blockId="T1B2" block="1/2"
T1B2 = 180,
//% blockId="T1B0" block="1"
T1B0 = 360,
//% blockId="T2B0" block="2"
T2B0 = 720,
//% blockId="T3B0" block="3"
T3B0 = 1080,
//% blockId="T4B0" block="4"
T4B0 = 1440,
//% blockId="T5B0" block="5"
T5B0 = 1800
}
export enum ValueUnit {
//% block="mm"
Millimeter,
//% block="cm"
Centimeters
}
let initialized = false
let initializedMatrix = false
let neoStrip: neopixel.Strip;
let matBuf = pins.createBuffer(17);
let distanceBuf = 0;
function i2cwrite(addr: number, reg: number, value: number) {
let buf = pins.createBuffer(2)
buf[0] = reg
buf[1] = value
pins.i2cWriteBuffer(addr, buf)
}
function i2ccmd(addr: number, value: number) {
let buf = pins.createBuffer(1)
buf[0] = value
pins.i2cWriteBuffer(addr, buf)
}
function i2cread(addr: number, reg: number) {
pins.i2cWriteNumber(addr, reg, NumberFormat.UInt8BE);
let val = pins.i2cReadNumber(addr, NumberFormat.UInt8BE);
return val;
}
function initPCA9685(): void {
i2cwrite(PCA9685_ADDRESS, MODE1, 0x00)
setFreq(50);
for (let idx = 0; idx < 16; idx++) {
setPwm(idx, 0, 0);
}
initialized = true
}
function setFreq(freq: number): void {
// Constrain the frequency
let prescaleval = 25000000;
prescaleval /= 4096;
prescaleval /= freq;
prescaleval -= 1;
let prescale = prescaleval; //Math.Floor(prescaleval + 0.5);
let oldmode = i2cread(PCA9685_ADDRESS, MODE1);
let newmode = (oldmode & 0x7F) | 0x10; // sleep
i2cwrite(PCA9685_ADDRESS, MODE1, newmode); // go to sleep
i2cwrite(PCA9685_ADDRESS, PRESCALE, prescale); // set the prescaler
i2cwrite(PCA9685_ADDRESS, MODE1, oldmode);
control.waitMicros(5000);
i2cwrite(PCA9685_ADDRESS, MODE1, oldmode | 0xa1);
}
function setPwm(channel: number, on: number, off: number): void {
if (channel < 0 || channel > 15)
return;
//serial.writeValue("ch", channel)
//serial.writeValue("on", on)
//serial.writeValue("off", off)
let buf = pins.createBuffer(5);
buf[0] = LED0_ON_L + 4 * channel;
buf[1] = on & 0xff;
buf[2] = (on >> 8) & 0xff;
buf[3] = off & 0xff;
buf[4] = (off >> 8) & 0xff;
pins.i2cWriteBuffer(PCA9685_ADDRESS, buf);
}
function setStepper(index: number, dir: boolean): void {
if (index == 1) {
if (dir) {
setPwm(0, STP_CHA_L, STP_CHA_H);
setPwm(2, STP_CHB_L, STP_CHB_H);
setPwm(1, STP_CHC_L, STP_CHC_H);
setPwm(3, STP_CHD_L, STP_CHD_H);
} else {
setPwm(3, STP_CHA_L, STP_CHA_H);
setPwm(1, STP_CHB_L, STP_CHB_H);
setPwm(2, STP_CHC_L, STP_CHC_H);
setPwm(0, STP_CHD_L, STP_CHD_H);
}
} else {
if (dir) {
setPwm(4, STP_CHA_L, STP_CHA_H);
setPwm(6, STP_CHB_L, STP_CHB_H);
setPwm(5, STP_CHC_L, STP_CHC_H);
setPwm(7, STP_CHD_L, STP_CHD_H);
} else {
setPwm(7, STP_CHA_L, STP_CHA_H);
setPwm(5, STP_CHB_L, STP_CHB_H);
setPwm(6, STP_CHC_L, STP_CHC_H);
setPwm(4, STP_CHD_L, STP_CHD_H);
}
}
}
function stopMotor(index: number) {
setPwm((index - 1) * 2, 0, 0);
setPwm((index - 1) * 2 + 1, 0, 0);
}
function matrixInit() {
i2ccmd(HT16K33_ADDRESS, 0x21);// turn on oscillator
i2ccmd(HT16K33_ADDRESS, HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (0 << 1));
i2ccmd(HT16K33_ADDRESS, HT16K33_CMD_BRIGHTNESS | 0xF);
}
function matrixShow() {
matBuf[0] = 0x00;
pins.i2cWriteBuffer(HT16K33_ADDRESS, matBuf);
}
/**
* Init RGB pixels mounted on robotbit
*/
//% blockId="robotbit_rgb" block="RGB"
//% weight=70
export function rgb(): neopixel.Strip {
if (!neoStrip) {
neoStrip = neopixel.create(DigitalPin.P16, 4, NeoPixelMode.RGB)
}
return neoStrip;
}
/**
* Servo Execute
* @param index Servo Channel; eg: S1
* @param degree [0-180] degree of servo; eg: 0, 90, 180
*/
//% blockId=robotbit_servo block="Servo|%index|degree %degree"
//% group="Servo" weight=62
//% degree.min=0 degree.max=180
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function Servo(index: Servos, degree: number): void {
if (!initialized) {
initPCA9685()
}
// 50hz: 20,000 us
let v_us = (degree * 1800 / 180 + 600) // 0.6 ~ 2.4
let value = v_us * 4096 / 20000
setPwm(index + 7, 0, value)
}
/**
* Geek Servo
* @param index Servo Channel; eg: S1
* @param degree [-45-225] degree of servo; eg: -45, 90, 225
*/
//% blockId=robotbit_gservo block="Geek Servo|%index|degree %degree"
///% group="Servo" weight=61
//% degree.min=-45 degree.max=225
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function GeekServo(index: Servos, degree: number): void {
if (!initialized) {
initPCA9685()
}
// 50hz: 20,000 us
let v_us = ((degree - 90) * 20 / 3 + 1500) // 0.6 ~ 2.4
let value = v_us * 4096 / 20000
setPwm(index + 7, 0, value)
}
/**
* GeekServo2KG
* @param index Servo Channel; eg: S1
* @param degree [0-360] degree of servo; eg: 0, 180, 360
*/
//% blockId=robotbit_gservo2kg block="GeekServo2KG|%index|degree %degree"
//% group="Servo" weight=60
//% blockGap=50
//% degree.min=0 degree.max=360
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function GeekServo2KG(index: Servos, degree: number): void {
if (!initialized) {
initPCA9685()
}
// 50hz: 20,000 us
//let v_us = (degree * 2000 / 360 + 500) 0.5 ~ 2.5
let v_us = (Math.floor((degree) * 2000 / 350) + 500) //fixed
let value = v_us * 4096 / 20000
setPwm(index + 7, 0, value)
}
/**
* GeekServo5KG
* @param index Servo Channel; eg: S1
* @param degree [0-360] degree of servo; eg: 0, 180, 360
*/
//% blockId=robotbit_gservo5kg block="GeekServo5KG|%index|degree %degree"
//% group="Servo" weight=59
//% degree.min=0 degree.max=360
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function GeekServo5KG(index: Servos, degree: number): void {
if (!initialized) {
initPCA9685()
}
const minInput = 0;
const maxInput = 355;//理论值为360
const minOutput = 500;
const maxOutput = 2500;
const v_us = ((degree - minInput) / (maxInput - minInput)) * (maxOutput - minOutput) + minOutput;
let value = v_us * 4096 / 20000
setPwm(index + 7, 0, value)
}
//% blockId=robotbit_gservo5kg_motor block="GeekServo5KG_MotorEN|%index|speed %speed"
//% group="Servo" weight=58
//% speed.min=-255 speed.max=255
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function GeekServo5KG_Motor(index: Servos, speed: number): void { //5KG的电机模式 3000-5000 4000是回中
if (!initialized) {
initPCA9685()
}
const minInput = -255;
const maxInput = 255;
const minOutput = 5000;
const maxOutput = 3000;
const v_us = ((speed - minInput) / (maxInput - minInput)) * (maxOutput - minOutput) + minOutput;
let value = v_us * 4096 / 20000
setPwm(index + 7, 0, value)
}
//% blockId=robotbit_stepper_degree block="Stepper 28BYJ-48|%index|degree %degree"
//% group="Motor" weight=54
export function StepperDegree(index: Steppers, degree: number): void {
if (!initialized) {
initPCA9685()
}
setStepper(index, degree > 0);
degree = Math.abs(degree);
basic.pause(10240 * degree / 360);
MotorStopAll()
}
//% blockId=robotbit_stepper_turn block="Stepper 28BYJ-48|%index|turn %turn"
//% group="Motor" weight=53
export function StepperTurn(index: Steppers, turn: Turns): void {
let degree = turn;
StepperDegree(index, degree);
}
//% blockId=robotbit_stepper_dual block="Dual Stepper(Degree) |M1 %degree1| M2 %degree2"
//% group="Motor" weight=52
export function StepperDual(degree1: number, degree2: number): void {
if (!initialized) {
initPCA9685()
}
setStepper(1, degree1 > 0);
setStepper(2, degree2 > 0);
degree1 = Math.abs(degree1);
degree2 = Math.abs(degree2);
basic.pause(10240 * Math.min(degree1, degree2) / 360);
if (degree1 > degree2) {
stopMotor(3); stopMotor(4);
basic.pause(10240 * (degree1 - degree2) / 360);
} else {
stopMotor(1); stopMotor(2);
basic.pause(10240 * (degree2 - degree1) / 360);
}
MotorStopAll()
}
/**
* Stepper Car move forward
* @param distance Distance to move in cm; eg: 10, 20
* @param diameter diameter of wheel in mm; eg: 48
*/
//% blockId=robotbit_stpcar_move block="Car Forward|Distance(cm) %distance|Wheel Diameter(mm) %diameter"
//% group="Motor" weight=51
export function StpCarMove(distance: number, diameter: number): void {
if (!initialized) {
initPCA9685()
}
let delay = 10240 * 10 * distance / 3 / diameter; // use 3 instead of pi
setStepper(1, delay > 0);
setStepper(2, delay > 0);
delay = Math.abs(delay);
basic.pause(delay);
MotorStopAll()
}
/**
* Stepper Car turn by degree
* @param turn Degree to turn; eg: 90, 180, 360
* @param diameter diameter of wheel in mm; eg: 48
* @param track track width of car; eg: 125
*/
//% blockId=robotbit_stpcar_turn block="Car Turn|Degree %turn|Wheel Diameter(mm) %diameter|Track(mm) %track"
//% group="Motor" weight=50
//% blockGap=50
export function StpCarTurn(turn: number, diameter: number, track: number): void {
if (!initialized) {
initPCA9685()
}
let delay = 10240 * turn * track / 360 / diameter;
setStepper(1, delay < 0);
setStepper(2, delay > 0);
delay = Math.abs(delay);
basic.pause(delay);
MotorStopAll()
}
//% blockId=robotbit_motor_run block="Motor|%index|speed %speed"
//% group="Motor" weight=59
//% speed.min=-255 speed.max=255
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function MotorRun(index: Motors, speed: number): void {
if (!initialized) {
initPCA9685()
}
speed = speed * 16; // map 255 to 4096
if (speed >= 4096) {
speed = 4095
}
if (speed <= -4096) {
speed = -4095
}
if (index > 4 || index <= 0)
return
let pp = (index - 1) * 2
let pn = (index - 1) * 2 + 1
if (speed >= 0) {
setPwm(pp, 0, speed)
setPwm(pn, 0, 0)
} else {
setPwm(pp, 0, 0)
setPwm(pn, 0, -speed)
}
}
/**
* Execute two motors at the same time
* @param motor1 First Motor; eg: M1A, M1B
* @param speed1 [-255-255] speed of motor; eg: 150, -150
* @param motor2 Second Motor; eg: M2A, M2B
* @param speed2 [-255-255] speed of motor; eg: 150, -150
*/
//% blockId=robotbit_motor_dual block="Motor|%motor1|speed %speed1|%motor2|speed %speed2"
//% group="Motor" weight=58
//% speed1.min=-255 speed1.max=255
//% speed2.min=-255 speed2.max=255
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function MotorRunDual(motor1: Motors, speed1: number, motor2: Motors, speed2: number): void {
MotorRun(motor1, speed1);
MotorRun(motor2, speed2);
}
/**
* Execute single motors with delay
* @param index Motor Index; eg: M1A, M1B, M2A, M2B
* @param speed [-255-255] speed of motor; eg: 150, -150
* @param delay seconde delay to stop; eg: 1
*/
//% blockId=robotbit_motor_rundelay block="Motor|%index|speed %speed|delay %delay|s"
//% group="Motor" weight=57
//% speed.min=-255 speed.max=255
//% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
export function MotorRunDelay(index: Motors, speed: number, delay: number): void {
MotorRun(index, speed);
basic.pause(delay * 1000);
MotorRun(index, 0);
}
//% blockId=robotbit_stop block="Motor Stop|%index|"
//% group="Motor" weight=56
export function MotorStop(index: Motors): void {
MotorRun(index, 0);
}
//% blockId=robotbit_stop_all block="Motor Stop All"
//% group="Motor" weight=55
//% blockGap=50
export function MotorStopAll(): void {
if (!initialized) {
initPCA9685()
}
for (let idx = 1; idx <= 4; idx++) {
stopMotor(idx);
}
}
//% blockId=robotbit_matrix_draw block="Matrix Draw|X %x|Y %y"
//% group="Modules" weight=44
export function MatrixDraw(x: number, y: number): void {
if (!initializedMatrix) {
matrixInit();
initializedMatrix = true;
}
x = Math.round(x)
y = Math.round(y)
let idx = y * 2 + Math.idiv(x, 8);
let tmp = matBuf[idx + 1];
tmp |= (1 << (x % 8));
matBuf[idx + 1] = tmp;
}
//% blockId=robotbit_matrix_refresh block="Matrix Refresh"
//% group="Modules" weight=43
export function MatrixRefresh(): void {
if (!initializedMatrix) {
matrixInit();
initializedMatrix = true;
}
matrixShow();
}
/*
//% blockId=robotbit_matrix_clean block="Matrix Clean|X %x|Y %y"
//% group="Servo" weight=68
export function MatrixClean(x: number, y: number): void {
if (!initializedMatrix) {
matrixInit();
initializedMatrix = true;
}
let idx = y * 2 + x / 8;
// todo: bitwise not throw err
matBuf[idx + 1] &=~(1 << (x % 8));
matrixShow();
}
*/
//% blockId=robotbit_matrix_clear block="Matrix Clear"
//% group="Modules" weight=42
//% blockGap=50
export function MatrixClear(): void {
if (!initializedMatrix) {
matrixInit();
initializedMatrix = true;
}
for (let i = 0; i < 16; i++) {
matBuf[i + 1] = 0;
}
matrixShow();
}
/**
* signal pin
* @param pin singal pin; eg: DigitalPin.P1
*/
//% blockId=robotbit_rgbultrasonic block="Ultrasonic(with RGB)|pin %pin"
//% group="Modules" weight=41
export function RgbUltrasonic(pin: DigitalPin): number {
pins.setPull(pin, PinPullMode.PullNone);
pins.digitalWritePin(pin, 0);
control.waitMicros(2);
pins.digitalWritePin(pin, 1);
control.waitMicros(10);
pins.digitalWritePin(pin, 0);
// read pulse
let d = pins.pulseIn(pin, PulseValue.High, 25000);
let ret = d;
// filter timeout spikes
if (ret == 0 && distanceBuf != 0) {
ret = distanceBuf;
}
distanceBuf = d;
return Math.floor(ret * 9 / 6 / 58);
}
/**
* signal pin
* @param pin singal pin; eg: DigitalPin.P1
* @param unit desired conversion unit
*/
//% blockId=robotbit_holeultrasonicver block="Ultrasonic(with LEGO Hole)|pin %pin|unit %unit"
//% group="Modules" weight=40
export function HoleUltrasonic(pin: DigitalPin, unit: ValueUnit): number {
pins.setPull(pin, PinPullMode.PullNone);
// pins.setPull(pin, PinPullMode.PullDown);
pins.digitalWritePin(pin, 0);
control.waitMicros(2);
pins.digitalWritePin(pin, 1);
control.waitMicros(10);
pins.digitalWritePin(pin, 0);
pins.setPull(pin, PinPullMode.PullUp);
// read pulse
let d = pins.pulseIn(pin, PulseValue.High, 30000);
let ret = d;
// filter timeout spikes
if (ret == 0 && distanceBuf != 0) {
ret = distanceBuf;
}
distanceBuf = d;
pins.digitalWritePin(pin, 0);
basic.pause(15)
if (parseInt(control.hardwareVersion()) == 2) {
d = ret *10 /58;
}
else{
// return Math.floor(ret / 40 + (ret / 800));
d = ret * 15 / 58;
}
switch (unit) {
case ValueUnit.Millimeter: return Math.floor(d)
case ValueUnit.Centimeters: return Math.floor(d/10)
default: return d;
}
}
}