-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbitbot.ts
More file actions
3085 lines (2889 loc) · 83.6 KB
/
bitbot.ts
File metadata and controls
3085 lines (2889 loc) · 83.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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
/**
* Eyeball directions
*/
enum eyePos
{
//% block="forward"
Forward,
//% block="down"
Down,
//% block="up"
Up,
//% block="left"
Left,
//% block="right"
Right,
//% block="down-left"
DownLeft,
//% block="down-right"
DownRight,
//% block="up-left"
UpLeft,
//% block="up-right"
UpRight
}
enum eyeSize
{
//% block="small"
Small,
//% block="large"
Large
}
enum bfEyes
{
//% block="left"
Left,
//% block="right"
Right,
//% block="both"
Both
}
enum bfMouth
{
//% block="smile"
Smile,
//% block="grin"
Grin,
//% block="sad"
Sad,
//% block="frown"
Frown,
//% block="straight"
Straight,
//% block="oooh"
Oooh,
//% block="eeeh"
Eeeh
}
/**
* Enumeration of motors.
*/
enum BBMotor
{
//% block="left"
Left,
//% block="right"
Right,
//% block="both"
Both
}
enum BBArms
{
//% block="both"
Both,
//% block="left"
Left,
//% block="right"
Right
}
/**
* Enumeration of forward/reverse directions
*/
enum BBDirection
{
//% block="forward"
Forward,
//% block="reverse"
Reverse
}
/**
* Enumeration of Arc movement directions
*/
enum BBArcDirection
{
//% block="forward left"
ForwardLeft,
//% block="forward right"
ForwardRight,
//% block="reverse left"
ReverseLeft,
//% block="reverse right"
ReverseRight
}
/**
* Enumeration of directions.
*/
enum BBRobotDirection
{
//% block="left"
Left,
//% block="right"
Right
}
/**
* Stop modes. Coast or Brake
*/
enum BBStopMode
{
//% block="no brake"
Coast,
//% block="brake"
Brake
}
/**
* Enable/Disable for Bluetooth and FireLeds
*/
enum BBBluetooth
{
//% block="Enable"
btEnable,
//% block="Disable"
btDisable
}
/**
* Values for buzzer. On or Off
*/
enum BBBuzz
{
//% block="on"
On,
//% block="off"
Off
}
/**
* Enumeration of line sensors.
*/
enum BBLineSensor
{
//% block="left"
Left,
//% block="right"
Right
}
/**
* BitBot Pro line sensors.
*/
enum BBPLineSensor
{
//% block="left"
Left,
//% block="right"
Right,
//% block="centre"
Centre
}
/**
* Enumeration of light sensors.
*/
enum BBLightSensor
{
//% block="left"
Left,
//% block="right"
Right
}
/**
* Enumeration of pulse sensors.
*/
enum BBPulseSensor
{
//% block="left"
Left,
//% block="right"
Right
}
/**
* Distance unit for ultrasonic sensor.
*/
enum BBPingUnit
{
//% block="cm"
Centimeters,
//% block="inches"
Inches,
//% block="μs"
MicroSeconds
}
/**
* Distance unit for wheel sensors
*/
enum BBSensorUnit
{
//% block="mm"
Millimeters,
//% block="cm"
Centimeters,
//% block="inches"
Inches,
//% block="pulses"
Pulses
}
enum BBServos
{
P1,
P2
}
/**
* Update mode for LEDs
* setting to Manual requires show LED changes blocks
* setting to Auto will update the LEDs everytime they change
*/
enum BBMode
{
Manual,
Auto
}
/**
* Model Types of BitBot
* Classic, XL or Pro
*/
enum BBModel
{
Classic,
XL,
PRO,
Auto
}
/**
* Pre-Defined LED colours
*/
enum BBColors
{
//% 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
}
/**
* BitBot Pro Line Sensor Indicator modes
*/
enum LIMode
{
Off,
On,
Auto
}
/**
* IR Key code translations
*/
enum BBirKeys
{
//% block="any"
Any=0,
//% block="1"
One=162,
//% block="2"
Two=98,
//% block="3"
Three=226,
//% block="4"
Four=34,
//% block="5"
Five=2,
//% block="6"
Six=194,
//% block="save"
Save=224,
//% block="■"
Stop=168,
//% block="load"
Load=144,
//% block="X"
Cross=104,
//% block="║"
Pause=152,
//% block="/"
Tick=176,
//% block="↑"
Up=24,
//% block="↓"
Down=74,
//% block="←"
Left=16,
//% block="→"
Right=90,
//% block="►"
Go=56
}
/**
* IR Key code translations without the Any code
*/
enum BBirNoAny
{
//% block="1"
One=162,
//% block="2"
Two=98,
//% block="3"
Three=226,
//% block="4"
Four=34,
//% block="5"
Five=2,
//% block="6"
Six=194,
//% block="save"
Save=224,
//% block="■"
Stop=168,
//% block="load"
Load=144,
//% block="X"
Cross=104,
//% block="║"
Pause=152,
//% block="/"
Tick=176,
//% block="↑"
Up=24,
//% block="↓"
Down=74,
//% block="←"
Left=16,
//% block="→"
Right=90,
//% block="►"
Go=56
}
/**
* Volume settings for Buzzer
*/
enum BBBuzzVolume
{
Off=0,
Quiet=130,
Medium=160,
Loud=200
}
/**
* Custom blocks
*/
//% weight=50 color=#e7660b icon="\uf1b9"
//% groups='["Basic","Motors","Advanced","Special","Ultrasonic","Line Sensor","5x5 Matrix","BitFace","OLED 128x64"]'
namespace bitbot
{
let fireBand: fireled.Band;
let _updateMode = BBMode.Auto;
const i2caddr = 0x1C // i2c address of I/O Expander
const i2cATMega = 0x22 // i2c address of ATMega on BitBot Pro
const EEROM = 0x50 // i2c address of EEROM
const reservedBytes = 50 // EEROM addresses reserved for system use
const startFlash = 50 // Commands all below 50. Values startFlash and above is EEROM address (+ startFlash)
const i2cACK = 0x55 // i2c acknowledge character for terminating motor commands
const NUMLEDS = 12
const ATMRESET = 20
const FIREDATA = 0
const FIREBRT = 1
const FIREUPDT = 2
const UPDATEMODE = 9
const SHIFTLEDS = 10
const ROTATELEDS = 11
const RAINBOW = 12
const SETPIXEL = 13
// BitBot Pro New Commands
const STOP = 21
const DRIVE = 22 // Speed +/- 100%
const SPIN = 23 // Speed +/- 100%
const DRIVEDIST = 24 // Speed, Distance (cm)
const SPINANGLE = 25 // Speed, Angle (degrees)
const ARC = 26 // Speed, Radius
const ARCANGLE = 27 // Speed, Radius, Angle
const DIRECTMODE = 28 // Speed, Motor. For compatability with older independent motor settings
const INDICATOR = 29 // Indicator (L/R), Value
const SETTHRESH = 30 // Theshold, hysteresis
const PIDENABLE = 31 // false/true, 0/1
const LINECALIB = 32 // Start calibration. No parameter
const RESETWHEEL = 33 // Left, Right, Both
const SETTRIMS = 34 // trimDistance and trimAngle. Both -100 to +100
// new for v1.1
const SETPIDCON = 35 // set the PID constants (used for debugging)
const SUMERRORS = 36 // disable/enable carrying PID errors to next command
const CLRERRORS = 37 // Clear the PID tracking error
const STOPBRAKE = 38 // Set Stop and braking thresholds in mm and speed in pulses
const STARTPWM = 39 // Set the starting PWM minimum and maximum values
// BitBot Pro IR constants
const irPin = DigitalPin.P14
const irEvent = 1995
// Input Channels - BitBot Pro only
const VERREV = 0
const DLINEL = 1
const DLINER = 2
const DLINEC = 3
const ALINEL = 4
const ALINER = 5
const ALINEC = 6
const LIGHTL = 7
const LIGHTR = 8
const PSU = 9
const ACKNAK = 20
const LPULSEL = 21 // left pulse count low word
const LPULSEH = 22 // left pulse count high word
const RPULSEL = 23
const RPULSEH = 24
const LASTERRL = 25
const LASTERRR = 26
// Motor Command Tracking
const cGO = 1
const cSTOP = 2
const cSPIN = 3
const cDIRECT = 4
const cGOCM = 5
const cSPINDEG = 6
const cARC = 7
const cARCDEG = 8
const cSTEER = 9
let lastCommand = cSTOP
let lastDirection = BBDirection.Forward
let lastSDirection = BBRobotDirection.Right
let lastADirection = BBArcDirection.ForwardLeft
let lastSpeed = 0
let lastRadius = 0
/////
let btDisabled = true
let matrix5: fireled.Band
let bitface: fireled.Band
let mouthSmile: number[] = [0,1,2,3,4,5]
let mouthGrin: number[] = [0,1,2,3,4,5,10,11,12,13]
let mouthSad: number[] = [0,5,6,7,8,9]
let mouthFrown: number[] = [0,5,6,7,8,9,10,11,12,13]
let mouthStraight: number[] = [0,5,10,11,12,13]
let mouthOooh: number[] = [1,2,3,4,6,7,8,9,10,13]
let mouthEeeh: number[] = [0,1,2,3,4,5,6,7,8,9]
let oled: firescreen.Screen
let leftBias = 0
let rightBias = 0
let calibration: number[] = [0, 0, 0]
let leftCalib = 0
let rightCalib = 0
let calibLoaded = false
let _model = BBModel.Auto
let versionCode = -1
let firmwareCode = -1
let lMotorD0: DigitalPin
let lMotorD1: DigitalPin
let lMotorA0: AnalogPin
let lMotorA1: AnalogPin
let rMotorD0: DigitalPin
let rMotorD1: DigitalPin
let rMotorA0: AnalogPin
let rMotorA1: AnalogPin
let _deadband = 2
let _p1Trim = 0
let _p2Trim = 0
let pidEnable = true
let pidActive = false
let i2cData2 = pins.createBuffer(2)
let i2cData3 = pins.createBuffer(3)
let i2cData4 = pins.createBuffer(4)
let i2cData5 = pins.createBuffer(5)
let i2cData6 = pins.createBuffer(6)
function clamp(value: number, min: number, max: number): number
{
return Math.max(Math.min(max, value), min)
}
// Helper function for BitBot Pro checks
function isPRO(): boolean
{
return getModel() == BBModel.PRO
}
// Commands via I2C on ATMega
function sendCommand2(command: number, para0: number): void
{
i2cData2[0] = command
i2cData2[1] = para0
pins.i2cWriteBuffer(i2cATMega, i2cData2)
}
function sendCommand3(command: number, para0: number, para1: number): void
{
i2cData3[0] = command
i2cData3[1] = para0
i2cData3[2] = para1
pins.i2cWriteBuffer(i2cATMega, i2cData3)
}
function sendCommand4(command: number, para0: number, para1: number, para2: number): void
{
i2cData4[0] = command
i2cData4[1] = para0
i2cData4[2] = para1
i2cData4[3] = para2
pins.i2cWriteBuffer(i2cATMega, i2cData4)
}
function sendCommand5(command: number, para0: number, para1: number, para2: number, para3: number): void
{
i2cData5[0] = command
i2cData5[1] = para0
i2cData5[2] = para1
i2cData5[3] = para2
i2cData5[4] = para3
pins.i2cWriteBuffer(i2cATMega, i2cData5)
}
function sendCommand6(command: number, para0: number, para1: number, para2: number, para3: number, para4: number): void
{
i2cData6[0] = command
i2cData6[1] = para0
i2cData6[2] = para1
i2cData6[3] = para2
i2cData6[4] = para3
i2cData6[5] = para4
pins.i2cWriteBuffer(i2cATMega, i2cData6)
}
function readSensor(sensor: number): number
{
pins.i2cWriteNumber(i2cATMega, sensor, NumberFormat.Int8LE, false)
return (pins.i2cReadNumber(i2cATMega, NumberFormat.UInt16LE))
}
function readSensorSigned(sensor: number): number
{
pins.i2cWriteNumber(i2cATMega, sensor, NumberFormat.Int8LE, false)
return (pins.i2cReadNumber(i2cATMega, NumberFormat.Int16LE))
}
// Block to enable Bluetooth and disable FireLeds.
/**
* Enable/Disable Bluetooth support by disabling/enabling FireLeds. Not required for built in FireLeds on BitBot Pro
* @param enable enable or disable Blueetoth
*/
//% blockId="BBEnableBluetooth"
//% block="%enable|Bluetooth"
//% blockGap=8
export function bbEnableBluetooth(enable: BBBluetooth)
{
if (enable == BBBluetooth.btEnable)
btDisabled = false
else
btDisabled = true
}
// Blocks for selecting BitBot Model
/**
* Force Model of BitBot (Determines Pins used)
* @param model Model of BitBot; Classic, XL or PRO
*/
//% blockId="bitbot_model" block="select BitBot model%model"
//% weight=100
//% subcategory=BitBot_Model
export function select_model(model: BBModel): void
{
if((model==BBModel.Classic) || (model==BBModel.XL) || (model==BBModel.PRO) || (model==BBModel.Auto))
{
_model = model;
if (_model == BBModel.Classic)
{
lMotorD0 = DigitalPin.P0
lMotorD1 = DigitalPin.P8
lMotorA0 = AnalogPin.P0
lMotorA1 = AnalogPin.P8
rMotorD0 = DigitalPin.P1
rMotorD1 = DigitalPin.P12
rMotorA0 = AnalogPin.P1
rMotorA1 = AnalogPin.P12
}
else if (_model == BBModel.XL)
{
lMotorD0 = DigitalPin.P16
lMotorD1 = DigitalPin.P8
lMotorA0 = AnalogPin.P16
lMotorA1 = AnalogPin.P8
rMotorD0 = DigitalPin.P14
rMotorD1 = DigitalPin.P12
rMotorA0 = AnalogPin.P14
rMotorA1 = AnalogPin.P12
}
}
}
/**
* get Model of BitBot (Classic, XL or PRO)
*/
//% blockId="bb_model" block="BitBot model"
//% weight=90
//% subcategory=BitBot_Model
export function getModel(): BBModel
{
getVersionCode()
if (_model == BBModel.Auto)
{
if (versionCode == 0)
{
select_model(BBModel.Classic)
}
else if (versionCode < 16)
{
select_model(BBModel.XL);
pins.digitalWritePin(DigitalPin.P0, 0)
}
else
select_model(BBModel.PRO)
}
return _model
}
/**
* Get numeric value of BitBot Model
*
* @param model BitBot Model eg: BBModel.Classic
*/
//% blockId="bb_models" block=%model
//% weight=80
//% subcategory=BitBot_Model
export function BBModels(model: BBModel): number
{
return model
}
/**
* Get versionCode
*/
//% blockId="getVersionCode"
//% block="version Code"
//% weight=80
//% subcategory=BitBot_Model
//% deprecated=true
export function getVersionCode(): number
{
// 0 = Classic, 1-15 = XL, 16+ = Pro
if (versionCode == -1) // first time requesting
{
getCode()
sendCommand2(PIDENABLE, 1) // first access to BitBot PRO, so ensure PID loop is enabled
sendCommand3(STARTPWM, 35, 18) // set m+start PWM values to lowest sensible. NB pwmMax is always doubled before use
}
return versionCode
}
function getCode() // this always gets the codes
{
versionCode = pins.i2cReadNumber(i2cATMega, NumberFormat.Int8LE, false) & 0xff // i2cATMega only valid for BitBot PRO
if(versionCode > 0) // BitBot PRO
{
let revision = pins.i2cReadNumber(i2cATMega, NumberFormat.UInt16LE, false) // low byte: Board rev, hi byte: Firmware rev
versionCode = 10 + revision & 0xff // use 10+ board version (so 16) for BitBot PRO versionCode
firmwareCode = (revision >> 8) & 0xff // firmware version only valid for BitBot PRO. First release was 10, second 11, etc.
}
else // so must be XL or Classic. Classic returns zero from below
{
versionCode = (pins.i2cReadNumber(i2caddr, NumberFormat.Int8LE, false) >> 4) & 0x0f
firmwareCode = 0
}
return versionCode
}
/**
* Get firmware revision
*/
//% blockId="getFirmwareCode"
//% block="BitBot PRO firmware revision"
//% weight=75
//% subcategory=Advanced
export function getFirmwareCode(): number
{
if(firmwareCode == -1)
{
firmwareCode = 0
if(isPRO())
getCode()
}
return firmwareCode
}
// "DRIVE STRAIGHT" and EEROM BLOCKS
// Uses bottom 3 bytes of EEROM for motor bias data on "version 5" only
// Bias values from -100 to +100. Negative values decrease Left speed, Positive decrease right speed
// Byte 0 = Bias at 30, 1 = Bias at 60, 2 = Bias at 90
/**
* Write a byte of data to EEROM at selected User address
* @param address Location in EEROM to write to
* @param data Byte of data to write
*/
//% blockId="writeEEROM"
//% block="write%data|to EEROM address%address"
//% subcategory="BitBot PRO"
//% group=EEROM
//% weight=100
export function writeEEROM(data: number, address: number): void
{
if(isPRO())
wrEEROM(data, address + reservedBytes)
else
wrEEROM(data, address)
}
/**
* Write a byte of data to EEROM at selected Raw address
* @param address Location in EEROM to write to
* @param data Byte of data to write
*/
//% blockId="rawWriteEEROM"
//% block="write%data|to EEROM raw address%address"
//% weight=95
//% deprecated=true
export function wrEEROM(data: number, address: number): void
{
if(isPRO() && ((address + startFlash) <= 255))
sendCommand2(address + startFlash, data)
else if (getVersionCode() == 5)
{
let i2cData = pins.createBuffer(3);
i2cData[0] = address >> 8 // address MSB
i2cData[1] = address & 0xff // address LSB
i2cData[2] = data & 0xff
pins.i2cWriteBuffer(EEROM, i2cData, false)
basic.pause(3) // needs a short pause. 3ms ok?
}
// else do nothing
}
/**
* Read a byte of data from EEROM at selected User address
* @param address Location in EEROM to read from
*/
//% blockId="readEEROM"
//% block="EEROM at address%address"
//% subcategory="BitBot PRO"
//% group=EEROM
//% weight=90
export function readEEROM(address: number): number
{
if(isPRO())
return rdEEROM(address + reservedBytes)
else
return rdEEROM(address)
}
/**
* Read a byte of data from EEROM at selected raw address
* @param address Location in EEROM to read from
*/
//% blockId="rawReadEEROM"
//% block="EEROM at raw address%address"
//% weight=80
//% deprecated=true
export function rdEEROM(address: number): number
{
if (((address + startFlash) <= 255) && isPRO())
{
let rval = readSensor(address + startFlash) & 0xff
return (rval > 127) ? rval-256 : rval
}
else if (getVersionCode() == 5)
{
let i2cRead = pins.createBuffer(2);
i2cRead[0] = address >> 8 // address MSB
i2cRead[1] = address & 0xff // address LSB
pins.i2cWriteBuffer(EEROM, i2cRead, false)
basic.pause(1)
return pins.i2cReadNumber(EEROM, NumberFormat.Int8LE)
}
else
return 0
}
/**
* Load Calibration data from EEROM
*/
//% blockId="loadCalibration"
//% block="Load calibration from EEROM"
//% weight=70
//% deprecated=true
export function loadCalibration(): void
{
if (getVersionCode() == 5)
{
for (let i=0; i<3; i++)
calibration[i] = rdEEROM(i)
}
calibLoaded = true
}
/**
* Save Calibration data to EEROM
*/
//% blockId="saveCalibration"
//% block="Save calibration to EEROM"
//% weight=60
//% deprecated=true
export function saveCalibration(): void
{
if (getVersionCode() == 5)
{
for (let i=0; i<3; i++)
wrEEROM(calibration[i],i)
}
}
/**
* Check Calibration Values for given speed
* @param speed selected speed 0 to 100. eg: 60
* @param side selected motor Left or Right
*/
//% blockId="checkCalibration"
//% block="Check side%side bias for speed%speed"
//% weight=60
//% deprecated=true
export function checkCalibration(side: BBMotor, speed: number): number
{
let calibVal = 0
leftCalib = 0
rightCalib = 0
if (getVersionCode() == 5)
{
if (speed < 60)
calibVal = calibration[1] - ((60 - speed)/30) * (calibration[1] - calibration[0])
else
calibVal = calibration[2] - ((90 - speed)/30) * (calibration[2] - calibration[1])
if (calibVal < 0)
leftCalib = Math.abs(calibVal)
else
rightCalib = calibVal
}
if (side == BBMotor.Left)
return leftCalib
else
return rightCalib
}
// New Style Motor Blocks
// slow PWM frequency for slower speeds to improve torque
function setPWM(speed: number): void
{
if(!isPRO())
{
if (speed < 200)
pins.analogSetPeriod(AnalogPin.P0, 60000)
else if (speed < 300)
pins.analogSetPeriod(AnalogPin.P0, 40000)
else
pins.analogSetPeriod(AnalogPin.P0, 30000)
}
}
/**
* Move robot forward (or backward) at speed.
* @param direction Move Forward or Reverse
* @param speed speed of motor between 0 and 100. eg: 60
*/
//% blockId="BBGo" block="go%direction|at speed%speed|\\%"
//% speed.min=0 speed.max=100
//% weight=100
//% subcategory=Motors
export function go(direction: BBDirection, speed: number): void
{
speed = clamp(speed, 0, 100)
if(isPRO() && pidEnable)
{
if(lastCommand!=cGO || lastDirection!=direction || lastSpeed!=speed)
{
if((getFirmwareCode() == 10) && pidActive) // first firmware release has bug
stop(BBStopMode.Coast)
sendCommand2(DRIVE, (direction == BBDirection.Reverse) ? -speed : speed);
}
pidActive = true
lastDirection = direction
lastSpeed = speed
}
else
move(BBMotor.Both, direction, speed)
lastCommand = cGO
}
/**
* Move robot forward (or backward) at speed for milliseconds
* @param direction Move Forward or Reverse
* @param speed speed of motor between 0 and 100. eg: 60
* @param milliseconds duration in milliseconds to drive forward for, then stop. eg: 1000
*/
//% blockId="BBGoms" block="go%direction|at speed%speed|\\% for%milliseconds|ms"
//% speed.min=0 speed.max=100
//% weight=90
//% subcategory=Motors
export function goms(direction: BBDirection, speed: number, milliseconds: number): void
{
go(direction, speed)
basic.pause(milliseconds)
stop(BBStopMode.Coast)
}
/**
* Rotate robot in direction at speed
* @param direction direction to turn
* @param speed speed of motors (0 to 100). eg: 60
*/
//% blockId="BBRotate" block="spin%direction|at speed%speed|\\%"
//% speed.min=0 speed.max=100
//% weight=80
//% subcategory=Motors
export function rotate(direction: BBRobotDirection, speed: number): void
{
speed = clamp(speed, 0, 100)
if(isPRO() && pidEnable)
{
if(lastCommand!=cSPIN || lastSDirection!=direction || lastSpeed!=speed)
{
if((getFirmwareCode() == 10) && pidActive) // first firmware release has bug
stop(BBStopMode.Coast)
sendCommand2(SPIN, (direction == BBRobotDirection.Right) ? -speed : speed)
}
pidActive = true
lastSDirection = direction
lastSpeed = speed
}
else
{
if (direction == BBRobotDirection.Left)
{
move(BBMotor.Left, BBDirection.Reverse, speed);
move(BBMotor.Right, BBDirection.Forward, speed);
}
else if (direction == BBRobotDirection.Right)
{
move(BBMotor.Left, BBDirection.Forward, speed);
move(BBMotor.Right, BBDirection.Reverse, speed);