-
Notifications
You must be signed in to change notification settings - Fork 13
/
v1.ts
1211 lines (1100 loc) · 35.7 KB
/
v1.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
namespace cutebotProV1 {
let IR_Val = 0
let _initEvents = true
let PidUseFlag = 0
let blocklength = 0
let distanceUnitsFlag = 0
let fourWayStateValue = 0
let pulseCntL = 0
let pulseCntR = 0
let irstate: number;
let state: number;
let i2cAddr: number = 0x10;
export class Packeta {
public mye: string;
public myparam: number;
}
/**
* PWM control the car to travel at a specific speed
*/
export function pwmCruiseControl(speedL: number, speedR: number): void {
let i2cBuffer = pins.createBuffer(7)
if (speedL == 0)
speedL = 200
else if (speedL > 0)
Math.map(speedL, 0, 100, 20, 100);
else
Math.map(speedL, -100, 0, -100, -20);
if (speedR == 0)
speedR = 200
else if (speedR > 0)
Math.map(speedR, 0, 100, 20, 100);
else
Math.map(speedR, -100, 0, -100, -20);
if (speedL > 0) {
i2cBuffer[0] = 0x99;
i2cBuffer[1] = 0x01;
i2cBuffer[2] = CutebotProWheel.LeftWheel;
i2cBuffer[3] = 0x01;
i2cBuffer[4] = speedL;
i2cBuffer[5] = 0x00;
i2cBuffer[6] = 0x88;
}
else {
i2cBuffer[0] = 0x99;
i2cBuffer[1] = 0x01;
i2cBuffer[2] = CutebotProWheel.LeftWheel;
i2cBuffer[3] = 0x00;
i2cBuffer[4] = -speedL;
i2cBuffer[5] = 0x00;
i2cBuffer[6] = 0x88;
}
pins.i2cWriteBuffer(i2cAddr, i2cBuffer)
//basic.pause
if (speedR > 0) {
i2cBuffer[0] = 0x99;
i2cBuffer[1] = 0x01;
i2cBuffer[2] = CutebotProWheel.RightWheel;
i2cBuffer[3] = 0x01;
i2cBuffer[4] = speedR;
i2cBuffer[5] = 0x00;
i2cBuffer[6] = 0x88;
}
else {
i2cBuffer[0] = 0x99;
i2cBuffer[1] = 0x01;
i2cBuffer[2] = CutebotProWheel.RightWheel;
i2cBuffer[3] = 0x00;
i2cBuffer[4] = -speedR;
i2cBuffer[5] = 0x00;
i2cBuffer[6] = 0x88;
}
pins.i2cWriteBuffer(i2cAddr, i2cBuffer)
}
/**
* full speed forward
*/
export function fullSpeedAhead(): void {
let buf = pins.createBuffer(7);
buf[0] = 0x99;
buf[1] = 0x07;
buf[2] = 0x00;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf);
}
/**
* full speed reverse
*/
export function fullAstern(): void {
let buf = pins.createBuffer(7);
buf[0] = 0x99;
buf[1] = 0x08;
buf[2] = 0x00;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf);
}
/**
* stop immediately
*/
export function stopImmediately(wheel: CutebotProMotors): void {
let buf = pins.createBuffer(7);
buf[0] = 0x99;
buf[1] = 0x09;
buf[2] = wheel;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf);
}
/**
* read motor speed
*/
export function readSpeed(motor: CutebotProMotors1, speedUnits: CutebotProSpeedUnits): number {
let speed: number
let buf = pins.createBuffer(7)
if (motor == 1) {
buf[0] = 0x99;
buf[1] = 0x05;
buf[2] = motor;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf);
speed = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
if (speedUnits == CutebotProSpeedUnits.Cms)
return speed;
else
return speed / 0.3937;
}
else {
buf[0] = 0x99;
buf[1] = 0x05;
buf[2] = motor;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf);
speed = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
if (speedUnits == CutebotProSpeedUnits.Cms)
return speed;
else
return speed / 0.3937;
}
}
/**
* 获取编码电机的脉冲数
*/
export function pulseNumber(): void {
let pulsenumberbuf = pins.createBuffer(10);
let buf = pins.createBuffer(7)
buf[0] = 0x99;
buf[1] = 0x16;
buf[2] = 0x00;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf);
pulsenumberbuf[0] = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
pulsenumberbuf[1] = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
pulsenumberbuf[2] = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
pulsenumberbuf[3] = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
pulseCntL = (pulsenumberbuf[0] << 24) | (pulsenumberbuf[1] << 16) | (pulsenumberbuf[2] << 8) | pulsenumberbuf[3]
pulsenumberbuf[4] = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
pulsenumberbuf[5] = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
pulsenumberbuf[6] = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
pulsenumberbuf[7] = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
pulseCntR = (pulsenumberbuf[4] << 24) | (pulsenumberbuf[5] << 16) | (pulsenumberbuf[6] << 8) | pulsenumberbuf[7]
pulsenumberbuf[8] = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
if (pulsenumberbuf[8] == 1) {
pulseCntL = -pulseCntL
}
pulsenumberbuf[9] = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
if (pulsenumberbuf[9] == 1) {
pulseCntR = -pulseCntR
}
}
/**
* obtain the number of pulses produced by the coded motor on both sides of the wheel
*/
export function readPulsenumberTest(motor: CutebotProMotors1): number {
pulseNumber()
if (motor == 1)
return pulseCntL;
else if (motor == 2)
return pulseCntR;
else
return 0
}
/**
* get the rotation degrees of wheel
*/
export function readDistance(motor: CutebotProMotors1): number {
let cylinderNumber: number;
pulseNumber()
if (motor == 1)
//return pulseCntL;
return Math.floor(pulseCntL * 360 / 1428 + 0.5);
else
//return pulseCntR;
return Math.floor(pulseCntR * 360 / 1428 + 0.5);
}
/**
* clear the rotation degrees of wheel
*/
export function clearWheelTurn(motor: CutebotProMotors1): void {
let buf = pins.createBuffer(7)
buf[0] = 0x99;
buf[1] = 0x0A;
buf[2] = motor;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf);
}
/**
* select a headlights and set the RGB color.
* @param R R color value of RGB color, eg: 0
* @param G G color value of RGB color, eg: 128
* @param B B color value of RGB color, eg: 255
*/
export function singleHeadlights(light: CutebotProRGBLight, r: number, g: number, b: number): void {
let buf = pins.createBuffer(7);
if (light == 3) {
buf[0] = 0x99;
buf[1] = 0x0F;
buf[2] = 0x03;
buf[3] = r;
buf[4] = g;
buf[5] = b;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf);
}
else {
if (light == 1) {
buf[2] = 0x01;
}
if (light == 2) {
buf[2] = 0x02;
}
buf[0] = 0x99;
buf[1] = 0x0F;
buf[3] = r;
buf[4] = g;
buf[5] = b;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf);
}
}
/**
* set LED headlights.
*/
export function colorLight(light: CutebotProRGBLight, color: number) {
let r: number, g: number, b: number = 0
let buf = pins.createBuffer(7)
r = color >> 16
g = (color >> 8) & 0xFF
b = color & 0xFF
buf[0] = 0x99;
buf[1] = 0x0F;
buf[2] = light;
buf[3] = r;
buf[4] = g;
buf[5] = b;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf)
}
/**
* turn off all the LED lights
*/
export function turnOffAllHeadlights(): void {
let buf = pins.createBuffer(7);
buf[0] = 0x99;
buf[1] = 0x10;
buf[2] = 0x03;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf);
}
/**
* get a status value of the 4-way line following sensor
*/
export function trackbitStateValue() {
let i2cBuffer = pins.createBuffer(7);
i2cBuffer[0] = 0x99;
i2cBuffer[1] = 0x12;
i2cBuffer[2] = 0x00;
i2cBuffer[3] = 0x00;
i2cBuffer[4] = 0x00;
i2cBuffer[5] = 0x00;
i2cBuffer[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, i2cBuffer)
fourWayStateValue = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
//basic.pause(5);
}
/**
* 4-way line following sensor offset
*/
export function getOffset(): number {
let offset: number;
let i2cBuffer = pins.createBuffer(7);
i2cBuffer[0] = 0x99;
i2cBuffer[1] = 0x14;
i2cBuffer[2] = 0x00;
i2cBuffer[3] = 0x00;
i2cBuffer[4] = 0x00;
i2cBuffer[5] = 0x00;
i2cBuffer[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, i2cBuffer)
const offsetLow = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false);
i2cBuffer[0] = 0x99;
i2cBuffer[1] = 0x14;
i2cBuffer[2] = 0x01;
i2cBuffer[3] = 0x00;
i2cBuffer[4] = 0x00;
i2cBuffer[5] = 0x00;
i2cBuffer[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, i2cBuffer)
const offsetHigh = pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
offset = (offsetHigh << 8) | offsetLow
offset = Math.map(offset, 0, 6000, -3000, 3000);
return offset;
}
/**
* get Grayscale Sensor State
*/
export function getGrayscaleSensorState(state: TrackbitStateType): boolean {
return fourWayStateValue == state
}
/**
* check whether the channel is online
*/
export function trackbitChannelState(channel: TrackbitChannel, state: TrackbitType): boolean {
if (state == TrackbitType.State_1)
if (fourWayStateValue & (1 << (channel - 1))) {
return true
}
else {
return false
}
else {
if (fourWayStateValue & (1 << (channel - 1))) {
return false
}
else {
return true
}
}
}
/**
* get gray value.The range is from 0 to 255.
*/
export function trackbitgetGray(channel: TrackbitChannel): number {
let i2cBuffer = pins.createBuffer(7);
i2cBuffer[0] = 0x99;
i2cBuffer[1] = 0x11;
i2cBuffer[2] = channel;
i2cBuffer[3] = 0x00;
i2cBuffer[4] = 0x00;
i2cBuffer[5] = 0x00;
i2cBuffer[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, i2cBuffer)
return pins.i2cReadNumber(i2cAddr, NumberFormat.UInt8LE, false)
}
/**
* cars can extend the ultrasonic function to prevent collisions and other functions..
* @param Sonarunit two states of ultrasonic module, eg: Centimeters
*/
export function ultrasonic(unit: SonarUnit, maxCmDistance = 500): number {
// send pulse
pins.setPull(DigitalPin.P8, PinPullMode.PullNone);
pins.digitalWritePin(DigitalPin.P8, 0);
control.waitMicros(2);
pins.digitalWritePin(DigitalPin.P8, 1);
control.waitMicros(10);
pins.digitalWritePin(DigitalPin.P8, 0);
// read pulse
const d = pins.pulseIn(DigitalPin.P12, PulseValue.High, maxCmDistance * 50);
switch (unit) {
case SonarUnit.Centimeters:
return Math.floor(d * 34 / 2 / 1000);
case SonarUnit.Inches:
return Math.floor(d * 34 / 2 / 1000 * 0.3937);
default:
return d;
}
}
/**
* control the car to travel at a specific speed (speed.min=20cm/s speed.max=50cm/s)
*/
export function cruiseControl(speedL: number, speedR: number, speedUnits: CutebotProSpeedUnits): void {
let buf = pins.createBuffer(7)
let orientationL = 0
let orientationR = 0
if (speedUnits == CutebotProSpeedUnits.Cms) {
speedL = speedL;
speedR = speedR;
}
else {
speedL = speedL / 0.3937;
speedR = speedR / 0.3937;
}
if (speedL < 0) {
speedL = -speedL
orientationL = CutebotProOrientation.Retreat
} else {
orientationL = CutebotProOrientation.Advance
}
if (speedR < 0) {
speedR = -speedR
orientationR = CutebotProOrientation.Retreat
} else {
orientationR = CutebotProOrientation.Advance
}
if (speedL > 50)
speedL = 50;
// else if (speedL != 0 && speedL < 20)
// speedL = 20;
if (speedR > 50)
speedR = 50;
// else if (speedR != 0 && speedR < 20)
// speedR = 20;
buf[0] = 0x99;
buf[1] = 0x02;
buf[2] = orientationL;
buf[3] = speedL;
buf[4] = orientationR;
buf[5] = speedR;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf)
//basic.pause(110)
}
/**
* set the car to travel a specific distance(distance.max=255cm, distance.min=0cm)
*/
export function distanceRunning(orientation: CutebotProOrientation, distance: number, distanceUnits: CutebotProDistanceUnits): void {
let buf = pins.createBuffer(7)
let curtime = 0
let oldtime = 0
let tempdistance = 0
let temp = 0
CutebotPro.pwmCruiseControl(0, 0)
if (distanceUnits == CutebotProDistanceUnits.Cm)
tempdistance = distance;
else if (distanceUnits == CutebotProDistanceUnits.Ft)
tempdistance = distance / 0.3937;
if (tempdistance > 3) {
temp = Math.floor(tempdistance / 50) + 1
tempdistance = tempdistance - temp
}
buf[0] = 0x99;
buf[1] = 0x03;
buf[2] = orientation;
buf[3] = tempdistance;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf)
/*oldtime = control.millis()
while(1)
{
curtime = control.millis()
if ((curtime - oldtime) == (distance * 1000 / 20 + 600))
break
}*/
basic.pause(tempdistance * 1000 / 20)
basic.pause(800)
}
/**
*
*/
export function angleRunning(orientation: CutebotProWheel, angle: number, angleUnits: CutebotProAngleUnits): void {
let buf = pins.createBuffer(7)
let curtime = 0
let oldtime = 0
let tempangle = 0
CutebotPro.pwmCruiseControl(0, 0)
if (angleUnits == CutebotProAngleUnits.Angle)
tempangle = angle;
else if (angleUnits == CutebotProAngleUnits.Circle)
tempangle = angle * 360;
if (tempangle < 0)
tempangle = -tempangle
buf[0] = 0x99;
buf[1] = 0x04;
buf[2] = orientation;
buf[3] = (tempangle >> 8) & 0xff;
buf[4] = (tempangle >> 0) & 0xff;
if (angle < 0)
buf[5] = 0x00;
else
buf[5] = 0x01;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf)
basic.pause(1000)
while (1) {
if (readSpeed(CutebotProMotors1.M1, CutebotProSpeedUnits.Cms) == 0 && readSpeed(CutebotProMotors1.M2, CutebotProSpeedUnits.Cms) == 0) {
basic.pause(1000)
if (readSpeed(CutebotProMotors1.M1, CutebotProSpeedUnits.Cms) == 0 && readSpeed(CutebotProMotors1.M2, CutebotProSpeedUnits.Cms) == 0)
break
}
}
/* let D_Value = 0
let I_Value = 0
let P_Value = 0
let temp = 0
let curvalue = 0
let expect = 0
let value = 0
let Derr = 0
let Ierr = 0
let Perr = 0
let prev = 0
let curerr = 0
let D = 0
let I = 0
let P = 0
P = 1.158
I = 0.5
D = 3.51
curerr = 0
prev = 0
Perr = 0
Ierr = 0
Derr = 0
value = 0
if (angleUnits == CutebotProAngleUnits.Angle)
expect = angle
else
expect = angle * 360
if (angle >= 0)
{
if (orientation == CutebotProWheel.LeftWheel)
curvalue = CutebotPro.readDistance(CutebotProMotors1.M1)
else
curvalue = CutebotPro.readDistance(CutebotProMotors1.M2)
temp = curvalue + expect
while (true) {
if (orientation == CutebotProWheel.LeftWheel)
curvalue = CutebotPro.readDistance(CutebotProMotors1.M1)
else
curvalue = CutebotPro.readDistance(CutebotProMotors1.M2)
curerr = temp - curvalue
Perr = curerr
Derr = curerr - prev
Ierr = curerr + Ierr
P_Value = P * Perr
if (P_Value >= 27) {
P_Value = 27
}
I_Value = I * Ierr
if (I_Value > 17) {
I_Value = 17
}
D_Value = D * Derr
if (D_Value > 40) {
D_Value = 40
}
value = P_Value + (I_Value + D_Value)
if (curerr <= 0) {
CutebotPro.pwmCruiseControl(0, 0)
value = 0
break;
}
if (value > 40) {
value = 40
}
if (value < 10) {
value = 0
}
if (orientation == CutebotProWheel.LeftWheel)
CutebotPro.pwmCruiseControl(value, 0)
else if (orientation == CutebotProWheel.RightWheel)
CutebotPro.pwmCruiseControl(0, value)
else
CutebotPro.pwmCruiseControl(value, value)
prev = curerr
basic.pause(10)
}
CutebotPro.pwmCruiseControl(0, 0)
}
else{
if (orientation == CutebotProWheel.LeftWheel)
curvalue = CutebotPro.readDistance(CutebotProMotors1.M1)
else
curvalue = CutebotPro.readDistance(CutebotProMotors1.M2)
temp = curvalue
while (true) {
if (orientation == CutebotProWheel.LeftWheel)
curvalue = CutebotPro.readDistance(CutebotProMotors1.M1)
else
curvalue = CutebotPro.readDistance(CutebotProMotors1.M2)
curerr = Math.abs(expect) - Math.abs(curvalue - temp)
Perr = curerr
Derr = curerr - prev
Ierr = curerr + Ierr
P_Value = P * Perr
if (P_Value >= 27) {
P_Value = 27
}
I_Value = I * Ierr
if (I_Value > 17) {
I_Value = 17
}
D_Value = D * Derr
if (D_Value > 40) {
D_Value = 40
}
value = P_Value + (I_Value + D_Value)
if (curerr <= 0) {
CutebotPro.pwmCruiseControl(0, 0)
value = 0
break;
}
if (value > 40) {
value = 40
}
if (value < 10) {
value = 0
}
if (orientation == CutebotProWheel.LeftWheel)
CutebotPro.pwmCruiseControl(-value, 0)
else if (orientation == CutebotProWheel.RightWheel)
CutebotPro.pwmCruiseControl(0, -value)
else
CutebotPro.pwmCruiseControl(-value, -value)
prev = curerr
basic.pause(10)
}
CutebotPro.pwmCruiseControl(0, 0)
}
return
*/
}
/**
* set block length
*/
export function setBlockCnt(length: number, distanceUnits: CutebotProDistanceUnits): void {
blocklength = length
distanceUnitsFlag = distanceUnits
}
/**
* run a specific number of block
*/
export function runBlockCnt(cnt: number): void {
distanceRunning(CutebotProOrientation.Advance, blocklength * cnt, distanceUnitsFlag)
}
/**
* set the trolley to rotate at a specific Angle
*/
export function trolleySteering(turn: CutebotProTurn, angle: number): void {
let buf = pins.createBuffer(7)
let curtime = 0
let oldtime = 0
let tempangle = 0
let orientation = 0
let cmd = 0
CutebotPro.pwmCruiseControl(0, 0)
basic.pause(1000)
if (angle == CutebotProAngle.Angle45)
tempangle = 150
else if (angle == CutebotProAngle.Angle90)
tempangle = 316
else if (angle == CutebotProAngle.Angle135)
tempangle = 450
else if (angle == CutebotProAngle.Angle180)
tempangle = 630
else if( angle < 180)
{
tempangle = 3.51 * angle
}
else
{
tempangle = 3.45 * angle
}
if (turn == CutebotProTurn.Left) {
orientation = CutebotProWheel.RightWheel
cmd = 0x04
}
else if (turn == CutebotProTurn.Right) {
orientation = CutebotProWheel.LeftWheel
cmd = 0x04
}
else {
orientation = CutebotProWheel.AllWheel
cmd = 23
tempangle = tempangle + 4
}
buf[0] = 0x99;
buf[1] = cmd;
buf[2] = orientation;
buf[3] = (tempangle >> 8) & 0xff;
buf[4] = (tempangle >> 0) & 0xff;
if (turn == CutebotProTurn.RightInPlace)
buf[5] = 0x00;
else
buf[5] = 0x01;
buf[6] = 0x88;
pins.i2cWriteBuffer(i2cAddr, buf)
basic.pause(1000)
while (1) {
if (readSpeed(CutebotProMotors1.M1, CutebotProSpeedUnits.Cms) == 0 && readSpeed(CutebotProMotors1.M2, CutebotProSpeedUnits.Cms) == 0) {
basic.pause(1000)
if (readSpeed(CutebotProMotors1.M1, CutebotProSpeedUnits.Cms) == 0 && readSpeed(CutebotProMotors1.M2, CutebotProSpeedUnits.Cms) == 0)
break
}
}
basic.pause(1000)
/*let D_Value = 0
let I_Value = 0
let P_Value = 0
let tempL = 0
let tempR = 0
let curvalueL = 0
let curvalueR = 0
let expect = 0
let valueL = 0
let valueR = 0
let Derr = 0
let IerrL = 0
let IerrR = 0
let Perr = 0
let prevL = 0
let prevR = 0
let curerrL = 0
let curerrR = 0
let flagL = 0
let flagR = 0
let D = 0
let I = 0
let P = 0
P = 1.158
I = 0.5
D = 3.51
prevL = 0
prevR = 0
curerrL = 0
curerrR = 0
Perr = 0
IerrL = 0
IerrR = 0
Derr = 0
valueL = 0
valueR = 0
if (turn == CutebotProTurn.Left) {
angleRunning(CutebotProWheel.RightWheel, ((angle + 1) * 150 + 2), CutebotProAngleUnits.Angle)
}
else if (turn == CutebotProTurn.Right) {
angleRunning(CutebotProWheel.LeftWheel, ((angle + 1) * 150 + 2), CutebotProAngleUnits.Angle)
}
else if (turn == CutebotProTurn.LeftInPlace) {
expect = (angle + 1) * 75
curvalueL = CutebotPro.readDistance(CutebotProMotors1.M1)
curvalueR = CutebotPro.readDistance(CutebotProMotors1.M2)
tempL = curvalueL
tempR = curvalueR
while (true) {
if(flagR == 0)
{
curvalueR = CutebotPro.readDistance(CutebotProMotors1.M2)
curerrR = Math.abs(expect) - Math.abs(curvalueR - tempR)
Perr = curerrR
Derr = curerrR - prevR
IerrR = curerrR + IerrR
P_Value = P * Perr
if (P_Value >= 27) {
P_Value = 27
}
I_Value = I * IerrR
if (I_Value > 17) {
I_Value = 17
}
D_Value = D * Derr
if (D_Value > 40) {
D_Value = 40
}
valueR = P_Value + (I_Value + D_Value)
prevR = curerrR
}
if(flagL == 0)
{
curvalueL = CutebotPro.readDistance(CutebotProMotors1.M1)
curerrL = Math.abs(expect) - Math.abs(curvalueL - tempL)
Perr = curerrL
Derr = curerrL - prevL
IerrL = curerrL + IerrL
P_Value = P * Perr
if (P_Value >= 27) {
P_Value = 27
}
I_Value = I * IerrL
if (I_Value > 17) {
I_Value = 17
}
D_Value = D * Derr
if (D_Value > 40) {
D_Value = 40
}
valueL = P_Value + (I_Value + D_Value)
prevL = curerrL
}
if (valueR > 40) {
valueR = 40
}
if (valueR < 10) {
valueR = 0
}
if (valueL > 40) {
valueL = 40
}
if (valueL < 10) {
valueL = 0
}
if (curerrL <= 0) {
valueL = 0
flagL = 1
}
if (curerrR <= 0) {
valueR = 0
flagR = 1
}
if (curerrL <= 0 && curerrR <= 0) {
CutebotPro.pwmCruiseControl(0, 0)
break
}
else{
CutebotPro.pwmCruiseControl(-valueL, valueR)
basic.pause(10)
}
}
}
else if (turn == CutebotProTurn.RightInPlace) {
expect = (angle + 1) * 75
curvalueL = CutebotPro.readDistance(CutebotProMotors1.M1)
curvalueR = CutebotPro.readDistance(CutebotProMotors1.M2)
tempL = curvalueL
tempR = curvalueR
while (true) {
if (flagR == 0) {
curvalueR = CutebotPro.readDistance(CutebotProMotors1.M2)
curerrR = Math.abs(expect) - Math.abs(curvalueR - tempR)
Perr = curerrR
Derr = curerrR - prevR
IerrR = curerrR + IerrR
P_Value = P * Perr
if (P_Value >= 27) {
P_Value = 27
}
I_Value = I * IerrR
if (I_Value > 17) {
I_Value = 17
}
D_Value = D * Derr
if (D_Value > 40) {
D_Value = 40
}
valueR = P_Value + (I_Value + D_Value)
prevR = curerrR
}
if (flagL == 0) {
curvalueL = CutebotPro.readDistance(CutebotProMotors1.M1)
curerrL = Math.abs(expect) - Math.abs(curvalueL - tempL)
Perr = curerrL
Derr = curerrL - prevL
IerrL = curerrL + IerrL
P_Value = P * Perr
if (P_Value >= 27) {
P_Value = 27
}
I_Value = I * IerrL
if (I_Value > 17) {
I_Value = 17
}
D_Value = D * Derr
if (D_Value > 40) {
D_Value = 40
}
valueL = P_Value + (I_Value + D_Value)
prevL = curerrL
}
if (valueR > 40) {
valueR = 40
}
if (valueR < 10) {
valueR = 0
}
if (valueL > 40) {
valueL = 40
}
if (valueL < 10) {
valueL = 0
}
if (curerrL <= 0) {
valueL = 0
flagL = 1
}
if (curerrR <= 0) {
valueR = 0
flagR = 1
}
if (curerrL <= 0 && curerrR <= 0) {