-
Notifications
You must be signed in to change notification settings - Fork 105
/
maqueen.ts
1166 lines (1032 loc) · 33.5 KB
/
maqueen.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file pxt-maqueen/maqueen.ts
* @brief DFRobot's maqueen makecode library.
* @n [Get the module here](https://www.dfrobot.com.cn/goods-1802.html)
* @n This is a MakeCode graphical programming education robot.
*
* @copyright [DFRobot](http://www.dfrobot.com), 2016
* @copyright MIT Lesser General Public License
*
* @author [email]([email protected])
* @date 2019-10-08
*/
let maqueencb: Action
let maqueenmycb: Action
let maqueene = "1"
let maqueenparam = 0
let alreadyInit = 0
let IrPressEvent = 0
const MOTER_ADDRESSS = 0x10
enum PingUnit {
//% block="cm"
Centimeters,
}
enum state {
state1=0x10,
state2=0x11,
state3=0x20,
state4=0x21
}
interface KV {
key: state;
action: Action;
}
//%
//% weight=100 color=#008B00 icon="\uf136" block="Maqueen v4"
//% groups=['Maqueen_v4','Maqueen_v5']
namespace maqueen {
let kbCallback: KV[] = []
export class Packeta {
public mye: string;
public myparam: number;
}
export enum Motors {
//% blockId="left motor" block="left"
M1 = 0,
//% blockId="right motor" block="right"
M2 = 1,
//% blockId="all motor" block="all"
All = 2
}
export enum Servos {
//% blockId="S1" block="S1"
S1 = 0,
//% blockId="S2" block="S2"
S2 = 1
}
export enum Dir {
//% blockId="CW" block="Forward"
CW = 0x0,
//% blockId="CCW" block="Backward"
CCW = 0x1
}
export enum Patrol {
//% blockId="patrolLeft" block="left"
PatrolLeft = 13,
//% blockId="patrolRight" block="right"
PatrolRight = 14
}
export enum Patrol1 {
//% blockId="patrolLeft" block="left"
PatrolLeft = 0x10,
//% blockId="patrolRight" block="right"
PatrolRight = 0x20
}
export enum Voltage {
//%block="high"
High = 0x01,
//% block="low"
Low = 0x00
}
export enum LED {
//% blockId="LEDLeft" block="left"
LEDLeft = 8,
//% blockId="LEDRight" block="right"
LEDRight = 12
}
export enum LEDswitch {
//% blockId="turnOn" block="ON"
turnOn = 0x01,
//% blockId="turnOff" block="OFF"
turnOff = 0x00
}
/**
* Read the version number.
*/
//% weight=10
//% blockId=IR_read_version block="get product information"
export function IR_read_version(): string {
pins.i2cWriteNumber(0x10, 50, NumberFormat.UInt8BE);
let dataLen = pins.i2cReadNumber(0x10, NumberFormat.UInt8BE);
pins.i2cWriteNumber(0x10, 51, NumberFormat.UInt8BE);
let buf = pins.i2cReadBuffer(0x10, dataLen, false);
let version = "";
for (let index = 0; index < dataLen; index++) {
version += String.fromCharCode(buf[index])
}
return version
}
/**
* Read ultrasonic sensor.
*/
let state1 = 0;
//% blockId=ultrasonic_sensor block="read ultrasonic sensor |%unit "
//% weight=95
export function Ultrasonic(): number {
let data;
let i = 0;
data = readUlt(PingUnit.Centimeters);
if (state1 == 1 && data != 0) {
state1 = 0;
}
if (data != 0) {
} else {
if (state1 == 0) {
do {
data = readUlt(PingUnit.Centimeters);
i++;
if (i > 3) {
state1 = 1;
data = 500;
break;
}
} while (data == 0)
}
}
if (data == 0)
data = 500
return data;
}
function readUlt(unit: number): number {
let d
pins.digitalWritePin(DigitalPin.P1, 1);
basic.pause(1)
pins.digitalWritePin(DigitalPin.P1, 0);
if (pins.digitalReadPin(DigitalPin.P2) == 0) {
pins.digitalWritePin(DigitalPin.P1, 0);
pins.digitalWritePin(DigitalPin.P1, 1);
basic.pause(20)
pins.digitalWritePin(DigitalPin.P1, 0);
d = pins.pulseIn(DigitalPin.P2, PulseValue.High, 500 * 58);//readPulseIn(1);
} else {
pins.digitalWritePin(DigitalPin.P1, 1);
pins.digitalWritePin(DigitalPin.P1, 0);
basic.pause(20)
pins.digitalWritePin(DigitalPin.P1, 0);
d = pins.pulseIn(DigitalPin.P2, PulseValue.Low, 500 * 58);//readPulseIn(0);
}
let x = d / 59;
switch (unit) {
case PingUnit.Centimeters: return Math.round(x);
default: return Math.idiv(d, 2.54);
}
}
/**
* Set the direction and speed of Maqueen motor.
*/
//% weight=90
//% blockId=motor_MotorRun block="motor|%index|move|%Dir|at speed|%speed"
//% speed.min=0 speed.max=255
//% index.fieldEditor="gridpicker" index.fieldOptions.columns=2
//% direction.fieldEditor="gridpicker" direction.fieldOptions.columns=2
export function motorRun(index: Motors, direction: Dir, speed: number): void {
let buf = pins.createBuffer(3);
if (index == 0) {
buf[0] = 0x00;
buf[1] = direction;
buf[2] = speed;
pins.i2cWriteBuffer(0x10, buf);
}
if (index == 1) {
buf[0] = 0x02;
buf[1] = direction;
buf[2] = speed;
pins.i2cWriteBuffer(0x10, buf);
}
if (index == 2) {
buf[0] = 0x00;
buf[1] = direction;
buf[2] = speed;
pins.i2cWriteBuffer(0x10, buf);
buf[0] = 0x02;
pins.i2cWriteBuffer(0x10, buf);
}
}
/**
* Stop the Maqueen motor.
*/
//% weight=20
//% blockId=motor_motorStop block="motor |%motors stop"
//% motors.fieldEditor="gridpicker" motors.fieldOptions.columns=2
export function motorStop(motors: Motors): void {
let buf = pins.createBuffer(3);
if (motors == 0) {
buf[0] = 0x00;
buf[1] = 0;
buf[2] = 0;
pins.i2cWriteBuffer(0x10, buf);
}
if (motors == 1) {
buf[0] = 0x02;
buf[1] = 0;
buf[2] = 0;
pins.i2cWriteBuffer(0x10, buf);
}
if (motors == 2) {
buf[0] = 0x00;
buf[1] = 0;
buf[2] = 0;
pins.i2cWriteBuffer(0x10, buf);
buf[0] = 0x02;
pins.i2cWriteBuffer(0x10, buf);
}
}
/**
* Read line tracking sensor.
*/
//% weight=20
//% blockId=read_Patrol block="read |%patrol line tracking sensor"
//% patrol.fieldEditor="gridpicker" patrol.fieldOptions.columns=2
export function readPatrol(patrol: Patrol): number {
if (patrol == Patrol.PatrolLeft) {
return pins.digitalReadPin(DigitalPin.P13)
} else if (patrol == Patrol.PatrolRight) {
return pins.digitalReadPin(DigitalPin.P14)
} else {
return -1
}
}
/**
* Turn on/off the LEDs.
*/
//% weight=20
//% blockId=writeLED block="LEDlight |%led turn |%ledswitch"
//% led.fieldEditor="gridpicker" led.fieldOptions.columns=2
//% ledswitch.fieldEditor="gridpicker" ledswitch.fieldOptions.columns=2
export function writeLED(led: LED, ledswitch: LEDswitch): void {
if (led == LED.LEDLeft) {
pins.digitalWritePin(DigitalPin.P8, ledswitch)
} else if (led == LED.LEDRight) {
pins.digitalWritePin(DigitalPin.P12, ledswitch)
} else {
return
}
}
/**
* Set the Maqueen servos.
*/
//% weight=90
//% blockId=servo_ServoRun block="servo|%index|angle|%angle"
//% angle.min=0 angle.max=180
//% index.fieldEditor="gridpicker" index.fieldOptions.columns=2
export function servoRun(index: Servos, angle: number): void {
let buf = pins.createBuffer(2);
if (index == 0) {
buf[0] = 0x14;
}
if (index == 1) {
buf[0] = 0x15;
}
buf[1] = angle;
pins.i2cWriteBuffer(0x10, buf);
}
/**
* Line tracking sensor event function
*/
//% weight=2
//% blockId=kb_event block="on|%value line tracking sensor|%vi"
export function ltEvent(value: Patrol1, vi: Voltage, a: Action) {
let state = value + vi;
serial.writeNumber(state)
let item: KV = { key: state, action: a };
kbCallback.push(item);
}
let x: number
let i: number = 1;
function patorlState(): number {
switch (i) {
case 1: x = pins.digitalReadPin(DigitalPin.P13) == 0 ? 0x10 : 0; break;
case 2: x = pins.digitalReadPin(DigitalPin.P13) == 1 ? 0x11 : 0; break;
case 3: x = pins.digitalReadPin(DigitalPin.P14) == 0 ? 0x20 : 0; break;
default: x = pins.digitalReadPin(DigitalPin.P14) == 1 ? 0x21 : 0; break;
}
i += 1;
if (i == 5) i = 1;
return x;
}
basic.forever(() => {
if (kbCallback != null) {
let sta = patorlState();
if (sta != 0) {
for (let item of kbCallback) {
if (item.key == sta) {
item.action();
}
}
}
}
basic.pause(50);
})
}
/*'''''''''''''''''''''''''''''''''' maqueen V5 ''''''''''''''''''''''''''''''''''''''''''''''''''*/
//% group="Maqueen_v5"
//% weight=100 color=#0fbc11 icon="\uf48b" block="Maqueen v5"
namespace Maqueen_V5 {
let neopixel_buf = pins.createBuffer(16 * 3);
for (let i = 0; i < 16 * 3; i++) {
neopixel_buf[i] = 0
}
let _brightness = 255
interface KV1 {
key: number;
key1: number;
action: Action;
}
let kbCallback1: KV1[] = [];
let kbCallback2: KV[] = [];
const I2CADDR = 0x10;
let servo1_num:number = 20;
let servo2_num: number = 20;
export enum Patrolling {
//% block="ON"
ON = 1,
//% block="OFF"
OFF = 2,
}
export enum PatrolSpeed {
//% block="1"
speed1 = 1,
//% block="2"
speed2 = 2,
//% block="3"
speed3 = 3,
}
export enum Motors {
//% blockId="left motor" block="left"
M1 = 0,
//% blockId="right motor" block="right"
M2 = 1,
//% blockId="all motor" block="all"
All = 2
}
export enum Servos {
//% blockId="S1" block="S1"
S1 = 0,
//% blockId="S2" block="S2"
S2 = 1
}
export enum Patrol {
//% blockId="patrolLeft" block="L1"
L1 = 1,
//% blockId="patrolMiddle" block="M"
M = 2,
//% blockId="patrolRight" block="R1"
R1 = 3
}
export enum SpeedGrade {
//% block="1"
speed1 = 1,
//% block="2"
speed2 = 2,
//% block="3"
speed3 = 3,
//% block="4"
speed4 = 4,
//% block="5"
speed5 = 5
}
export enum Dir {
//% blockId="CW" block="Forward"
CW = 0x0,
//% blockId="CCW" block="Backward"
CCW = 0x1
}
export enum Voltage {
//%block="high"
High = 0x01,
//% block="low"
Low = 0x00
}
export enum DirectionType {
//% block="left led light"
Left = 0,
//% block="right led light"
Right = 1,
//% block="all led light"
All = 2,
}
export enum DirectionType2 {
//% block="left led light"
Left = 0,
//% block="right led light"
Right = 1,
}
export enum BatteryType {
//% block="Alkaline battery"
Alkaline = 1,
//% block="Lithium battery"
Lithium = 0,
}
/**
* Well known colors
*/
export enum NeoPixelColors {
//% block=red
Red = 0xFF0000,
//% block=orange
Orange = 0xFFA500,
//% block=yellow
Yellow = 0xFFFF00,
//% block=green
Green = 0x00FF00,
//% block=blue
Blue = 0x0000FF,
//% block=indigo
Indigo = 0x4b0082,
//% block=violet
Violet = 0x8a2be2,
//% block=purple
Purple = 0xFF00FF,
//% block=white
White = 0xFFFFFF,
//% block=black
Black = 0x000000
}
export enum CarLightColors {
//% block=red
Red = 1,
//% block=green
Green = 2,
//% block=yellow
Yellow = 3,
//% block=blue
Blue = 4,
//% block=purple
Purple = 5,
//% block=cyan
Cyan = 6,
//% block=white
White = 7,
//% block=black
Black = 8
}
const enum BleCmd {
BleForward = 1, /**< advance */
BleBackward = 2, /**< astern */
BleLeft = 3, /**< turn left */
BleRight = 4, /**< turn right */
BleRgbR = 5, /**< Red */
BleRgbG = 6, /**< Green */
BleRgbB = 7, /**< Blue */
BleRgbRB = 8, /**< Purple */
BleRgbRG = 9, /**< Yellow */
BleRgbGB = 10, /**< Cyan */
BleRgbRGB = 11, /**< White */
BleRgbOff = 12, /**< Off */
BleServo1Right = 13, /**< servo1 turn right */
BleServo1Left = 14, /**< servo1 turn left */
BleServo2Right = 15, /**< servo2 turn right */
BleServo2Left = 16, /**< servo2 turn left */
};
const MOTOR_0 =0
const SPEED_0 =1
const MOTOR_1 =2
const SPEED_1 =3
const RGB_L =11
const RGB_R =12
const RGB_BLINK_NUM_L =13
const RGB_BLINK_GRADE_L =14
const RGB_BLINK_NUM_R =15
const RGB_BLINK_GRADE_R =16
const RGB_GRADUAL_CHANGE_GRADE_L =17
const RGB_GRADUAL_CHANGE_GRADE_R =18
const SERVO_1 =20
const SERVO_2 =21
const BLACK_ADC_STATE =29
const ADC_COLLECT_0 =30
const ADC_COLLECT_1 =32
const ADC_COLLECT_2 =34
const ADC_COLLECT_3 =36
const ADC_COLLECT_4 =38
const LIGHTL_H =41
const LIGHTL_L =42
const LIGHTR_H =43
const LIGHTR_L =44
const BATTERY_SET =45
const BATTERY =46
const MOTOR_TYPE_H =47
const MOTOR_TYPE_L =48
const VERSON_LEN =50
const VERSON_DATA =51
const MY_SYS_INIT =70
const LINE_WALKING =71
const LINE_SPEED_GRADE =72
const CAR_STATE =73
const CROSS_DEFAULT =75
const T1_DEFAULT =76
const T2_DEFAULT =77
const T3_DEFAULT =78
const BLECMD =80
const BLEEN = 81
/**
* Init I2C until success
*/
//% weight=255
//% blockId=I2CInit block="initialize via I2C until success"
//%group="Maqueen_v5"
export function I2CInit(): void {
let versionLen = 0;
let allBuffer = pins.createBuffer(2);
allBuffer[0] = 0x46;
allBuffer[1] = 1;
pins.i2cWriteBuffer(I2CADDR, allBuffer); //V5 systemInit
basic.pause(100);//waiting reset
pins.i2cWriteNumber(I2CADDR, 0x32, NumberFormat.Int8LE);
versionLen = pins.i2cReadNumber(I2CADDR, NumberFormat.Int8LE);
while (versionLen == 0) {
basic.showLeds(`
# . . . #
. # . # .
. . # . .
. # . # .
# . . . #
`, 10)
basic.pause(500)
basic.clearScreen()
pins.i2cWriteNumber(I2CADDR, 0x32, NumberFormat.Int8LE);
versionLen = pins.i2cReadNumber(I2CADDR, NumberFormat.Int8LE);
}
basic.showLeds(`
. . . . .
. . . . #
. . . # .
# . # . .
. # . . .
`, 10)
basic.pause(500)
basic.clearScreen()
}
function I2CWirte(Reg:number,data:number){
let allBuffer = pins.createBuffer(2);
allBuffer[0] = Reg;
allBuffer[1] = data;
pins.i2cWriteBuffer(I2CADDR, allBuffer)
}
/**
* Start or close the line patrol
* @param patrol to patrol ,eg: Patrolling.ON
*/
//% weight=253
//% blockId=patrolling block="Line patrolling| %Patrolling"
//% group="Maqueen_v5"
export function patrolling(patrol: Patrolling){
let allBuffer = pins.createBuffer(2);
if (patrol == Patrolling.ON)
allBuffer[1] = 1
else
allBuffer[1] = 0;
allBuffer[0] = LINE_WALKING;
pins.i2cWriteBuffer(I2CADDR, allBuffer)
}
/**
* Set the tracking speed
* @param speed of Line patrol
//% weight=254
//% blockId=patrolSpeed block="set the speed of Line patrol| %PatrolSpeed"
//% group="Maqueen_v5"
export function patrolSpeed(speed: PatrolSpeed) {
let allBuffer = pins.createBuffer(2);
allBuffer[0] = LINE_SPEED_GRADE;
allBuffer[1] = speed;
pins.i2cWriteBuffer(I2CADDR, allBuffer)
}
*/
/**
* Control motor module running
* @param motor Motor selection enumeration
* @param dir Motor direction selection enumeration
* @param speed Motor speed control, eg:100
*/
//% weight=252
//% blockId= V5_motor block="motor|%index|move|%Dir|at speed|%speed"
//% speed.min=0 speed.max=255
//% index.fieldEditor="gridpicker" index.fieldOptions.columns=2
//% direction.fieldEditor="gridpicker" direction.fieldOptions.columns=2
//% group="Maqueen_v5"
export function motorRun(index: Motors, direction: Dir, speed: number): void {
let buf = pins.createBuffer(3);
if (index == 0) {
buf[0] = MOTOR_0;
buf[1] = direction;
buf[2] = speed;
pins.i2cWriteBuffer(MOTER_ADDRESSS, buf);
}
if (index == 1) {
buf[0] = MOTOR_1;
buf[1] = direction;
buf[2] = speed;
pins.i2cWriteBuffer(MOTER_ADDRESSS, buf);
}
if (index == 2) {
buf[0] = MOTOR_0;
buf[1] = direction;
buf[2] = speed;
pins.i2cWriteBuffer(MOTER_ADDRESSS, buf);
buf[0] = MOTOR_1;
pins.i2cWriteBuffer(MOTER_ADDRESSS, buf);
}
}
/**
* Read the version number.
*/
//% weight=1
//% blockId=readVersion block="get product information"
//% group="Maqueen_v5"
export function readVersion(): string {
pins.i2cWriteNumber(I2CADDR, 50, NumberFormat.UInt8BE);
let dataLen = pins.i2cReadNumber(I2CADDR, NumberFormat.UInt8BE);
pins.i2cWriteNumber(I2CADDR, 51, NumberFormat.UInt8BE);
let buf = pins.i2cReadBuffer(I2CADDR, dataLen, false);
let version = "";
for (let index = 0; index < dataLen; index++) {
version += String.fromCharCode(buf[index])
}
return version
}
/**
* Read ultrasonic sensor(uint cm).
*/
let state1 = 0;
//% blockId=V5_ultrasonic_sensor block="read ultrasonic sensor |%unit "
//% weight=95
//% group="Maqueen_v5"
export function Ultrasonic(): number {
let data;
let i = 0;
data = readUlt(PingUnit.Centimeters);
if (state1 == 1 && data != 0) {
state1 = 0;
}
if (data != 0) {
} else {
if (state1 == 0) {
do {
data = readUlt(PingUnit.Centimeters);
i++;
if (i > 3) {
state1 = 1;
data = 500;
break;
}
} while (data == 0)
}
}
if (data == 0)
data = 500
return data;
}
function readUlt(unit: number): number {
let d
pins.digitalWritePin(DigitalPin.P1, 1);
basic.pause(1)
pins.digitalWritePin(DigitalPin.P1, 0);
if (pins.digitalReadPin(DigitalPin.P2) == 0) {
pins.digitalWritePin(DigitalPin.P1, 0);
pins.digitalWritePin(DigitalPin.P1, 1);
basic.pause(20)
pins.digitalWritePin(DigitalPin.P1, 0);
d = pins.pulseIn(DigitalPin.P2, PulseValue.High, 500 * 58);//readPulseIn(1);
} else {
pins.digitalWritePin(DigitalPin.P1, 1);
pins.digitalWritePin(DigitalPin.P1, 0);
basic.pause(20)
pins.digitalWritePin(DigitalPin.P1, 0);
d = pins.pulseIn(DigitalPin.P2, PulseValue.Low, 500 * 58);//readPulseIn(0);
}
let x = d / 59;
switch (unit) {
case PingUnit.Centimeters: return Math.round(x);
default: return Math.idiv(d, 2.54);
}
}
/**
* Control the motor module to stop running
* @param emotor Motor selection enumeration
*/
//% weight=240
//% blockId=V5_motorStop block="motor |%motors stop"
//% motors.fieldEditor="gridpicker" motors.fieldOptions.columns=2
//% group="Maqueen_v5"
export function motorStop(motors: Motors): void {
let buf = pins.createBuffer(3);
if (motors == 0) {
buf[0] = MOTOR_0;
buf[1] = 0;
buf[2] = 0;
pins.i2cWriteBuffer(I2CADDR, buf);
}
if (motors == 1) {
buf[0] = MOTOR_1;
buf[1] = 0;
buf[2] = 0;
pins.i2cWriteBuffer(I2CADDR, buf);
}
if (motors == 2) {
buf[0] = MOTOR_0;
buf[1] = 0;
buf[2] = 0;
pins.i2cWriteBuffer(I2CADDR, buf);
buf[0] = MOTOR_1;
pins.i2cWriteBuffer(I2CADDR, buf);
}
}
/**
* Get the state of the patrol sensor. placing the cart on white paper returns 0 and placing the cart on air/black paper returns 1
* @param eline Select the inspection sensor enumeration
*/
//% weight=20
//% blockId=readPatrol block="read line sensor |%Patrol state"
//% group="Maqueen_v5"
export function readPatrol(patrol: Patrol): number {
pins.i2cWriteNumber(I2CADDR, BLACK_ADC_STATE, NumberFormat.UInt8BE);
let buf = pins.i2cReadNumber(I2CADDR, NumberFormat.UInt8BE, false);
if (buf & (1 << (3 - patrol)))
return 1;
else
return 0;
}
/**
* The ADC data of the patrol sensor is obtained
* @param eline Select the inspection sensor enumeration
*/
//% weight=21
//% blockId= readPatrolData block="read line sensor |%Patrol ADC data"
//% patrol.fieldEditor="gridpicker" patrol.fieldOptions.columns=2
//% group="Maqueen_v5"
export function readPatrolData(patrol: Patrol): number {
let data;
switch (patrol) {
case Patrol.L1:
pins.i2cWriteNumber(I2CADDR, ADC_COLLECT_1, NumberFormat.Int8LE);
let adc0Buffer = pins.i2cReadBuffer(I2CADDR, 2);
data = adc0Buffer[0] << 8 | adc0Buffer[1]
break;
case Patrol.M:
pins.i2cWriteNumber(I2CADDR, ADC_COLLECT_2, NumberFormat.Int8LE);
let adc1Buffer = pins.i2cReadBuffer(I2CADDR, 2);
data = adc1Buffer[0] << 8 | adc1Buffer[1];
break;
case Patrol.R1:
pins.i2cWriteNumber(I2CADDR, ADC_COLLECT_3, NumberFormat.Int8LE);
let adc2Buffer = pins.i2cReadBuffer(I2CADDR, 2);
data = adc2Buffer[0] << 8 | adc2Buffer[1];
break;
default:
data=0;
break;
}
return data;
}
/**
* Control the Maqueen steering Angle(0-180)
* @param eline Select the inspection sensor enumeration
*/
//% weight=90
//% blockId=V5_Servo block="servo|%index|angle|%angle"
//% angle.min=0 angle.max=180
//% index.fieldEditor="gridpicker" index.fieldOptions.columns=2
//% group="Maqueen_v5"
export function servoRun(index: Servos, angle: number): void {
let buf = pins.createBuffer(2);
if (index == 0) {
buf[0] = SERVO_1;
}
if (index == 1) {
buf[0] = SERVO_2;
}
buf[1] = angle;
pins.i2cWriteBuffer(I2CADDR, buf);
}
/**
* Sets the color of the RGB lamp
* @param type to type ,eg: DirectionType.Left
* @param rgb to rgb ,eg: CarLightColors.Red
*/
//% block="RGB Car Lights %type color %rgb"
//% weight=11
//% group="Maqueen_v5"
export function setRgblLed(type: DirectionType, rgb: CarLightColors) {
let allBuffer = pins.createBuffer(2);
allBuffer[1] = rgb;
if (type == DirectionType.Left) {
allBuffer[0] = RGB_L;
pins.i2cWriteBuffer(I2CADDR, allBuffer)
} else if (type == DirectionType.Right) {
allBuffer[0] = RGB_R;
pins.i2cWriteBuffer(I2CADDR, allBuffer)
} else if (type == DirectionType.All) {
allBuffer[0] = RGB_L;
pins.i2cWriteBuffer(I2CADDR, allBuffer)
allBuffer[0] = RGB_R;
pins.i2cWriteBuffer(I2CADDR, allBuffer)
}
}
/**
* Set RGB lights to flash
* @param type to type ,eg: DirectionType.Left
* @param num of flashes
* @param grade Select the speed level enumeration
* @param rgb Select color
*/
//% inlineInputMode=inline
//% block="RGB Car Lights |%type Number of flashes |%number Level of flashes |%SpeedGrade color |%rgb"
//% weight=11
//% num.min=0 num.max=255
//% group="Maqueen_v5"
export function setRgbBlink(type: DirectionType, num: number, grade: SpeedGrade, rgb: CarLightColors) {
if (type == DirectionType.Left) {
I2CWirte(RGB_L, rgb);
I2CWirte(RGB_BLINK_GRADE_L, grade);
I2CWirte(RGB_BLINK_NUM_L, num);
} else if (type == DirectionType.Right) {
I2CWirte(RGB_R, rgb);
I2CWirte(RGB_BLINK_GRADE_R, grade);
I2CWirte(RGB_BLINK_NUM_R, num);
} else if (type == DirectionType.All) {
I2CWirte(RGB_L, rgb);
I2CWirte(RGB_BLINK_GRADE_L, grade);
I2CWirte(RGB_BLINK_NUM_L, num);
I2CWirte(RGB_R, rgb);
I2CWirte(RGB_BLINK_GRADE_R, grade);
I2CWirte(RGB_BLINK_NUM_R, num);
}
}
/**
* Set the RGB light gradient
* @param type to type ,eg: DirectionType.Left
* @param Select the speed level enumeration
*/
//% block="RGB Car Lights |%type Level of change |%SpeedGrade"
//% weight=11
//% group="Maqueen_v5"
export function setRgbchange(type: DirectionType, grade: SpeedGrade) {
if (type == DirectionType.Left) {
I2CWirte(RGB_GRADUAL_CHANGE_GRADE_L, grade);
} else if (type == DirectionType.Right) {
I2CWirte(RGB_GRADUAL_CHANGE_GRADE_R, grade);
}
else if (type == DirectionType.All) {
I2CWirte(RGB_GRADUAL_CHANGE_GRADE_L, grade);
I2CWirte(RGB_GRADUAL_CHANGE_GRADE_R, grade);
}
}
/**
* Turn off RGB
* @param type to type ,eg: DirectionType.Left
*/
//% block="Close |%type RGB Car Lights "
//% weight=11
//% group="Maqueen_v5"
export function setRgbOff(type: DirectionType) {
setRgblLed(type,CarLightColors.Black);
}
/**