forked from LuLucas233/wdcm_tmp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcustom.ts
1156 lines (1087 loc) · 36.7 KB
/
custom.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
/**
wondercam package
*/
//% weight=100 color=#FF8844 icon=""
namespace wondercam {
export enum Functions {
//% block="NoFunction"
NoFunction,
//% block="Facial recognition"
FaceDetect = 1,
//% block="Object detection"
ObjectDetect,
//% block="Classification"
Classification,
//% block="FeatureLearning"
FeatureLearning,
//% block="ColorDetect"
ColorDetect,
//% block="LineFollowing"
LineFollowing,
//% block="AprilTag"
AprilTag,
//% block="QrcodeScan"
QrcodeScan,
//% block="BarcodeScan"
BarcodeScan,
//% block="Number Recognition"
NumberRecognition,
//% block="Landmark Recognition"
LandmarkRecognition,
}
export enum Landmarks {
//% bock="NoLandmark"
NoLandmark,
//% block="Go forward"
GoForward = 1,
//% block="Turn left"
TurnLeft = 2,
//% block="Turn right"
TurnRight = 3,
//% block="Turn About"
TurnAbout = 4,
//% block="Stop"
Stop = 5,
}
export enum Objects {
//% block="Aeroplane"
Aeroplane = 1,
//% block="Bicycle"
Bicycle,
//% block="Bird"
Bird,
//% block="Boar"
Boar,
//% block="Bootle"
Bootle,
//% block="Bus"
Bus,
//% block="Car"
Car,
//% block="Cat"
Cat,
//% block="Chair"
Chair,
//% block="Cow"
Cow,
//% block="Diningtable"
Diningtable,
//% block="Dog"
Dog,
//% block="Horse"
Horse,
//% block="Motorbike"
Motorbike,
//% block="Person"
Person,
//% block="Pottedplant"
Pottedplant,
//% block="Sheep"
Sheep,
//% block="Sofa"
Sofa,
//% block="Train"
Train,
//% block="TvMonitorn"
TvMonitorn
}
export enum Options {
//% block="X"
Pos_X = 0,
//% block="Y"
Pos_Y = 0x02,
//% block="Width"
Width = 0x04,
//% block="Height"
Height = 0x06
}
export enum Obj_Options {
//% block="X"
Pos_X = 0,
//% block="Y"
Pos_Y = 0x02,
//% block="Width"
Width = 0x04,
//% block="Height"
Height = 0x06,
//% block="Confidence"
Confidence = 0x08
}
export enum Line_Options {
//% block="Start X"
Start_X = 0x00,
//% block="Start Y"
Start_Y = 0x02,
//% block="End X"
END_X = 0x04,
//% block="End Y"
END_Y = 0x06,
//% block="Angle"
Angle = 0x08,
//% block="Offset"
Offset = 0x0A
}
export enum AprilTag_Options {
//% block="Center X"
Pos_X = 0x00,
//% block="Center Y"
Pos_Y = 0x02,
//% block="W"
Width = 0x04,
//% block="H"
Height = 0x06,
//% block="X Translation"
X_T = 0x08,
//% block="X Rotation"
X_R = 0x0A,
//% block="Y Translation"
Y_T = 0x0C,
//% block="Y Rotation"
Y_R = 0x0E,
//% block="Z Translation"
Z_T = 0x10,
//% block="Z Rotation"
Z_R = 0x12,
}
export enum LED_STATE {
//% block="ON"
ON = 1,
//% block="OFF"
OFF = 0
}
export enum DEV_ADDR {
//% block="0x32"
x32 = 0x32,
//% block="0x21"
x21 = 0x21,
//% block="0x22"
x22 = 0x22,
//% block="0x23"
x23 = 0x23,
//% block="0x24"
x24 = 0x24,
//% block="0x31"
x31 = 0x31,
//% block="0x33"
x33 = 0x33,
//% block="0x34"
x34 = 0x34,
//% block="0x41"
x41 = 0x41,
//% block="0x42"
x42 = 0x42,
//% block="0x43"
x43 = 0x43,
//% block="0x44"
x44 = 0x44,
//% block="0x51"
x51 = 0x45,
//% block="0x52"
x52 = 0x52,
//% block="0x53"
x53 = 0x53,
//% block="0x54"
x54 = 0x54,
}
let Current = Functions.NoFunction;
let ResultBuf: Buffer;
let WONDERCAM_I2C_ADDR = 0x32
function i2cwrite(reg: number, value: number) {
let buf = pins.createBuffer(3)
buf.setNumber(NumberFormat.UInt8LE, 0, reg & 0xFF)
buf.setNumber(NumberFormat.UInt8LE, 1, (reg >> 8) & 0xFF)
buf.setNumber(NumberFormat.UInt8LE, 2, value & 0xFF)
pins.i2cWriteBuffer(WONDERCAM_I2C_ADDR, buf)
}
function i2creadtobuf(reg: number, length: number): Buffer {
let buf = pins.createBuffer(2)
buf.setNumber(NumberFormat.UInt8LE, 0, reg & 0xFF)
buf.setNumber(NumberFormat.UInt8LE, 1, (reg >> 8) & 0xFF)
pins.i2cWriteBuffer(WONDERCAM_I2C_ADDR, buf)
return pins.i2cReadBuffer(WONDERCAM_I2C_ADDR, length)
}
function i2creadnum(reg: number): number {
let buf = i2creadtobuf(reg, 1)
return buf.getNumber(NumberFormat.UInt8LE, 0)
}
/**
* TODO:初始化I2C, 初始化WonderCam
* @param dev_addr i2c address, eg: DEV_ADDR.x32
*/
//% weight=180
//% block="Initialize WonderCam at |$dev_addr|"
export function wondercam_init(dev_addr = DEV_ADDR.x32): void {
WONDERCAM_I2C_ADDR = dev_addr
while (i2creadnum(0) != 'v'.charCodeAt(0)) {
basic.showString("E")
}
basic.clearScreen()
}
/**
* TODO: 获取WonderCam正在运行的功能,返回当前运行功能的序号
*/
//% weight=145
//% block="The running function"
export function CurrentFunc(): Functions {
return i2creadnum(0x0035)
}
/**
* TODO: 判断当前运行的功能是否是某个功能
*/
//% weight=149
//% block="Is the running function |$func|?"
//% func.defl=Functions.FaceDetect
export function CurrentFuncIs(func: Functions): boolean {
if (i2creadnum(0x0035) == func) {
return true
}
return false
}
/**
* TODO: 获取不同功能对应的功能序号
*/
//% weight=1
//% block="$func"
//% func.defl=Functions.FaceDetect
export function FunctoNum(func: Functions): number {
return func
}
/**
* TODO: 切换功能
*/
//% weight=140
//% block="Switch to $newfunc"
//% newfunc.defl=Functions.FaceDetect
export function ChangeFunc(newfunc: Functions): void {
let count = 0;
i2cwrite(0x0035, newfunc);
basic.pause(100)
while (true) {
if (CurrentFuncIs(newfunc)) {
break
} else {
if (count >= 80) {
break;
}
basic.pause(50)
count++
}
}
}
/**
* TODO: 开关LED
*/
//% weight=100
//% block="Turn |$newstate| led"
//% newstate.defl=LED_STATE.ON
export function TurnOnOrOffLed(newstate: LED_STATE): void {
i2cwrite(0x0030, newstate);
}
/**
* TODO: 设置LED亮度
*/
//% weight=90
//% block="Set led brightness as $newlevel"
//% newlevel.defl=100 newlevel.min=0 newlevel.max=100
export function SetLedBrightness(newlevel: number): void {
i2cwrite(0x0031, newlevel);
}
/**
* TODO: 更新WonderCam的处理结果
*/
//% weight=120
//% block="Update and get results"
export function UpdateResult(): void {
let func = CurrentFunc()
switch (func) {
case Functions.FaceDetect: //人脸识别 结果地址
ResultBuf = i2creadtobuf(0x0400, 512)
Current = Functions.FaceDetect;
break;
case Functions.ObjectDetect: //物品识别 结果地址
ResultBuf = i2creadtobuf(0x0800, 512)
Current = Functions.ObjectDetect
break;
case Functions.Classification: //图像分类 结果地址
ResultBuf = i2creadtobuf(0x0C00, 128)
Current = Functions.Classification
break;
case Functions.NumberRecognition: //图像分类 结果地址
ResultBuf = i2creadtobuf(0x0D00, 128)
Current = Functions.NumberRecognition
break;
case Functions.LandmarkRecognition: //图像分类 结果地址
ResultBuf = i2creadtobuf(0x0D80, 128)
Current = Functions.LandmarkRecognition
break;
case Functions.FeatureLearning: //特征学习 结果地址
ResultBuf = i2creadtobuf(0x0E00, 128)
Current = Functions.FeatureLearning
break;
case Functions.ColorDetect: // 颜色识别 结果地址
ResultBuf = i2creadtobuf(0x1000, 400)
Current = Functions.ColorDetect
break;
case Functions.LineFollowing: //视觉巡线 结果地址
ResultBuf = i2creadtobuf(0x1400, 256)
Current = Functions.LineFollowing
break;
case Functions.AprilTag:
ResultBuf = i2creadtobuf(0x1E00, 512);
Current = Functions.AprilTag
break;
case Functions.QrcodeScan: //QRCODE 结果地址
ResultBuf = i2creadtobuf(0x1800, 512)
Current = Functions.QrcodeScan
break;
case Functions.BarcodeScan: //BAR CODE 结果地址
ResultBuf = i2creadtobuf(0x1C00, 512)
Current = Functions.BarcodeScan
break;
default:
Current = Functions.NoFunction
break;
}
}
/**
* TODO: 是否检测到了人脸
*/
//% weight=160
//% block="Is any face detected?"
//% subcategory="Facial recognition"
export function IsDetectFace(): boolean {
if (Current == Functions.FaceDetect) {
if (ResultBuf.getNumber(NumberFormat.UInt8LE, 1) > 0) {
return true
}
}
return false
}
/**
* TODO: 获取识别到的人脸个数
*/
//% weight=150
//% block="Total number of detected faces"
//% subcategory="Facial recognition"
//% subcategory.loc.zh="人脸识别"
export function FaceNum(): number {
if (Current == Functions.FaceDetect) {
return ResultBuf.getNumber(NumberFormat.UInt8LE, 1);
}
return 0;
}
/**
* TODO: 是否识别到已经学习的人脸
*/
//% weight=140
//% block="Is any learned face recognized?"
//% subcategory="Facial recognition"
export function IsDetectedLearnedFace(): boolean {
if (Current == Functions.FaceDetect) {
if (ResultBuf.getNumber(NumberFormat.UInt8LE, 2) > 0) {
return true;
}
}
return false;
}
/**
* TODO: 获取识别到的已经学习的人脸个数
*/
//% weight=135
//% block="Number of learned faces recognized"
//% subcategory="Facial recognition"
export function LearnedFaceNum(): number {
if (Current == Functions.FaceDetect) {
return ResultBuf.getNumber(NumberFormat.UInt8LE, 2);
}
return 0;
}
/**
* TODO: 获取识别到的未学习的人脸个数
*/
//% weight=130
//% block="Is any unlearned face detected?"
//% subcategory="Facial recognition"
export function IsDetectUnLeanedFace(): boolean {
if (Current == Functions.FaceDetect) {
if (ResultBuf.getNumber(NumberFormat.UInt8LE, 3) > 0) {
return true;
}
}
return false;
}
/**
* TODO: 获取识别到的未学习的人脸个数
*/
//% weight=120
//% block="Number of unlearned faces detected"
//% subcategory="Facial recognition"
export function UnLearnedFaceNum(): number {
if (Current == Functions.FaceDetect) {
return ResultBuf.getNumber(NumberFormat.UInt8LE, 3);
}
return 0;
}
/**
* TODO: 是否识别到了指定ID的人脸
* @param id[1-5] eg: 1
*/
//% weight=110
//% block="Is the face ID:$id recognized"
//% subcategory="Facial recognition"
export function IsDetectedFace(id: number): boolean {
if (Current == Functions.FaceDetect) {
for (let i = 4; i < 4 + 29; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
return true;
}
}
}
return false;
}
/**
* TODO: 获返回指定ID的人脸的位置数据。若成功返回数据,失败返回0
*/
//% weight=95
//% block="$opt of face ID: $id"
//% id.defl=1 id.min=1 id.max=5
//% opt.defl=Options.Pos_X
//% subcategory="Facial recognition"
export function getlearnedFaceY(opt: Options, id: number): number {
for (let i = 4; i < 4 + 29; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
let index = i - 4;
return ResultBuf.getNumber(NumberFormat.Int16LE, (0x30 + opt) + index * 16)
}
}
return 0;
}
/**
* TODO: 获返回指定Index的未学习的人脸的位置数据。若成功返回数据,失败返回0
*/
//% weight=13
//% block="|$opt| of the no.|$index| unlearned face recognized"
//% index.defl=1 index.min=1 index.max=20
//% opt.defl=Options.Pos_X
//% subcategory="Facial recognition"
export function getUnlearnedFaceX(opt: Options, index: number): number {
let num = 0;
for (let i = 4; i < 4 + 29; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == 0xFF) {
num += 1
if (num == index) {
return ResultBuf.getNumber(NumberFormat.Int16LE, (0x30 + opt) + (i - 4) * 16)
}
}
}
return 0;
}
/**
* TODO: 是否识别到了物品
*/
//% weight=100 block="Is any object detected?"
//% subcategory="Object detection"
export function IsDetectObject(): boolean {
if (Current == Functions.ObjectDetect) {
if (ResultBuf.getNumber(NumberFormat.UInt8LE, 1) > 0) {
return true
}
}
return false
}
/**
* TODO: 获取识别到的物品总数
*/
//% weight=97 blockId=ObjNum block="Total number of objects detected"
//% subcategory="Object detection"
export function ObjNum(): number {
if (Current == Functions.ObjectDetect) {
return ResultBuf.getNumber(NumberFormat.UInt8LE, 1);
} else {
return 0;
}
}
/**
* TODO: 是否识别到了指定ID的物品
*/
//% weight=95 blockId=IsDetectedObject block="Is $id| detected?"
//% subcategory="Object detection"
export function IsDetectedObjectOfId(id: Objects): boolean {
if (Current == Functions.ObjectDetect) {
for (let i = 2; i < 2 + 29; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
return true;
}
}
return false;
} else {
return false;
}
}
/**
* TODO: 识别到的指定ID的物品的个数
*/
//% weight=85 block="Number of |$id| detected"
//% subcategory="Object detection"
export function NumOfDetectedObject(id: Objects): number {
let num = 0;
if (Current == Functions.ObjectDetect) {
for (let i = 2; i < 2 + 29; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
num += 1;
}
}
}
return num;
}
/**
* TODO: 获取识别到的指定物品的指定序号的结果的数据
*/
//% weight=75 block="|$opt| of the no.|$index| |$id| detected"
//% opt.defl=Obj_Options.Pos_X
//% index.defl=1 index.min=1 index.max=10
//% id.defl=Objects.Aeroplane
//% subcategory="Object detection"
export function getObjectW(opt: Obj_Options, index: number, id: Objects): number {
let num = 0
let addr = 0
if (Current == Functions.ObjectDetect) {
for (let i = 2; i < 2 + 29; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
num += 1
if (num == index) {
return ResultBuf.getNumber(NumberFormat.UInt16LE, (0x30 + opt) + (i - 2) * 16)
}
}
}
}
return 0
}
//图像分类
/**
* TODO: 获取置信度最大的ID
*/
//% weight=82 blockId=MaxConfidenceID block="The most confident ID of classification"
//% id.defl=1 id.min=1 id.max=20
//% subcategory="Classification"
export function MaxConfidenceID(): number {
if (Current == Functions.Classification) {
return ResultBuf.getNumber(NumberFormat.UInt8LE, 0x01);
}
return 0
}
/**
* TODO: 获取最大的置信度
*/
//% weight=81 blockId=MaxConfidence block="The most confident of classification"
//% id.defl=1 id.min=1 id.max=20
//% subcategory="Classification"
export function MaxConfidence(): number {
if (Current == Functions.Classification) {
let c = ResultBuf.getNumber(NumberFormat.UInt16LE, 0x02);
return (c / 10000.0)
}
return 0
}
/**
* TODO: 获取指定ID的的置信度
*/
//% weight=80 blockId=ConfidenceOfId block="Classification confident of ID:$id"
//% id.defl=1 id.min=1 id.max=20
//% subcategory="Classification"
export function ConfidenceOfIdClassification(id: number): number {
if (Current == Functions.Classification) {
let c = ResultBuf.getNumber(NumberFormat.UInt16LE, 0x10 + ((id - 1) * 4))
return (c / 10000.0)
}
return 0
}
//数字识别
/**
* TODO: 获取置信度最大的数字
*/
//% weight=82 blockId=NumberWithMaxConfidence block="The most confident Number"
//% subcategory="Number recognition"
export function NumberWithMaxConfidence(): number {
if (Current == Functions.NumberRecognition) {
return ResultBuf.getNumber(NumberFormat.UInt8LE, 0x01);
}
return 0
}
/**
* TODO: 获取数字识别最大的置信度
*/
//% weight=81 blockId=MaxConfidenceOfNumber block="The most confident of number recognition"
//% id.defl=1 id.min=1 id.max=5
//% subcategory="Number recognition"
export function MaxConfidenceOfNumber(): number {
if (Current == Functions.NumberRecognition) {
let c = ResultBuf.getNumber(NumberFormat.UInt16LE, 0x02);
return (c / 10000.0)
}
return 0
}
/**
* TODO: 获取指定数字的的置信度
*/
//% weight=80 blockId=ConfidenceOfNumber block="Confident of Number:$id"
//% id.defl=1 id.min=1 id.max=5
//% subcategory="Number recognition"
export function ConfidenceOfNumber(id: number): number {
if (Current == Functions.NumberRecognition) {
let c = ResultBuf.getNumber(NumberFormat.UInt16LE, 0x10 + ((id - 1) * 4))
return (c / 10000.0)
}
return 0
}
//路标识别
/**
* TODO: 获取置信度最大的路标
*/
//% weight=82 blockId=LandmarkWithMaxConfidence block="The most confident landmark"
//% subcategory="Landmark recognition"
export function LandmarkWithMaxConfidence(): Landmarks {
if (Current == Functions.LandmarkRecognition) {
return ResultBuf.getNumber(NumberFormat.UInt8LE, 0x01);
}
return 0
}
/**
* TODO: 获取路标识别最大的置信度
*/
//% weight=81 blockId=MaxConfidenceOfLandmark block="The most confident of landmark recognition"
//% subcategory="Landmark recognition"
export function MaxConfidenceOfLandmark(): number {
if (Current == Functions.LandmarkRecognition) {
let c = ResultBuf.getNumber(NumberFormat.UInt16LE, 0x02);
return (c / 10000.0)
}
return 0
}
/**
* TODO: 获取指路标的的置信度
*/
//% weight=80 blockId=ConfidenceOfLandmark block="Confident of landmark:$id"
//% id.defl=1 id.min=1 id.max=5
//% subcategory="Landmark recognition"
export function ConfidenceOfLandmark(id: Landmarks): number {
if (Current == Functions.LandmarkRecognition) {
let c = ResultBuf.getNumber(NumberFormat.UInt16LE, 0x10 + ((id - 1) * 4))
return (c / 10000.0)
}
return 0
}
//% weight=60 blockId=GetLandmarkObj block="|$in_|"
//% subcategory="Landmark recognition"
export function LandmarkObj(in_: Landmarks): Landmarks {
return in_
}
//特征学习
/**
* TODO: 获取置信度最大的ID
*/
//% weight=82 block="The most confident ID of featureLearning"
//% id.defl=1 id.min=1 id.max=20
//% subcategory="FeatureLearning"
export function FlMaxConfidenceID(): number {
if (Current == Functions.FeatureLearning) {
return ResultBuf.getNumber(NumberFormat.UInt8LE, 0x01);
}
return 0
}
/**
* TODO: 获取最大的置信度
*/
//% weight=81 block="The most confident of featureLearning"
//% id.defl=1 id.min=1 id.max=20
//% subcategory="FeatureLearning"
export function FlMaxConfidence(): number {
if (Current == Functions.FeatureLearning) {
let c = ResultBuf.getNumber(NumberFormat.UInt16LE, 0x02);
return (c / 10000.0)
}
return 0
}
/**
* TODO: 获取指定ID的的置信度
*/
//% weight=80 block="FeatureLearning confident of ID:$id"
//% id.defl=1 id.min=1 id.max=7
//% subcategory="FeatureLearning"
export function FlConfidenceOfId(id: number): number {
if (Current == Functions.FeatureLearning) {
let c = ResultBuf.getNumber(NumberFormat.UInt16LE, 0x10 + ((id - 1) * 4))
return (c / 10000.0)
}
return 0
}
//颜色识别
/**
* TODO: 是否识别到了色块
*/
//% weight=99 block="Is color detected?"
//% subcategory="Color detection"
export function IsDetectedColorblobs(): boolean {
if (Current == Functions.ColorDetect) {
if (ResultBuf.getNumber(NumberFormat.UInt8LE, 0x01) > 0) {
return true;
}
}
return false
}
/**
* TODO: 识别到的色块总数
*/
//% weight=90 block="Total number of detected colors"
//% subcategory="Color detection"
export function NumberOfColorblobs(): number {
if (Current == Functions.ColorDetect) {
return ResultBuf.getNumber(NumberFormat.UInt8LE, 0x01);
}
return 0
}
/**
* TODO: 是否识别到了指定ID的颜色
*/
//% weight=80 block="Is color ID:$id detected"
//% id.defl=1 id.min=1 id.max=7
//% subcategory="Color detection"
export function isDetectedColorId(id: number): boolean {
let num = NumberOfColorblobs()
if (Current == Functions.ColorDetect) {
for (let i = 2; i < 2 + num; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
return true;
}
}
}
return false
}
/**
* TODO: 返回指定ID颜色的位置数据
*/
//% weight=75 block="|$opt| of color ID:|$id| detected"
//% id.defl=1 id.min=1 id.max=7
//% opt.defl=Options.Pos_X
//% subcategory="Color detection"
export function XOfColorId(opt: Options, id: number): number {
let num = NumberOfColorblobs()
if (Current == Functions.ColorDetect) {
for (let i = 2; i < 2 + num; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
return ResultBuf.getNumber(NumberFormat.Int16LE, (0x30 + opt) + ((i - 2) * 16));
}
}
}
return 0
}
//视觉巡线
/**
* TODO: 是否识别到了线
*/
//% weight=100 block="Is any line detected?"
//% subcategory="LineFollowing"
export function isDetectedLine(): boolean {
let num = NumberOfLines()
if (Current == Functions.LineFollowing) {
if (ResultBuf.getNumber(NumberFormat.UInt8LE, 0x01) > 0) {
return true;
}
}
return false
}
/**
* TODO: 识别到的线总数
*/
//% weight=90 block="Total number of lines detected"
//% subcategory="LineFollowing"
export function NumberOfLines(): number {
if (Current == Functions.LineFollowing) {
return ResultBuf.getNumber(NumberFormat.UInt8LE, 0x01);
}
return 0
}
/**
* TODO: 是否识别到了指定ID的线
*/
//% weight=85 block="Is line ID:$id detected?"
//% id.defl=1 id.min=1 id.max=3
//% subcategory="LineFollowing"
export function isDetectedLineId(id: number): boolean {
let num = NumberOfLines()
if (Current == Functions.LineFollowing) {
for (let i = 2; i < 2 + num; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
return true;
}
}
}
return false
}
/**
* TODO: 返回指定ID的线的位置数据
*/
//% weight=80 block="|$opt| of line ID:|$id|"
//% id.defl=1 id.min=1 id.max=3
//% opt.defl=Line_Options
//% subcategory="LineFollowing"
export function StartXOfLineId(opt: Line_Options, id: number): number {
let num = NumberOfLines()
if (Current == Functions.LineFollowing) {
for (let i = 2; i < 2 + num; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
let tmp = ResultBuf.getNumber(NumberFormat.Int16LE, (0x30 + opt) + ((i - 2) * 16))
switch (opt) {
case Line_Options.Angle:
if (tmp > 90) {
return tmp - 180
} else {
return tmp
}
case Line_Options.Offset:
tmp = Math.abs(tmp)
return tmp - 160
default:
return tmp
}
}
}
}
return 0
}
//AprilTag
/**
* TODO: 是否识别到了标签
*/
//% weight=99 block="Is any Tag detected?"
//% subcategory="AprilTag"
export function isDetectedAprilTag(): boolean {
if (Current == Functions.AprilTag) {
if (ResultBuf.getNumber(NumberFormat.Int8LE, 0x01) > 0) {
return true
}
}
return false
}
/**
* TODO: 识别到的全部标签个数
*/
//% weight=90 block="Number of all tags detected "
//% subcategory="AprilTag"
export function numberAllTagDetected(): number {
if (Current == Functions.AprilTag) {
return ResultBuf.getNumber(NumberFormat.Int8LE, 0x01)
}
return 0
}
/**
* TODO: 是否识别到了指定ID的标签
*/
//% weight=80 block="Is tag ID:$id detected?"
//% id.defl=1
//% subcategory="AprilTag"
export function isDetecteAprilTagId(id: number): boolean {
if (Current == Functions.AprilTag) {
let num = ResultBuf.getNumber(NumberFormat.Int8LE, 0x01)
for (let i = 2; i < 2 + num; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
return true;
}
}
}
return false
}
/**
* TODO: 识别到的指定ID标签个数
*/
//% weight=70 block="Number of tag ID:|$id| detected "
//% subcategory="AprilTag"
export function numTagIdDetected(id: number): number {
let count = 0
if (Current == Functions.AprilTag) {
let num = ResultBuf.getNumber(NumberFormat.Int8LE, 0x01)
for (let i = 2; i < 2 + num; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
count += 1
}
}
}
return count
}
/**
* TODO: 返回指定标签的位置数据
*/
//% weight=60 block="|$opt| of No.|$index| Tag ID:|$id|"
//% index.defl=1 index.min=1
//% id.defl=0
//% subcategory="AprilTag"
export function getTagDataId(opt: AprilTag_Options, index: number, id: number): number {
if (Current == Functions.AprilTag) {
let num = ResultBuf.getNumber(NumberFormat.Int8LE, 0x01)
for (let i = 2; i < 2 + num; i++) { // 逐个对比是否有这个id
if (ResultBuf.getNumber(NumberFormat.UInt8LE, i) == id) {
index -= 1
if (index == 0) {
switch (opt) {
case AprilTag_Options.Pos_X:
return ResultBuf.getNumber(NumberFormat.Int16LE, 0x30 + (32 * (i - 2)));
case AprilTag_Options.Pos_Y:
return ResultBuf.getNumber(NumberFormat.Int16LE, 0x30 + 2 + (32 * (i - 2)));
case AprilTag_Options.Width:
return ResultBuf.getNumber(NumberFormat.UInt16LE, 0x30 + 4 + (32 * (i - 2)));
case AprilTag_Options.Height:
return ResultBuf.getNumber(NumberFormat.UInt16LE, 0x30 + 6 + (32 * (i - 2)));
case AprilTag_Options.X_T:
return ResultBuf.getNumber(NumberFormat.Float32LE, 0x38 + (32 * (i - 2)));
case AprilTag_Options.X_R:
return ResultBuf.getNumber(NumberFormat.Float32LE, 0x38 + 4 + (32 * (i - 2)));
case AprilTag_Options.Y_T:
return ResultBuf.getNumber(NumberFormat.Float32LE, 0x38 + 8 + (32 * (i - 2)));